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 (1000000LU)
35 
39 #define SEC_PER_MIN (60LU)
40 
44 #define CS_PER_SEC (100LU)
45 
49 #define MS_PER_SEC (1000LU)
50 
54 #define US_PER_MS (1000LU)
55 
59 #define US_PER_CS (10000U)
60 
64 #define NS_PER_US (1000LU)
65 
69 #define NS_PER_SEC (1000000000U)
70 
74 #define TIMEX_MAX_STR_LEN (20)
75 /* 20 =
76  * + 10 byte: 2^32-1 for seconds
77  * + 1 byte: decimal point
78  * + 6 byte: microseconds (normalized)
79  * + 2 byte: " s" (unit)
80  * + 1 byte: '\0'
81  */
82 
89 typedef struct {
90  uint32_t seconds;
91  uint32_t microseconds;
92 } timex_t;
93 
102 timex_t timex_add(const timex_t a, const timex_t b);
103 
112 timex_t timex_sub(const timex_t a, const timex_t b);
113 
122 timex_t timex_set(uint32_t seconds, uint32_t microseconds);
123 
134 int timex_cmp(const timex_t a, const timex_t b);
135 
141 static inline void timex_normalize(timex_t *time)
142 {
143  time->seconds += (time->microseconds / US_PER_SEC);
144  time->microseconds %= US_PER_SEC;
145 }
146 
155 static inline int timex_isnormalized(const timex_t *time)
156 {
157  return (time->microseconds < US_PER_SEC);
158 }
159 
167 static inline uint64_t timex_uint64(const timex_t a)
168 {
169  return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
170 }
171 
179 static inline timex_t timex_from_uint64(const uint64_t timestamp)
180 {
181  return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
182 }
183 
196 const char *timex_to_str(timex_t t, char *timestamp);
197 
198 #ifdef __cplusplus
199 }
200 #endif
201 
203 #endif /* TIMEX_H */
timex_uint64
static uint64_t timex_uint64(const timex_t a)
Converts a timex timestamp to a 64 bit value.
Definition: timex.h:167
timex_t
A timex timestamp.
Definition: timex.h:89
timex_isnormalized
static int timex_isnormalized(const timex_t *time)
Tests a timex timestamp for normalization.
Definition: timex.h:155
timex_to_str
const char * timex_to_str(timex_t t, char *timestamp)
Converts a timex timestamp to a string.
timex_cmp
int timex_cmp(const timex_t a, const timex_t b)
Compares two timex timestamps.
timex_t::microseconds
uint32_t microseconds
number of microseconds
Definition: timex.h:91
timex_t::seconds
uint32_t seconds
number of seconds
Definition: timex.h:90
timex_set
timex_t timex_set(uint32_t seconds, uint32_t microseconds)
Initializes a timex timestamp.
timex_from_uint64
static timex_t timex_from_uint64(const uint64_t timestamp)
Converts a 64 bit value of microseconds to a timex timestamp.
Definition: timex.h:179
timex_add
timex_t timex_add(const timex_t a, const timex_t b)
Adds two timestamps.
timex_sub
timex_t timex_sub(const timex_t a, const timex_t b)
Subtracts two timestamps.
inttypes.h
Adds include for missing inttype definitions.
timex_normalize
static void timex_normalize(timex_t *time)
Corrects timex structure so that microseconds < 1000000.
Definition: timex.h:141
US_PER_SEC
#define US_PER_SEC
The number of microseconds per second.
Definition: timex.h:34