]>
Commit | Line | Data |
---|---|---|
1c193821 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/msw/wince/time.cpp | |
3 | // Purpose: Implements missing time functionality for WinCE | |
f22cc0b4 JS |
4 | // Author: Marco Cavallini (MCK) - wx@koansoftware.com |
5 | // Modified by: | |
6 | // Created: 31-08-2003 | |
7 | // RCS-ID: | |
991420e6 | 8 | // Copyright: (c) Marco Cavallini |
65571936 | 9 | // Licence: wxWindows licence |
1c193821 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // =========================================================================== | |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1c193821 JS |
21 | #pragma implementation "window.h" |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
9ed0d735 | 32 | #include "wx/msw/wrapwin.h" |
1c193821 JS |
33 | #endif |
34 | ||
35 | #include "wx/msw/wince/time.h" | |
f22cc0b4 | 36 | #include <winbase.h> |
1c193821 | 37 | |
f22cc0b4 | 38 | #define is_leap(y) (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) |
991420e6 WS |
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 | |
45 | #define DO_GMTIME 0 | |
46 | #define DO_LOCALTIME 1 | |
1c193821 | 47 | |
1c193821 | 48 | |
991420e6 | 49 | long timezone ; // global variable |
f22cc0b4 JS |
50 | |
51 | ||
52 | //////////////////////////////////////////////////////////////////////// | |
53 | // Common code for localtime and gmtime (static) | |
54 | //////////////////////////////////////////////////////////////////////// | |
cff614f6 JS |
55 | |
56 | static struct tm * __cdecl common_localtime(const time_t *t, BOOL bLocal) | |
1c193821 | 57 | { |
cff614f6 JS |
58 | wxLongLong i64; |
59 | FILETIME ft; | |
991420e6 WS |
60 | wxString str ; |
61 | SYSTEMTIME SystemTime; | |
62 | TIME_ZONE_INFORMATION pTz; | |
63 | static struct tm st_res ; // data holder | |
64 | static struct tm * res = &st_res ; // data pointer | |
65 | int iLeap; | |
66 | const unsigned short int __mon_yday[2][13] = | |
67 | { | |
68 | // Normal years | |
69 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, | |
70 | // Leap years | |
71 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } | |
72 | }; | |
73 | ||
cff614f6 JS |
74 | if (!*t) |
75 | ::GetLocalTime(&SystemTime); | |
76 | else | |
77 | { | |
78 | i64 = *t; | |
79 | i64 = i64 * 10000000 + 116444736000000000; | |
80 | ||
81 | ft.dwLowDateTime = i64.GetLo(); | |
82 | ft.dwHighDateTime = i64.GetHi(); | |
83 | ||
84 | ::FileTimeToSystemTime(&ft, &SystemTime); | |
85 | } | |
86 | ||
991420e6 WS |
87 | ::GetTimeZoneInformation(&pTz); |
88 | ||
89 | /////////////////////////////////////////////// | |
90 | // Set timezone | |
91 | timezone = pTz.Bias * SECONDS_IN_ONE_MINUTE ; | |
92 | /////////////////////////////////////////////// | |
93 | ||
94 | iLeap = is_leap(SystemTime.wYear) ; | |
95 | ||
96 | res->tm_hour = SystemTime.wHour; | |
97 | res->tm_min = SystemTime.wMinute; | |
98 | res->tm_sec = SystemTime.wSecond; | |
99 | ||
100 | res->tm_mday = SystemTime.wDay; | |
101 | res->tm_mon = SystemTime.wMonth - 1; // this the correct month but localtime returns month aligned to zero | |
102 | res->tm_year = SystemTime.wYear; // this the correct year | |
103 | res->tm_year = res->tm_year - 1900; // but localtime returns the value starting at the 1900 | |
104 | ||
105 | res->tm_wday = SystemTime.wDayOfWeek; | |
106 | res->tm_yday = __mon_yday[iLeap][res->tm_mon] + SystemTime.wDay - 1; // localtime returns year-day aligned to zero | |
107 | ||
108 | // if localtime behavior and daylight saving | |
109 | if (bLocal && pTz.DaylightBias != 0) | |
110 | res->tm_isdst = 1; | |
111 | else | |
112 | res->tm_isdst = 0; // without daylight saving or gmtime | |
f22cc0b4 JS |
113 | |
114 | return res; | |
1c193821 JS |
115 | } |
116 | ||
92bdf9f7 VZ |
117 | extern "C" |
118 | { | |
119 | ||
f22cc0b4 JS |
120 | //////////////////////////////////////////////////////////////////////// |
121 | // Receive the number of seconds elapsed since midnight(00:00:00) | |
122 | // and convert a time value and corrects for the local time zone | |
123 | //////////////////////////////////////////////////////////////////////// | |
124 | struct tm * __cdecl localtime(const time_t * t) | |
1c193821 | 125 | { |
991420e6 | 126 | return common_localtime(t, DO_LOCALTIME) ; |
1c193821 JS |
127 | } |
128 | ||
f22cc0b4 JS |
129 | //////////////////////////////////////////////////////////////////////// |
130 | // Receives the number of seconds elapsed since midnight(00:00:00) | |
131 | // and converts a time value WITHOUT correcting for the local time zone | |
132 | //////////////////////////////////////////////////////////////////////// | |
133 | struct tm * __cdecl gmtime(const time_t *t) | |
1c193821 | 134 | { |
991420e6 | 135 | return common_localtime(t, DO_GMTIME) ; |
1c193821 JS |
136 | } |
137 | ||
92bdf9f7 | 138 | } |
f22cc0b4 JS |
139 | |
140 | //////////////////////////////////////////////////////////////////////// | |
141 | // Common code for conversion of struct tm into time_t (static) | |
142 | //////////////////////////////////////////////////////////////////////// | |
143 | static time_t __cdecl common_tm_to_time(int day, int month, int year, int hour, int minute, int second) | |
1c193821 | 144 | { |
991420e6 WS |
145 | time_t prog = 0 ; |
146 | static int mdays[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ; | |
147 | ||
148 | while (--month) | |
149 | { | |
150 | prog += mdays[month - 1] ; | |
151 | if (month == 2 && is_leap(year)) | |
152 | prog++ ; | |
153 | } | |
154 | ||
155 | // Calculate seconds in elapsed days | |
156 | prog = day - 1 ; // align first day of the year to zero | |
157 | prog += (DAYS_IN_ONE_YEAR * (year - 1970) + (year - 1901) / 4 - 19) ; | |
158 | prog *= SECONDS_IN_ONE_DAY ; | |
159 | ||
160 | // Add Calculated elapsed seconds in the current day | |
161 | prog += (hour * SECONDS_IN_ONE_HOUR + minute * | |
162 | SECONDS_IN_ONE_MIN + second) ; | |
163 | ||
164 | return prog ; | |
1c193821 JS |
165 | } |
166 | ||
92bdf9f7 VZ |
167 | extern "C" |
168 | { | |
f22cc0b4 JS |
169 | |
170 | //////////////////////////////////////////////////////////////////////// | |
171 | // Returns the number of seconds elapsed since | |
172 | // midnight(00:00:00) of 1 January 1970 | |
173 | //////////////////////////////////////////////////////////////////////// | |
174 | time_t __cdecl time(time_t *t) | |
1c193821 | 175 | { |
991420e6 | 176 | time_t prog = 0 ; |
f22cc0b4 | 177 | |
991420e6 WS |
178 | if (t != NULL) |
179 | { | |
180 | SYSTEMTIME SystemTime; | |
f22cc0b4 | 181 | |
991420e6 WS |
182 | ::GetLocalTime(&SystemTime) ; |
183 | prog = common_tm_to_time(SystemTime.wDay, SystemTime.wMonth, SystemTime.wYear, | |
184 | SystemTime.wHour, SystemTime.wMinute, SystemTime.wSecond) ; | |
185 | *t = prog ; | |
186 | } | |
f22cc0b4 | 187 | |
991420e6 | 188 | return prog ; |
1c193821 JS |
189 | } |
190 | ||
f22cc0b4 JS |
191 | //////////////////////////////////////////////////////////////////////// |
192 | // Converts the local time provided by struct tm | |
193 | // into a time_t calendar value | |
194 | //////////////////////////////////////////////////////////////////////// | |
195 | time_t __cdecl mktime(struct tm *t) | |
196 | { | |
991420e6 | 197 | 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)) ; |
f22cc0b4 JS |
198 | } |
199 | ||
200 | ||
201 | size_t __cdecl wcsftime(wchar_t *, size_t, const wchar_t *, const struct tm *) | |
202 | { | |
203 | // TODO : maybe copy something from wxString::Printf | |
204 | ||
205 | return 0; | |
206 | } | |
92bdf9f7 VZ |
207 | |
208 | } // extern "C" |