WvStreams
wvtimeutils.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  */
00005 #ifndef __WVTIMEUTILS_H
00006 #define __WVTIMEUTILS_H
00007 
00008 #ifdef _WIN32
00009 #include "winsock2.h"
00010 #include <time.h>
00011 int gettimeofday(struct timeval *tv, struct timezone *tz);
00012 #else
00013 #include <sys/time.h>
00014 #endif
00015 
00017 class WvTime : public timeval
00018 {
00019 public:
00020     WvTime()
00021         { } // WARNING: leaves members uninitialized, like timeval would do!
00022     WvTime(long long t)
00023         { tv_sec = long(t/1000000L); tv_usec = long(t%1000000L); }
00024     WvTime(time_t sec, time_t usec)
00025         { tv_sec = long(sec); tv_usec = long(usec); }
00026     WvTime(const struct timeval &tv)
00027         { tv_sec = tv.tv_sec; tv_usec = tv.tv_usec; }
00028     WvTime(const WvTime &tv)
00029         { tv_sec = tv.tv_sec; tv_usec = tv.tv_usec; }
00030     
00031     operator long long() const
00032         { return ((long long)tv_sec)*1000000LL + tv_usec; }
00033 };
00034 
00035 static const WvTime wvtime_zero(0, 0);
00036 
00042 time_t msecdiff(const WvTime &a, const WvTime &b);
00043 
00045 WvTime wvtime();
00046 
00048 WvTime msecadd(const WvTime &a, time_t msec);
00049 
00051 WvTime tvdiff(const WvTime &a, const WvTime &b);
00052 
00057 inline void normalize(WvTime &tv)
00058 {
00059     tv.tv_sec += tv.tv_usec < 0 ? (tv.tv_usec/1000000)-1 : tv.tv_usec/1000000;
00060     tv.tv_usec %= 1000000;
00061     tv.tv_usec += tv.tv_usec < 0 ? 1000000 : 0;
00062 }
00063 
00064 // Stepped time functions.  Used to synchronize wvstreams.
00065 const WvTime &wvstime(); 
00066 void wvstime_sync();
00067 
00068 // This function is just like wvstime_sync(), but will never make the
00069 // time go backward.
00070 void wvstime_sync_forward();
00071 
00072 // This sets the time returned by wvstime() to the specified value. To
00073 // be used for unit testing.
00074 void wvstime_set(const WvTime &);
00075 
00079 void wvdelay(int msec_delay);
00080 
00081 #endif // __WVTIMEUTILS_H