implement mktime for WinCE WINCE_NSPRPUB_RELEASE_4_2_BETA1
authorblythe%netscape.com
Fri, 08 Feb 2002 00:40:26 +0000
branchWINCE_NSPRPUB_RELEASE_4_2_BETA1
changeset 2227 45e92b49703b360baf751848ecddd0919d91e592
parent 2226 90e6fb0388ae1319837b3fcad4d400eedd05109c
child 2228 2446f2e70a9eec3f17fae9d496d33e5e60c9426c
push idunknown
push userunknown
push dateunknown
implement mktime for WinCE
pr/include/md/_win32_time.h
pr/include/md/_wince.h
pr/src/md/windows/w32time.c
--- a/pr/include/md/_win32_time.h
+++ b/pr/include/md/_win32_time.h
@@ -146,16 +146,30 @@
  */
 #define _MD_SYSTEMTIME_2_PRTime(outPRTime, inSystemTime) \
     PR_BEGIN_MACRO \
         FILETIME result; \
         \
         SystemTimeToFileTime(&inSystemTime, &result); \
         _MD_FILETIME_2_PRTime(outPRTime, result); \
     PR_END_MACRO
+#define _MD_SYSTEMTIME_2_time_t(outTimeT, inSystemTime) \
+    PR_BEGIN_MACRO \
+        FILETIME result; \
+        \
+        SystemTimeToFileTime(&inSystemTime, &result); \
+        _MD_FILETIME_2_time_t(outTimeT, result); \
+    PR_END_MACRO
+#define _MD_time_t_2_SYSTEMTIME(outSystemTime, inTimeT) \
+    PR_BEGIN_MACRO \
+        FILETIME conversion; \
+        \
+        _MD_time_t_2_FILETIME(conversion, inTimeT); \
+        FileTimeToSystemTime(&conversion, &outSystemTime); \
+    PR_END_MACRO
 #define _MD_time_t_2_LOCALSYSTEMTIME(outSystemTime, inTimeT) \
     PR_BEGIN_MACRO \
         FILETIME conversion; \
         FILETIME localConversion; \
         \
         _MD_time_t_2_FILETIME(conversion, inTimeT); \
         FileTimeToLocalFileTime(&conversion, &localConversion); \
         FileTimeToSystemTime(&localConversion, &outSystemTime); \
--- a/pr/include/md/_wince.h
+++ b/pr/include/md/_wince.h
@@ -546,16 +546,17 @@ struct tm {
     int tm_hour;
     int tm_mday;
     int tm_mon;
     int tm_year;
     int tm_wday;
     int tm_yday;
     int tm_isdst;
 };
+extern struct tm* Wingmtime(const time_t* inTimeT);
 extern struct tm* Winlocaltime(const time_t* inTimeT);
 extern time_t Winmktime(struct tm* inTM);
 extern size_t Winstrftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);
 
 /*
  * struct protoent is actually defined, but the functions that use it are not.
  *
  * And related windows specific functions to mimic these absent sockets funcs.
--- a/pr/src/md/windows/w32time.c
+++ b/pr/src/md/windows/w32time.c
@@ -39,46 +39,49 @@
  * This file exists mainly to provide an implementation of the time
  *  functions that are missing from LIBC for certain toolsets; namely
  *  MS eMbedded Visual Tools (Windows CE).
  */
 
 #include "primpl.h"
 
 /*
- * Winlocaltime
+ * Ugh, LIBC docs should have warned you.
+ * A signle static storage for the struct tm's returned by some funcs.
+ */
+static const int sDaysOfYear[12] = {
+    0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
+};
+static struct tm tmStorage;
+
+/*
+ *  Winlocaltime
  *
- * As LIBC localtime
+ *  As LIBC localtime
  */
 struct tm* Winlocaltime(const time_t* inTimeT)
 {
     struct tm* retval = NULL;
 
     if(NULL != inTimeT)
     {
-        static struct tm tmStorage;
-        static const int daysOfYear[12] =
-        {
-            0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
-        };
         SYSTEMTIME winLocalTime;
-        TIME_ZONE_INFORMATION  winTZInfo;
-        DWORD winDST;
         
         _MD_time_t_2_LOCALSYSTEMTIME(winLocalTime, *inTimeT);
         
         tmStorage.tm_sec = (int)winLocalTime.wSecond;
         tmStorage.tm_min = (int)winLocalTime.wMinute;
         tmStorage.tm_hour = (int)winLocalTime.wHour;
         tmStorage.tm_mday = (int)winLocalTime.wDay;
         tmStorage.tm_mon = (int)(winLocalTime.wMonth - 1);
         tmStorage.tm_year = (int)(winLocalTime.wYear - 1900);
         tmStorage.tm_wday = (int)winLocalTime.wDayOfWeek;
-        
-        tmStorage.tm_yday = (int)winLocalTime.wDay + daysOfYear[tmStorage.tm_mon];
+        tmStorage.tm_isdst = -1;
+
+        tmStorage.tm_yday = (int)winLocalTime.wDay + sDaysOfYear[tmStorage.tm_mon];
         if(0 == (winLocalTime.wYear & 3))
         {
             if(2 < winLocalTime.wMonth)
             {
                 if(0 == winLocalTime.wYear % 100)
                 {
                     if(0 == winLocalTime.wYear % 400)
                     {
@@ -87,51 +90,112 @@ struct tm* Winlocaltime(const time_t* in
                 }
                 else
                 {
                     tmStorage.tm_yday++;
                 }
             }
         }
 
-        winDST = GetTimeZoneInformation(&winTZInfo);
+        retval = &tmStorage;
+    }
+
+    return retval;
+}
+
+/*
+ *  Wingmtime
+ *
+ *  As LIBC gmtime
+ */
+struct tm* Wingmtime(const time_t* inTimeT)
+{
+    struct tm* retval = NULL;
 
-        switch(winDST)
+    if(NULL != inTimeT)
+    {
+        SYSTEMTIME winGMTime;
+        
+        _MD_time_t_2_SYSTEMTIME(winGMTime, *inTimeT);
+        
+        tmStorage.tm_sec = (int)winGMTime.wSecond;
+        tmStorage.tm_min = (int)winGMTime.wMinute;
+        tmStorage.tm_hour = (int)winGMTime.wHour;
+        tmStorage.tm_mday = (int)winGMTime.wDay;
+        tmStorage.tm_mon = (int)(winGMTime.wMonth - 1);
+        tmStorage.tm_year = (int)(winGMTime.wYear - 1900);
+        tmStorage.tm_wday = (int)winGMTime.wDayOfWeek;
+        tmStorage.tm_isdst = -1;
+
+        tmStorage.tm_yday = (int)winGMTime.wDay + sDaysOfYear[tmStorage.tm_mon];
+        if(0 == (winGMTime.wYear & 3))
         {
-        case TIME_ZONE_ID_STANDARD:
-            tmStorage.tm_isdst = 0;
-            break;
-        case TIME_ZONE_ID_DAYLIGHT:
-            tmStorage.tm_isdst = 1;
-            break;
-        default:
-            tmStorage.tm_isdst = -1;
-            break;
+            if(2 < winGMTime.wMonth)
+            {
+                if(0 == winGMTime.wYear % 100)
+                {
+                    if(0 == winGMTime.wYear % 400)
+                    {
+                        tmStorage.tm_yday++;
+                    }
+                }
+                else
+                {
+                    tmStorage.tm_yday++;
+                }
+            }
         }
 
         retval = &tmStorage;
     }
 
     return retval;
 }
 
 /*
  *  Winmktime
  *
  *  As LIBCs mktime
+ *  We likely have a deficiency with the handling of tm_isdst...
  */
 time_t Winmktime(struct tm* inTM)
 {
     time_t retval = (time_t)-1;
 
-    /*
-    ** FIXME TODO
-    **
-    ** More here
-    */
+    if(NULL != inTM)
+    {
+        SYSTEMTIME winTime;
+        struct tm* gmTime = NULL;
+
+        memset(&winTime, 0, sizeof(winTime));
+
+        /*
+         * Ignore tm_wday and tm_yday.
+         */
+        winTime.wSecond = inTM->tm_sec;
+        winTime.wMinute = inTM->tm_min;
+        winTime.wHour = inTM->tm_hour;
+        winTime.wDay = inTM->tm_mday;
+        winTime.wMonth = inTM->tm_mon + 1;
+        winTime.wYear = inTM->tm_year + 1900;
+
+        /*
+         * First get our time_t.
+         */
+        _MD_SYSTEMTIME_2_time_t(retval, winTime);
+
+        /*
+         * Now overwrite the struct passed in with what we believe it should be.
+         */
+        gmTime = Wingmtime(&retval);
+        if(gmTime != inTM)
+        {
+            memcpy(inTM, gmTime, sizeof(struct tm));
+        }
+    }
 
     return retval;
 }
 
 /*
  *  Winstrftime
  *
  *  As LIBCs strftime