Loading...
Searching...
No Matches
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#pragma once
11
21
22#include <stdint.h>
23#include <inttypes.h>
24#include "time_units.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
33#define TIMEX_MAX_STR_LEN (20)
34/* 20 =
35 * + 10 byte: 2^32-1 for seconds
36 * + 1 byte: decimal point
37 * + 6 byte: microseconds (normalized)
38 * + 2 byte: " s" (unit)
39 * + 1 byte: '\0'
40 */
41
48typedef struct {
49 uint32_t seconds;
50 uint32_t microseconds;
51} timex_t;
52
61timex_t timex_add(const timex_t a, const timex_t b);
62
71timex_t timex_sub(const timex_t a, const timex_t b);
72
81timex_t timex_set(uint32_t seconds, uint32_t microseconds);
82
93int timex_cmp(const timex_t a, const timex_t b);
94
100static inline void timex_normalize(timex_t *time)
101{
102 time->seconds += (time->microseconds / US_PER_SEC);
103 time->microseconds %= US_PER_SEC;
104}
105
114static inline int timex_isnormalized(const timex_t *time)
115{
116 return (time->microseconds < US_PER_SEC);
117}
118
126static inline uint64_t timex_uint64(const timex_t a)
127{
128 return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
129}
130
138static inline timex_t timex_from_uint64(const uint64_t timestamp)
139{
140 return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
141}
142
155const char *timex_to_str(timex_t t, char *timestamp);
156
157#ifdef __cplusplus
158}
159#endif
160
#define US_PER_SEC
The number of microseconds per second.
Definition time_units.h:84
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:100
static int timex_isnormalized(const timex_t *time)
Tests a timex timestamp for normalization.
Definition timex.h:114
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.
static uint64_t timex_uint64(const timex_t a)
Converts a timex timestamp to a 64 bit value.
Definition timex.h:126
static timex_t timex_from_uint64(const uint64_t timestamp)
Converts a 64 bit value of microseconds to a timex timestamp.
Definition timex.h:138
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:48
uint32_t seconds
number of seconds
Definition timex.h:49
uint32_t microseconds
number of microseconds
Definition timex.h:50
Utility header providing time unit defines.