timex.h
4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (C) 2010 Kaspar Schleiser <kaspar@schleiser.de>
* Copyright (C) 2014 Oliver Hahm <oliver.hahm@inria.fr>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup sys_timex Timex
* @brief Timestamp representation, computation, and conversion
* @ingroup sys
*
* @{
* @file
* @brief Utility library for comparing and computing timestamps
*/
#ifndef TIMEX_H
#define TIMEX_H
#include <stdint.h>
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief The number of microseconds per second
*/
#define US_PER_SEC (1000000U)
/**
* @brief The number of seconds per minute
*/
#define SEC_PER_MIN (60U)
/**
* @brief The number of centiseconds per second
*/
#define CS_PER_SEC (100U)
/**
* @brief The number of milliseconds per second
*/
#define MS_PER_SEC (1000U)
/**
* @brief The number of microseconds per millisecond
*/
#define US_PER_MS (1000U)
/**
* @brief The number of nanoseconds per microsecond
*/
#define NS_PER_US (1000)
/**
* @brief The maximum length of the string representation of a timex timestamp
*/
#define TIMEX_MAX_STR_LEN (20)
/* 20 =
* + 10 byte: 2^32-1 for seconds
* + 1 byte: decimal point
* + 6 byte: microseconds (normalized)
* + 2 byte: " s" (unit)
* + 1 byte: '\0'
*/
/**
* @brief A timex timestamp
*
* @note If a timestamp is not normalized, the number of microseconds might be
* > 1000000
*/
typedef struct {
uint32_t seconds; /**< number of seconds */
uint32_t microseconds; /**< number of microseconds */
} timex_t;
/**
* @brief Adds two timestamps
*
* @param[in] a First summand
* @param[in] b Second summand
*
* @return The sum of the two timestamps
*/
timex_t timex_add(const timex_t a, const timex_t b);
/**
* @brief Subtracts two timestamps
*
* @param[in] a The minuend
* @param[in] b The subtrahend
*
* @return The difference a - b
*/
timex_t timex_sub(const timex_t a, const timex_t b);
/**
* @brief Initializes a timex timestamp
*
* @param[in] seconds Number of seconds to set
* @param[in] microseconds Number of microseconds to set
*
* @return The initialized timex timestamp
*/
timex_t timex_set(uint32_t seconds, uint32_t microseconds);
/**
* @brief Compares two timex timestamps
*
* @param[in] a The first timestamp to compare to
* @param[in] b The second timestamp to compare with
*
* @return -1 when a is smaller
* @return 0 if equal
* @return 1 if a is bigger
*/
int timex_cmp(const timex_t a, const timex_t b);
/**
* @brief Corrects timex structure so that microseconds < 1000000
*
* @param[in, out] time Pointer to the timestamp to normalize
*/
static inline void timex_normalize(timex_t *time)
{
time->seconds += (time->microseconds / US_PER_SEC);
time->microseconds %= US_PER_SEC;
}
/**
* @brief Tests a timex timestamp for normalization
*
* @param[in] time Pointer to the timestamp to check
*
* @return true for a normalized timex_t
* @return false otherwise
*/
static inline int timex_isnormalized(const timex_t *time)
{
return (time->microseconds < US_PER_SEC);
}
/**
* @brief Converts a timex timestamp to a 64 bit value
*
* @param[in] a The timestamp to convert
*
* @return timex representation as uint64_t
*/
static inline uint64_t timex_uint64(const timex_t a)
{
return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
}
/**
* @brief Converts a 64 bit value of microseconds to a timex timestamp
*
* @param[in] timestamp The timestamp to convert.
*
* @return a timex representation of an uint64 timestamp.
*/
static inline timex_t timex_from_uint64(const uint64_t timestamp)
{
return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
}
/**
* @brief Converts a timex timestamp to a string
*
* @pre memory at timestamp >= TIMEX_MAX_STR_LEN
*
* @param[in] t The timestamp to convert
* @param[out] timestamp The output char buffer for the converted timestamp
*
* @note The timestamp will be normalized
*
* @return A pointer to the string representation of the timestamp
*/
const char *timex_to_str(timex_t t, char *timestamp);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* TIMEX_H */