The LIBS line was apparently accidentally deleted from configure-mingw32.
[wvstreams.git] / utils / wvtimeutils.cc
blob0b7b31cef646b6045f03dfc69dfeca3fc797974f
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
5 * Various little time functions...
6 */
7 #include <limits.h>
9 #include "wvtimeutils.h"
11 time_t msecdiff(const WvTime &a, const WvTime &b)
13 long long secdiff = a.tv_sec - b.tv_sec;
14 long long usecdiff = a.tv_usec - b.tv_usec;
15 long long msecs = secdiff * 1000 + usecdiff / 1000;
17 time_t rval;
18 if (msecs > INT_MAX)
19 rval = INT_MAX;
20 else if (msecs < INT_MIN)
21 rval = INT_MIN;
22 else
23 rval = msecs;
24 return rval;
28 WvTime wvtime()
30 struct timeval tv;
31 gettimeofday(&tv, 0);
32 return tv;
36 WvTime msecadd(const WvTime &a, time_t msec)
38 WvTime b;
39 b.tv_sec = a.tv_sec + msec / 1000;
40 b.tv_usec = a.tv_usec + (msec % 1000) * 1000;
41 normalize(b);
42 return b;
46 WvTime tvdiff(const WvTime &a, const WvTime &b)
48 WvTime c;
49 c.tv_sec = a.tv_sec - b.tv_sec;
50 c.tv_usec = a.tv_usec;
52 if (b.tv_usec > a.tv_usec)
54 c.tv_sec--;
55 c.tv_usec += 1000000;
58 c.tv_usec -= b.tv_usec;
60 normalize(c);
61 return c;
65 static WvTime wvstime_cur = wvtime();
68 const WvTime &wvstime()
70 return wvstime_cur;
74 static void do_wvstime_sync(bool forward_only)
76 if (!forward_only)
78 wvstime_cur = wvtime();
80 else
82 WvTime now = wvtime();
83 if (wvstime_cur < now)
84 wvstime_cur = now;
89 void wvstime_sync()
91 do_wvstime_sync(false);
95 void wvstime_sync_forward()
97 do_wvstime_sync(true);
101 void wvstime_set(const WvTime &_new_time)
103 wvstime_cur = _new_time;