blob: bdf73cc037318fb5ade59540e19c3378a0f34baa [file] [log] [blame]
henrike@webrtc.orgf7795df2014-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
14#include <time.h>
15
16#include "webrtc/base/basictypes.h"
17
18namespace rtc {
19
20static const int64 kNumMillisecsPerSec = INT64_C(1000);
21static const int64 kNumMicrosecsPerSec = INT64_C(1000000);
22static const int64 kNumNanosecsPerSec = INT64_C(1000000000);
23
24static const int64 kNumMicrosecsPerMillisec = kNumMicrosecsPerSec /
25 kNumMillisecsPerSec;
26static const int64 kNumNanosecsPerMillisec = kNumNanosecsPerSec /
27 kNumMillisecsPerSec;
28static const int64 kNumNanosecsPerMicrosec = kNumNanosecsPerSec /
29 kNumMicrosecsPerSec;
30
31// January 1970, in NTP milliseconds.
32static const int64 kJan1970AsNtpMillisecs = INT64_C(2208988800000);
33
34typedef uint32 TimeStamp;
35
36// Returns the current time in milliseconds.
37uint32 Time();
38// Returns the current time in microseconds.
39uint64 TimeMicros();
40// Returns the current time in nanoseconds.
41uint64 TimeNanos();
42
43// Stores current time in *tm and microseconds in *microseconds.
44void CurrentTmTime(struct tm *tm, int *microseconds);
45
46// Returns a future timestamp, 'elapsed' milliseconds from now.
47uint32 TimeAfter(int32 elapsed);
48
49// Comparisons between time values, which can wrap around.
50bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later); // Inclusive
51bool TimeIsLaterOrEqual(uint32 earlier, uint32 later); // Inclusive
52bool TimeIsLater(uint32 earlier, uint32 later); // Exclusive
53
54// Returns the later of two timestamps.
55inline uint32 TimeMax(uint32 ts1, uint32 ts2) {
56 return TimeIsLaterOrEqual(ts1, ts2) ? ts2 : ts1;
57}
58
59// Returns the earlier of two timestamps.
60inline uint32 TimeMin(uint32 ts1, uint32 ts2) {
61 return TimeIsLaterOrEqual(ts1, ts2) ? ts1 : ts2;
62}
63
64// Number of milliseconds that would elapse between 'earlier' and 'later'
65// timestamps. The value is negative if 'later' occurs before 'earlier'.
66int32 TimeDiff(uint32 later, uint32 earlier);
67
68// The number of milliseconds that have elapsed since 'earlier'.
69inline int32 TimeSince(uint32 earlier) {
70 return TimeDiff(Time(), earlier);
71}
72
73// The number of milliseconds that will elapse between now and 'later'.
74inline int32 TimeUntil(uint32 later) {
75 return TimeDiff(later, Time());
76}
77
78// Converts a unix timestamp in nanoseconds to an NTP timestamp in ms.
79inline int64 UnixTimestampNanosecsToNtpMillisecs(int64 unix_ts_ns) {
80 return unix_ts_ns / kNumNanosecsPerMillisec + kJan1970AsNtpMillisecs;
81}
82
83} // namespace rtc
84
85#endif // WEBRTC_BASE_TIMEUTILS_H_