-
Notifications
You must be signed in to change notification settings - Fork 13
/
timefuncs.c
88 lines (75 loc) · 1.52 KB
/
timefuncs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "dtypes.h"
#ifdef WIN32
#include <malloc.h>
#include <sys/timeb.h>
#include <windows.h>
#else
#include <sys/time.h>
#include <sys/poll.h>
#include <unistd.h>
#endif
#include "timefuncs.h"
#ifdef WIN32
double floattime(void)
{
struct timeb tstruct;
ftime(&tstruct);
return (double)tstruct.time + (double)tstruct.millitm/1.0e3;
}
#else
double tv2float(struct timeval *tv)
{
return (double)tv->tv_sec + (double)tv->tv_usec/1.0e6;
}
double diff_time(struct timeval *tv1, struct timeval *tv2)
{
return tv2float(tv1) - tv2float(tv2);
}
#endif
// return as many bits of system randomness as we can get our hands on
u_int64_t i64time(void)
{
u_int64_t a;
#ifdef WIN32
struct timeb tstruct;
ftime(&tstruct);
a = (((u_int64_t)tstruct.time)<<32) + (u_int64_t)tstruct.millitm;
#else
struct timeval now;
gettimeofday(&now, NULL);
a = (((u_int64_t)now.tv_sec)<<32) + (u_int64_t)now.tv_usec;
#endif
return a;
}
double clock_now(void)
{
#ifdef WIN32
return floattime();
#else
struct timeval now;
gettimeofday(&now, NULL);
return tv2float(&now);
#endif
}
void sleep_ms(int ms)
{
if (ms == 0)
return;
#ifdef WIN32
Sleep(ms);
#else
struct timeval timeout;
timeout.tv_sec = ms/1000;
timeout.tv_usec = (ms % 1000) * 1000;
select(0, NULL, NULL, NULL, &timeout);
#endif
}