51bed638b2ffd6ff87e2b47cf7e7c907f48a9c53
[sdk] / ecere / src / sys / Time.ec
1 namespace sys;
2
3 #define set _set
4 #define Date _Date
5 #define uint _uint
6 #define Method _Method
7 #define FileName _FileName
8 #define Instance _Instance
9 #define Size _Size
10 #define File _File
11 #define byte _byte
12 #define int64 _int64
13 #define uint64 _uint64
14 #define Alignment _Alignment
15
16 #undef __BLOCKS__
17
18 #if defined(__WIN32__)
19 #define WIN32_LEAN_AND_MEAN
20 #define String String_
21 #include <windows.h>
22 #undef String
23 #include <mmsystem.h>
24 #elif defined(__unix__) || defined(__APPLE__)
25 #include <sys/time.h>
26 #include <sched.h>
27 #include <unistd.h>
28 #endif
29 #include <time.h>
30 #include <stdlib.h>
31
32 #undef set
33 #undef uint
34 #undef int64
35 #undef uint64
36 #undef byte
37 #undef Method
38 #undef Alignment
39 #undef FileName
40 #undef Instance
41 #undef File
42 #undef Size
43 #undef Date
44
45 import "instance"
46
47 define EPOCH_YEAR      = 1970;
48 define EPOCH_WEEKDAY   = thursday;
49 static define SECS_PER_HOUR   = 60 * 60;
50 static define SECS_PER_DAY    = SECS_PER_HOUR * 24;
51
52 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
53 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
54 #define ISLEAP(y) (!((y)%4) && (((y) % 100) || (!((y)% 400))))
55
56 const int daysInAYearBeforeMonth[2][13] =
57 {
58    // Normal years.
59    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
60    // Leap years.
61    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
62 };
63
64 int monthLengths[2][12] =
65 {
66         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
67         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
68 };
69
70 #if defined(__WIN32__)
71
72 #define LL2FILETIME( ll, pft )   (pft)->dwLowDateTime = (UINT)(ll); (pft)->dwHighDateTime = (UINT)((ll) >> 32);
73 #define FILETIME2LL( pft, ll)    ll = (((LONGLONG)((pft)->dwHighDateTime))<<32) + (pft)-> dwLowDateTime ;
74
75 static int TIME_DayLightCompareDate(const SYSTEMTIME *date, const SYSTEMTIME *compareDate)
76 {
77    int limit_day, dayinsecs;
78
79    if(date->wMonth < compareDate->wMonth) return -1;
80    if(date->wMonth > compareDate->wMonth) return 1;
81
82    if(compareDate->wDayOfWeek <= 6)
83    {
84       WORD First;
85       int weekofmonth = compareDate->wDay;
86       First = (WORD)(( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay ) % 7 + 1);
87       limit_day = First + 7 * (weekofmonth - 1);
88       if(limit_day > monthLengths[date->wMonth==2 && ISLEAP(date->wYear)][date->wMonth - 1])
89          limit_day -= 7;
90    }
91    else
92    {
93       limit_day = compareDate->wDay;
94    }
95
96    limit_day = ((limit_day * 24  + compareDate->wHour) * 60 + compareDate->wMinute ) * 60;
97    dayinsecs = ((date->wDay * 24  + date->wHour) * 60 + date->wMinute ) * 60 + date->wSecond;
98    return dayinsecs < limit_day ? -1 : dayinsecs > limit_day ? 1 : 0;
99 }
100
101 static uint TIME_CompTimeZoneID(const TIME_ZONE_INFORMATION *pTZinfo, FILETIME *lpFileTime, bool islocal)
102 {
103    int ret;
104    bool beforeStandardDate, afterDaylightDate;
105    DWORD retval = TIME_ZONE_ID_INVALID;
106    LONGLONG llTime = 0;
107    SYSTEMTIME SysTime;
108    FILETIME ftTemp;
109
110    if (pTZinfo->DaylightDate.wMonth != 0)
111    {
112       if (pTZinfo->StandardDate.wMonth == 0 ||
113          pTZinfo->StandardDate.wDay<1 ||
114          pTZinfo->StandardDate.wDay>5 ||
115          pTZinfo->DaylightDate.wDay<1 ||
116          pTZinfo->DaylightDate.wDay>5)
117       {
118          SetLastError(ERROR_INVALID_PARAMETER);
119          return TIME_ZONE_ID_INVALID;
120       }
121
122       if (!islocal)
123       {
124          FILETIME2LL( lpFileTime, llTime );
125          llTime -= ( pTZinfo->Bias + pTZinfo->DaylightBias ) * (LONGLONG)600000000;
126          LL2FILETIME( llTime, &ftTemp)
127          lpFileTime = &ftTemp;
128       }
129
130       FileTimeToSystemTime(lpFileTime, &SysTime);
131
132       ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->StandardDate);
133       if (ret == -2)
134          return TIME_ZONE_ID_INVALID;
135
136       beforeStandardDate = ret < 0;
137
138       if (!islocal)
139       {
140          llTime -= ( pTZinfo->StandardBias - pTZinfo->DaylightBias ) * (LONGLONG)600000000;
141          LL2FILETIME( llTime, &ftTemp)
142          FileTimeToSystemTime(lpFileTime, &SysTime);
143       }
144
145       ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->DaylightDate);
146       if (ret == -2)
147        return TIME_ZONE_ID_INVALID;
148
149       afterDaylightDate = ret >= 0;
150
151       retval = TIME_ZONE_ID_STANDARD;
152       if( pTZinfo->DaylightDate.wMonth <  pTZinfo->StandardDate.wMonth )
153       {
154          if( beforeStandardDate && afterDaylightDate )
155              retval = TIME_ZONE_ID_DAYLIGHT;
156       }
157       else if( beforeStandardDate || afterDaylightDate )
158          retval = TIME_ZONE_ID_DAYLIGHT;
159    }
160    else
161       retval = TIME_ZONE_ID_UNKNOWN;
162    return retval;
163 }
164
165 static bool TIME_GetTimezoneBias(const TIME_ZONE_INFORMATION *pTZinfo, FILETIME *lpFileTime, bool islocal, LONG *pBias)
166 {
167    LONG bias = pTZinfo->Bias;
168    DWORD tzid = TIME_CompTimeZoneID( pTZinfo, lpFileTime, islocal);
169
170    if( tzid == TIME_ZONE_ID_INVALID)
171       return false;
172    if (tzid == TIME_ZONE_ID_DAYLIGHT)
173       bias += pTZinfo->DaylightBias;
174    else if (tzid == TIME_ZONE_ID_STANDARD)
175       bias += pTZinfo->StandardBias;
176    *pBias = bias;
177    return true;
178 }
179
180 static bool _TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation, LPSYSTEMTIME lpLocalTime, LPSYSTEMTIME lpUniversalTime)
181 {
182    FILETIME ft;
183    LONG lBias;
184    LONGLONG t;
185    TIME_ZONE_INFORMATION tzinfo;
186
187    if(lpTimeZoneInformation)
188    {
189       memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
190    }
191    else
192    {
193       if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
194          return false;
195    }
196
197    if (!SystemTimeToFileTime(lpLocalTime, &ft))
198       return false;
199    FILETIME2LL( &ft, t)
200    if (!TIME_GetTimezoneBias(&tzinfo, &ft, true, &lBias))
201       return false;
202
203    t += (LONGLONG)lBias * 600000000;
204    LL2FILETIME( t, &ft)
205    return (bool)FileTimeToSystemTime(&ft, lpUniversalTime);
206 }
207 #endif
208
209 import "System"
210
211 public class Time : double
212 {
213    const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
214    {
215       Time time = this;
216       int value;
217       char temp[256];
218       tempString[0] = 0;
219       value = (int)(time / (60 * 60 * 24));
220       if(value)
221       {
222
223          sprintf(temp, "%d:", value);
224          strcat(tempString, temp);
225          time -= value * 60 * 60 * 24;
226       }
227       value = (int)(time / (60 * 60));
228       if(value)
229       {
230          sprintf(temp, "%d:", value);
231          strcat(tempString, temp);
232          time -= value * 60 * 60;
233       }
234
235       value = (int)(time / 60);
236       sprintf(temp, "%d:", value);
237       strcat(tempString, temp);
238       time -= value * 60;
239
240       value = (int)(time);
241       sprintf(temp, "%02d", value);
242       strcat(tempString, temp);
243       time -= value;
244
245       /*if(time > 0.00001)
246       {
247          sprintf(temp, "%f" time);
248          strcat(tempString, temp+1);
249       }*/
250       return tempString;
251    }
252 }
253
254 public class Seconds : Time { public property Time {} };
255
256 #if !defined(__WIN32__)
257 static time_t MakeTimeT(SecSince1970 t)
258 {
259    struct tm tm;
260    time_t result;
261    DateTime dt = t;
262    tm.tm_year = dt.year - 1900;
263    tm.tm_mon = dt.month;
264    tm.tm_mday = dt.day;
265    tm.tm_hour = dt.hour;
266    tm.tm_min = dt.minute;
267    tm.tm_sec = dt.second;
268    tm.tm_yday = dt.dayInTheYear;
269    tm.tm_wday = dt.dayOfTheWeek;
270    result = mktime(&tm);
271    return result;
272 }
273
274 static time_t MakeTimeTfromDT(DateTime dt)
275 {
276    struct tm tm;
277    time_t result;
278    tm.tm_year = dt.year - 1900;
279    tm.tm_mon = dt.month;
280    tm.tm_mday = dt.day;
281    tm.tm_hour = dt.hour;
282    tm.tm_min = dt.minute;
283    tm.tm_sec = dt.second;
284    tm.tm_yday = dt.dayInTheYear;
285    tm.tm_wday = dt.dayOfTheWeek;
286    result = mktime(&tm);
287    return result;
288 }
289
290 #endif
291
292 public class SecSince1970 : int64
293 {
294    const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
295    {
296       return ((DateTime)this).OnGetString(tempString, fieldData, needClass);
297    }
298
299    bool OnGetDataFromString(const char * string)
300    {
301       DateTime dt { };
302       if(dt.OnGetDataFromString(string))
303       {
304          this = dt;
305          return true;
306       }
307       return false;
308    }
309
310    // Is this required?
311    int OnCompare(SecSince1970 data2)
312    {
313       int result = 0;
314       if(this && data2)
315       {
316          if(this > data2)
317             result = 1;
318          else if(this < data2)
319             result = -1;
320       }
321       return result;
322    }
323 public:
324    property SecSince1970 global
325    {
326       get
327       {
328       #if defined(__WIN32__)
329          SYSTEMTIME localTime, systemTime;
330          //FILETIME fileTime, localFileTime;
331          DateTime input, global;
332
333          input = this;
334
335          localTime.wYear = (short)input.year;
336          localTime.wMonth = (short)(input.month + 1);
337          localTime.wDay = (short)input.day;
338          localTime.wHour = (short)input.hour;
339          localTime.wMinute = (short)input.minute;
340          localTime.wSecond = (short)input.second;
341          localTime.wMilliseconds = 0;
342
343          /*
344          SystemTimeToFileTime(&localTime, &fileTime);
345          LocalFileTimeToFileTime(&fileTime, &localFileTime);
346          FileTimeToSystemTime(&localFileTime, &systemTime);
347          */
348
349          _TzSpecificLocalTimeToSystemTime(null, &localTime, &systemTime);
350
351          global.year = systemTime.wYear;
352          global.month = (Month)(systemTime.wMonth - 1);
353          global.day = systemTime.wDay;
354          global.hour = systemTime.wHour;
355          global.minute = systemTime.wMinute;
356          global.second = systemTime.wSecond;
357
358          return global;
359       #else
360          struct tm tm;
361          DateTime global;
362          time_t t = MakeTimeT(this);
363          // gmtime_r((time_t *)&this, &tm);
364          gmtime_r(&t, &tm);
365          global.year = tm.tm_year + 1900;
366          global.month = (Month)tm.tm_mon;
367          global.day = tm.tm_mday;
368          global.hour = tm.tm_hour;
369          global.minute = tm.tm_min;
370          global.second = tm.tm_sec;
371          global.dayInTheYear = tm.tm_yday;
372          global.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
373          return global;
374       #endif
375       }
376    };
377    property SecSince1970 local
378    {
379       get
380       {
381 #if defined(__WIN32__)
382          SYSTEMTIME systemTime, localTime;
383          DateTime utc, local;
384
385          utc = this;
386
387          systemTime.wYear = (short)utc.year;
388          systemTime.wMonth = (short)(utc.month + 1);
389          systemTime.wDay = (short)utc.day;
390          systemTime.wHour = (short)utc.hour;
391          systemTime.wMinute = (short)utc.minute;
392          systemTime.wSecond = (short)utc.second;
393          systemTime.wMilliseconds = 0;
394
395          SystemTimeToTzSpecificLocalTime(null, &systemTime, &localTime);
396
397          local.year = localTime.wYear;
398          local.month = (Month)(localTime.wMonth - 1);
399          local.day = localTime.wDay;
400          local.hour = localTime.wHour;
401          local.minute = localTime.wMinute;
402          local.second = localTime.wSecond;
403          local.dayOfTheWeek = (DayOfTheWeek)localTime.wDayOfWeek;
404
405          local.FixDayOfYear();
406          return local;
407 #else
408          DateTime local;
409          struct tm tm;
410          time_t t = MakeTimeT(this);
411          //localtime_r((time_t *)&this, &tm);
412          localtime_r(&t, &tm);
413          local.year = tm.tm_year + 1900;
414          local.month = (Month)tm.tm_mon;
415          local.day = tm.tm_mday;
416          local.hour = tm.tm_hour;
417          local.minute = tm.tm_min;
418          local.second = tm.tm_sec;
419          local.dayInTheYear = tm.tm_yday;
420          local.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
421          return local;
422 #endif
423       }
424    };
425 };
426
427 public class TimeStamp32 : uint32
428 {
429 public:
430    const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
431    {
432       return ((DateTime)(TimeStamp)this).OnGetString(tempString, fieldData, needClass);
433    }
434
435    // Is this required?
436    int OnCompare(TimeStamp32 data2)
437    {
438       int result = 0;
439       if(this && data2)
440       {
441          if(this > data2)
442             result = 1;
443          else if(this < data2)
444             result = -1;
445       }
446       return result;
447    }
448 };
449
450 public class TimeStamp : SecSince1970
451 {
452    public property SecSince1970 {};
453 };
454 public enum DayOfTheWeek { sunday, monday, tuesday, wednesday, thursday, friday, saturday };
455 public struct DateTime
456 {
457    int year;
458    Month month;
459    int day, hour, minute, second;
460    DayOfTheWeek dayOfTheWeek;
461    int dayInTheYear;
462
463    bool GetLocalTime()
464    {
465    #if defined(__WIN32__)
466       SYSTEMTIME systemTime;
467       ::GetLocalTime(&systemTime);
468
469       year = systemTime.wYear;
470       month = (Month)(systemTime.wMonth - 1);
471       day = systemTime.wDay;
472       hour = systemTime.wHour;
473       minute = systemTime.wMinute;
474       second = systemTime.wSecond;
475
476       FixDayOfYear();
477       {
478          Date date { year, month, day };
479          dayOfTheWeek = date.dayOfTheWeek;
480       }
481    #else
482       struct tm tm;
483       time_t currentTime = time(null);
484       localtime_r(&currentTime, &tm);
485
486       year = tm.tm_year + 1900;
487       month = (Month)tm.tm_mon;
488       day = tm.tm_mday;
489       hour = tm.tm_hour;
490       minute = tm.tm_min;
491       second = tm.tm_sec;
492       dayInTheYear = tm.tm_yday;
493       dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
494    #endif
495
496       return true;
497    }
498
499    bool FixDayOfYear()
500    {
501       dayInTheYear = daysInAYearBeforeMonth[ISLEAP(year)][month] + day - 1;
502       return true;
503    }
504
505    property DateTime global
506    {
507       set { this = value.local; }
508       get
509       {
510       #if defined(__WIN32__)
511          SYSTEMTIME localTime, systemTime;
512          FILETIME fileTime, localFileTime;
513
514          localTime.wYear = (short)year;
515          localTime.wMonth = (short)(month + 1);
516          localTime.wDay = (short)day;
517          localTime.wHour = (short)hour;
518          localTime.wMinute = (short)minute;
519          localTime.wSecond = (short)second;
520          localTime.wMilliseconds = 0;
521
522          SystemTimeToFileTime(&localTime, &fileTime);
523          LocalFileTimeToFileTime(&fileTime, &localFileTime);
524          FileTimeToSystemTime(&localFileTime, &systemTime);
525
526          value.year = systemTime.wYear;
527          value.month = (Month)(systemTime.wMonth - 1);
528          value.day = systemTime.wDay;
529          value.hour = systemTime.wHour;
530          value.minute = systemTime.wMinute;
531          value.second = systemTime.wSecond;
532       #else
533          struct tm tm;
534          //time_t t = (time_t)(SecSince1970)this;
535          time_t t = MakeTimeTfromDT(this);
536          gmtime_r(&t, &tm);
537          value.year = tm.tm_year + 1900;
538          value.month = (Month)tm.tm_mon;
539          value.day = tm.tm_mday;
540          value.hour = tm.tm_hour;
541          value.minute = tm.tm_min;
542          value.second = tm.tm_sec;
543          value.dayInTheYear = tm.tm_yday;
544          value.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
545       #endif
546       }
547    };
548    property DateTime local
549    {
550       set { this = value.global; }
551       get
552       {
553       #if defined(__WIN32__)
554          SYSTEMTIME systemTime, localTime;
555
556          systemTime.wYear = (short)year;
557          systemTime.wMonth = (short)(month + 1);
558          systemTime.wDay = (short)day;
559          systemTime.wHour = (short)hour;
560          systemTime.wMinute = (short)minute;
561          systemTime.wSecond = (short)second;
562          systemTime.wMilliseconds = 0;
563
564          SystemTimeToTzSpecificLocalTime(null, &systemTime, &localTime);
565
566          value.year = localTime.wYear;
567          value.month = (Month)(localTime.wMonth - 1);
568          value.day = localTime.wDay;
569          value.hour = localTime.wHour;
570          value.minute = localTime.wMinute;
571          value.second = localTime.wSecond;
572          value.dayOfTheWeek = (DayOfTheWeek)localTime.wDayOfWeek;
573
574          value.FixDayOfYear();
575       #else
576          struct tm tm;
577          // time_t t = (time_t)(SecSince1970)this;
578          time_t t = MakeTimeTfromDT(this);
579          localtime_r(&t, &tm);
580          value.year = tm.tm_year + 1900;
581          value.month = (Month)tm.tm_mon;
582          value.day = tm.tm_mday;
583          value.hour = tm.tm_hour;
584          value.minute = tm.tm_min;
585          value.second = tm.tm_sec;
586          value.dayInTheYear = tm.tm_yday;
587          value.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
588       #endif
589       }
590    };
591    property SecSince1970
592    {
593       set
594       {
595          int64 days, y;
596          int rem;
597
598          days = value / SECS_PER_DAY;
599          rem = (int)(value % SECS_PER_DAY);
600
601          while(rem < 0)             { rem += SECS_PER_DAY; days--; }
602          while(rem >= SECS_PER_DAY) { rem -= SECS_PER_DAY; days++; }
603
604          hour = rem / SECS_PER_HOUR;
605
606          rem %= SECS_PER_HOUR;
607          minute = rem / 60;
608          second = rem % 60;
609
610          dayOfTheWeek = (EPOCH_WEEKDAY + days) % 7;
611          if(dayOfTheWeek < 0)
612             dayOfTheWeek += 7;
613
614          y = EPOCH_YEAR;
615          while(days < 0 || days >= daysInAYearBeforeMonth[ISLEAP(y)][12])
616          {
617             int64 yg = y + days / 365 - (days % 365 < 0);
618             days -= ((yg - y) * 365 + LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1));
619             y = yg;
620          }
621          year = (int)y;
622          {
623             const int * daysBeforeMonth = daysInAYearBeforeMonth[ISLEAP(y)];
624             dayInTheYear = (int)days;
625             for(y = 11; days < daysBeforeMonth[y]; y--);
626             days -= daysBeforeMonth[y];
627             month = (Month)y;
628             day = (int)(days + 1);
629          }
630       }
631
632       get
633       {
634          int month = this.month;
635          int monthRemainder = month % 12;
636          bool negativeMonthRemainder = monthRemainder < 0;
637          int monthYears = month / 12 - negativeMonthRemainder;
638          int year = this.year + monthYears;
639          // if(year >= 1970)
640          {
641             int a4 = (year       / 4) - !(year       & 3);
642             int b4 = (EPOCH_YEAR / 4) - !(EPOCH_YEAR & 3);
643             int a100 = a4 / 25 - (a4 % 25 < 0);
644             int b100 = b4 / 25 - (b4 % 25 < 0);
645             int a400 = a100 / 4;
646             int b400 = b100 / 4;
647             int leapDays = (a4 - b4) - (a100 - b100) + (a400 - b400);
648             int64 days = 365 * (year - EPOCH_YEAR) + leapDays;
649             month = monthRemainder + 12 * negativeMonthRemainder;
650             days += daysInAYearBeforeMonth[ISLEAP(year)][month] + day - 1;
651             return 60 * (60 * (24 * days + hour) + minute) + second;
652          }
653          return 0;
654       }
655    };
656    property Date
657    {
658       set
659       {
660          year = value.year;
661          month = value.month;
662          day = value.day;
663          hour = 0;
664          minute = 0;
665          second = 0;
666       }
667       get { value = Date { year, month, day }; }
668    }
669
670    const char * OnGetString(char * stringOutput, void * fieldData, bool * needClass)
671    {
672       static const char ampm[2][3] = { "AM", "PM" };
673       int hour = this.hour;
674       bool pm = false;
675       if(hour > 12) { hour -= 12; pm = true; }
676       else if(hour == 12) pm = true;
677       if(!hour) hour = 12;
678
679       if(!year && !day && !month && !this.hour && !minute && !second)
680          stringOutput[0] = 0;
681       else
682          sprintf(stringOutput, "%s %s %2d %2d:%02d:%02d %s %04d",
683             shortDaysNames[dayOfTheWeek], shortMonthsNames[month], day, hour, minute, second, ampm[pm], year);
684
685       return stringOutput;
686    }
687
688    bool OnGetDataFromString(const char * string)
689    {
690       char * s = CopyString(string);
691       char * tokens[20];
692       int count = TokenizeWith(s, 20, tokens, " ", false);
693       int c;
694       bool foundDayOfTheWeek = false;
695       bool foundDate = false;
696       DayOfTheWeek dayOfTheWeek = 0;
697       int day = 0;
698       int minute = 0;
699       int second = 0;
700       int hour = 0;
701       int year = 0;
702
703       for(c = 0; c < count; c++)
704       {
705          int i;
706          for(i = 0; i<7; i++)
707             if(!strcmpi(tokens[c], shortDaysNames[i]) || !strcmpi(tokens[c], longDaysNames[i]) ||
708                !strcmpi(tokens[c], enShortDaysNames[i]) || !strcmpi(tokens[c], enLongDaysNames[i]))
709                break;
710          if(i < 7) { dayOfTheWeek = (DayOfTheWeek)i; foundDayOfTheWeek = true; continue; }
711
712          for(i = 0; i<12; i++)
713             if(!strcmpi(tokens[c], shortMonthsNames[i]) || !strcmpi(tokens[c], longMonthsNames[i]) ||
714                !strcmpi(tokens[c], enShortMonthsNames[i]) || !strcmpi(tokens[c], enLongMonthsNames[i]))
715                break;
716          if(i < 12) { month = (Month)i; continue; }
717
718          if(strchr(tokens[c], ':'))
719          {
720             char * subTokens[20];
721             int sCount = TokenizeWith(tokens[c], 20, subTokens, " :", false);
722             int t;
723             bool pm = false, am = false;
724             for(t = 0; t<sCount; t++)
725             {
726                if(!strcmpi(subTokens[t], "am")) am = true;
727                else if(!strcmpi(subTokens[t], "pm")) pm = true;
728                else if(t-am-pm == 0) hour = atoi(subTokens[t]);
729                else if(t-am-pm == 1) minute = atoi(subTokens[t]);
730                else if(t-am-pm == 2) second = atoi(subTokens[t]);
731             }
732
733             if(c < count - 1)
734             {
735                if(!strcmpi(tokens[c+1], "am")) am = true;
736                else if(!strcmpi(tokens[c+1], "pm")) pm = true;
737             }
738
739             if(am && hour == 12) hour = 0;
740             else if(pm && hour < 12) hour += 12;
741
742             continue;
743          }
744
745          if(!foundDate)
746          {
747             if(strchr(tokens[c], '/') || strchr(tokens[c], '-'))
748             {
749                Date date;
750                if(date.OnGetDataFromString(tokens[c]))
751                {
752                   day = date.day;
753                   year = date.year;
754                   month = date.month;
755                }
756                foundDate = true;
757             }
758             else
759             {
760                i = atoi(tokens[c]);
761                if(i > 31)
762                {
763                   year = i;
764                   continue;
765                }
766                else if(i)
767                   day = i;
768             }
769          }
770       }
771       if(day)
772       {
773          Date date { year, month, day };
774          this.dayOfTheWeek = date.dayOfTheWeek;
775          this.day = day;
776          this.minute = minute;
777          this.second = second;
778          this.hour = hour;
779          this.year = year;
780       }
781       else if(foundDayOfTheWeek)
782       {
783          SecSince1970 weWant;
784          GetLocalTime();
785          if(dayOfTheWeek <= this.dayOfTheWeek) dayOfTheWeek += 7;
786          weWant = (SecSince1970)this + (int)(dayOfTheWeek - this.dayOfTheWeek) * 24 * 60 * 60;
787          this = (DateTime)weWant;
788       }
789       else if(!strcmpi(s, "today") || !strcmpi(s, $"today") ||
790               !strcmpi(s, "now") || !strcmpi(s, $"now"))
791          GetLocalTime();
792       else if(!strcmpi(s, "tomorrow") || !strcmpi(s, $"tomorrow"))
793       {
794          SecSince1970 weWant;
795          GetLocalTime();
796          weWant = (SecSince1970)this + 24 * 60 * 60;
797          this = (DateTime)weWant;
798       }
799       else if(!strcmpi(s, "yesterday") || !strcmpi(s, $"yesterday"))
800       {
801          SecSince1970 weWant;
802          GetLocalTime();
803          weWant = (SecSince1970)this - 24 * 60 * 60;
804          this = (DateTime)weWant;
805       }
806       else if(!s[0])
807       {
808          this = { };
809          delete s;
810          return true;
811       }
812       delete s;
813       return this.day != 0;
814    }
815 };
816
817 public Time GetTime(void)
818 {
819 #if defined(__WIN32__)
820    return timeGetTime() / 1000.0;
821 #elif defined(__unix__) || defined(__APPLE__)
822    struct timeval tp;
823    struct timezone tzp;
824    static int secbase = 0;
825
826    gettimeofday(&tp, &tzp);
827
828    if(!secbase)
829    {
830       secbase = (int)tp.tv_sec;
831       return tp.tv_usec / 1000000.0;
832    }
833    return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
834 #endif
835 }
836
837 public void Sleep(Seconds seconds)
838 {
839 #if defined(__WIN32__)
840    ::Sleep((uint)(seconds * 1000));
841 #else
842    if(!seconds)
843       sched_yield();
844    else
845    {
846       // usleep((uint)(seconds * 1000000));
847       struct timeval tv = { (int)seconds, (int)((seconds - (int)seconds) * 1000000) };
848       select(0,null,null,null, &tv);
849    }
850 #endif
851 }
852
853 public void RandomSeed(uint seed)
854 {
855 #if defined(__linux__) || defined(__DJGPP__)
856    srandom(seed);
857 #else
858    srand(seed);
859 #endif
860 }
861
862 public int GetRandom(int lo, int hi)
863 {
864    if(hi >= lo)
865    {
866 #if defined(__linux__) || defined(__DJGPP__)
867    #define rand_fn random
868 #else
869    #define rand_fn rand
870 #endif
871       if(hi - lo > RAND_MAX)
872          return (int)(lo + ((uint)(hi - lo) + 1.0) * (rand_fn() * ((double)RAND_MAX + 1.0) + rand_fn()) / (RAND_MAX * (RAND_MAX + 1.0) + RAND_MAX + 1.0));
873       else
874          return (int)(lo + ((uint)(hi - lo) + 1.0) * rand_fn() / (RAND_MAX + 1.0));
875    }
876    else
877       return lo;
878 }