mux/src/timeutil.h

Go to the documentation of this file.
00001 // timeutil.h -- CLinearTimeAbsolute, and CLinearTimeDelta modules.
00002 //
00003 // $Id: timeutil.h,v 1.21 2006/10/05 06:08:02 sdennis Exp $
00004 //
00005 // MUX 2.4
00006 // Copyright (C) 1998 through 2004 Solid Vertical Domains, Ltd. All
00007 // rights not explicitly given are reserved.
00008 //
00009 // Date/Time code based on algorithms presented in "Calendrical Calculations",
00010 // Cambridge Press, 1998.
00011 //
00012 #ifndef TIMEUTIL_H
00013 #define TIMEUTIL_H
00014 
00015 typedef struct
00016 {
00017              short iYear;       // 1900 would be stored as 1900.
00018     unsigned short iMonth;      // January is 1. December is 12.
00019     unsigned short iDayOfWeek;  // 0 is Sunday, 1 is Monday, etc.
00020     unsigned short iDayOfMonth; // Day of Month, 1..31
00021     unsigned short iDayOfYear;  // January 1st is 1, etc.
00022     unsigned short iHour;
00023     unsigned short iMinute;
00024     unsigned short iSecond;
00025     unsigned short iMillisecond; // Milliseconds less than a second.
00026     unsigned short iMicrosecond; // Microseconds less than a Millisecond.
00027     unsigned short iNanosecond;  // Nanoseconds less than a Microsecond.
00028 } FIELDEDTIME;
00029 
00030 class CLinearTimeDelta;
00031 
00032 class CLinearTimeAbsolute
00033 {
00034     friend class CLinearTimeDelta;
00035     friend bool operator<(const CLinearTimeAbsolute& lta, const CLinearTimeAbsolute& ltb);
00036     friend bool operator>(const CLinearTimeAbsolute& lta, const CLinearTimeAbsolute& ltb);
00037     friend bool operator==(const CLinearTimeAbsolute& lta, const CLinearTimeAbsolute& ltb);
00038     friend bool operator<=(const CLinearTimeAbsolute& lta, const CLinearTimeAbsolute& ltb);
00039     friend CLinearTimeAbsolute operator+(const CLinearTimeAbsolute& lta, const CLinearTimeDelta& ltd);
00040     friend CLinearTimeAbsolute operator-(const CLinearTimeAbsolute& lta, const CLinearTimeDelta& ltd);
00041     friend CLinearTimeDelta operator-(const CLinearTimeAbsolute& lta, const CLinearTimeAbsolute& ltb);
00042 
00043 private:
00044     INT64  m_tAbsolute;
00045     static int m_nCount;
00046     static char m_Buffer[204];
00047 
00048 public:
00049     //CLinearTimeAbsolute(int tInitial);
00050     CLinearTimeAbsolute(void);
00051     CLinearTimeAbsolute(const CLinearTimeAbsolute& ltaOrigin, const CLinearTimeDelta& ltdOffset);
00052     CLinearTimeAbsolute(const CLinearTimeAbsolute& lta);
00053     void operator=(const CLinearTimeAbsolute& lta);
00054     void operator+=(const CLinearTimeDelta& ltdOffset);
00055     void operator-=(const CLinearTimeDelta& ltdOffset);
00056 
00057     void GetUTC(void);
00058     void GetLocal(void);
00059 
00060     void  ReturnUniqueString(char *buffer);
00061     char *ReturnDateString(int nFracDigits = 0);
00062     bool  ReturnFields(FIELDEDTIME *arg_tStruct);
00063     INT64 ReturnSeconds(void);
00064     char *ReturnSecondsString(int nFracDigits = 0);
00065     INT64 Return100ns(void);
00066 
00067     void SetSeconds(INT64 arg_tSeconds);
00068     bool SetSecondsString(char *arg_szSeconds);
00069     bool SetFields(FIELDEDTIME *arg_tStruct);
00070     bool SetString(const char *arg_tBuffer);
00071     void Set100ns(INT64 arg_t100ns);
00072 
00073     void UTC2Local(void);
00074     void Local2UTC(void);
00075 };
00076 
00077 bool FieldedTimeToLinearTime(FIELDEDTIME *ft, INT64 *plt);
00078 bool LinearTimeToFieldedTime(INT64 lt, FIELDEDTIME *ft);
00079 
00080 class CLinearTimeDelta
00081 {
00082     friend class CLinearTimeAbsolute;
00083     friend bool operator<(const CLinearTimeDelta& lta, const CLinearTimeDelta& ltb);
00084     friend bool operator>(const CLinearTimeDelta& lta, const CLinearTimeDelta& ltb);
00085     friend bool operator==(const CLinearTimeDelta& lta, const CLinearTimeDelta& ltb);
00086     friend bool operator<=(const CLinearTimeDelta& lta, const CLinearTimeDelta& ltb);
00087     friend bool operator!=(const CLinearTimeDelta& lta, const CLinearTimeDelta& ltb);
00088     friend CLinearTimeDelta operator-(const CLinearTimeAbsolute& lta, const CLinearTimeAbsolute& ltb);
00089     friend CLinearTimeDelta operator-(const CLinearTimeDelta& lta, const CLinearTimeDelta& ltb);
00090     friend int operator/(const CLinearTimeDelta& ltdA, const CLinearTimeDelta& ltdB);
00091     friend CLinearTimeDelta operator*(const CLinearTimeDelta& ltdA, int nScaler);
00092     friend CLinearTimeAbsolute operator+(const CLinearTimeAbsolute& ltdA, const CLinearTimeDelta& ltdB);
00093     friend CLinearTimeAbsolute operator-(const CLinearTimeAbsolute& lta, const CLinearTimeDelta& ltd);
00094 
00095 private:
00096     INT64 m_tDelta;
00097     static char m_Buffer[204];
00098 
00099 public:
00100     CLinearTimeDelta(void);
00101     CLinearTimeDelta(INT64 arg_t100ns);
00102     CLinearTimeDelta(CLinearTimeAbsolute, CLinearTimeAbsolute);
00103 
00104     void ReturnTimeValueStruct(struct timeval *tv);
00105 #ifdef HAVE_NANOSLEEP
00106     void ReturnTimeSpecStruct(struct timespec *ts);
00107 #endif
00108     long ReturnMilliseconds(void);
00109     INT64 ReturnMicroseconds(void);
00110     long ReturnDays(void);
00111     long ReturnSeconds(void);
00112     char *ReturnSecondsString(int nFracDigits = 0);
00113     INT64 Return100ns(void);
00114 
00115     void SetTimeValueStruct(struct timeval *tv);
00116     void SetMilliseconds(unsigned long arg_dwMilliseconds);
00117     void SetSeconds(INT64 arg_tSeconds);
00118     void SetSecondsString(char *arg_szSeconds);
00119     void Set100ns(INT64 arg_t100ns);
00120 
00121     void operator+=(const CLinearTimeDelta& ltd);
00122 };
00123 
00124 class CMuxAlarm
00125 {
00126 private:
00127     bool bAlarmSet;
00128 #ifdef WIN32
00129     HANDLE hThread;
00130 #endif
00131 
00132 public:
00133     bool bAlarmed;
00134 
00135 #ifdef WIN32
00136     volatile HANDLE hSemAlarm;
00137     volatile DWORD  dwWait;
00138     ~CMuxAlarm();
00139 #endif
00140 
00141     CMuxAlarm(void);
00142     void Sleep(CLinearTimeDelta ltd);
00143     void SurrenderSlice(void);
00144 
00145     void Set(CLinearTimeDelta ltdDeadline);
00146     void Clear(void);
00147 
00148 #ifndef WIN32
00149     void Signal(void);
00150 #endif
00151 };
00152 
00153 extern CMuxAlarm MuxAlarm;
00154 
00155 #define FACTOR_NANOSECONDS_PER_100NS 100
00156 #define FACTOR_100NS_PER_MICROSECOND 10
00157 #define FACTOR_100NS_PER_MILLISECOND 10000
00158 #ifdef WIN32
00159 #define EPOCH_OFFSET 116444736000000000i64
00160 #define EARLIEST_VALID_DATE (-9106391088000000000i64)
00161 #define LATEST_VALID_DATE   ( 9222834959999999999i64)
00162 #define TIMEUTIL_TIME_T_MIN_VALUE (-922283539200i64)
00163 #define TIMEUTIL_TIME_T_MAX_VALUE ( 910638979199i64)
00164 #else // WIN32
00165 #define EPOCH_OFFSET 116444736000000000ll
00166 #define EARLIEST_VALID_DATE (-9106391088000000000ll)
00167 #define LATEST_VALID_DATE   ( 9222834959999999999ll)
00168 #define TIMEUTIL_TIME_T_MIN_VALUE (-922283539200ll)
00169 #define TIMEUTIL_TIME_T_MAX_VALUE ( 910638979199ll)
00170 #endif // WIN32
00171 extern const INT64 FACTOR_100NS_PER_SECOND;
00172 extern const INT64 FACTOR_100NS_PER_MINUTE;
00173 extern const INT64 FACTOR_100NS_PER_HOUR;
00174 extern const INT64 FACTOR_100NS_PER_DAY;
00175 extern const INT64 FACTOR_100NS_PER_WEEK;
00176 
00177 const CLinearTimeDelta time_200us = 200*FACTOR_100NS_PER_MICROSECOND;
00178 const CLinearTimeDelta time_5ms   = 5*FACTOR_100NS_PER_MILLISECOND;
00179 const CLinearTimeDelta time_250ms = 250*FACTOR_100NS_PER_MILLISECOND;
00180 const CLinearTimeDelta time_1s    = FACTOR_100NS_PER_SECOND;
00181 const CLinearTimeDelta time_15s   = 15*FACTOR_100NS_PER_SECOND;
00182 const CLinearTimeDelta time_30s   = 30*FACTOR_100NS_PER_SECOND;
00183 const CLinearTimeDelta time_45s   = 45*FACTOR_100NS_PER_SECOND;
00184 const CLinearTimeDelta time_15m   = 15*FACTOR_100NS_PER_MINUTE;
00185 const CLinearTimeDelta time_30m   = 30*FACTOR_100NS_PER_MINUTE;
00186 const CLinearTimeDelta time_1w    = FACTOR_100NS_PER_WEEK;
00187 
00188 extern void TIME_Initialize(void);
00189 #ifdef WIN32
00190 extern bool CalibrateQueryPerformance(void);
00191 #endif
00192 
00193 #ifdef SMALLEST_INT_GTE_NEG_QUOTIENT
00194 INT64 i64Mod(INT64 x, INT64 y);
00195 INT64 i64FloorDivision(INT64 x, INT64 y);
00196 DCL_INLINE INT64 i64Division(INT64 x, INT64 y) { return x / y; }
00197 DCL_INLINE INT64 i64Remainder(INT64 x, INT64 y) { return x % y; }
00198 int iFloorDivisionMod(int x, int y, int *piMod);
00199 #else // SMALLEST_INT_GTE_NEG_QUOTIENT
00200 DCL_INLINE INT64 i64Mod(INT64 x, INT64 y) { return x % y; }
00201 DCL_INLINE INT64 i64FloorDivision(INT64 x, INT64 y) { return x / y; }
00202 INT64 i64Division(INT64 x, INT64 y);
00203 INT64 i64Remainder(INT64 x, INT64 y);
00204 int DCL_INLINE iFloorDivisionMod(int x, int y, int *piMod) \
00205 {                   \
00206     *piMod = x % y; \
00207     return x / y;   \
00208 }
00209 
00210 #endif // SMALLEST_INT_GTE_NEG_QUOTIENT
00211 
00212 extern bool ParseDate(CLinearTimeAbsolute &lta, char *pDateString, bool *pbZoneSpecified);
00213 extern bool isLeapYear(long iYear);
00214 extern const char *monthtab[];
00215 extern char *DayOfWeekString[];
00216 extern int iMod(int x, int y);
00217 
00218 #endif // TIMEUTIL_H

Generated on Mon May 28 04:40:12 2007 for MUX by  doxygen 1.4.7