blob: 222d5c26e4774efc568736bd2f9fcfa417978ed1 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Granlund46c9cc02015-12-01 13:06:34 +010014#include <ctime>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <time.h>
16
17#include "webrtc/base/basictypes.h"
18
19namespace rtc {
20
Peter Boström0c4e06b2015-10-07 12:23:21 +020021static const int64_t kNumMillisecsPerSec = INT64_C(1000);
22static const int64_t kNumMicrosecsPerSec = INT64_C(1000000);
23static const int64_t kNumNanosecsPerSec = INT64_C(1000000000);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
Peter Boström0c4e06b2015-10-07 12:23:21 +020025static const int64_t kNumMicrosecsPerMillisec =
26 kNumMicrosecsPerSec / kNumMillisecsPerSec;
27static const int64_t kNumNanosecsPerMillisec =
28 kNumNanosecsPerSec / kNumMillisecsPerSec;
29static const int64_t kNumNanosecsPerMicrosec =
30 kNumNanosecsPerSec / kNumMicrosecsPerSec;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031
Honghai Zhang82d78622016-05-06 11:29:15 -070032// TODO(honghaiz): Define a type for the time value specifically.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033
honghaiz34b11eb2016-03-16 08:55:44 -070034// Returns the current time in milliseconds in 32 bits.
35uint32_t Time32();
36
37// Returns the current time in milliseconds in 64 bits.
nisse1bffc1d2016-05-02 08:18:55 -070038int64_t TimeMillis();
Honghai Zhang82d78622016-05-06 11:29:15 -070039// Deprecated. Do not use this in any new code.
40inline int64_t Time() {
41 return TimeMillis();
honghaiz34b11eb2016-03-16 08:55:44 -070042}
43
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044// Returns the current time in microseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020045uint64_t TimeMicros();
Honghai Zhang82d78622016-05-06 11:29:15 -070046
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047// Returns the current time in nanoseconds.
Peter Boström0c4e06b2015-10-07 12:23:21 +020048uint64_t TimeNanos();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050// Returns a future timestamp, 'elapsed' milliseconds from now.
Honghai Zhang82d78622016-05-06 11:29:15 -070051int64_t TimeAfter(int64_t elapsed);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052
53// Number of milliseconds that would elapse between 'earlier' and 'later'
54// timestamps. The value is negative if 'later' occurs before 'earlier'.
Honghai Zhang82d78622016-05-06 11:29:15 -070055int64_t TimeDiff(int64_t later, int64_t earlier);
56int32_t TimeDiff32(uint32_t later, uint32_t earlier);
honghaiz34b11eb2016-03-16 08:55:44 -070057
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058// The number of milliseconds that have elapsed since 'earlier'.
Honghai Zhang82d78622016-05-06 11:29:15 -070059inline int64_t TimeSince(int64_t earlier) {
60 return TimeMillis() - earlier;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061}
62
63// The number of milliseconds that will elapse between now and 'later'.
Honghai Zhang82d78622016-05-06 11:29:15 -070064inline int64_t TimeUntil(uint64_t later) {
65 return later - TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000066}
67
henrike@webrtc.org99b41622014-05-21 20:42:17 +000068class TimestampWrapAroundHandler {
69 public:
70 TimestampWrapAroundHandler();
71
Peter Boström0c4e06b2015-10-07 12:23:21 +020072 int64_t Unwrap(uint32_t ts);
henrike@webrtc.org99b41622014-05-21 20:42:17 +000073
74 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +020075 uint32_t last_ts_;
76 int64_t num_wrap_;
henrike@webrtc.org99b41622014-05-21 20:42:17 +000077};
78
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +010079// 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.
82int64_t TmToSeconds(const std::tm& tm);
83
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084} // namespace rtc
85
86#endif // WEBRTC_BASE_TIMEUTILS_H_