henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2005 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_BASE_TIMEUTILS_H_ |
| 12 | #define WEBRTC_BASE_TIMEUTILS_H_ |
| 13 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 14 | #include <ctime> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #include <time.h> |
| 16 | |
| 17 | #include "webrtc/base/basictypes.h" |
| 18 | |
| 19 | namespace rtc { |
| 20 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 21 | static const int64_t kNumMillisecsPerSec = INT64_C(1000); |
| 22 | static const int64_t kNumMicrosecsPerSec = INT64_C(1000000); |
| 23 | static const int64_t kNumNanosecsPerSec = INT64_C(1000000000); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 25 | static const int64_t kNumMicrosecsPerMillisec = |
| 26 | kNumMicrosecsPerSec / kNumMillisecsPerSec; |
| 27 | static const int64_t kNumNanosecsPerMillisec = |
| 28 | kNumNanosecsPerSec / kNumMillisecsPerSec; |
| 29 | static const int64_t kNumNanosecsPerMicrosec = |
| 30 | kNumNanosecsPerSec / kNumMicrosecsPerSec; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 32 | // TODO(honghaiz): Define a type for the time value specifically. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 34 | // Returns the current time in milliseconds in 32 bits. |
| 35 | uint32_t Time32(); |
| 36 | |
| 37 | // Returns the current time in milliseconds in 64 bits. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 38 | int64_t TimeMillis(); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 39 | // Deprecated. Do not use this in any new code. |
| 40 | inline int64_t Time() { |
| 41 | return TimeMillis(); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 42 | } |
| 43 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | // Returns the current time in microseconds. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 45 | uint64_t TimeMicros(); |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 46 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | // Returns the current time in nanoseconds. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 48 | uint64_t TimeNanos(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | // Returns a future timestamp, 'elapsed' milliseconds from now. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 51 | int64_t TimeAfter(int64_t elapsed); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | |
| 53 | // Number of milliseconds that would elapse between 'earlier' and 'later' |
| 54 | // timestamps. The value is negative if 'later' occurs before 'earlier'. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 55 | int64_t TimeDiff(int64_t later, int64_t earlier); |
| 56 | int32_t TimeDiff32(uint32_t later, uint32_t earlier); |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 57 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | // The number of milliseconds that have elapsed since 'earlier'. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 59 | inline int64_t TimeSince(int64_t earlier) { |
| 60 | return TimeMillis() - earlier; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // The number of milliseconds that will elapse between now and 'later'. |
Honghai Zhang | 82d7862 | 2016-05-06 11:29:15 -0700 | [diff] [blame] | 64 | inline int64_t TimeUntil(uint64_t later) { |
| 65 | return later - TimeMillis(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | } |
| 67 | |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 68 | class TimestampWrapAroundHandler { |
| 69 | public: |
| 70 | TimestampWrapAroundHandler(); |
| 71 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 72 | int64_t Unwrap(uint32_t ts); |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 73 | |
| 74 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 75 | uint32_t last_ts_; |
| 76 | int64_t num_wrap_; |
henrike@webrtc.org | 99b4162 | 2014-05-21 20:42:17 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Torbjorn Granlund | 46c9cc0 | 2015-12-01 13:06:34 +0100 | [diff] [blame] | 79 | // Convert from std::tm, which is relative to 1900-01-01 00:00 to number of |
| 80 | // seconds from 1970-01-01 00:00 ("epoch"). Don't return time_t since that |
| 81 | // is still 32 bits on many systems. |
| 82 | int64_t TmToSeconds(const std::tm& tm); |
| 83 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 84 | } // namespace rtc |
| 85 | |
| 86 | #endif // WEBRTC_BASE_TIMEUTILS_H_ |