| 1 | /* | 
|---|
| 2 | * Legal Notice | 
|---|
| 3 | * | 
|---|
| 4 | * This document and associated source code (the "Work") is a part of a | 
|---|
| 5 | * benchmark specification maintained by the TPC. | 
|---|
| 6 | * | 
|---|
| 7 | * The TPC reserves all right, title, and interest to the Work as provided | 
|---|
| 8 | * under U.S. and international laws, including without limitation all patent | 
|---|
| 9 | * and trademark rights therein. | 
|---|
| 10 | * | 
|---|
| 11 | * No Warranty | 
|---|
| 12 | * | 
|---|
| 13 | * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION | 
|---|
| 14 | *     CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE | 
|---|
| 15 | *     AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER | 
|---|
| 16 | *     WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, | 
|---|
| 17 | *     INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES, | 
|---|
| 18 | *     DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR | 
|---|
| 19 | *     PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF | 
|---|
| 20 | *     WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE. | 
|---|
| 21 | *     ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, | 
|---|
| 22 | *     QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT | 
|---|
| 23 | *     WITH REGARD TO THE WORK. | 
|---|
| 24 | * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO | 
|---|
| 25 | *     ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE | 
|---|
| 26 | *     COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS | 
|---|
| 27 | *     OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, | 
|---|
| 28 | *     INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY, | 
|---|
| 29 | *     OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT | 
|---|
| 30 | *     RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD | 
|---|
| 31 | *     ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. | 
|---|
| 32 | * | 
|---|
| 33 | * Contributors | 
|---|
| 34 | * - Sergey Vasilevskiy | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #ifndef DATE_TIME_H | 
|---|
| 38 | #define DATE_TIME_H | 
|---|
| 39 |  | 
|---|
| 40 | #ifdef WIN32 | 
|---|
| 41 | #include <windows.h> | 
|---|
| 42 | #endif | 
|---|
| 43 |  | 
|---|
| 44 | #ifdef COMPILE_ODBC_LOAD | 
|---|
| 45 | // ODBC headers | 
|---|
| 46 | #include <sql.h> | 
|---|
| 47 | #include <sqlext.h> | 
|---|
| 48 | #include <odbcss.h> | 
|---|
| 49 | #endif // COMPILE_ODBC_LOAD | 
|---|
| 50 |  | 
|---|
| 51 | #include <sstream> | 
|---|
| 52 |  | 
|---|
| 53 | #include "EGenStandardTypes.h" | 
|---|
| 54 |  | 
|---|
| 55 | namespace TPCE { | 
|---|
| 56 |  | 
|---|
| 57 | // Common datetime structure. | 
|---|
| 58 | // Identical to ODBC's TIMESTAMP_STRUCT | 
|---|
| 59 | // | 
|---|
| 60 | typedef struct tagTIMESTAMP_STRUCT { | 
|---|
| 61 | INT16 year; | 
|---|
| 62 | UINT16 month; | 
|---|
| 63 | UINT16 day; | 
|---|
| 64 | UINT16 hour; | 
|---|
| 65 | UINT16 minute; | 
|---|
| 66 | UINT16 second; | 
|---|
| 67 | UINT32 fraction; | 
|---|
| 68 | } TIMESTAMP_STRUCT; | 
|---|
| 69 |  | 
|---|
| 70 | // Date/Time constants | 
|---|
| 71 | const double NsPerSecondDivisor = 1000000000.0; | 
|---|
| 72 | const INT32 NsPerSecond = 1000000000; | 
|---|
| 73 | const double MsPerSecondDivisor = 1000.000; | 
|---|
| 74 | const INT32 MsPerSecond = 1000; | 
|---|
| 75 | const INT32 SecondsPerMinute = 60; | 
|---|
| 76 | const INT32 MinutesPerHour = 60; | 
|---|
| 77 | const INT32 HoursPerDay = 24; | 
|---|
| 78 | const INT32 HoursPerWorkDay = 8; | 
|---|
| 79 | const INT32 DaysPerWorkWeek = 5; | 
|---|
| 80 | const INT32 DaysPerWeek = 7; | 
|---|
| 81 |  | 
|---|
| 82 | const INT32 SecondsPerHour = SecondsPerMinute * MinutesPerHour; | 
|---|
| 83 | const INT32 SecondsPerDay = SecondsPerMinute * MinutesPerHour * HoursPerDay; | 
|---|
| 84 | const INT32 SecondsPerWorkDay = SecondsPerMinute * MinutesPerHour * HoursPerWorkDay; | 
|---|
| 85 | const INT32 MsPerDay = SecondsPerDay * MsPerSecond; | 
|---|
| 86 | const INT32 MsPerWorkDay = SecondsPerWorkDay * MsPerSecond; | 
|---|
| 87 |  | 
|---|
| 88 | #define RoundToNearestNsec(d_Seconds) (((INT64)(((d_Seconds)*NsPerSecond) + 0.5)) / NsPerSecondDivisor) | 
|---|
| 89 |  | 
|---|
| 90 | class CDateTime { | 
|---|
| 91 | private: | 
|---|
| 92 | INT32 m_dayno;  // absolute day number since 1-Jan-0001, starting from zero | 
|---|
| 93 | INT32 m_msec;   // milliseconds from the beginning of the day | 
|---|
| 94 | char *m_szText; // text representation; only allocated if needed | 
|---|
| 95 |  | 
|---|
| 96 | friend bool operator>(const CDateTime &l_dt, const CDateTime &r_dt); | 
|---|
| 97 |  | 
|---|
| 98 | // | 
|---|
| 99 | // Ranges used for date/time validation | 
|---|
| 100 | // | 
|---|
| 101 | static const INT32 minValidYear = 1; | 
|---|
| 102 | static const INT32 maxValidYear = 9999; | 
|---|
| 103 | static const INT32 minValidMonth = 1; | 
|---|
| 104 | static const INT32 maxValidMonth = 12; | 
|---|
| 105 | static const INT32 minValidDay = 1; | 
|---|
| 106 | static const INT32 maxValidDay = 31; | 
|---|
| 107 | static const INT32 minValidHour = 0; | 
|---|
| 108 | static const INT32 maxValidHour = 23; | 
|---|
| 109 | static const INT32 minValidMinute = 0; | 
|---|
| 110 | static const INT32 maxValidMinute = 59; | 
|---|
| 111 | static const INT32 minValidSecond = 0; | 
|---|
| 112 | static const INT32 maxValidSecond = 59; | 
|---|
| 113 | static const INT32 minValidMilliSecond = 0; | 
|---|
| 114 | static const INT32 maxValidMilliSecond = 999; | 
|---|
| 115 |  | 
|---|
| 116 | static const INT32 minValidDayNumber = 0; | 
|---|
| 117 |  | 
|---|
| 118 | // days in 1-year period (not including any leap year exceptions) = 365 | 
|---|
| 119 | static const INT32 dy1 = 365; | 
|---|
| 120 | // days in 4-year period (not including 400 and 100-year exceptions) = 1,461 | 
|---|
| 121 | static const INT32 dy4 = 4 * dy1 + 1; // fourth year is a leap year | 
|---|
| 122 | // days in 100-year period (not including 400-year exception) = 36,524 | 
|---|
| 123 | static const INT32 dy100 = 25 * dy4 - 1; // 100th year is not a leap year | 
|---|
| 124 | // days in 400-year period = 146,097 | 
|---|
| 125 | static const INT32 dy400 = 4 * dy100 + 1; // 400th year is a leap year | 
|---|
| 126 |  | 
|---|
| 127 | // month array contains days of months for months in a non leap-year | 
|---|
| 128 | static const INT32 monthArray[12]; | 
|---|
| 129 |  | 
|---|
| 130 | // month array contains days of months for months in a leap-year | 
|---|
| 131 | static const INT32 monthArrayLY[12]; | 
|---|
| 132 |  | 
|---|
| 133 | // MonthArray contains cumulative days for months in a non leap-year | 
|---|
| 134 | static const INT32 cumulativeMonthArray[]; | 
|---|
| 135 |  | 
|---|
| 136 | // | 
|---|
| 137 | // Utility routine to calculate the day number for a given year/month/day. | 
|---|
| 138 | // | 
|---|
| 139 | static INT32 CalculateDayNumber(INT32 year, INT32 month, INT32 day); | 
|---|
| 140 |  | 
|---|
| 141 | // | 
|---|
| 142 | // Validation routines used to check inputs to constructors and Set methods. | 
|---|
| 143 | // | 
|---|
| 144 | static bool IsValid(INT32 year, INT32 month, INT32 day, INT32 hour, INT32 minute, INT32 second, INT32 msec); | 
|---|
| 145 | static void Validate(INT32 year, INT32 month, INT32 day, INT32 hour, INT32 minute, INT32 second, INT32 msec); | 
|---|
| 146 | static void Validate(INT32 dayNumber); | 
|---|
| 147 | static void Validate(INT32 dayNumber, INT32 msecSoFarToday); | 
|---|
| 148 |  | 
|---|
| 149 | // | 
|---|
| 150 | // Leap Year determinination routine. | 
|---|
| 151 | // | 
|---|
| 152 | static bool IsLeapYear(INT32 year); | 
|---|
| 153 |  | 
|---|
| 154 | public: | 
|---|
| 155 | CDateTime(void);        // current local date/time | 
|---|
| 156 | CDateTime(INT32 dayno); // date as specified; time set to 0:00:00 (midnight) | 
|---|
| 157 | CDateTime(INT32 year, INT32 month, | 
|---|
| 158 | INT32 day); // date as specified; time set to 0:00:00 (midnight) | 
|---|
| 159 | CDateTime(INT32 year, INT32 month, INT32 day, INT32 hour, INT32 minute, INT32 second, INT32 msec); | 
|---|
| 160 |  | 
|---|
| 161 | CDateTime(TIMESTAMP_STRUCT *ts); // date specified in the TIMESTAMP struct | 
|---|
| 162 |  | 
|---|
| 163 | CDateTime(const CDateTime &dt); // proper copy constructor - does not copy m_szText | 
|---|
| 164 | ~CDateTime(void); | 
|---|
| 165 |  | 
|---|
| 166 | void Set(void);        // set to current local date/time | 
|---|
| 167 | void Set(INT32 dayno); // set to specified day number | 
|---|
| 168 | void Set(INT32 year, INT32 month, | 
|---|
| 169 | INT32 day); // set to specified date; time set to 0:00:00 (midnight) | 
|---|
| 170 | void Set(INT32 hour, INT32 minute, INT32 second, | 
|---|
| 171 | INT32 msec); // set to specified time, date not changed. | 
|---|
| 172 | void Set(INT32 year, INT32 month, INT32 day, INT32 hour, INT32 minute, INT32 second, INT32 msec); | 
|---|
| 173 |  | 
|---|
| 174 | inline INT32 DayNo(void) { | 
|---|
| 175 | return m_dayno; | 
|---|
| 176 | }; | 
|---|
| 177 | inline INT32 MSec(void) { | 
|---|
| 178 | return m_msec; | 
|---|
| 179 | }; | 
|---|
| 180 | void GetYMD(INT32 *year, INT32 *month, INT32 *day); | 
|---|
| 181 | void GetYMDHMS(INT32 *year, INT32 *month, INT32 *day, INT32 *hour, INT32 *minute, INT32 *second, INT32 *msec); | 
|---|
| 182 | void GetHMS(INT32 *hour, INT32 *minute, INT32 *second, INT32 *msec); | 
|---|
| 183 |  | 
|---|
| 184 | void GetTimeStamp(TIMESTAMP_STRUCT *ts); | 
|---|
| 185 |  | 
|---|
| 186 | #ifdef COMPILE_ODBC_LOAD | 
|---|
| 187 | void GetDBDATETIME(DBDATETIME *dt); | 
|---|
| 188 | #endif // COMPILE_ODBC_LOAD | 
|---|
| 189 |  | 
|---|
| 190 | static INT32 YMDtoDayno(INT32 yr, INT32 mm, INT32 dd); | 
|---|
| 191 | char *ToStr(INT32 style); | 
|---|
| 192 |  | 
|---|
| 193 | void Add(INT32 days, INT32 msec, bool adjust_weekend = false); | 
|---|
| 194 | void AddMinutes(INT32 Minutes); | 
|---|
| 195 | void AddWorkMs(INT64 WorkMs); | 
|---|
| 196 |  | 
|---|
| 197 | bool operator<(const CDateTime &); | 
|---|
| 198 | bool operator<=(const CDateTime &); | 
|---|
| 199 | // operator > is defined as an external (not in-class) operator in | 
|---|
| 200 | // CDateTime.cpp | 
|---|
| 201 | bool operator>=(const CDateTime &); | 
|---|
| 202 | bool operator==(const CDateTime &); | 
|---|
| 203 |  | 
|---|
| 204 | // compute the difference between two DateTimes; | 
|---|
| 205 | // result in seconds | 
|---|
| 206 | double operator-(const CDateTime &dt); | 
|---|
| 207 | INT32 DiffInMilliSeconds(const CDateTime &BaseTime); | 
|---|
| 208 | INT32 DiffInMilliSeconds(CDateTime *pBaseTime); | 
|---|
| 209 | // add another DateTime to this one | 
|---|
| 210 | CDateTime &operator+=(const CDateTime &dt); | 
|---|
| 211 | // Proper assignment operator - does not copy szText | 
|---|
| 212 | CDateTime &operator=(const CDateTime &dt); | 
|---|
| 213 | }; | 
|---|
| 214 |  | 
|---|
| 215 | } // namespace TPCE | 
|---|
| 216 |  | 
|---|
| 217 | #endif // DATE_TIME_H | 
|---|
| 218 |  | 
|---|