2014-01-24 18:57:04 +01:00
|
|
|
/**
|
|
|
|
* Timex implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, 2010, 2013, 2014 Freie Universitaet Berlin (FUB).
|
2014-10-19 20:44:10 +02:00
|
|
|
* Copyright (C) 2013, 2014 INRIA.
|
2014-01-24 18:57:04 +01:00
|
|
|
*
|
2014-07-31 19:45:27 +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.
|
2014-01-24 18:57:04 +01:00
|
|
|
*/
|
2014-02-11 18:15:43 +01:00
|
|
|
|
2014-01-24 18:57:04 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
|
|
|
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
|
2014-06-06 21:55:45 +02:00
|
|
|
* @author Daniel Jentsch <d.jentsch@fu-berlin.de>
|
2014-01-24 18:57:04 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-06-14 20:38:27 +02:00
|
|
|
#include <stdio.h>
|
2013-06-20 14:59:42 +02:00
|
|
|
#include <inttypes.h>
|
2013-06-14 20:38:27 +02:00
|
|
|
|
2020-10-22 11:35:22 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2013-12-25 18:13:55 +01:00
|
|
|
#include "debug.h"
|
|
|
|
|
2013-06-14 20:38:27 +02:00
|
|
|
#include "timex.h"
|
2010-12-01 17:13:37 +01:00
|
|
|
|
2013-06-20 14:59:42 +02:00
|
|
|
timex_t timex_add(const timex_t a, const timex_t b)
|
|
|
|
{
|
2020-10-23 00:40:57 +02:00
|
|
|
if (IS_ACTIVE(ENABLE_DEBUG)) {
|
|
|
|
if (!timex_isnormalized(&a) || !timex_isnormalized(&b)) {
|
|
|
|
puts("timex_add on denormalized value");
|
|
|
|
}
|
2013-12-25 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
2010-12-01 17:13:37 +01:00
|
|
|
timex_t result;
|
|
|
|
result.seconds = a.seconds + b.seconds;
|
2011-12-01 13:01:36 +01:00
|
|
|
result.microseconds = a.microseconds + b.microseconds;
|
2010-12-01 17:13:37 +01:00
|
|
|
|
2017-01-17 16:44:05 +01:00
|
|
|
if (result.microseconds > US_PER_SEC) {
|
|
|
|
result.microseconds -= US_PER_SEC;
|
2010-12-01 17:13:37 +01:00
|
|
|
result.seconds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2010-12-01 17:23:28 +01:00
|
|
|
|
2013-06-20 14:59:42 +02:00
|
|
|
timex_t timex_set(uint32_t seconds, uint32_t microseconds)
|
|
|
|
{
|
2010-12-01 17:23:28 +01:00
|
|
|
timex_t result;
|
|
|
|
result.seconds = seconds;
|
2011-12-01 13:01:36 +01:00
|
|
|
result.microseconds = microseconds;
|
2010-12-01 17:23:28 +01:00
|
|
|
|
2020-10-23 00:40:57 +02:00
|
|
|
if (IS_ACTIVE(ENABLE_DEBUG)) {
|
|
|
|
if (!timex_isnormalized(&result)) {
|
|
|
|
puts("timex_set on denormalized value");
|
|
|
|
}
|
2013-12-25 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
2010-12-01 17:23:28 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:59:42 +02:00
|
|
|
timex_t timex_sub(const timex_t a, const timex_t b)
|
|
|
|
{
|
2020-10-23 00:40:57 +02:00
|
|
|
if (IS_ACTIVE(ENABLE_DEBUG)) {
|
|
|
|
if (!timex_isnormalized(&a) || !timex_isnormalized(&b)) {
|
|
|
|
puts("timex_sub on denormalized value");
|
|
|
|
}
|
2013-12-25 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
2010-12-01 17:23:28 +01:00
|
|
|
timex_t result;
|
2014-06-06 21:55:45 +02:00
|
|
|
|
|
|
|
if (a.microseconds >= b.microseconds) {
|
|
|
|
result.seconds = a.seconds - b.seconds;
|
|
|
|
result.microseconds = a.microseconds - b.microseconds;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.seconds = a.seconds - b.seconds - 1;
|
2017-01-17 16:44:05 +01:00
|
|
|
result.microseconds = a.microseconds + US_PER_SEC - b.microseconds;
|
2014-06-06 21:55:45 +02:00
|
|
|
}
|
2010-12-01 17:23:28 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2010-12-06 16:02:40 +01:00
|
|
|
|
2013-06-20 14:59:42 +02:00
|
|
|
int timex_cmp(const timex_t a, const timex_t b)
|
|
|
|
{
|
2020-10-23 00:40:57 +02:00
|
|
|
if (IS_ACTIVE(ENABLE_DEBUG)) {
|
|
|
|
if (!timex_isnormalized(&a) || !timex_isnormalized(&b)) {
|
|
|
|
puts("timex_cmp on denormalized value");
|
|
|
|
}
|
2013-12-25 18:13:55 +01:00
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (a.seconds < b.seconds) {
|
2013-06-20 14:59:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (a.seconds == b.seconds) {
|
|
|
|
if (a.microseconds < b.microseconds) {
|
2013-06-20 14:59:42 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (a.microseconds == b.microseconds) {
|
2013-06-20 14:59:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-12-06 16:02:40 +01:00
|
|
|
}
|
2013-06-20 14:59:42 +02:00
|
|
|
|
2010-12-06 16:02:40 +01:00
|
|
|
return 1;
|
|
|
|
}
|