Mobile API Reference  MicroStrategy 2019
DateTime.h
Go to the documentation of this file.
1 //==============================================================================================
2 // FILENAME : DateTime.h
3 // AUTHOR : Yuxiao Xiao
4 // CREATION : 8/24/01
5 // Copyright (C) MicroStrategy, Inc. 2001
6 //==============================================================================================
7 #ifndef MDataType_DateTime_h
8 #define MDataType_DateTime_h
9 
10 #include "DataType.h"
11 
12 #include "TimeSpan.h"
13 #include "Base/Base/ReturnString.h"
14 
15 namespace MDataType
16 {
17 #ifndef DATETIME_MSISTRUCTURE
18 #define DATETIME_MSISTRUCTURE
19  typedef struct MsiDateTime
20  {
29 
32 
34  } MsiDateTime; // for backward compatibility
35 
36  typedef struct ODBCDateTime
37  {
38  unsigned short year;
39  unsigned short month;
40  unsigned short day;
41  unsigned short hour;
42  unsigned short minute;
43  unsigned short second;
44  unsigned Int32 fraction; // nanosecond
45  } ODBCDateTime;
46 #endif
47  //==============================================================================================
48  // DateTime is a raw datetime class. It represents a datetime as a normal break down structure,
49  // What it really stands for is up to the client's interpretation.
50  // It can do conversion to/from VB DATE, ODBC datetime structure, and conversion between UTC and localtime
51  //
52  // The constructor is lenient to invalid input, any error will be corrected silently by the following rule
53  // any year less than 0 is set to 0;
54  // any month less than JAN is set to JAN; larger than DEC is set to DEC
55  // any day less than 1 is set to 1; larger than the last day of the month is set to the last day
56  // any minute less than 0 is set to 0; larger than 59 is set to 59
57  // any second less than 0 is set to 0; larger than 59 is set to 59
58  // any millisecond less than 0 is set to 0; larger than 999 is set to 999
59  // The user is responsible for passing in valid data to construct a DateTime.
60  //
61  // PutXXX method checks input by the same rule. Any input fall out of the valid range is rejected,
62  // which leaves the object intact.
63  // The only exception is PutDay, as long as the input day is in the range of [1, 31],
64  // even though it might be larger than the last day of the month, it's allowed to proceed.
65  //
66  // It's size is IMMUTABLE - you can not make a bug fix which creates new member!!!
67  //==============================================================================================
68 #ifndef DATETIME_LITERAL
69 #define DATETIME_LITERAL
70  typedef enum { SUN=0, MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6 } DAY_OF_WEEK;
71  typedef enum { JAN=1, FEB=2, MAR=3, APR=4, MAY=5, JUN=6,
72  JUL=7, AUG=8, SEP=9, OCT=10, NOV=11, DEC=12 } MONTH_OF_YEAR;
73 #endif
75  {
76  public:
77 
78  DateTime(Int32 inYear, Int32 inMonth, Int32 inDay, Int32 inHour=0, Int32 inMinute=0, Int32 inSecond=0, Int32 inMillisecond=0);
79  // construct from ODBCDateTime
80  DateTime(const ODBCDateTime& iTime);
81  // construct from VB time DATE
82  // input must be in the range of -693593 (Year 0001) to 23242571 (Year 0xFFFF),
83  // anything beyond will be truncated to the closest within range
84  explicit DateTime(double ifTime = 0.0);
85  // copy constructor
86  DateTime(const DateTime& irSrc);
87 
88  DateTime& operator = (const DateTime& irSrc);
89 
90  ~DateTime() throw();
91 
92  Int32 GetYear() const;
93  Int32 GetMonth() const;
94  Int32 GetDay() const;
95  Int32 GetHour() const;
96  Int32 GetMinute() const;
97  Int32 GetSecond() const;
98  Int32 GetMillisecond() const;
99  Int32 GetWeek() const;
100  Int32 GetDayOfWeek() const;
101  // 1 - 366
102  Int32 GetDayOfYear() const;
103 
104  // return totals day in current month
105  Int32 GetDaysInMonth() const;
106 
107  // return the total number of weeks in current month
108  // int GetWeeksOfMonth() const;
109 
110  // return false if inYear < 0
111  bool PutYear(Int32 inYear);
112 
113  // return false if inMonth < JAN || inMonth > DEC
114  bool PutMonth(Int32 inMonth);
115 
116  // return false if inDay !([-31, -1] || [1, 31])
117  // negative inDay implies count the days from the end of the month
118  bool PutDay(Int32 inDay);
119 
120  // return false if inHour < 0 || inHour > 23
121  bool PutHour(Int32 inHour);
122 
123  // return false if inMinute < 0 || inMinute > 59
124  bool PutMinute(Int32 inMinute);
125 
126  // return false if inSecond < 0 || inSecond > 59
127  bool PutSecond(Int32 inSecond);
128 
129  // return false if inMillisecond < 0 || inMillisecond > 999
130  bool PutMillisecond(Int32 inMillisecond);
131 
132  // return false if inWeek < 1 || inWeek > 6
133  bool PutWeek(Int32 inWeek);
134 
135  // return false if inDayofweek < SUN || inDayofweek > SAT
136  bool PutDayOfWeek(Int32 inDayofweek, bool ibChangeMonth=false);
137 
138  bool PutWeekAndDayOfWeek(Int32 inWeek, Int32 inDayofWeek);
139 
140  // YYYY-MM-DD hh:mm:ss.msec, according to ISO 8601
142 
143  // parse a string in ISO 8601 format (YYYY-MM-DD hh:mm:ss.msec)
144  // and return the number of parsed characters
145  unsigned int FromString(const char* ipISO8601String);
146 
147  DateTime operator + (const TimeSpan&) const;
148  DateTime operator - (const TimeSpan&) const;
149  TimeSpan operator - (const DateTime&) const;
150 
151  bool operator < (const DateTime&) const;
152  bool operator > (const DateTime&) const;
153 
154  bool operator <= (const DateTime&) const;
155  bool operator >= (const DateTime&) const;
156 
157  bool operator == (const DateTime&) const;
158  bool operator != (const DateTime&) const;
159 
160  double ToDATE() const;
161 
162  ODBCDateTime ToODBCDateTime() const;
163 
164  // see it as a localtime and convert to UTC according to local time zone
165  DateTime ToUTCTime() const;
166  // see it as a UTC time and convert to localtime according to local time zone
167  DateTime ToLocalTime() const;
168 
169  // give the current local time
170  static DateTime Now();
171 
172  // give the current UTC time
173  static DateTime NowUTC();
174 
175  // 12/05/2003 yuwen give the time zone information
176  static bool GetTimeZoneInfo(Int32& inOffsetInMinutes);
177 
178  bool IsLeapYear() const;
179 
180  static bool IsLeapYear(Int32 inYear);
181 
182  // Added at request of engine team in order to significantly improve
183  // performance of xtab sorting
184  // return -1 if a<b, return 0 if a==b, return 1 if a>b
185  static Int32 DateTimeOrder(const DateTime& inTimeA, const DateTime& inTimeB);
186 
187  // This method maps inYear to a year in [1970, 2038] with the same pattern
188  // It can only handle inYear in range of [0, 9999], otherwise -1 will be returned.
189  static int YearOfSamePattern(int inYear);
190 
191  // for backward compatibility
192  static unsigned Int32 GetSaveObjSize();
193  // caller is responsible to allocate memory for ipBuffer
194  Int32 SaveObj(unsigned Int32 inSize, void* ipBuffer, Int32 mode = 0 /* LOCAL */) const;
195  Int32 LoadObj(unsigned int lnSize, void* ipBuffer,bool bAssert = true);
196 
197  // input represents the seconds elapsed since midnight (00:00:00), January 1, 1970
198  explicit DateTime(time_t inSecond, Int32 inMillisecond=0);
199  time_t Totime_t() const;
200 
201  // The Time Zone Bias is the number of minutes from GMT of the current locale. Positive
202  // means one direction from GMT and negative means the other direction from GMT
203  // comment out because link error of timezone on Android
204  //Int32 GetTimeZoneBias() const;
205 
206  // lpan 2010-04-28: TQMS 388539. According to Tao He's TQMS comment, NCS use format MM/DD/YYYY
207  MBase::ReturnString ToDateString() const;
208 
209  Int64 Totime_t64() const;
210 
211  private:
212  Int32 Sanitize(bool bAssert = true);
213  void SetWeekAndDayOfWeek() const;
214  void AdjustDay();
215  Int32 mktime(struct tm& orTime) const;
216  static void DAYSToYMD(Int32 inDays, unsigned short& ornYear, short& ornMonth, short& ornDay);
217  static int YMDToDAYS(unsigned short inYear, short inMonth, short inDay);
218  static Int64 ToMilliseconds(const DateTime& irTime);
219  static double MillisecondsToDATE(Int64 inMilliseconds);
220  static int WeekFromDayAndDayOfWeek(int inDay, int inDayOfWeek);
221 
222 
223  private:
224  unsigned short mnYear;
225  short mnMonth;
226  short mnDay;
227  short mnHour;
228  short mnMinute;
229  short mnSecond;
230  short mnMillisecond;
231  mutable short mnWeek;
232  mutable short mnDayOfWeek;
233  };
234 }
235 
236 #endif // MDataType_DateTime_h
Int32 DynamicTime
Definition: DateTime.h:21
bool operator<(const ::GUID &irGUID1, const ::GUID &irGUID2)
Definition: Base/Base/GUID.h:156
Definition: DateTime.h:72
unsigned short hour
Definition: DateTime.h:41
Definition: DateTime.h:71
Definition: ReturnString.h:36
bool operator==(const Allocator< _Ty > &left, const Allocator< _UT > &right)
Definition: Allocator.h:204
Int32 Mode
Definition: DateTime.h:33
Definition: DateTime.h:72
unsigned short day
Definition: DateTime.h:40
struct MDataType::ODBCDateTime ODBCDateTime
Definition: TimeSpan.h:35
#define Int64
Definition: BasicTypes.h:36
Int32 Month
Definition: DateTime.h:23
DAY_OF_WEEK
Definition: DateTime.h:70
#define DLL_DATATYPE_EXIM
Definition: DataType.h:22
Int32 Week
Definition: DateTime.h:30
Definition: DateTime.h:72
Definition: DateTime.h:70
Int32 Seconds
Definition: DateTime.h:27
Definition: DateTime.h:72
Definition: BigDecimal.h:18
unsigned short year
Definition: DateTime.h:38
Int32 Year
Definition: DateTime.h:22
Int32 Day
Definition: DateTime.h:24
Definition: DateTime.h:72
unsigned short minute
Definition: DateTime.h:42
#define Int32
Definition: BasicTypes.h:20
unsigned short month
Definition: DateTime.h:39
unsigned Int32 fraction
Definition: DateTime.h:44
Int32 Hour
Definition: DateTime.h:25
Definition: DateTime.h:70
DLL_BASE_EXIM const char * ToString(MBase::MessageDomain iDomain)
MONTH_OF_YEAR
Definition: DateTime.h:71
Definition: DateTime.h:71
bool operator!=(const Allocator< _Ty > &left, const Allocator< _UT > &right)
Definition: Allocator.h:211
Definition: DateTime.h:70
Definition: DateTime.h:70
unsigned short second
Definition: DateTime.h:43
Definition: DateTime.h:72
Int32 MilliSeconds
Definition: DateTime.h:28
Int32 DayOfWeek
Definition: DateTime.h:31
Definition: DateTime.h:70
Definition: DateTime.h:71
Definition: DateTime.h:71
Definition: DateTime.h:70
Definition: DateTime.h:71
Definition: DateTime.h:74
Definition: DateTime.h:19
Definition: DateTime.h:70
Definition: DateTime.h:36
struct MDataType::MsiDateTime MsiDateTime
Int32 Minutes
Definition: DateTime.h:26
Definition: DateTime.h:71