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