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 #include "time_units.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 #define TIMEX_MAX_STR_LEN (20)
35 /* 20 =
36  * + 10 byte: 2^32-1 for seconds
37  * + 1 byte: decimal point
38  * + 6 byte: microseconds (normalized)
39  * + 2 byte: " s" (unit)
40  * + 1 byte: '\0'
41  */
42 
49 typedef struct {
50  uint32_t seconds;
51  uint32_t microseconds;
52 } timex_t;
53 
62 timex_t timex_add(const timex_t a, const timex_t b);
63 
72 timex_t timex_sub(const timex_t a, const timex_t b);
73 
82 timex_t timex_set(uint32_t seconds, uint32_t microseconds);
83 
94 int timex_cmp(const timex_t a, const timex_t b);
95 
101 static inline void timex_normalize(timex_t *time)
102 {
103  time->seconds += (time->microseconds / US_PER_SEC);
104  time->microseconds %= US_PER_SEC;
105 }
106 
115 static inline int timex_isnormalized(const timex_t *time)
116 {
117  return (time->microseconds < US_PER_SEC);
118 }
119 
127 static inline uint64_t timex_uint64(const timex_t a)
128 {
129  return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
130 }
131 
139 static inline timex_t timex_from_uint64(const uint64_t timestamp)
140 {
141  return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
142 }
143 
156 const char *timex_to_str(timex_t t, char *timestamp);
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
163 #endif /* TIMEX_H */
#define US_PER_SEC
The number of microseconds per second.
Definition: time_units.h:85
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.
static void timex_normalize(timex_t *time)
Corrects timex structure so that microseconds < 1000000.
Definition: timex.h:101
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:115
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:127
static timex_t timex_from_uint64(const uint64_t timestamp)
Converts a 64 bit value of microseconds to a timex timestamp.
Definition: timex.h:139
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:49
uint32_t seconds
number of seconds
Definition: timex.h:50
uint32_t microseconds
number of microseconds
Definition: timex.h:51
Utility header providing time unit defines.