1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/wince/time.cpp 
   3 // Purpose:     Implements missing time functionality for WinCE 
   4 // Author:      Marco Cavallini (MCK) - wx@koansoftware.com 
   8 // Copyright:   (c) Marco Cavallini 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // =========================================================================== 
  14 // =========================================================================== 
  16 // --------------------------------------------------------------------------- 
  18 // --------------------------------------------------------------------------- 
  20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  21     #pragma implementation "window.h" 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  32     #include "wx/msw/wrapwin.h" 
  35 #include "wx/msw/wince/time.h" 
  38 #define is_leap(y)   (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) 
  39 #define SECONDS_IN_ONE_MINUTE      60 
  40 #define DAYS_IN_ONE_YEAR          365 
  41 #define SECONDS_IN_ONE_MIN         60 
  42 #define SECONDS_IN_ONE_HOUR      3600 
  43 #define SECONDS_IN_ONE_DAY      86400 
  44 #define DEFAULT_TIMEZONE        28800 
  46 #define DO_LOCALTIME                1 
  49 long timezone 
; // global variable 
  52 //////////////////////////////////////////////////////////////////////// 
  53 // Common code for localtime and gmtime (static) 
  54 //////////////////////////////////////////////////////////////////////// 
  55 static struct tm 
* __cdecl 
common_localtime(const time_t * WXUNUSED(t
), BOOL bLocal
) 
  58     SYSTEMTIME SystemTime
; 
  59     TIME_ZONE_INFORMATION pTz
; 
  60     static struct tm st_res 
;             // data holder 
  61     static struct tm 
* res 
= &st_res 
;    // data pointer 
  63     const unsigned short int __mon_yday
[2][13] = 
  66         { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, 
  68         { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } 
  71     ::GetLocalTime(&SystemTime
) ; 
  72     ::GetTimeZoneInformation(&pTz
); 
  74     /////////////////////////////////////////////// 
  76     timezone 
= pTz
.Bias 
* SECONDS_IN_ONE_MINUTE 
; 
  77     /////////////////////////////////////////////// 
  79     iLeap 
= is_leap(SystemTime
.wYear
) ; 
  81     res
->tm_hour 
= SystemTime
.wHour
; 
  82     res
->tm_min  
= SystemTime
.wMinute
; 
  83     res
->tm_sec  
= SystemTime
.wSecond
; 
  85     res
->tm_mday 
= SystemTime
.wDay
; 
  86     res
->tm_mon 
= SystemTime
.wMonth 
- 1; // this the correct month but localtime returns month aligned to zero 
  87     res
->tm_year 
= SystemTime
.wYear
;     // this the correct year 
  88     res
->tm_year 
= res
->tm_year 
- 1900;  // but localtime returns the value starting at the 1900 
  90     res
->tm_wday 
= SystemTime
.wDayOfWeek
; 
  91     res
->tm_yday 
= __mon_yday
[iLeap
][res
->tm_mon
] + SystemTime
.wDay 
- 1; // localtime returns year-day aligned to zero 
  93     // if localtime behavior and daylight saving 
  94     if (bLocal 
&& pTz
.DaylightBias 
!= 0) 
  97         res
->tm_isdst 
= 0; // without daylight saving or gmtime 
 102 //////////////////////////////////////////////////////////////////////// 
 103 // Receive the number of seconds elapsed since midnight(00:00:00) 
 104 // and convert a time value and corrects for the local time zone 
 105 //////////////////////////////////////////////////////////////////////// 
 106 struct tm 
* __cdecl 
localtime(const time_t * t
) 
 108     return common_localtime(t
, DO_LOCALTIME
) ; 
 111 //////////////////////////////////////////////////////////////////////// 
 112 // Receives the number of seconds elapsed since midnight(00:00:00) 
 113 // and converts a time value WITHOUT correcting for the local time zone 
 114 //////////////////////////////////////////////////////////////////////// 
 115 struct tm 
* __cdecl 
gmtime(const time_t *t
) 
 117     return common_localtime(t
, DO_GMTIME
) ; 
 121 //////////////////////////////////////////////////////////////////////// 
 122 // Common code for conversion of struct tm into time_t   (static) 
 123 //////////////////////////////////////////////////////////////////////// 
 124 static time_t __cdecl 
common_tm_to_time(int day
, int month
, int year
, int hour
, int minute
, int second
) 
 127     static int mdays
[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ; 
 131         prog 
+= mdays
[month 
- 1] ; 
 132         if (month 
== 2 && is_leap(year
)) 
 136     // Calculate seconds in elapsed days 
 137     prog 
= day 
- 1 ;        // align first day of the year to zero 
 138     prog 
+= (DAYS_IN_ONE_YEAR 
* (year 
- 1970) + (year 
- 1901) / 4 - 19) ; 
 139     prog 
*= SECONDS_IN_ONE_DAY 
; 
 141     // Add Calculated elapsed seconds in the current day 
 142     prog 
+= (hour 
* SECONDS_IN_ONE_HOUR 
+ minute 
* 
 143                                SECONDS_IN_ONE_MIN 
+ second
) ; 
 149 //////////////////////////////////////////////////////////////////////// 
 150 // Returns the number of seconds elapsed since 
 151 // midnight(00:00:00) of 1 January 1970 
 152 //////////////////////////////////////////////////////////////////////// 
 153 time_t __cdecl 
time(time_t *t
) 
 159         SYSTEMTIME SystemTime
; 
 161         ::GetLocalTime(&SystemTime
) ; 
 162         prog 
= common_tm_to_time(SystemTime
.wDay
, SystemTime
.wMonth
, SystemTime
.wYear
, 
 163                                  SystemTime
.wHour
, SystemTime
.wMinute
, SystemTime
.wSecond
) ; 
 170 //////////////////////////////////////////////////////////////////////// 
 171 // Converts the local time provided by struct tm 
 172 // into a time_t calendar value 
 173 //////////////////////////////////////////////////////////////////////// 
 174 time_t __cdecl 
mktime(struct tm 
*t
) 
 176     return (common_tm_to_time(t
->tm_mday
, t
->tm_mon
+1, t
->tm_year
+1900, t
->tm_hour
, t
->tm_min
, t
->tm_sec
)) ; 
 180 size_t __cdecl 
wcsftime(wchar_t *, size_t, const wchar_t *, const struct tm 
*) 
 182     // TODO : maybe copy something from wxString::Printf