2014-09-16 13:09:14 +02:00
|
|
|
/*
|
2014-10-19 20:44:10 +02:00
|
|
|
* Copyright (C) 2010 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* Copyright (C) 2014 Oliver Hahm <oliver.hahm@inria.fr>
|
2014-09-16 13:09:14 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
2013-11-27 16:28:31 +01:00
|
|
|
/**
|
2013-11-27 17:54:30 +01:00
|
|
|
* @defgroup sys_timex Timex
|
2014-10-19 21:27:23 +02:00
|
|
|
* @brief Timestamp representation, computation, and conversion
|
2013-11-27 17:54:30 +01:00
|
|
|
* @ingroup sys
|
2014-10-19 21:27:23 +02:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
* @file
|
2013-11-27 17:54:30 +01:00
|
|
|
* @brief Utility library for comparing and computing timestamps
|
2013-11-27 16:28:31 +01:00
|
|
|
*/
|
|
|
|
|
2016-10-26 21:53:58 +02:00
|
|
|
#ifndef TIMEX_H
|
|
|
|
#define TIMEX_H
|
2010-12-01 17:13:37 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2013-10-01 15:31:03 +02:00
|
|
|
#include <inttypes.h>
|
2021-12-15 14:33:20 +01:00
|
|
|
#include "time_units.h"
|
2013-10-01 15:31:03 +02:00
|
|
|
|
2014-10-10 11:51:11 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-10-19 21:27:23 +02:00
|
|
|
/**
|
|
|
|
* @brief The maximum length of the string representation of a timex timestamp
|
|
|
|
*/
|
2015-08-05 16:55:41 +02:00
|
|
|
#define TIMEX_MAX_STR_LEN (20)
|
|
|
|
/* 20 =
|
|
|
|
* + 10 byte: 2^32-1 for seconds
|
|
|
|
* + 1 byte: decimal point
|
|
|
|
* + 6 byte: microseconds (normalized)
|
|
|
|
* + 2 byte: " s" (unit)
|
|
|
|
* + 1 byte: '\0'
|
|
|
|
*/
|
2014-10-19 20:41:37 +02:00
|
|
|
|
2014-10-19 21:27:23 +02:00
|
|
|
/**
|
|
|
|
* @brief A timex timestamp
|
|
|
|
*
|
|
|
|
* @note If a timestamp is not normalized, the number of microseconds might be
|
|
|
|
* > 1000000
|
|
|
|
*/
|
2014-10-19 20:48:07 +02:00
|
|
|
typedef struct {
|
2014-10-19 21:27:23 +02:00
|
|
|
uint32_t seconds; /**< number of seconds */
|
|
|
|
uint32_t microseconds; /**< number of microseconds */
|
2010-12-01 17:13:37 +01:00
|
|
|
} timex_t;
|
|
|
|
|
2014-10-19 21:27:23 +02:00
|
|
|
/**
|
|
|
|
* @brief Adds two timestamps
|
|
|
|
*
|
|
|
|
* @param[in] a First summand
|
|
|
|
* @param[in] b Second summand
|
|
|
|
*
|
|
|
|
* @return The sum of the two timestamps
|
|
|
|
*/
|
2013-06-22 05:11:53 +02:00
|
|
|
timex_t timex_add(const timex_t a, const timex_t b);
|
2010-12-01 17:13:37 +01:00
|
|
|
|
2014-10-19 21:27:23 +02:00
|
|
|
/**
|
|
|
|
* @brief Subtracts two timestamps
|
|
|
|
*
|
|
|
|
* @param[in] a The minuend
|
|
|
|
* @param[in] b The subtrahend
|
|
|
|
*
|
|
|
|
* @return The difference a - b
|
|
|
|
*/
|
2010-12-01 17:23:28 +01:00
|
|
|
timex_t timex_sub(const timex_t a, const timex_t b);
|
|
|
|
|
2014-10-19 21:27:23 +02:00
|
|
|
/**
|
|
|
|
* @brief Initializes a timex timestamp
|
|
|
|
*
|
|
|
|
* @param[in] seconds Number of seconds to set
|
|
|
|
* @param[in] microseconds Number of microseconds to set
|
|
|
|
*
|
|
|
|
* @return The initialized timex timestamp
|
|
|
|
*/
|
2011-12-01 13:01:36 +01:00
|
|
|
timex_t timex_set(uint32_t seconds, uint32_t microseconds);
|
2010-12-01 17:23:28 +01:00
|
|
|
|
2010-12-06 16:02:40 +01:00
|
|
|
/**
|
2014-10-19 21:27:23 +02:00
|
|
|
* @brief Compares two timex timestamps
|
|
|
|
*
|
|
|
|
* @param[in] a The first timestamp to compare to
|
|
|
|
* @param[in] b The second timestamp to compare with
|
2010-12-06 16:02:40 +01:00
|
|
|
*
|
2014-10-19 21:27:23 +02:00
|
|
|
* @return -1 when a is smaller
|
|
|
|
* @return 0 if equal
|
|
|
|
* @return 1 if a is bigger
|
2010-12-06 16:02:40 +01:00
|
|
|
*/
|
2012-01-11 17:02:43 +01:00
|
|
|
int timex_cmp(const timex_t a, const timex_t b);
|
2010-12-06 16:02:40 +01:00
|
|
|
|
|
|
|
/**
|
2014-10-19 21:27:23 +02:00
|
|
|
* @brief Corrects timex structure so that microseconds < 1000000
|
|
|
|
*
|
|
|
|
* @param[in, out] time Pointer to the timestamp to normalize
|
2010-12-06 16:02:40 +01:00
|
|
|
*/
|
2014-10-19 20:00:24 +02:00
|
|
|
static inline void timex_normalize(timex_t *time)
|
|
|
|
{
|
2017-01-17 16:44:05 +01:00
|
|
|
time->seconds += (time->microseconds / US_PER_SEC);
|
|
|
|
time->microseconds %= US_PER_SEC;
|
2014-10-19 20:00:24 +02:00
|
|
|
}
|
2010-12-06 16:02:40 +01:00
|
|
|
|
2013-12-25 18:13:55 +01:00
|
|
|
/**
|
2014-10-19 21:27:23 +02:00
|
|
|
* @brief Tests a timex timestamp for normalization
|
|
|
|
*
|
|
|
|
* @param[in] time Pointer to the timestamp to check
|
2013-12-25 18:13:55 +01:00
|
|
|
*
|
2014-10-19 21:27:23 +02:00
|
|
|
* @return true for a normalized timex_t
|
|
|
|
* @return false otherwise
|
2013-12-25 18:13:55 +01:00
|
|
|
*/
|
2015-01-28 01:49:23 +01:00
|
|
|
static inline int timex_isnormalized(const timex_t *time)
|
2014-10-19 20:00:24 +02:00
|
|
|
{
|
2017-01-17 16:44:05 +01:00
|
|
|
return (time->microseconds < US_PER_SEC);
|
2014-10-19 20:00:24 +02:00
|
|
|
}
|
2013-12-25 18:13:55 +01:00
|
|
|
|
2013-12-25 17:01:55 +01:00
|
|
|
/**
|
2014-10-19 21:27:23 +02:00
|
|
|
* @brief Converts a timex timestamp to a 64 bit value
|
|
|
|
*
|
|
|
|
* @param[in] a The timestamp to convert
|
2013-12-25 17:01:55 +01:00
|
|
|
*
|
|
|
|
* @return timex representation as uint64_t
|
|
|
|
*/
|
2014-10-19 20:00:24 +02:00
|
|
|
static inline uint64_t timex_uint64(const timex_t a)
|
|
|
|
{
|
2017-01-17 16:44:05 +01:00
|
|
|
return (uint64_t) a.seconds * US_PER_SEC + a.microseconds;
|
2014-10-19 20:00:24 +02:00
|
|
|
}
|
2013-12-25 17:01:55 +01:00
|
|
|
|
2014-06-06 21:55:45 +02:00
|
|
|
/**
|
2014-10-19 21:27:23 +02:00
|
|
|
* @brief Converts a 64 bit value of microseconds to a timex timestamp
|
|
|
|
*
|
|
|
|
* @param[in] timestamp The timestamp to convert.
|
2014-06-06 21:55:45 +02:00
|
|
|
*
|
|
|
|
* @return a timex representation of an uint64 timestamp.
|
|
|
|
*/
|
2014-10-19 20:00:24 +02:00
|
|
|
static inline timex_t timex_from_uint64(const uint64_t timestamp)
|
|
|
|
{
|
2017-01-17 16:44:05 +01:00
|
|
|
return timex_set(timestamp / US_PER_SEC, timestamp % US_PER_SEC);
|
2014-10-19 20:00:24 +02:00
|
|
|
}
|
2014-06-06 21:55:45 +02:00
|
|
|
|
2013-06-14 20:38:27 +02:00
|
|
|
/**
|
2014-10-19 21:27:23 +02:00
|
|
|
* @brief Converts a timex timestamp to a string
|
|
|
|
*
|
2015-08-05 16:55:41 +02:00
|
|
|
* @pre memory at timestamp >= TIMEX_MAX_STR_LEN
|
|
|
|
*
|
2014-10-19 21:27:23 +02:00
|
|
|
* @param[in] t The timestamp to convert
|
|
|
|
* @param[out] timestamp The output char buffer for the converted timestamp
|
|
|
|
*
|
|
|
|
* @note The timestamp will be normalized
|
|
|
|
*
|
|
|
|
* @return A pointer to the string representation of the timestamp
|
2013-06-14 20:38:27 +02:00
|
|
|
*/
|
2015-08-05 16:55:41 +02:00
|
|
|
const char *timex_to_str(timex_t t, char *timestamp);
|
2013-06-14 20:38:27 +02:00
|
|
|
|
2014-10-10 11:51:11 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-19 21:27:23 +02:00
|
|
|
/** @} */
|
2016-10-26 21:53:58 +02:00
|
|
|
#endif /* TIMEX_H */
|