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 static WvTime wvstime_cur = wvtime();
00065 
00066 const WvTime &wvstime()
00067 {
00068     return wvstime_cur;
00069 }
00070 
00071 void wvstime_sync()
00072 {
00073     wvstime_cur = wvtime();
00074 }

Generated on Fri Oct 5 18:20:29 2007 for WvStreams by  doxygen 1.5.3