Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
time.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/time.h
10 //! @brief Time definitions.
11 
12 #ifndef ROC_CORE_TIME_H_
13 #define ROC_CORE_TIME_H_
14 
15 #include "roc_core/stddefs.h"
16 
17 namespace roc {
18 namespace core {
19 
20 //! Clock identifier.
21 enum clock_t {
22  //! Virtual monotonic clock.
23  //!
24  //! @remarks
25  //! Starts at unspecified point of time.
26  //!
27  //! When platform supports it, uses the clock source that grows monotonically. It
28  //! never jumps backwards and is not affected by system clock change.
29  //!
30  //! This clock is still subject to clock *rate* adjustments applied by NTP daemon.
31  //! When it performs synchronization, it may slightly speed up or slow down both
32  //! unix and monotonic clocks for a while.
33  //!
34  //! Usually this clock is reset after reboot.
35  //! Usually this clock does not count time spent in suspended state.
36  //!
37  //! @note
38  //! If platform does not have monotonic clock source, unix clock is used.
39  //! Actual precision is platform-dependent.
41 
42  //! Real-time Unix-time UTC clock.
43  //!
44  //! @remarks
45  //! Starts at 1 Jan 1970 00:00:00 UTC.
46  //!
47  //! May instantly jump forwards or backwards when system administrator sets time.
48  //! May speed up or slow down when NTP daemon adjusts clock rate.
49  //! May experience discontinuities when NTP daemon inserts leap seconds.
50  //!
51  //! @note
52  //! Available on all platforms.
53  //! Actual precision is platform-dependent.
54  ClockUnix
55 };
56 
57 //! Nanoseconds.
58 typedef int64_t nanoseconds_t;
59 
60 //! One nanosecond represented in nanoseconds.
62 
63 //! One microsecond represented in nanoseconds.
65 
66 //! One millisecond represented in nanoseconds.
68 
69 //! One second represented in nanoseconds.
71 
72 //! One minute represented in nanoseconds.
74 
75 //! One hour represented in nanoseconds.
76 const nanoseconds_t Hour = 60 * Minute;
77 
78 //! One day represented in nanoseconds.
79 const nanoseconds_t Day = 24 * Hour;
80 
81 //! Get current timestamp in nanoseconds.
83 
84 //! Sleep until the specified absolute time point has been reached.
85 //! @remarks
86 //! @p timestamp specifies absolute time point in nanoseconds.
88 
89 //! Sleep specified amount of time.
90 //! @remarks
91 //! @p duration specifies number of nanoseconds to sleep.
92 void sleep_for(clock_t clock, nanoseconds_t duration);
93 
94 //! Convert timestamp in nanoseconds format to broken-down time.
95 //! @note
96 //! std::tm has precision of one second.
98 
99 //! Convert timestamp from broken-down time to nanoseconds format.
100 //! @note
101 //! std::tm has precision of one second.
103 
104 //! Compares a and b if they close enough.
106 
107 } // namespace core
108 } // namespace roc
109 
110 #endif // ROC_CORE_TIME_H_
const nanoseconds_t Millisecond
One millisecond represented in nanoseconds.
Definition: time.h:67
const nanoseconds_t Nanosecond
One nanosecond represented in nanoseconds.
Definition: time.h:61
bool ns_equal_delta(nanoseconds_t a, nanoseconds_t b, nanoseconds_t delta)
Compares a and b if they close enough.
const nanoseconds_t Day
One day represented in nanoseconds.
Definition: time.h:79
nanoseconds_t timestamp(clock_t clock)
Get current timestamp in nanoseconds.
std::tm nanoseconds_2_tm(nanoseconds_t timestamp)
Convert timestamp in nanoseconds format to broken-down time.
const nanoseconds_t Second
One second represented in nanoseconds.
Definition: time.h:70
const nanoseconds_t Minute
One minute represented in nanoseconds.
Definition: time.h:73
const nanoseconds_t Microsecond
One microsecond represented in nanoseconds.
Definition: time.h:64
clock_t
Clock identifier.
Definition: time.h:21
@ ClockUnix
Real-time Unix-time UTC clock.
Definition: time.h:54
@ ClockMonotonic
Virtual monotonic clock.
Definition: time.h:40
void sleep_for(clock_t clock, nanoseconds_t duration)
Sleep specified amount of time.
const nanoseconds_t Hour
One hour represented in nanoseconds.
Definition: time.h:76
void sleep_until(clock_t clock, nanoseconds_t timestamp)
Sleep until the specified absolute time point has been reached.
nanoseconds_t tm_2_nanoseconds(std::tm tm)
Convert timestamp from broken-down time to nanoseconds format.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Commonly used types and functions.