- move _timezone to time.c, and correct its type
[wine] / dlls / msvcrt / time.c
1 /*
2  * msvcrt.dll date/time functions
3  *
4  * Copyright 1996,1998 Marcus Meissner
5  * Copyright 1996 Jukka Iivonen
6  * Copyright 1997,2000 Uwe Bonnes
7  * Copyright 2000 Jon Griffiths
8  * Copyright 2004 Hans Leidekker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include <time.h>
28 #ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
30 #endif
31 #include <limits.h>
32
33 #include "msvcrt.h"
34 #include "winbase.h"
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38
39 static const int MonthLengths[2][12] =
40 {
41     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
42     { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
43 };
44
45 static inline int IsLeapYear(int Year)
46 {
47     return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
48 }
49
50 #define SECSPERDAY        86400
51 /* 1601 to 1970 is 369 years plus 89 leap days */
52 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
53 #define TICKSPERSEC       10000000
54 #define TICKSPERMSEC      10000
55 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
56
57 /* native uses a single static buffer for localtime/gmtime/mktime */
58 static struct MSVCRT_tm tm;
59
60 /**********************************************************************
61  *              mktime (MSVCRT.@)
62  */
63 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
64 {
65     MSVCRT_time_t secs;
66     FILETIME lft, uft;
67     ULONGLONG time;
68     struct MSVCRT_tm ts;
69     int cleaps, day;
70
71     ts=*t;
72     /* to prevent arithmetic overflows put constraints on some fields */
73     /* whether the effective date falls in the 1970-2038 time period */
74     /* will be tested later */
75     /* BTW, I have no idea what limits native msvcrt has. */
76     if ( ts.tm_year < 0 || ts.tm_year > 140  ||
77             ts.tm_mon < -840 || ts.tm_mon > 840 ||
78             ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
79             ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
80             ts.tm_min < -29000000 || ts.tm_min > 29000000 )
81         return -1;
82            
83     /* normalize the tm month fields */
84     if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
85     if( ts.tm_mon < 0) {
86         int dy = (11 - ts.tm_mon) / 12;
87         ts.tm_year -= dy;
88         ts.tm_mon += dy * 12;
89     }
90     /* now calculate a day count from the date
91      * First start counting years from March. This way the leap days
92      * are added at the end of the year, not somewhere in the middle.
93      * Formula's become so much less complicate that way.
94      * To convert: add 12 to the month numbers of Jan and Feb, and 
95      * take 1 from the year */
96     if(ts.tm_mon < 2) {
97         ts.tm_mon += 14;
98         ts.tm_year += 1899;
99     } else {
100         ts.tm_mon += 2;
101         ts.tm_year += 1900;
102     }
103     cleaps = (3 * (ts.tm_year / 100) + 3) / 4;   /* nr of "century leap years"*/
104     day =  (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
105              (1959 * ts.tm_mon) / 64 +    /* months * daypermonth */
106              ts.tm_mday -                 /* day of the month */
107              584817 ;                     /* zero that on 1601-01-01 */
108     /* done */
109
110     /* convert to 100 ns ticks */
111     time = ((((ULONGLONG) day * 24 +
112             ts.tm_hour) * 60 +
113             ts.tm_min) * 60 +
114             ts.tm_sec ) * TICKSPERSEC;
115     
116     lft.dwHighDateTime = (DWORD) (time >> 32);
117     lft.dwLowDateTime = (DWORD) time;
118
119     LocalFileTimeToFileTime(&lft, &uft);
120
121     time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
122     time /= TICKSPERSEC;
123     if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
124         return -1;
125     secs = time - SECS_1601_TO_1970;
126     /* compute tm_wday, tm_yday and renormalize the other fields of the
127      * tm structure */
128     if( MSVCRT_localtime( &secs)) *t = tm;
129
130     return secs; 
131 }
132
133 /*********************************************************************
134  *      localtime (MSVCRT.@)
135  */
136 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
137 {
138   int i;
139
140   FILETIME ft, lft;
141   SYSTEMTIME st;
142   DWORD tzid;
143   TIME_ZONE_INFORMATION tzinfo;
144
145   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
146
147   ft.dwHighDateTime = (UINT)(time >> 32);
148   ft.dwLowDateTime  = (UINT)time;
149
150   FileTimeToLocalFileTime(&ft, &lft);
151   FileTimeToSystemTime(&lft, &st);
152
153   if (st.wYear < 1970) return NULL;
154
155   tm.tm_sec  = st.wSecond;
156   tm.tm_min  = st.wMinute;
157   tm.tm_hour = st.wHour;
158   tm.tm_mday = st.wDay;
159   tm.tm_year = st.wYear - 1900;
160   tm.tm_mon  = st.wMonth  - 1;
161   tm.tm_wday = st.wDayOfWeek;
162
163   for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
164     tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
165   }
166
167   tm.tm_yday += st.wDay - 1;
168  
169   tzid = GetTimeZoneInformation(&tzinfo);
170
171   if (tzid == TIME_ZONE_ID_INVALID)
172     tm.tm_isdst = -1;
173   else 
174     tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
175
176   return &tm;
177 }
178
179 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
180 {
181   int i;
182
183   FILETIME ft;
184   SYSTEMTIME st;
185
186   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
187
188   ft.dwHighDateTime = (UINT)(time >> 32);
189   ft.dwLowDateTime  = (UINT)time;
190
191   FileTimeToSystemTime(&ft, &st);
192
193   if (st.wYear < 1970) return NULL;
194
195   tm.tm_sec  = st.wSecond;
196   tm.tm_min  = st.wMinute;
197   tm.tm_hour = st.wHour;
198   tm.tm_mday = st.wDay;
199   tm.tm_year = st.wYear - 1900;
200   tm.tm_mon  = st.wMonth - 1;
201   tm.tm_wday = st.wDayOfWeek;
202   for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
203     tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
204   }
205
206   tm.tm_yday += st.wDay - 1;
207   tm.tm_isdst = 0;
208
209   return &tm;
210 }
211
212 /**********************************************************************
213  *              _strdate (MSVCRT.@)
214  */
215 char* _strdate(char* date)
216 {
217   LPCSTR format = "MM'/'dd'/'yy";
218
219   GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
220
221   return date;
222 }
223
224 /*********************************************************************
225  *              _strtime (MSVCRT.@)
226  */
227 char* _strtime(char* date)
228 {
229   LPCSTR format = "HH':'mm':'ss";
230
231   GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9); 
232
233   return date;
234 }
235
236 /*********************************************************************
237  *              clock (MSVCRT.@)
238  */
239 MSVCRT_clock_t MSVCRT_clock(void)
240 {
241   FILETIME ftc, fte, ftk, ftu;
242   ULONGLONG utime, ktime;
243  
244   MSVCRT_clock_t clock;
245
246   GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
247
248   ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
249   utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
250
251   clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
252
253   return clock;
254 }
255
256 /*********************************************************************
257  *              difftime (MSVCRT.@)
258  */
259 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
260 {
261   return (double)(time1 - time2);
262 }
263
264 /*********************************************************************
265  *              _ftime (MSVCRT.@)
266  */
267 void _ftime(struct MSVCRT__timeb *buf)
268 {
269   TIME_ZONE_INFORMATION tzinfo;
270   FILETIME ft;
271   ULONGLONG time;
272
273   DWORD tzid = GetTimeZoneInformation(&tzinfo);
274   GetSystemTimeAsFileTime(&ft);
275
276   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
277
278   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
279   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
280   buf->timezone = tzinfo.Bias +
281       ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
282       ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
283   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
284 }
285
286 /*********************************************************************
287  *              time (MSVCRT.@)
288  */
289 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
290 {
291   MSVCRT_time_t curtime;
292   struct MSVCRT__timeb tb;
293
294   _ftime(&tb);
295
296   curtime = tb.time;
297   return buf ? *buf = curtime : curtime;
298 }
299
300 /*********************************************************************
301  *              _daylight (MSVCRT.@)
302  */
303 int MSVCRT___daylight = 0;
304
305 /*********************************************************************
306  *              __p_daylight (MSVCRT.@)
307  */
308 int *MSVCRT___p__daylight(void)
309 {
310         return &MSVCRT___daylight;
311 }
312
313 /*********************************************************************
314  *              _timezone (MSVCRT.@)
315  */
316 long MSVCRT___timezone = 0;
317
318 /*********************************************************************
319  *              __p_timezone (MSVCRT.@)
320  */
321 long *MSVCRT___p__timezone(void)
322 {
323         return &MSVCRT___timezone;
324 }
325
326 /*********************************************************************
327  *              _tzname (MSVCRT.@)
328  * NOTES
329  *  Some apps (notably Mozilla) insist on writing to these, so the buffer
330  *  must be large enough.  The size is picked based on observation of
331  *  Windows XP.
332  */
333 static char tzname_std[64] = "";
334 static char tzname_dst[64] = "";
335 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
336
337 /*********************************************************************
338  *              __p_tzname (MSVCRT.@)
339  */
340 char **__p__tzname(void)
341 {
342         return MSVCRT__tzname;
343 }
344
345 /*********************************************************************
346  *              _tzset (MSVCRT.@)
347  */
348 void MSVCRT__tzset(void)
349 {
350     tzset();
351     MSVCRT___daylight = daylight;
352     MSVCRT___timezone = timezone;
353     lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
354     tzname_std[sizeof(tzname_std) - 1] = '\0';
355     lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
356     tzname_dst[sizeof(tzname_dst) - 1] = '\0';
357 }