wvdbusserver: implement NameHasOwner request.
[wvstreams.git] / include / wvtimeutils.h
blobd4cd298ceaf78dae73c2211633663ecc6f08e274
1 /* -*- Mode: C++ -*-
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 */
5 #ifndef __WVTIMEUTILS_H
6 #define __WVTIMEUTILS_H
8 #ifdef _WIN32
9 #include "winsock2.h"
10 #include <time.h>
11 int gettimeofday(struct timeval *tv, struct timezone *tz);
12 #else
13 #include <sys/time.h>
14 #endif
16 /** Based on (and interchangeable with) struct timeval. */
17 class WvTime : public timeval
19 public:
20 WvTime()
21 { } // WARNING: leaves members uninitialized, like timeval would do!
22 WvTime(long long t)
23 { tv_sec = long(t/1000000L); tv_usec = long(t%1000000L); }
24 WvTime(time_t sec, time_t usec)
25 { tv_sec = long(sec); tv_usec = long(usec); }
26 WvTime(const struct timeval &tv)
27 { tv_sec = tv.tv_sec; tv_usec = tv.tv_usec; }
28 WvTime(const WvTime &tv)
29 { tv_sec = tv.tv_sec; tv_usec = tv.tv_usec; }
31 operator long long() const
32 { return ((long long)tv_sec)*1000000LL + tv_usec; }
35 static const WvTime wvtime_zero(0, 0);
37 /** Returns the number of milliseconds between times a and b (ie a-b).
38 * Value returned is not rounded, it is chopped via integer division
39 * For example: if the result of the subtraction is 0.9, the returned
40 * value is 0 if the result is -0.9, the result is also 0.
42 time_t msecdiff(const WvTime &a, const WvTime &b);
44 /** Returns the current time of day. */
45 WvTime wvtime();
47 /** Adds the specified number of milliseconds to a time value. */
48 WvTime msecadd(const WvTime &a, time_t msec);
50 /** Returns the timeval difference between two timevals (ie a-b). */
51 WvTime tvdiff(const WvTime &a, const WvTime &b);
53 /** Normalizes the time value.
54 * This means that any two 'equally valued' timevals will be converted
55 * so that the seconds and the microseconds are the same in each timeval.
57 inline void normalize(WvTime &tv)
59 tv.tv_sec += tv.tv_usec < 0 ? (tv.tv_usec/1000000)-1 : tv.tv_usec/1000000;
60 tv.tv_usec %= 1000000;
61 tv.tv_usec += tv.tv_usec < 0 ? 1000000 : 0;
64 // Stepped time functions. Used to synchronize wvstreams.
65 const WvTime &wvstime();
66 void wvstime_sync();
68 // This function is just like wvstime_sync(), but will never make the
69 // time go backward.
70 void wvstime_sync_forward();
72 // This sets the time returned by wvstime() to the specified value. To
73 // be used for unit testing.
74 void wvstime_set(const WvTime &);
76 /**
77 * Delay for a requested number of milliseconds.
79 void wvdelay(int msec_delay);
81 #endif // __WVTIMEUTILS_H