henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2005 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_TIMEUTILS_H_ |
| 29 | #define TALK_BASE_TIMEUTILS_H_ |
| 30 | |
| 31 | #include <time.h> |
| 32 | |
| 33 | #include "talk/base/basictypes.h" |
| 34 | |
| 35 | namespace talk_base { |
| 36 | |
| 37 | static const int64 kNumMillisecsPerSec = INT64_C(1000); |
| 38 | static const int64 kNumMicrosecsPerSec = INT64_C(1000000); |
| 39 | static const int64 kNumNanosecsPerSec = INT64_C(1000000000); |
| 40 | |
| 41 | static const int64 kNumMicrosecsPerMillisec = kNumMicrosecsPerSec / |
| 42 | kNumMillisecsPerSec; |
| 43 | static const int64 kNumNanosecsPerMillisec = kNumNanosecsPerSec / |
| 44 | kNumMillisecsPerSec; |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 45 | static const int64 kNumNanosecsPerMicrosec = kNumNanosecsPerSec / |
| 46 | kNumMicrosecsPerSec; |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 47 | |
| 48 | // January 1970, in NTP milliseconds. |
| 49 | static const int64 kJan1970AsNtpMillisecs = INT64_C(2208988800000); |
| 50 | |
| 51 | typedef uint32 TimeStamp; |
| 52 | |
| 53 | // Returns the current time in milliseconds. |
| 54 | uint32 Time(); |
wu@webrtc.org | f89a403 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 55 | // Returns the current time in microseconds. |
| 56 | uint64 TimeMicros(); |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | // Returns the current time in nanoseconds. |
| 58 | uint64 TimeNanos(); |
| 59 | |
| 60 | // Stores current time in *tm and microseconds in *microseconds. |
| 61 | void CurrentTmTime(struct tm *tm, int *microseconds); |
| 62 | |
| 63 | // Returns a future timestamp, 'elapsed' milliseconds from now. |
| 64 | uint32 TimeAfter(int32 elapsed); |
| 65 | |
| 66 | // Comparisons between time values, which can wrap around. |
| 67 | bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later); // Inclusive |
| 68 | bool TimeIsLaterOrEqual(uint32 earlier, uint32 later); // Inclusive |
| 69 | bool TimeIsLater(uint32 earlier, uint32 later); // Exclusive |
| 70 | |
| 71 | // Returns the later of two timestamps. |
| 72 | inline uint32 TimeMax(uint32 ts1, uint32 ts2) { |
| 73 | return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1; |
| 74 | } |
| 75 | |
| 76 | // Returns the earlier of two timestamps. |
| 77 | inline uint32 TimeMin(uint32 ts1, uint32 ts2) { |
| 78 | return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2; |
| 79 | } |
| 80 | |
| 81 | // Number of milliseconds that would elapse between 'earlier' and 'later' |
| 82 | // timestamps. The value is negative if 'later' occurs before 'earlier'. |
| 83 | int32 TimeDiff(uint32 later, uint32 earlier); |
| 84 | |
| 85 | // The number of milliseconds that have elapsed since 'earlier'. |
| 86 | inline int32 TimeSince(uint32 earlier) { |
| 87 | return TimeDiff(Time(), earlier); |
| 88 | } |
| 89 | |
| 90 | // The number of milliseconds that will elapse between now and 'later'. |
| 91 | inline int32 TimeUntil(uint32 later) { |
| 92 | return TimeDiff(later, Time()); |
| 93 | } |
| 94 | |
| 95 | // Converts a unix timestamp in nanoseconds to an NTP timestamp in ms. |
| 96 | inline int64 UnixTimestampNanosecsToNtpMillisecs(int64 unix_ts_ns) { |
| 97 | return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs; |
| 98 | } |
| 99 | |
buildbot@webrtc.org | 148fbde | 2014-05-21 17:02:15 +0000 | [diff] [blame] | 100 | class TimestampWrapAroundHandler { |
| 101 | public: |
| 102 | TimestampWrapAroundHandler(); |
| 103 | |
| 104 | int64 Unwrap(uint32 ts); |
| 105 | |
| 106 | private: |
| 107 | uint32 last_ts_; |
| 108 | int64 num_wrap_; |
| 109 | }; |
| 110 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | } // namespace talk_base |
| 112 | |
| 113 | #endif // TALK_BASE_TIMEUTILS_H_ |