1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00
RIOT/sys/timex/timex.c

110 lines
2.2 KiB
C
Raw Normal View History

#include <stdio.h>
2013-06-20 14:59:42 +02:00
#include <inttypes.h>
2013-12-25 18:13:55 +01:00
#define ENABLE_DEBUG (0)
#include "debug.h"
#include "timex.h"
2010-12-01 17:13:37 +01:00
2013-12-25 17:01:55 +01:00
#define SEC_IN_USEC 1000000
2013-06-20 14:59:42 +02:00
timex_t timex_add(const timex_t a, const timex_t b)
{
2013-12-25 18:13:55 +01:00
#if ENABLE_DEBUG
if (!timex_isnormalized(a) || !timex_isnormalized(b)) {
puts("timex_add on denormalized value");
}
#endif
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
if (result.microseconds < a.microseconds) {
2010-12-01 17:13:37 +01:00
result.seconds++;
}
2013-12-25 17:01:55 +01:00
/* if (result.microseconds > SEC_IN_USEC) {
result.microseconds -= SEC_IN_USEC;
2013-06-20 14:59:42 +02:00
result.seconds++;
}
*/
2010-12-01 17:13:37 +01:00
return result;
}
2010-12-01 17:23:28 +01:00
2013-06-20 14:59:42 +02:00
void timex_normalize(timex_t *time)
{
2013-12-25 17:01:55 +01:00
time->seconds += (time->microseconds / SEC_IN_USEC);
time->microseconds %= SEC_IN_USEC;
}
2013-12-25 18:13:55 +01:00
int timex_isnormalized(timex_t *time)
{
return (time->microseconds < SEC_IN_USEC);
}
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
2013-12-25 18:13:55 +01:00
#if ENABLE_DEBUG
if (!timex_isnormalized(result)) {
puts("timex_set on denormalized value");
}
#endif
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)
{
2013-12-25 18:13:55 +01:00
#if ENABLE_DEBUG
if (!timex_isnormalized(a) || !timex_isnormalized(b)) {
puts("timex_sub on denormalized value");
}
#endif
2010-12-01 17:23:28 +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:23:28 +01:00
return result;
}
2013-06-20 14:59:42 +02:00
int timex_cmp(const timex_t a, const timex_t b)
{
2013-12-25 18:13:55 +01:00
#if ENABLE_DEBUG
if (!timex_isnormalized(a) || !timex_isnormalized(b)) {
puts("timex_cmp on denormalized value");
}
#endif
if (a.seconds < b.seconds) {
2013-06-20 14:59:42 +02:00
return -1;
}
if (a.seconds == b.seconds) {
if (a.microseconds < b.microseconds) {
2013-06-20 14:59:42 +02:00
return -1;
}
if (a.microseconds == b.microseconds) {
2013-06-20 14:59:42 +02:00
return 0;
}
}
2013-06-20 14:59:42 +02:00
return 1;
}
2013-12-25 17:01:55 +01:00
uint64_t timex_uint64(const timex_t a)
{
return (uint64_t) a.seconds * SEC_IN_USEC + a.microseconds;
}
2013-06-20 14:59:42 +02:00
void timex_print(const timex_t t)
{
printf("Seconds: %"PRIu32" - Microseconds: %"PRIu32"\n", t.seconds, t.microseconds);
}