wvtimeutils.cc

00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Various little time functions...
00006  */
00007 #include <limits.h>
00008 
00009 #include "wvtimeutils.h"
00010 
00011 time_t msecdiff(const WvTime &a, const WvTime &b)
00012 {
00013     long long secdiff = a.tv_sec - b.tv_sec;
00014     long long usecdiff = a.tv_usec - b.tv_usec;
00015     long long msecs = secdiff * 1000 + usecdiff / 1000;
00016 
00017     time_t rval;
00018     if (msecs > INT_MAX)
00019         rval = INT_MAX;
00020     else if (msecs < INT_MIN)
00021         rval = INT_MIN;
00022     else
00023         rval = msecs;
00024     return rval;
00025 }
00026 
00027 
00028 WvTime wvtime()
00029 {
00030     struct timeval tv;
00031     gettimeofday(&tv, 0);
00032     return tv;
00033 }
00034 
00035 
00036 WvTime msecadd(const WvTime &a, time_t msec)
00037 {
00038     WvTime b;
00039     b.tv_sec = a.tv_sec + msec / 1000;
00040     b.tv_usec = a.tv_usec + (msec % 1000) * 1000;
00041     normalize(b);
00042     return b;
00043 }
00044 
00045 
00046 WvTime tvdiff(const WvTime &a, const WvTime &b)
00047 {
00048     WvTime c;
00049     c.tv_sec = a.tv_sec - b.tv_sec;
00050     c.tv_usec = a.tv_usec;
00051 
00052     if (b.tv_usec > a.tv_usec)
00053     {
00054         c.tv_sec--;
00055         c.tv_usec += 1000000;
00056     }
00057 
00058     c.tv_usec -= b.tv_usec;
00059 
00060     normalize(c);
00061     return c;
00062 }
00063 
00064 
00065 static WvTime wvstime_cur = wvtime();
00066 
00067 
00068 const WvTime &wvstime()
00069 {
00070     return wvstime_cur;
00071 }
00072 
00073 
00074 static void do_wvstime_sync(bool forward_only)
00075 {
00076     if (!forward_only)
00077     {
00078         wvstime_cur = wvtime();
00079     }
00080     else
00081     {
00082         WvTime now = wvtime();
00083         if (wvstime_cur < now)
00084             wvstime_cur = now;
00085     }
00086 }
00087 
00088 
00089 void wvstime_sync()
00090 {
00091     do_wvstime_sync(false);
00092 }
00093 
00094 
00095 void wvstime_sync_forward()
00096 {
00097     do_wvstime_sync(true);
00098 }
00099 
00100 
00101 void wvstime_set(const WvTime &_new_time)
00102 {
00103     wvstime_cur = _new_time;
00104 }
00105 

Generated on Thu Jan 24 16:50:57 2008 for WvStreams by  doxygen 1.5.4