71 mike 1.1 return false;
72
73 memset(x, 0, sizeof(MI_Datetime));
74
75 if (s[21] == '+' || s[21] == '-')
76 {
77 MI_Uint32 utc;
78
79 /* It must be a timestamp (YYYYMMDDHHMMSS.MMMMMMSUTC) */
80 if (_StrToU32(s, 0, 4, &x->u.timestamp.year) != 0 ||
81 _StrToU32(s, 4, 2, &x->u.timestamp.month) != 0 ||
82 _StrToU32(s, 6, 2, &x->u.timestamp.day) != 0 ||
83 _StrToU32(s, 8, 2, &x->u.timestamp.hour) != 0 ||
84 _StrToU32(s, 10, 2, &x->u.timestamp.minute) != 0 ||
85 _StrToU32(s, 12, 2, &x->u.timestamp.second) != 0 ||
86 s[14] != '.' ||
87 _StrToU32(s, 15, 6, &x->u.timestamp.microseconds) != 0 ||
88 _StrToU32(s, 22, 3, &utc) != 0)
89 {
90 return false;
91 }
92 mike 1.1
93 if (s[21] == '+')
94 x->u.timestamp.utc = (MI_Sint32)utc;
95 else
96 x->u.timestamp.utc = -(MI_Sint32)utc;
97
98 x->isTimestamp = 1;
99 }
100 else if (s[21] == ':')
101 {
102 /* It must be an interval (DDDDDDDDHHMMSS.MMMMMM:000) */
103 if (_StrToU32(s, 0, 8, &x->u.interval.days) != 0 ||
104 _StrToU32(s, 8, 2, &x->u.interval.hours) != 0 ||
105 _StrToU32(s, 10, 2, &x->u.interval.minutes) != 0 ||
106 _StrToU32(s, 12, 2, &x->u.interval.seconds) != 0 ||
107 s[14] != '.' ||
108 _StrToU32(s, 15, 6, &x->u.interval.microseconds) != 0 ||
109 s[22] != '0' || s[23] != '0' || s[24] != '0')
110 {
111 return false;
112 }
113 mike 1.1
114 x->isTimestamp = 0;
115 }
116 else
117 {
118 return false;
119 }
120
121 return true;
122 }
123
124 static int _GetCurrentTimeInUsec(MI_Uint64& usec)
125 {
126 #if defined(_MSC_VER)
127 FILETIME ft;
128 ULARGE_INTEGER tmp;
129
130 GetSystemTimeAsFileTime(&ft);
131 tmp.u.LowPart = ft.dwLowDateTime;
132 tmp.u.HighPart = ft.dwHighDateTime;
133 tmp.QuadPart -= 0X19DB1DED53E8000;
134 mike 1.1 usec = tmp.QuadPart / (UINT64)10;
135
136 return 0;
137 #else
138 struct timeval tv;
139 struct timezone tz;
140 memset(&tv, 0, sizeof(tv));
141 memset(&tz, 0, sizeof(tz));
142
143 if (gettimeofday(&tv, &tz) != 0)
144 return -1;
145
146 usec = (MI_Uint64)tv.tv_sec * (MI_Uint64)1000000 + (MI_Uint64)tv.tv_usec;
147 return 0;
148 #endif
149 }
150
151 Datetime::Datetime(
152 MI_Uint32 year,
153 MI_Uint32 month,
154 MI_Uint32 day,
155 mike 1.1 MI_Uint32 hour,
156 MI_Uint32 minute,
157 MI_Uint32 second,
158 MI_Uint32 microseconds,
159 MI_Sint32 utc)
160 {
161 _rep.isTimestamp = MI_TRUE;
162 _rep.u.timestamp.year = year;
163 _rep.u.timestamp.month = month;
164 _rep.u.timestamp.day = day;
165 _rep.u.timestamp.hour = hour;
166 _rep.u.timestamp.minute = minute;
167 _rep.u.timestamp.second = second;
168 _rep.u.timestamp.microseconds = microseconds;
169 _rep.u.timestamp.utc = utc;
170 }
171
172 Datetime::Datetime(
173 MI_Uint32 days,
174 MI_Uint32 hours,
175 MI_Uint32 minutes,
176 mike 1.1 MI_Uint32 seconds,
177 MI_Uint32 microseconds)
178 {
179 _rep.isTimestamp = MI_FALSE;
180 _rep.u.interval.days = days;
181 _rep.u.interval.hours = hours;
182 _rep.u.interval.minutes = minutes;
183 _rep.u.interval.seconds = seconds;
184 _rep.u.interval.microseconds = microseconds;
185 }
186
187 bool Datetime::Set(
188 MI_Uint32 year,
189 MI_Uint32 month,
190 MI_Uint32 day,
191 MI_Uint32 hour,
192 MI_Uint32 minute,
193 MI_Uint32 second,
194 MI_Uint32 microseconds,
195 MI_Sint32 utc)
196 {
197 mike 1.1 // ATTN: check ranges!
198 _rep.isTimestamp = MI_TRUE;
199 _rep.u.timestamp.year = year;
200 _rep.u.timestamp.month = month;
201 _rep.u.timestamp.day = day;
202 _rep.u.timestamp.hour = hour;
203 _rep.u.timestamp.minute = minute;
204 _rep.u.timestamp.second = second;
205 _rep.u.timestamp.microseconds = microseconds;
206 _rep.u.timestamp.utc = utc;
207 return true;
208 }
209
210 bool Datetime::Set(
211 MI_Uint32 days,
212 MI_Uint32 hours,
213 MI_Uint32 minutes,
214 MI_Uint32 seconds,
215 MI_Uint32 microseconds)
216 {
217 // ATTN: check ranges!
218 mike 1.1 _rep.isTimestamp = MI_FALSE;
219 _rep.u.interval.days = days;
220 _rep.u.interval.hours = hours;
221 _rep.u.interval.minutes = minutes;
222 _rep.u.interval.seconds = seconds;
223 _rep.u.interval.microseconds = microseconds;
224
225 return 0;
226 }
227
228 bool Datetime::Set(const MI_Char* str)
229 {
230 if (!_StrToDatetime(str, &_rep))
231 {
232 Clear();
233 return false;
234 }
235
236 return true;
237 }
238
239 mike 1.1 bool Datetime::Get(
240 MI_Uint32& year,
241 MI_Uint32& month,
242 MI_Uint32& day,
243 MI_Uint32& hour,
244 MI_Uint32& minute,
245 MI_Uint32& second,
246 MI_Uint32& microseconds,
247 MI_Sint32& utc) const
248 {
249 if (!_rep.isTimestamp)
250 return false;
251
252 year = _rep.u.timestamp.year;
253 month = _rep.u.timestamp.month;
254 day = _rep.u.timestamp.day;
255 hour = _rep.u.timestamp.hour;
256 minute = _rep.u.timestamp.minute;
257 second = _rep.u.timestamp.second;
258 microseconds = _rep.u.timestamp.microseconds;
259 utc = _rep.u.timestamp.utc;
260 mike 1.1
261 return true;
262 }
263
264 bool Datetime::Get(
265 MI_Uint32& days,
266 MI_Uint32& hours,
267 MI_Uint32& minutes,
268 MI_Uint32& seconds,
269 MI_Uint32& microseconds) const
270 {
271 if (_rep.isTimestamp)
272 return false;
273
274 days = _rep.u.interval.days;
275 hours = _rep.u.interval.hours;
276 minutes = _rep.u.interval.minutes;
277 seconds = _rep.u.interval.seconds;
278 microseconds = _rep.u.interval.microseconds;
279
280 return true;
281 mike 1.1 }
282
|