Add util.c
This commit is contained in:
parent
8d9ab31b79
commit
c6d02936e9
1 changed files with 36 additions and 0 deletions
36
util.c
Normal file
36
util.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
#include <time.h>
|
||||
|
||||
#ifdef NMRPFLASH_OSX
|
||||
#include <mach/mach_time.h>
|
||||
#endif
|
||||
|
||||
time_t time_monotonic()
|
||||
{
|
||||
#ifndef NMRPFLASH_WINDOWS
|
||||
#ifndef NMRPFLASH_OSX
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ts.ts_sec;
|
||||
#else
|
||||
static double factor = 0.0;
|
||||
mach_timebase_info_data_t timebase;
|
||||
if (factor == 0.0) {
|
||||
mach_timebase_info(&timebase);
|
||||
factor = (double)timebase.numer / timebase.denom;
|
||||
}
|
||||
|
||||
return round(mach_absolute_time() * factor / 1e9);
|
||||
#endif
|
||||
#else
|
||||
return round(GetTickCount() / 1000.0);
|
||||
#endif
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
time_t beg = time_monotonic();
|
||||
printf("now: %ld\n", beg);
|
||||
sleep(2);
|
||||
printf("+2s: %ld\n", time_monotonic());
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue