timex.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Kaspar Schleiser <kaspar@schleiser.de>
3  * Copyright (C) 2014 Oliver Hahm <oliver.hahm@inria.fr>
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
20 #ifndef TIMEX_H
21 #define TIMEX_H
22 
23 #include <stdint.h>
24 #include <inttypes.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
33 #define US_PER_SEC (1000000LU)
34 
38 #define SEC_PER_MIN (60LU)
39 
43 #define CS_PER_SEC (100LU)
44 
48 #define MS_PER_SEC (1000LU)
49 
53 #define US_PER_MS (1000LU)
54 
58 #define US_PER_CS (10000U)
59 
63 #define MS_PER_CS (10U)
64 
68 #define NS_PER_US (1000LU)
69 
73 #define NS_PER_SEC (1000000000LLU)
74 
78 #define TIMEX_MAX_STR_LEN (20)
79 /* 20 =
80  * + 10 byte: 2^32-1 for seconds
81  * + 1 byte: decimal point
82  * + 6 byte: microseconds (normalized)
83  * + 2 byte: " s" (unit)
84  * + 1 byte: '\0'
85  */
86 
93 typedef struct {
94  uint32_t seconds;
95  uint32_t microseconds;
96 } timex_t;
97 
106 timex_t timex_add(const timex_t a, const timex_t b);
107 
116 timex_t timex_sub(const timex_t a, const timex_t b);
117 
126 timex_t timex_set(uint32_t seconds, uint32_t microseconds);
127 
138 int timex_cmp(const timex_t a, const timex_t b);
139 
145 static inline void timex_normalize(timex_t *time)
146 {
147  time->seconds += (time->microseconds / US_PER_SEC);
148  time->microseconds %= US_PER_SEC;
149 }
150 
159 static inline int timex_isnormalized(const timex_t *time)
160 {
161  return (time->microseconds < US_PER_SEC);
162 }
163 
171 static inline uint64_t timex_uint64(const timex_t a)
172 {
173  return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
174 }
175 
183 static inline timex_t timex_from_uint64(const uint64_t timestamp)
184 {
185  return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
186 }
187 
200 const char *timex_to_str(timex_t t, char *timestamp);
201 
202 #ifdef __cplusplus
203 }
204 #endif
205 
207 #endif /* TIMEX_H */
timex_t timex_add(const timex_t a, const timex_t b)
Adds two timestamps.
int timex_cmp(const timex_t a, const timex_t b)
Compares two timex timestamps.
#define US_PER_SEC
The number of microseconds per second.
Definition: timex.h:33
static void timex_normalize(timex_t *time)
Corrects timex structure so that microseconds < 1000000.
Definition: timex.h:145
const char * timex_to_str(timex_t t, char *timestamp)
Converts a timex timestamp to a string.
static int timex_isnormalized(const timex_t *time)
Tests a timex timestamp for normalization.
Definition: timex.h:159
timex_t timex_sub(const timex_t a, const timex_t b)
Subtracts two timestamps.
static uint64_t timex_uint64(const timex_t a)
Converts a timex timestamp to a 64 bit value.
Definition: timex.h:171
static timex_t timex_from_uint64(const uint64_t timestamp)
Converts a 64 bit value of microseconds to a timex timestamp.
Definition: timex.h:183
timex_t timex_set(uint32_t seconds, uint32_t microseconds)
Initializes a timex timestamp.
Adds include for missing inttype definitions.
A timex timestamp.
Definition: timex.h:93
uint32_t seconds
number of seconds
Definition: timex.h:94
uint32_t microseconds
number of microseconds
Definition: timex.h:95