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 US_PER_CS (10000U)
60 
64 #define NS_PER_US (1000U)
65 
69 #define TIMEX_MAX_STR_LEN (20)
70 /* 20 =
71  * + 10 byte: 2^32-1 for seconds
72  * + 1 byte: decimal point
73  * + 6 byte: microseconds (normalized)
74  * + 2 byte: " s" (unit)
75  * + 1 byte: '\0'
76  */
77 
84 typedef struct {
85  uint32_t seconds;
86  uint32_t microseconds;
87 } timex_t;
88 
97 timex_t timex_add(const timex_t a, const timex_t b);
98 
107 timex_t timex_sub(const timex_t a, const timex_t b);
108 
117 timex_t timex_set(uint32_t seconds, uint32_t microseconds);
118 
129 int timex_cmp(const timex_t a, const timex_t b);
130 
136 static inline void timex_normalize(timex_t *time)
137 {
138  time->seconds += (time->microseconds / US_PER_SEC);
139  time->microseconds %= US_PER_SEC;
140 }
141 
150 static inline int timex_isnormalized(const timex_t *time)
151 {
152  return (time->microseconds < US_PER_SEC);
153 }
154 
162 static inline uint64_t timex_uint64(const timex_t a)
163 {
164  return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
165 }
166 
174 static inline timex_t timex_from_uint64(const uint64_t timestamp)
175 {
176  return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
177 }
178 
191 const char *timex_to_str(timex_t t, char *timestamp);
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
198 #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:84
static uint64_t timex_uint64(const timex_t a)
Converts a timex timestamp to a 64 bit value.
Definition: timex.h:162
static int timex_isnormalized(const timex_t *time)
Tests a timex timestamp for normalization.
Definition: timex.h:150
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:174
#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:136
uint32_t microseconds
number of microseconds
Definition: timex.h:86
uint32_t seconds
number of seconds
Definition: timex.h:85
timex_t timex_add(const timex_t a, const timex_t b)
Adds two timestamps.