2010-12-01 17:13:37 +01:00
|
|
|
#include <timex.h>
|
|
|
|
|
|
|
|
timex_t timex_add(const timex_t a, const timex_t b) {
|
|
|
|
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
|
|
|
|
2011-12-01 13:01:36 +01:00
|
|
|
if (result.microseconds < a.microseconds) {
|
2010-12-01 17:13:37 +01:00
|
|
|
result.seconds++;
|
|
|
|
}
|
|
|
|
|
2011-12-01 13:01:36 +01:00
|
|
|
/* if (result.microseconds > 1000000) {
|
|
|
|
result.microseconds -= 1000000;
|
2010-12-06 16:02:40 +01:00
|
|
|
result.seconds++;
|
|
|
|
}
|
|
|
|
*/
|
2010-12-01 17:13:37 +01:00
|
|
|
return result;
|
|
|
|
}
|
2010-12-01 17:23:28 +01:00
|
|
|
|
2010-12-06 16:02:40 +01:00
|
|
|
void timex_normalize(timex_t *time) {
|
2011-12-01 13:01:36 +01:00
|
|
|
time->seconds += (time->microseconds / 1000000);
|
|
|
|
time->microseconds %= 1000000;
|
2010-12-06 16:02:40 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
timex_t timex_sub(const timex_t a, const timex_t b) {
|
|
|
|
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;
|
|
|
|
}
|
2010-12-06 16:02:40 +01:00
|
|
|
|
|
|
|
int timex_cmp(const timex_t a, const timex_t b) {
|
|
|
|
if (a.seconds < b.seconds) return -1;
|
|
|
|
if (a.seconds == b.seconds) {
|
2011-12-01 13:01:36 +01:00
|
|
|
if (a.microseconds < b.microseconds) return -1;
|
|
|
|
if (a.microseconds == b.microseconds) return 0;
|
2010-12-06 16:02:40 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|