forked from wlach/wvstreams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwvtimeutils.cc
117 lines (92 loc) · 1.71 KB
/
wvtimeutils.cc
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* Various little time functions...
*/
#include "wvtimeutils.h"
#include <limits.h>
#ifndef _MSC_VER
#include <unistd.h>
#include <utime.h>
#endif
time_t msecdiff(const WvTime &a, const WvTime &b)
{
long long secdiff = a.tv_sec - b.tv_sec;
long long usecdiff = a.tv_usec - b.tv_usec;
long long msecs = secdiff * 1000 + usecdiff / 1000;
time_t rval;
if (msecs > INT_MAX)
rval = INT_MAX;
else if (msecs < INT_MIN)
rval = INT_MIN;
else
rval = msecs;
return rval;
}
WvTime wvtime()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv;
}
WvTime msecadd(const WvTime &a, time_t msec)
{
WvTime b;
b.tv_sec = a.tv_sec + msec / 1000;
b.tv_usec = a.tv_usec + (msec % 1000) * 1000;
normalize(b);
return b;
}
WvTime tvdiff(const WvTime &a, const WvTime &b)
{
WvTime c;
c.tv_sec = a.tv_sec - b.tv_sec;
c.tv_usec = a.tv_usec;
if (b.tv_usec > a.tv_usec)
{
c.tv_sec--;
c.tv_usec += 1000000;
}
c.tv_usec -= b.tv_usec;
normalize(c);
return c;
}
static WvTime wvstime_cur = wvtime();
const WvTime &wvstime()
{
return wvstime_cur;
}
static void do_wvstime_sync(bool forward_only)
{
if (!forward_only)
{
wvstime_cur = wvtime();
}
else
{
WvTime now = wvtime();
if (wvstime_cur < now)
wvstime_cur = now;
}
}
void wvstime_sync()
{
do_wvstime_sync(false);
}
void wvstime_sync_forward()
{
do_wvstime_sync(true);
}
void wvstime_set(const WvTime &_new_time)
{
wvstime_cur = _new_time;
}
void wvdelay(int msec_delay)
{
#ifdef _WIN32
Sleep(msec_delay);
#else
usleep(msec_delay * 1000);
#endif
}