Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #43 from WatchBeam/pingfix
Browse files Browse the repository at this point in the history
Pingfix
  • Loading branch information
sslivins authored Feb 18, 2017
2 parents 3572037 + 108cb5c commit 7d997b2
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions libftl/gettimeofday/gettimeofday.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@

#include "gettimeofday.h"
#include <stdint.h>


#define NSEC_IN_SEC 1000000000
#define USEC_IN_SEC 1000000
#define MSEC_IN_SEC 1000
#define MSEC_IN_USEC 1000
#define MSEC_IN_NSEC 1000000

#define MSEC_TO_SEC(x) ((x) / MSEC_IN_SEC)
#define MSEC_TO_USEC(x) ((x) * MSEC_IN_USEC)
#define MSEC_TO_NSEC(x) ((x) * MSEC_IN_NSEC)
#define SEC_TO_USEC(x) ((x) * USEC_IN_SEC)
#define SEC_TO_NSEC(x) ((x) * NSEC_IN_SEC)

#ifdef _WIN32
#include <Windows.h>

Expand Down Expand Up @@ -58,15 +72,21 @@ int gettimeofday(struct timeval * tp, struct timezone * tzp)
}
#else
void timespec_add_ms(struct timespec *ts, int ms) {
int sec_a, sec_b, usec;
long ns_adjust;
time_t sec_adjust;

sec_adjust = MSEC_TO_SEC((time_t)ms);
ns_adjust = MSEC_TO_NSEC((long)ms);

sec_a = ms / 1000;
ms -= sec_a * 1000;
ns_adjust -= SEC_TO_NSEC((long)sec_adjust);

sec_b = ts->tv_nsec / 1000000000;
ts->tv_nsec -= sec_b * 1000000000;
ts->tv_sec += sec_adjust;
ts->tv_nsec += ns_adjust;

ts->tv_sec += sec_a + sec_b;
if(ts->tv_nsec >= NSEC_IN_SEC) {
ts->tv_nsec -= NSEC_IN_SEC;
ts->tv_sec++;
}
}
#endif // _WIN32

Expand Down Expand Up @@ -99,15 +119,21 @@ int timeval_subtract_to_ms(const struct timeval *end, const struct timeval *star

void timeval_add_ms(struct timeval *tv, int ms)
{
int sec_a, sec_b;
long us_adjust;
time_t sec_adjust;

sec_adjust = MSEC_TO_SEC((time_t)ms);
us_adjust = MSEC_TO_USEC((long)ms);

sec_a = ms / 1000;
ms -= sec_a * 1000;
us_adjust -= SEC_TO_USEC((long)sec_adjust);

sec_b = tv->tv_usec / 1000000;
tv->tv_usec -= sec_b * 1000000;
tv->tv_sec += sec_adjust;
tv->tv_usec += us_adjust;

tv->tv_sec += sec_a + sec_b;
if (tv->tv_usec >= USEC_IN_SEC) {
tv->tv_usec -= USEC_IN_SEC;
tv->tv_sec++;
}
}


Expand Down

0 comments on commit 7d997b2

Please sign in to comment.