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 (1000U)
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 timex_t timex_add(const timex_t a, const timex_t b);
93 
102 timex_t timex_sub(const timex_t a, const timex_t b);
103 
112 timex_t timex_set(uint32_t seconds, uint32_t microseconds);
113 
124 int timex_cmp(const timex_t a, const timex_t b);
125 
131 static inline void timex_normalize(timex_t *time)
132 {
133  time->seconds += (time->microseconds / US_PER_SEC);
134  time->microseconds %= US_PER_SEC;
135 }
136 
145 static inline int timex_isnormalized(const timex_t *time)
146 {
147  return (time->microseconds < US_PER_SEC);
148 }
149 
157 static inline uint64_t timex_uint64(const timex_t a)
158 {
159  return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
160 }
161 
169 static inline timex_t timex_from_uint64(const uint64_t timestamp)
170 {
171  return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
172 }
173 
186 const char *timex_to_str(timex_t t, char *timestamp);
187 
188 #ifdef __cplusplus
189 }
190 #endif
191 
193 #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:157
static int timex_isnormalized(const timex_t *time)
Tests a timex timestamp for normalization.
Definition: timex.h:145
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:169
#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:131
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.