jdate.cpp
9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "Stdafx.h"
#include "jdate.h"
#include "jruntimeexception.h"
namespace jcommon {
#ifdef _WIN32
// Number of 100 nanosecond units from 1/1/1601 to 1/1/1970
#define EPOCH_BIAS 116444736000000000i64
typedef union {
unsigned __int64 ft_scalar;
FILETIME ft_struct;
} FT;
uint64_t SystemTime2LongTime(SYSTEMTIME *stm)
{
FT nt_time;
SystemTimeToFileTime(stm, &nt_time.ft_struct);
return (uint64_t)((__time64_t)((nt_time.ft_scalar - EPOCH_BIAS) / 10000000i64));
}
#endif
Date::Date():
jcommon::Object()
{
jcommon::Object::SetClassName("jcommon::Date");
#ifdef _WIN32
GetLocalTime(&_zone);
_time = (time_t)SystemTime2LongTime(&_zone);
#else
_time = time(NULL);
_zone = localtime(&_time);
if (_zone == NULL) {
try {
_zone = new struct tm;
} catch (std::bad_alloc &e) {
jcommon::RuntimeException("Cannot allocate memory");
}
memset(_zone, 0, sizeof(struct tm));
}
#endif
}
Date::Date(time_t time_):
jcommon::Object()
{
jcommon::Object::SetClassName("jcommon::Date");
_time = time_;
#ifdef _WIN32
int Y2,
M2,
Y,
M,
D,
K,
hora,
horas,
minutos,
segundos,
MJD;
MJD = (int)(_time >> 24);
hora = (int)(_time & 0xffffff);
Y2 = (int) ((MJD - 15078.2) / 365.25);
M2 = (int) ((MJD - 14956.1 - (int) (Y2 * 365.25)) / 30.6001);
D = MJD - 14956 - (int) (Y2 * 365.25) - (int) (M2 * 30.6001);
K = M2 == 14 || M2 == 15 ? 1 : 0;
Y = Y2 + K;
M = M2 - 1 - K * 12;
horas = ((hora >> 20) & 0x0f) * 10 + ((hora >> 16) & 0x0f);
minutos = ((hora >> 12) & 0x0f) * 10 + ((hora >> 8) & 0x0f);
segundos = ((hora >> 4) & 0x0f) * 10 + ((hora >> 0) & 0x0f);
_zone.wDay = D; // 1-31
_zone.wMonth = M; // 1-12
_zone.wYear = Y + 1900; // 1601-30827
_zone.wHour = horas; // 0-23
_zone.wMinute = minutos; // 0-59
_zone.wSecond = segundos; // 0-59
#else
_zone = localtime(&_time);
if (_zone == NULL) {
try {
_zone = new struct tm;
} catch (std::bad_alloc &e) {
jcommon::RuntimeException("Cannot allocate memory");
}
memset(_zone, 0, sizeof(struct tm));
}
#endif
}
Date::Date(int day, int month, int year):
jcommon::Object()
{
jcommon::Object::SetClassName("jcommon::Date");
#ifdef _WIN32
GetLocalTime(&_zone);
_zone.wDay = day; // 1-31
_zone.wMonth = month; // 1-12
_zone.wYear = year; // 1601-30827
// _zone.wSecond = seconds; // 0-59
// _zone.wMinute = minutes; // 0-59
// _zone.wHour = hour; // 0-23
_time = (time_t)SystemTime2LongTime(&_zone);
#else
_time = time(NULL);
struct tm t,
*l = localtime(&_time);
t.tm_sec = l->tm_sec; // seconds (0 - 60)
t.tm_min = l->tm_min; // minutes (0 - 59)
t.tm_hour = l->tm_hour; // hours (0 - 23)
t.tm_mday = day; // day of month (1 - 31)
t.tm_mon = month-1; // month of year (0 - 11)
t.tm_year = year-1900; // year - 1900
t.tm_wday = 0; // day of week (Sunday = 0)
t.tm_yday = 0; // day of year (0 - 365)
t.tm_isdst = 0; // is summer time in effect?
t.tm_zone = NULL; // abbreviation of timezone name
// long tm_gmtoff; // offset from UTC in seconds
_time = mktime(&t);
_zone = localtime(&_time);
if (_zone == NULL) {
try {
_zone = new struct tm;
} catch (std::bad_alloc &e) {
jcommon::RuntimeException("Cannot allocate memory");
}
memset(_zone, 0, sizeof(struct tm));
}
#endif
}
Date::Date(int day, int month, int year, int hours, int minutes, int seconds):
jcommon::Object()
{
jcommon::Object::SetClassName("jcommon::Date");
#ifdef _WIN32
GetLocalTime(&_zone);
_zone.wDay = day; // 1-31
_zone.wMonth = month; // 1-12
_zone.wYear = year; // 1601-30827
_zone.wHour = hours; // 0-23
_zone.wMinute = minutes; // 0-59
_zone.wSecond = seconds; // 0-59
_time = (time_t)SystemTime2LongTime(&_zone);
#else
_time = time(NULL);
struct tm t;
t.tm_sec = seconds; // seconds (0 - 60)
t.tm_min = minutes; // minutes (0 - 59)
t.tm_hour = hours; // hours (0 - 23)
t.tm_mday = day; // day of month (1 - 31)
t.tm_mon = month-1; // month of year (0 - 11)
t.tm_year = year-1900; // year - 1900
t.tm_wday = 0; // day of week (Sunday = 0)
t.tm_yday = 0; // day of year (0 - 365)
t.tm_isdst = 0; // is summer time in effect?
t.tm_zone = NULL; // abbreviation of timezone name
// long tm_gmtoff; // offset from UTC in seconds
_time = mktime(&t);
_zone = localtime(&_time);
if (_zone == NULL) {
try {
_zone = new struct tm;
} catch (std::bad_alloc &e) {
jcommon::RuntimeException("Cannot allocate memory");
}
memset(_zone, 0, sizeof(struct tm));
}
#endif
}
Date::Date(double julian)
{
jcommon::Object::SetClassName("jcommon::Date");
_time = ((julian-2299160.5)*864000000000.0);
#ifdef _WIN32
int Y2,
M2,
Y,
M,
D,
K,
hora,
horas,
minutos,
segundos,
MJD;
MJD = (int)(_time >> 24);
hora = (int)(_time & 0xffffff);
Y2 = (int) ((MJD - 15078.2) / 365.25);
M2 = (int) ((MJD - 14956.1 - (int) (Y2 * 365.25)) / 30.6001);
D = MJD - 14956 - (int) (Y2 * 365.25) - (int) (M2 * 30.6001);
K = M2 == 14 || M2 == 15 ? 1 : 0;
Y = Y2 + K;
M = M2 - 1 - K * 12;
horas = ((hora >> 20) & 0x0f) * 10 + ((hora >> 16) & 0x0f);
minutos = ((hora >> 12) & 0x0f) * 10 + ((hora >> 8) & 0x0f);
segundos = ((hora >> 4) & 0x0f) * 10 + ((hora >> 0) & 0x0f);
_zone.wDay = D; // 1-31
_zone.wMonth = M; // 1-12
_zone.wYear = Y + 1900; // 1601-30827
_zone.wHour = horas; // 0-23
_zone.wMinute = minutos; // 0-59
_zone.wSecond = segundos; // 0-59
#else
_zone = localtime(&_time);
if (_zone == NULL) {
try {
_zone = new struct tm;
} catch (std::bad_alloc &e) {
jcommon::RuntimeException("Cannot allocate memory");
}
memset(_zone, 0, sizeof(struct tm));
}
#endif
}
Date::~Date()
{
#ifdef _WIN32
#else
if (_zone != NULL) {
// free(_zone);
}
#endif
}
double Date::ToJulian()
{
return double(_time)/864000000000.0+2299160.5; // first day of Gregorian reform (Oct 15 1582)
}
uint64_t Date::CurrentTimeSeconds()
{
#ifdef _WIN32
return (uint64_t)(GetTickCount64()/1000);
#else
return (uint64_t)time(NULL);
#endif
}
uint64_t Date::CurrentTimeMillis()
{
#ifdef _WIN32
return (uint64_t)GetTickCount64();
#else
timeval t;
gettimeofday(&t, NULL);
return (uint64_t)t.tv_sec*1000LL + (uint64_t)t.tv_usec/1000LL;
#endif
}
uint64_t Date::CurrentTimeMicros()
{
#ifdef _WIN32
FT nt_time;
GetSystemTimeAsFileTime(&nt_time.ft_struct);
return (uint64_t)((__time64_t)((nt_time.ft_scalar - EPOCH_BIAS) / 10000000i64));
#else
timeval t;
gettimeofday(&t, NULL);
return (uint64_t)t.tv_sec*1000000LL + (uint64_t)t.tv_usec;
#endif
}
uint64_t Date::GetTime()
{
return _time;
}
int Date::GetDayOfMonth()
{
#ifdef _WIN32
return _zone.wDay;
#else
return _zone->tm_mday;
#endif
}
int Date::GetMonth()
{
#ifdef _WIN32
return (_zone.wMonth);
#else
return _zone->tm_mon+1;
#endif
}
int Date::GetYear()
{
#ifdef _WIN32
return _zone.wYear;
#else
return (1900 + _zone->tm_year);
#endif
}
int Date::GetSecond()
{
#ifdef _WIN32
return _zone.wSecond;
#else
return _zone->tm_sec;
#endif
}
int Date::GetMinute()
{
#ifdef _WIN32
return _zone.wMinute;
#else
return _zone->tm_min;
#endif
}
int Date::GetHour()
{
#ifdef _WIN32
return _zone.wHour;
#else
return _zone->tm_hour;
#endif
}
int Date::GetDayOfWeek()
{
#ifdef _WIN32
return _zone.wDayOfWeek;
#else
// INFO:: segunda, terca, quarta, quinta, sexta, sabado, domingo
return ((GetYear()-1901)*365+(GetYear()-1901)/4+GetDayOfMonth()+(GetMonth()-1)*31-((GetMonth()*4+23)/10)*((GetMonth()+12)/15)+((4-GetYear()%4)/4)*((GetMonth()+12)/15)+1)%7;
// return _zone->tm_wday;
#endif
}
int Date::GetDayOfYear()
{
#ifdef _WIN32
Calendar c1(1, 1, _zone.wYear),
c2(_zone.wDay, _zone.wMonth, _zone.wYear);
return c1.CountDays(&c2);
#else
return _zone->tm_yday;
#endif
}
std::string Date::what()
{
std::ostringstream date;
std::string month;
switch (GetMonth()) {
case 1: month = "Jan"; break;
case 2: month = "Feb"; break;
case 3: month = "Mar"; break;
case 4: month = "Apr"; break;
case 5: month = "May"; break;
case 6: month = "Jun"; break;
case 7: month = "Jul"; break;
case 8: month = "Ago"; break;
case 9: month = "Sep"; break;
case 10: month = "Oct"; break;
case 11: month = "Nov"; break;
case 12: month = "Dez"; break;
}
date << month << " " << GetDayOfMonth() << " " << GetHour() << ":" << GetMinute() << ":" << GetSecond() << " " << GetYear();
return date.str();
}
}