8 #define FileName _FileName
9 #define Instance _Instance
14 #define uint64 _uint64
15 #define Alignment _Alignment
17 #if defined(__WIN32__)
18 #define WIN32_LEAN_AND_MEAN
21 #elif defined(__unix__) || defined(__APPLE__)
41 define EPOCH_YEAR = 1970;
42 define EPOCH_WEEKDAY = thursday;
43 static define SECS_PER_HOUR = 60 * 60;
44 static define SECS_PER_DAY = SECS_PER_HOUR * 24;
46 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
47 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
48 #define ISLEAP(y) (!((y)%4) && (((y) % 100) || (!((y)% 400))))
50 const int daysInAYearBeforeMonth[2][13] =
53 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
55 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
58 int monthLengths[2][12] =
60 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
61 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
64 char daysNames[7][4] =
66 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
68 char monthsNames[12][4] =
70 "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
73 #if defined(__WIN32__)
75 #define LL2FILETIME( ll, pft ) (pft)->dwLowDateTime = (UINT)(ll); (pft)->dwHighDateTime = (UINT)((ll) >> 32);
76 #define FILETIME2LL( pft, ll) ll = (((LONGLONG)((pft)->dwHighDateTime))<<32) + (pft)-> dwLowDateTime ;
78 static int TIME_DayLightCompareDate(const SYSTEMTIME *date, const SYSTEMTIME *compareDate)
80 int limit_day, dayinsecs;
82 if(date->wMonth < compareDate->wMonth) return -1;
83 if(date->wMonth > compareDate->wMonth) return 1;
85 if(compareDate->wDayOfWeek <= 6)
88 int weekofmonth = compareDate->wDay;
89 First = ( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay ) % 7 + 1;
90 limit_day = First + 7 * (weekofmonth - 1);
91 if(limit_day > monthLengths[date->wMonth==2 && ISLEAP(date->wYear)][date->wMonth - 1])
96 limit_day = compareDate->wDay;
99 limit_day = ((limit_day * 24 + compareDate->wHour) * 60 + compareDate->wMinute ) * 60;
100 dayinsecs = ((date->wDay * 24 + date->wHour) * 60 + date->wMinute ) * 60 + date->wSecond;
101 return dayinsecs < limit_day ? -1 : dayinsecs > limit_day ? 1 : 0;
104 static uint TIME_CompTimeZoneID(const TIME_ZONE_INFORMATION *pTZinfo, FILETIME *lpFileTime, bool islocal)
107 bool beforeStandardDate, afterDaylightDate;
108 DWORD retval = TIME_ZONE_ID_INVALID;
113 if (pTZinfo->DaylightDate.wMonth != 0)
115 if (pTZinfo->StandardDate.wMonth == 0 ||
116 pTZinfo->StandardDate.wDay<1 ||
117 pTZinfo->StandardDate.wDay>5 ||
118 pTZinfo->DaylightDate.wDay<1 ||
119 pTZinfo->DaylightDate.wDay>5)
121 SetLastError(ERROR_INVALID_PARAMETER);
122 return TIME_ZONE_ID_INVALID;
127 FILETIME2LL( lpFileTime, llTime );
128 llTime -= ( pTZinfo->Bias + pTZinfo->DaylightBias ) * (LONGLONG)600000000;
129 LL2FILETIME( llTime, &ftTemp)
130 lpFileTime = &ftTemp;
133 FileTimeToSystemTime(lpFileTime, &SysTime);
135 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->StandardDate);
137 return TIME_ZONE_ID_INVALID;
139 beforeStandardDate = ret < 0;
143 llTime -= ( pTZinfo->StandardBias - pTZinfo->DaylightBias ) * (LONGLONG)600000000;
144 LL2FILETIME( llTime, &ftTemp)
145 FileTimeToSystemTime(lpFileTime, &SysTime);
148 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->DaylightDate);
150 return TIME_ZONE_ID_INVALID;
152 afterDaylightDate = ret >= 0;
154 retval = TIME_ZONE_ID_STANDARD;
155 if( pTZinfo->DaylightDate.wMonth < pTZinfo->StandardDate.wMonth )
157 if( beforeStandardDate && afterDaylightDate )
158 retval = TIME_ZONE_ID_DAYLIGHT;
160 else if( beforeStandardDate || afterDaylightDate )
161 retval = TIME_ZONE_ID_DAYLIGHT;
164 retval = TIME_ZONE_ID_UNKNOWN;
168 static bool TIME_GetTimezoneBias(const TIME_ZONE_INFORMATION *pTZinfo, FILETIME *lpFileTime, bool islocal, LONG *pBias)
170 LONG bias = pTZinfo->Bias;
171 DWORD tzid = TIME_CompTimeZoneID( pTZinfo, lpFileTime, islocal);
173 if( tzid == TIME_ZONE_ID_INVALID)
175 if (tzid == TIME_ZONE_ID_DAYLIGHT)
176 bias += pTZinfo->DaylightBias;
177 else if (tzid == TIME_ZONE_ID_STANDARD)
178 bias += pTZinfo->StandardBias;
183 static bool _TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation, LPSYSTEMTIME lpLocalTime, LPSYSTEMTIME lpUniversalTime)
188 TIME_ZONE_INFORMATION tzinfo;
190 if(lpTimeZoneInformation)
192 memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
196 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
200 if (!SystemTimeToFileTime(lpLocalTime, &ft))
203 if (!TIME_GetTimezoneBias(&tzinfo, &ft, true, &lBias))
206 t += (LONGLONG)lBias * 600000000;
208 return (bool)FileTimeToSystemTime(&ft, lpUniversalTime);
214 public class Time : double
216 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
222 value = (int)(time / (60 * 60 * 24));
226 sprintf(temp, "%d:", value);
227 strcat(tempString, temp);
228 time -= value * 60 * 60 * 24;
230 value = (int)(time / (60 * 60));
233 sprintf(temp, "%d:", value);
234 strcat(tempString, temp);
235 time -= value * 60 * 60;
238 value = (int)(time / 60);
239 sprintf(temp, "%d:", value);
240 strcat(tempString, temp);
244 sprintf(temp, "%02d", value);
245 strcat(tempString, temp);
250 sprintf(temp, "%f" time);
251 strcat(tempString, temp+1);
257 public class Seconds : Time { public property Time {} };
259 #if !defined(__WIN32__)
260 static time_t MakeTimeT(SecSince1970 t)
265 tm.tm_year = dt.year - 1900;
266 tm.tm_mon = dt.month;
268 tm.tm_hour = dt.hour;
269 tm.tm_min = dt.minute;
270 tm.tm_sec = dt.second;
271 tm.tm_yday = dt.dayInTheYear;
272 tm.tm_wday = dt.dayOfTheWeek;
273 result = mktime(&tm);
277 static time_t MakeTimeTfromDT(DateTime dt)
281 tm.tm_year = dt.year - 1900;
282 tm.tm_mon = dt.month;
284 tm.tm_hour = dt.hour;
285 tm.tm_min = dt.minute;
286 tm.tm_sec = dt.second;
287 tm.tm_yday = dt.dayInTheYear;
288 tm.tm_wday = dt.dayOfTheWeek;
289 result = mktime(&tm);
295 public class SecSince1970 : int64
297 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
301 // TOFIX: passing argument 2 of '__ecereProp___ecereNameSpace__ecere__sys__DateTime_Set___ecereNameSpace__ecere__sys__SecSince1970' makes integer from pointer without a cast
303 return t.OnGetString(tempString, fieldData, needClass);
305 // return ((DateTime)this).OnGetString(tempString, fieldData, needClass);
311 int OnCompare(SecSince1970 data2)
318 else if(this < data2)
324 property SecSince1970 global
326 // TOFIX: 'return' with a value, in function returning void
327 set { return value.local; }
330 #if defined(__WIN32__)
331 SYSTEMTIME localTime, systemTime;
332 FILETIME fileTime, localFileTime;
333 DateTime input, global;
337 localTime.wYear = (short)input.year;
338 localTime.wMonth = (short)input.month + 1;
339 localTime.wDay = (short)input.day;
340 localTime.wHour = (short)input.hour;
341 localTime.wMinute = (short)input.minute;
342 localTime.wSecond = (short)input.second;
343 localTime.wMilliseconds = 0;
346 SystemTimeToFileTime(&localTime, &fileTime);
347 LocalFileTimeToFileTime(&fileTime, &localFileTime);
348 FileTimeToSystemTime(&localFileTime, &systemTime);
351 _TzSpecificLocalTimeToSystemTime(null, &localTime, &systemTime);
353 global.year = systemTime.wYear;
354 global.month = (Month)(systemTime.wMonth - 1);
355 global.day = systemTime.wDay;
356 global.hour = systemTime.wHour;
357 global.minute = systemTime.wMinute;
358 global.second = systemTime.wSecond;
364 time_t t = MakeTimeT(this);
365 // gmtime_r((time_t *)&this, &tm);
367 global.year = tm.tm_year + 1900;
368 global.month = (Month)tm.tm_mon;
369 global.day = tm.tm_mday;
370 global.hour = tm.tm_hour;
371 global.minute = tm.tm_min;
372 global.second = tm.tm_sec;
373 global.dayInTheYear = tm.tm_yday;
374 global.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
379 property SecSince1970 local
381 // TOFIX: warning: 'return' with a value, in function returning void
382 set { return value.global; }
385 #if defined(__WIN32__)
386 SYSTEMTIME systemTime, localTime;
391 systemTime.wYear = (short)utc.year;
392 systemTime.wMonth = (short)utc.month + 1;
393 systemTime.wDay = (short)utc.day;
394 systemTime.wHour = (short)utc.hour;
395 systemTime.wMinute = (short)utc.minute;
396 systemTime.wSecond = (short)utc.second;
397 systemTime.wMilliseconds = 0;
399 SystemTimeToTzSpecificLocalTime(null, &systemTime, &localTime);
401 local.year = localTime.wYear;
402 local.month = (Month)(localTime.wMonth - 1);
403 local.day = localTime.wDay;
404 local.hour = localTime.wHour;
405 local.minute = localTime.wMinute;
406 local.second = localTime.wSecond;
407 local.dayOfTheWeek = (DayOfTheWeek)localTime.wDayOfWeek;
409 local.FixDayOfYear();
414 time_t t = MakeTimeT(this);
415 //localtime_r((time_t *)&this, &tm);
416 localtime_r(&t, &tm);
417 local.year = tm.tm_year + 1900;
418 local.month = (Month)tm.tm_mon;
419 local.day = tm.tm_mday;
420 local.hour = tm.tm_hour;
421 local.minute = tm.tm_min;
422 local.second = tm.tm_sec;
423 local.dayInTheYear = tm.tm_yday;
424 local.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
431 public class TimeStamp32 : uint32
434 char * OnGetString(char * tempString, void * fieldData, bool * needClass)
438 DateTime t = (SecSince1970)(int)this;
439 return t.OnGetString(tempString, fieldData, needClass);
441 // return ((DateTime)this).OnGetString(tempString, fieldData, needClass);
447 int OnCompare(TimeStamp32 data2)
454 else if(this < data2)
461 public class TimeStamp : SecSince1970
463 public property SecSince1970 {};
465 public enum DayOfTheWeek { sunday, monday, tuesday, wednesday, thursday, friday, saturday };
466 public struct DateTime
470 int day, hour, minute, second;
471 DayOfTheWeek dayOfTheWeek;
476 #if defined(__WIN32__)
477 SYSTEMTIME systemTime;
478 ::GetLocalTime(&systemTime);
480 year = systemTime.wYear;
481 month = (Month)(systemTime.wMonth - 1);
482 day = systemTime.wDay;
483 hour = systemTime.wHour;
484 minute = systemTime.wMinute;
485 second = systemTime.wSecond;
489 Date date { year, month, day };
490 dayOfTheWeek = date.dayOfTheWeek;
494 time_t currentTime = time(null);
495 localtime_r(¤tTime, &tm);
497 year = tm.tm_year + 1900;
498 month = (Month)tm.tm_mon;
503 dayInTheYear = tm.tm_yday;
504 dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
512 dayInTheYear = daysInAYearBeforeMonth[ISLEAP(year)][month] + day - 1;
516 property DateTime global
518 set { this = value.local; }
521 #if defined(__WIN32__)
522 SYSTEMTIME localTime, systemTime;
523 FILETIME fileTime, localFileTime;
525 localTime.wYear = (short)year;
526 localTime.wMonth = (short)month + 1;
527 localTime.wDay = (short)day;
528 localTime.wHour = (short)hour;
529 localTime.wMinute = (short)minute;
530 localTime.wSecond = (short)second;
531 localTime.wMilliseconds = 0;
533 SystemTimeToFileTime(&localTime, &fileTime);
534 LocalFileTimeToFileTime(&fileTime, &localFileTime);
535 FileTimeToSystemTime(&localFileTime, &systemTime);
537 value.year = systemTime.wYear;
538 value.month = (Month)(systemTime.wMonth - 1);
539 value.day = systemTime.wDay;
540 value.hour = systemTime.wHour;
541 value.minute = systemTime.wMinute;
542 value.second = systemTime.wSecond;
545 //time_t t = (time_t)(SecSince1970)this;
546 time_t t = MakeTimeTfromDT(this);
548 value.year = tm.tm_year + 1900;
549 value.month = (Month)tm.tm_mon;
550 value.day = tm.tm_mday;
551 value.hour = tm.tm_hour;
552 value.minute = tm.tm_min;
553 value.second = tm.tm_sec;
554 value.dayInTheYear = tm.tm_yday;
555 value.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
559 property DateTime local
561 set { this = value.global; }
564 #if defined(__WIN32__)
565 SYSTEMTIME systemTime, localTime;
567 systemTime.wYear = (short)year;
568 systemTime.wMonth = (short)month + 1;
569 systemTime.wDay = (short)day;
570 systemTime.wHour = (short)hour;
571 systemTime.wMinute = (short)minute;
572 systemTime.wSecond = (short)second;
573 systemTime.wMilliseconds = 0;
575 SystemTimeToTzSpecificLocalTime(null, &systemTime, &localTime);
577 value.year = localTime.wYear;
578 value.month = (Month)(localTime.wMonth - 1);
579 value.day = localTime.wDay;
580 value.hour = localTime.wHour;
581 value.minute = localTime.wMinute;
582 value.second = localTime.wSecond;
583 value.dayOfTheWeek = (DayOfTheWeek)localTime.wDayOfWeek;
585 value.FixDayOfYear();
588 // time_t t = (time_t)(SecSince1970)this;
589 time_t t = MakeTimeTfromDT(this);
590 localtime_r(&t, &tm);
591 value.year = tm.tm_year + 1900;
592 value.month = (Month)tm.tm_mon;
593 value.day = tm.tm_mday;
594 value.hour = tm.tm_hour;
595 value.minute = tm.tm_min;
596 value.second = tm.tm_sec;
597 value.dayInTheYear = tm.tm_yday;
598 value.dayOfTheWeek = (DayOfTheWeek)tm.tm_wday;
602 property SecSince1970
609 days = value / SECS_PER_DAY;
610 rem = (int)(value % SECS_PER_DAY);
612 while(rem < 0) { rem += SECS_PER_DAY; days--; }
613 while(rem >= SECS_PER_DAY) { rem -= SECS_PER_DAY; days++; }
615 hour = rem / SECS_PER_HOUR;
617 rem %= SECS_PER_HOUR;
621 dayOfTheWeek = (EPOCH_WEEKDAY + days) % 7;
626 while(days < 0 || days >= daysInAYearBeforeMonth[ISLEAP(y)][12])
628 int64 yg = y + days / 365 - (days % 365 < 0);
629 days -= ((yg - y) * 365 + LEAPS_THRU_END_OF(yg - 1) - LEAPS_THRU_END_OF(y - 1));
634 const int * daysBeforeMonth = daysInAYearBeforeMonth[ISLEAP(y)];
635 dayInTheYear = (int)days;
636 for(y = 11; days < daysBeforeMonth[y]; y--);
637 days -= daysBeforeMonth[y];
639 day = (int)(days + 1);
645 int month = this.month;
646 int monthRemainder = month % 12;
647 bool negativeMonthRemainder = monthRemainder < 0;
648 int monthYears = month / 12 - negativeMonthRemainder;
649 int year = this.year + monthYears;
652 int a4 = (year / 4) - !(year & 3);
653 int b4 = (EPOCH_YEAR / 4) - !(EPOCH_YEAR & 3);
654 int a100 = a4 / 25 - (a4 % 25 < 0);
655 int b100 = b4 / 25 - (b4 % 25 < 0);
658 int leapDays = (a4 - b4) - (a100 - b100) + (a400 - b400);
659 int64 days = 365 * (year - EPOCH_YEAR) + leapDays;
660 month = monthRemainder + 12 * negativeMonthRemainder;
661 days += daysInAYearBeforeMonth[ISLEAP(year)][month] + day - 1;
662 return 60 * (60 * (24 * days + hour) + minute) + second;
668 char * OnGetString(char * stringOutput, void * fieldData, bool * needClass)
670 static const char ampm[2][3] = { "AM", "PM" };
671 int hour = this.hour;
673 if(hour > 12) { hour -= 12; pm = true; }
674 else if(hour == 12) pm = true;
677 if(!year && !day && !month && !this.hour && !minute && !second)
680 sprintf(stringOutput, "%s %s %2d %2d:%02d:%02d %s %04d",
681 daysNames[dayOfTheWeek], monthsNames[month], day, hour, minute, second, ampm[pm], year);
686 bool OnGetDataFromString(char * string)
688 char * s = CopyString(string);
690 int count = TokenizeWith(s, 20, tokens, " ", false);
692 bool foundDayOfTheWeek = false;
693 bool foundDate = false;
694 DayOfTheWeek dayOfTheWeek;
701 for(c = 0; c < count; c++)
705 if(!strcmpi(tokens[c], daysNames[i]) || !strcmpi(tokens[c], longDaysNames[i]))
707 if(i < 7) { dayOfTheWeek = (DayOfTheWeek)i; foundDayOfTheWeek = true; continue; }
709 for(i = 0; i<12; i++)
710 if(!strcmpi(tokens[c], monthsNames[i]) || !strcmpi(tokens[c], longMonthsNames[i]))
712 if(i < 12) { month = (Month)i; continue; }
714 if(strchr(tokens[c], ':'))
716 char * subTokens[20];
717 int sCount = TokenizeWith(tokens[c], 20, subTokens, " :", false);
719 bool pm = false, am = false;
720 for(t = 0; t<sCount; t++)
722 if(!strcmpi(subTokens[t], "am")) am = true;
723 else if(!strcmpi(subTokens[t], "pm")) pm = true;
724 else if(t-am-pm == 0) hour = atoi(subTokens[t]);
725 else if(t-am-pm == 1) minute = atoi(subTokens[t]);
726 else if(t-am-pm == 2) second = atoi(subTokens[t]);
731 if(!strcmpi(tokens[c+1], "am")) am = true;
732 else if(!strcmpi(tokens[c+1], "pm")) pm = true;
735 if(am && hour == 12) hour = 0;
736 else if(pm && hour < 12) hour += 12;
743 if(strchr(tokens[c], '/') || strchr(tokens[c], '-'))
746 if(date.OnGetDataFromString(tokens[c]))
769 Date date { year, month, day };
770 this.dayOfTheWeek = date.dayOfTheWeek;
772 this.minute = minute;
773 this.second = second;
777 else if(foundDayOfTheWeek)
781 if(dayOfTheWeek <= this.dayOfTheWeek) dayOfTheWeek += 7;
782 weWant = (SecSince1970)this + (int)(dayOfTheWeek - this.dayOfTheWeek) * 24 * 60 * 60;
783 this = (DateTime)weWant;
785 else if(!strcmpi(s, "today") || !strcmpi(s, "now"))
787 else if(!strcmpi(s, "tomorrow"))
791 weWant = (SecSince1970)this + 24 * 60 * 60;
792 this = (DateTime)weWant;
794 else if(!strcmpi(s, "yesterday"))
798 weWant = (SecSince1970)this - 24 * 60 * 60;
799 this = (DateTime)weWant;
808 return this.day != 0;
812 public Time GetTime(void)
814 #if defined(__WIN32__)
815 return timeGetTime() / 1000.0;
816 #elif defined(__unix__) || defined(__APPLE__)
819 static int secbase = 0;
821 gettimeofday(&tp, &tzp);
826 return tp.tv_usec / 1000000.0;
828 return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
832 public void Sleep(Seconds seconds)
834 #if defined(__WIN32__)
835 ::Sleep((uint)(seconds * 1000));
841 // usleep((uint)(seconds * 1000000));
842 struct timeval tv = { (int)seconds, (int)((seconds - (int)seconds) * 1000000) };
843 select(0,null,null,null, &tv);
848 public void RandomSeed(uint seed)
850 #if defined(__linux__) || defined(__DJGPP__)
857 public int GetRandom(int lo, int hi)
861 #if defined(__linux__) || defined(__DJGPP__)
862 // return lo+(int)(((uint)(hi - lo) + 1.0)*random()/(RAND_MAX+1.0));
863 return (int)(lo + ((uint)(hi - lo) + 1.0) * random() / (RAND_MAX + 1.0));
865 // return lo+(int)(((uint)(hi - lo) + 1.0)*rand()/(RAND_MAX+1.0));
866 return (int)(lo + ((uint)(hi - lo) + 1.0) * rand() / (RAND_MAX + 1.0));