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 
10 
21 #ifndef TIMEX_H
22 #define TIMEX_H
23 
24 #include <stdint.h>
25 #include <inttypes.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 #define US_PER_SEC (1000000U)
35 
39 #define SEC_PER_MIN (60U)
40 
44 #define CS_PER_SEC (100U)
45 
49 #define MS_PER_SEC (1000U)
50 
54 #define US_PER_MS (1000U)
55 
59 #define NS_PER_US (1000)
60 
64 #define TIMEX_MAX_STR_LEN (20)
65 /* 20 =
66  * + 10 byte: 2^32-1 for seconds
67  * + 1 byte: decimal point
68  * + 6 byte: microseconds (normalized)
69  * + 2 byte: " s" (unit)
70  * + 1 byte: '\0'
71  */
72 
79 typedef struct {
80  uint32_t seconds;
81  uint32_t microseconds;
82 } timex_t;
83 
92 /* cppcheck-suppress passedByValue */
93 timex_t timex_add(const timex_t a, const timex_t b);
94 
103 /* cppcheck-suppress passedByValue */
104 timex_t timex_sub(const timex_t a, const timex_t b);
105 
114 timex_t timex_set(uint32_t seconds, uint32_t microseconds);
115 
126 /* cppcheck-suppress passedByValue */
127 int timex_cmp(const timex_t a, const timex_t b);
128 
134 static inline void timex_normalize(timex_t *time)
135 {
136  time->seconds += (time->microseconds / US_PER_SEC);
137  time->microseconds %= US_PER_SEC;
138 }
139 
148 static inline int timex_isnormalized(const timex_t *time)
149 {
150  return (time->microseconds < US_PER_SEC);
151 }
152 
160 /* cppcheck-suppress passedByValue */
161 static inline uint64_t timex_uint64(const timex_t a)
162 {
163  return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
164 }
165 
173 static inline timex_t timex_from_uint64(const uint64_t timestamp)
174 {
175  return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
176 }
177 
190 const char *timex_to_str(timex_t t, char *timestamp);
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
197 #endif /* TIMEX_H */
timex_t timex_set(uint32_t seconds, uint32_t microseconds)
Initializes a timex timestamp.
int timex_cmp(const timex_t a, const timex_t b)
Compares two timex timestamps.
A timex timestamp.
Definition: timex.h:79
static uint64_t timex_uint64(const timex_t a)
Converts a timex timestamp to a 64 bit value.
Definition: timex.h:161
static int timex_isnormalized(const timex_t *time)
Tests a timex timestamp for normalization.
Definition: timex.h:148
const char * timex_to_str(timex_t t, char *timestamp)
Converts a timex timestamp to a string.
timex_t timex_sub(const timex_t a, const timex_t b)
Subtracts two timestamps.
Adds include for missing inttype definitions.
static timex_t timex_from_uint64(const uint64_t timestamp)
Converts a 64 bit value of microseconds to a timex timestamp.
Definition: timex.h:173
#define US_PER_SEC
The number of microseconds per second.
Definition: timex.h:34
static void timex_normalize(timex_t *time)
Corrects timex structure so that microseconds < 1000000.
Definition: timex.h:134
uint32_t microseconds
number of microseconds
Definition: timex.h:81
uint32_t seconds
number of seconds
Definition: timex.h:80
timex_t timex_add(const timex_t a, const timex_t b)
Adds two timestamps.