compiler/libec: (#205) Corrections to integer promotions
[sdk] / ecere / src / sys / Time.ec
index 789bd4a..a69e7de 100644 (file)
@@ -1,7 +1,6 @@
 namespace sys;
 
-import "instance"
-
+#define set _set
 #define Date _Date
 #define uint _uint
 #define Method _Method
@@ -14,9 +13,13 @@ import "instance"
 #define uint64 _uint64
 #define Alignment _Alignment
 
+#undef __BLOCKS__
+
 #if defined(__WIN32__)
 #define WIN32_LEAN_AND_MEAN
+#define String String_
 #include <windows.h>
+#undef String
 #include <mmsystem.h>
 #elif defined(__unix__) || defined(__APPLE__)
 #include <sys/time.h>
@@ -26,6 +29,7 @@ import "instance"
 #include <time.h>
 #include <stdlib.h>
 
+#undef set
 #undef uint
 #undef int64
 #undef uint64
@@ -38,6 +42,8 @@ import "instance"
 #undef Size
 #undef Date
 
+import "instance"
+
 define EPOCH_YEAR      = 1970;
 define EPOCH_WEEKDAY   = thursday;
 static define SECS_PER_HOUR   = 60 * 60;
@@ -61,15 +67,6 @@ int monthLengths[2][12] =
        { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
 };
 
-char daysNames[7][4] =
-{
-   "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-};
-char monthsNames[12][4] = 
-{
-   "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
-};
-
 #if defined(__WIN32__)
 
 #define LL2FILETIME( ll, pft )   (pft)->dwLowDateTime = (UINT)(ll); (pft)->dwHighDateTime = (UINT)((ll) >> 32);
@@ -86,7 +83,7 @@ static int TIME_DayLightCompareDate(const SYSTEMTIME *date, const SYSTEMTIME *co
    {
       WORD First;
       int weekofmonth = compareDate->wDay;
-      First = ( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay ) % 7 + 1;
+      First = (WORD)(( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay ) % 7 + 1);
       limit_day = First + 7 * (weekofmonth - 1);
       if(limit_day > monthLengths[date->wMonth==2 && ISLEAP(date->wYear)][date->wMonth - 1])
          limit_day -= 7;
@@ -160,7 +157,7 @@ static uint TIME_CompTimeZoneID(const TIME_ZONE_INFORMATION *pTZinfo, FILETIME *
       else if( beforeStandardDate || afterDaylightDate )
          retval = TIME_ZONE_ID_DAYLIGHT;
    }
-   else 
+   else
       retval = TIME_ZONE_ID_UNKNOWN;
    return retval;
 }
@@ -180,7 +177,7 @@ static bool TIME_GetTimezoneBias(const TIME_ZONE_INFORMATION *pTZinfo, FILETIME
    return true;
 }
 
-static bool _TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation, LPSYSTEMTIME lpLocalTime, LPSYSTEMTIME lpUniversalTime)       
+static bool _TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformation, LPSYSTEMTIME lpLocalTime, LPSYSTEMTIME lpUniversalTime)
 {
    FILETIME ft;
    LONG lBias;
@@ -213,7 +210,7 @@ import "System"
 
 public class Time : double
 {
-   char * OnGetString(char * tempString, void * fieldData, bool * needClass)
+   const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
    {
       Time time = this;
       int value;
@@ -222,7 +219,7 @@ public class Time : double
       value = (int)(time / (60 * 60 * 24));
       if(value)
       {
-         
+
          sprintf(temp, "%d:", value);
          strcat(tempString, temp);
          time -= value * 60 * 60 * 24;
@@ -294,17 +291,20 @@ static time_t MakeTimeTfromDT(DateTime dt)
 
 public class SecSince1970 : int64
 {
-   char * OnGetString(char * tempString, void * fieldData, bool * needClass)
+   const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
    {
-      if(this || !this)
+      return ((DateTime)this).OnGetString(tempString, fieldData, needClass);
+   }
+
+   bool OnGetDataFromString(const char * string)
+   {
+      DateTime dt { };
+      if(dt.OnGetDataFromString(string))
       {
-         // TOFIX:  passing argument 2 of '__ecereProp___ecereNameSpace__ecere__sys__DateTime_Set___ecereNameSpace__ecere__sys__SecSince1970' makes integer from pointer without a cast
-         DateTime t = this;
-         return t.OnGetString(tempString, fieldData, needClass);
-         // TOFIX:
-         // return ((DateTime)this).OnGetString(tempString, fieldData, needClass);
+         this = dt;
+         return true;
       }
-      return tempString;
+      return false;
    }
 
    // Is this required?
@@ -323,19 +323,17 @@ public class SecSince1970 : int64
 public:
    property SecSince1970 global
    {
-      // TOFIX: 'return' with a value, in function returning void
-      set { return value.local; }
       get
       {
       #if defined(__WIN32__)
          SYSTEMTIME localTime, systemTime;
-         FILETIME fileTime, localFileTime;
+         //FILETIME fileTime, localFileTime;
          DateTime input, global;
 
          input = this;
 
          localTime.wYear = (short)input.year;
-         localTime.wMonth = (short)input.month + 1;
+         localTime.wMonth = (short)(input.month + 1);
          localTime.wDay = (short)input.day;
          localTime.wHour = (short)input.hour;
          localTime.wMinute = (short)input.minute;
@@ -378,8 +376,6 @@ public:
    };
    property SecSince1970 local
    {
-      // TOFIX: warning: 'return' with a value, in function returning void
-      set { return value.global; }
       get
       {
 #if defined(__WIN32__)
@@ -389,7 +385,7 @@ public:
          utc = this;
 
          systemTime.wYear = (short)utc.year;
-         systemTime.wMonth = (short)utc.month + 1;
+         systemTime.wMonth = (short)(utc.month + 1);
          systemTime.wDay = (short)utc.day;
          systemTime.wHour = (short)utc.hour;
          systemTime.wMinute = (short)utc.minute;
@@ -431,16 +427,9 @@ public:
 public class TimeStamp32 : uint32
 {
 public:
-   char * OnGetString(char * tempString, void * fieldData, bool * needClass)
+   const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
    {
-      if(this || !this)
-      {
-         DateTime t = (SecSince1970)(int)this;
-         return t.OnGetString(tempString, fieldData, needClass);
-         // TOFIX:
-         // return ((DateTime)this).OnGetString(tempString, fieldData, needClass);
-      }
-      return tempString;
+      return ((DateTime)(TimeStamp)this).OnGetString(tempString, fieldData, needClass);
    }
 
    // Is this required?
@@ -523,7 +512,7 @@ public struct DateTime
          FILETIME fileTime, localFileTime;
 
          localTime.wYear = (short)year;
-         localTime.wMonth = (short)month + 1;
+         localTime.wMonth = (short)(month + 1);
          localTime.wDay = (short)day;
          localTime.wHour = (short)hour;
          localTime.wMinute = (short)minute;
@@ -543,7 +532,7 @@ public struct DateTime
       #else
          struct tm tm;
          //time_t t = (time_t)(SecSince1970)this;
-         time_t t = MakeTimeTfromDT(this);         
+         time_t t = MakeTimeTfromDT(this);
          gmtime_r(&t, &tm);
          value.year = tm.tm_year + 1900;
          value.month = (Month)tm.tm_mon;
@@ -565,7 +554,7 @@ public struct DateTime
          SYSTEMTIME systemTime, localTime;
 
          systemTime.wYear = (short)year;
-         systemTime.wMonth = (short)month + 1;
+         systemTime.wMonth = (short)(month + 1);
          systemTime.wDay = (short)day;
          systemTime.wHour = (short)hour;
          systemTime.wMinute = (short)minute;
@@ -605,7 +594,7 @@ public struct DateTime
       {
          int64 days, y;
          int rem;
-            
+
          days = value / SECS_PER_DAY;
          rem = (int)(value % SECS_PER_DAY);
 
@@ -664,8 +653,21 @@ public struct DateTime
          return 0;
       }
    };
+   property Date
+   {
+      set
+      {
+         year = value.year;
+         month = value.month;
+         day = value.day;
+         hour = 0;
+         minute = 0;
+         second = 0;
+      }
+      get { value = Date { year, month, day }; }
+   }
 
-   char * OnGetString(char * stringOutput, void * fieldData, bool * needClass)
+   const char * OnGetString(char * stringOutput, void * fieldData, bool * needClass)
    {
       static const char ampm[2][3] = { "AM", "PM" };
       int hour = this.hour;
@@ -677,13 +679,13 @@ public struct DateTime
       if(!year && !day && !month && !this.hour && !minute && !second)
          stringOutput[0] = 0;
       else
-         sprintf(stringOutput, "%s %s %2d %2d:%02d:%02d %s %04d", 
-            daysNames[dayOfTheWeek], monthsNames[month], day, hour, minute, second, ampm[pm], year);
+         sprintf(stringOutput, "%s %s %2d %2d:%02d:%02d %s %04d",
+            shortDaysNames[dayOfTheWeek], shortMonthsNames[month], day, hour, minute, second, ampm[pm], year);
 
       return stringOutput;
    }
 
-   bool OnGetDataFromString(char * string)
+   bool OnGetDataFromString(const char * string)
    {
       char * s = CopyString(string);
       char * tokens[20];
@@ -691,7 +693,7 @@ public struct DateTime
       int c;
       bool foundDayOfTheWeek = false;
       bool foundDate = false;
-      DayOfTheWeek dayOfTheWeek;
+      DayOfTheWeek dayOfTheWeek = 0;
       int day = 0;
       int minute = 0;
       int second = 0;
@@ -701,14 +703,16 @@ public struct DateTime
       for(c = 0; c < count; c++)
       {
          int i;
-         for(i = 0; i<7; i++) 
-            if(!strcmpi(tokens[c], daysNames[i]) || !strcmpi(tokens[c], longDaysNames[i]))
-               break;               
+         for(i = 0; i<7; i++)
+            if(!strcmpi(tokens[c], shortDaysNames[i]) || !strcmpi(tokens[c], longDaysNames[i]) ||
+               !strcmpi(tokens[c], enShortDaysNames[i]) || !strcmpi(tokens[c], enLongDaysNames[i]))
+               break;
          if(i < 7) { dayOfTheWeek = (DayOfTheWeek)i; foundDayOfTheWeek = true; continue; }
 
-         for(i = 0; i<12; i++) 
-            if(!strcmpi(tokens[c], monthsNames[i]) || !strcmpi(tokens[c], longMonthsNames[i]))
-               break;               
+         for(i = 0; i<12; i++)
+            if(!strcmpi(tokens[c], shortMonthsNames[i]) || !strcmpi(tokens[c], longMonthsNames[i]) ||
+               !strcmpi(tokens[c], enShortMonthsNames[i]) || !strcmpi(tokens[c], enLongMonthsNames[i]))
+               break;
          if(i < 12) { month = (Month)i; continue; }
 
          if(strchr(tokens[c], ':'))
@@ -723,9 +727,9 @@ public struct DateTime
                else if(!strcmpi(subTokens[t], "pm")) pm = true;
                else if(t-am-pm == 0) hour = atoi(subTokens[t]);
                else if(t-am-pm == 1) minute = atoi(subTokens[t]);
-               else if(t-am-pm == 2) second = atoi(subTokens[t]);               
+               else if(t-am-pm == 2) second = atoi(subTokens[t]);
             }
-            
+
             if(c < count - 1)
             {
                if(!strcmpi(tokens[c+1], "am")) am = true;
@@ -737,7 +741,7 @@ public struct DateTime
 
             continue;
          }
-         
+
          if(!foundDate)
          {
             if(strchr(tokens[c], '/') || strchr(tokens[c], '-'))
@@ -782,16 +786,17 @@ public struct DateTime
          weWant = (SecSince1970)this + (int)(dayOfTheWeek - this.dayOfTheWeek) * 24 * 60 * 60;
          this = (DateTime)weWant;
       }
-      else if(!strcmpi(s, "today") || !strcmpi(s, "now"))
+      else if(!strcmpi(s, "today") || !strcmpi(s, $"today") ||
+              !strcmpi(s, "now") || !strcmpi(s, $"now"))
          GetLocalTime();
-      else if(!strcmpi(s, "tomorrow"))
+      else if(!strcmpi(s, "tomorrow") || !strcmpi(s, $"tomorrow"))
       {
          SecSince1970 weWant;
          GetLocalTime();
          weWant = (SecSince1970)this + 24 * 60 * 60;
          this = (DateTime)weWant;
       }
-      else if(!strcmpi(s, "yesterday"))
+      else if(!strcmpi(s, "yesterday") || !strcmpi(s, $"yesterday"))
       {
          SecSince1970 weWant;
          GetLocalTime();
@@ -822,7 +827,7 @@ public Time GetTime(void)
 
    if(!secbase)
    {
-      secbase = tp.tv_sec;
+      secbase = (int)tp.tv_sec;
       return tp.tv_usec / 1000000.0;
    }
    return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;