make_dlls: Recursively ignore testlist.c in all tests directories.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26
27 #define _POSIX_PTHREAD_SEMANTICS /* switch to a 2 arg style asctime_r on Solaris */
28 #include <time.h>
29 #ifdef HAVE_SYS_TIMES_H
30 # include <sys/times.h>
31 #endif
32 #include <limits.h>
33
34 #include "msvcrt.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
40
41 static const int MonthLengths[2][12] =
42 {
43     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
44     { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
45 };
46
47 static inline int IsLeapYear(int Year)
48 {
49     return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
50 }
51
52 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
53 {
54     memset( dest, 0, sizeof(*dest) );
55     dest->tm_sec   = src->tm_sec;
56     dest->tm_min   = src->tm_min;
57     dest->tm_hour  = src->tm_hour;
58     dest->tm_mday  = src->tm_mday;
59     dest->tm_mon   = src->tm_mon;
60     dest->tm_year  = src->tm_year;
61     dest->tm_wday  = src->tm_wday;
62     dest->tm_yday  = src->tm_yday;
63     dest->tm_isdst = src->tm_isdst;
64 }
65
66 #define SECSPERDAY        86400
67 /* 1601 to 1970 is 369 years plus 89 leap days */
68 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
69 #define TICKSPERSEC       10000000
70 #define TICKSPERMSEC      10000
71 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
72
73 /**********************************************************************
74  *              mktime (MSVCRT.@)
75  */
76 MSVCRT_time_t CDECL MSVCRT_mktime(struct MSVCRT_tm *t)
77 {
78     MSVCRT_time_t secs;
79     FILETIME lft, uft;
80     ULONGLONG time;
81     struct MSVCRT_tm ts, *ptm;
82     int cleaps, day;
83
84     ts=*t;
85     /* to prevent arithmetic overflows put constraints on some fields */
86     /* whether the effective date falls in the 1970-2038 time period */
87     /* will be tested later */
88     /* BTW, I have no idea what limits native msvcrt has. */
89     if ( ts.tm_year < 0 || ts.tm_year > 140  ||
90             ts.tm_mon < -840 || ts.tm_mon > 840 ||
91             ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
92             ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
93             ts.tm_min < -29000000 || ts.tm_min > 29000000 )
94         return -1;
95            
96     /* normalize the tm month fields */
97     if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
98     if( ts.tm_mon < 0) {
99         int dy = (11 - ts.tm_mon) / 12;
100         ts.tm_year -= dy;
101         ts.tm_mon += dy * 12;
102     }
103     /* now calculate a day count from the date
104      * First start counting years from March. This way the leap days
105      * are added at the end of the year, not somewhere in the middle.
106      * Formula's become so much less complicate that way.
107      * To convert: add 12 to the month numbers of Jan and Feb, and 
108      * take 1 from the year */
109     if(ts.tm_mon < 2) {
110         ts.tm_mon += 14;
111         ts.tm_year += 1899;
112     } else {
113         ts.tm_mon += 2;
114         ts.tm_year += 1900;
115     }
116     cleaps = (3 * (ts.tm_year / 100) + 3) / 4;   /* nr of "century leap years"*/
117     day =  (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
118              (1959 * ts.tm_mon) / 64 +    /* months * daypermonth */
119              ts.tm_mday -                 /* day of the month */
120              584817 ;                     /* zero that on 1601-01-01 */
121     /* done */
122
123     /* convert to 100 ns ticks */
124     time = ((((ULONGLONG) day * 24 +
125             ts.tm_hour) * 60 +
126             ts.tm_min) * 60 +
127             ts.tm_sec ) * TICKSPERSEC;
128     
129     lft.dwHighDateTime = (DWORD) (time >> 32);
130     lft.dwLowDateTime = (DWORD) time;
131
132     LocalFileTimeToFileTime(&lft, &uft);
133
134     time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
135     time /= TICKSPERSEC;
136     if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
137         return -1;
138     secs = time - SECS_1601_TO_1970;
139     /* compute tm_wday, tm_yday and renormalize the other fields of the
140      * tm structure */
141     if ((ptm = MSVCRT_localtime( &secs ))) *t = *ptm;
142
143     return secs; 
144 }
145
146 /*********************************************************************
147  *      localtime (MSVCRT.@)
148  */
149 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT_time_t* secs)
150 {
151   thread_data_t * const data = msvcrt_get_thread_data();
152   int i;
153   FILETIME ft, lft;
154   SYSTEMTIME st;
155   DWORD tzid;
156   TIME_ZONE_INFORMATION tzinfo;
157   ULONGLONG time;
158
159   /* time < 0 means a date before midnight of January 1, 1970 */
160   if (*secs < 0) return NULL;
161
162   time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
163
164   ft.dwHighDateTime = (UINT)(time >> 32);
165   ft.dwLowDateTime  = (UINT)time;
166
167   FileTimeToLocalFileTime(&ft, &lft);
168   FileTimeToSystemTime(&lft, &st);
169
170   data->time_buffer.tm_sec  = st.wSecond;
171   data->time_buffer.tm_min  = st.wMinute;
172   data->time_buffer.tm_hour = st.wHour;
173   data->time_buffer.tm_mday = st.wDay;
174   data->time_buffer.tm_year = st.wYear - 1900;
175   data->time_buffer.tm_mon  = st.wMonth  - 1;
176   data->time_buffer.tm_wday = st.wDayOfWeek;
177
178   for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
179     data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
180   }
181
182   data->time_buffer.tm_yday += st.wDay - 1;
183  
184   tzid = GetTimeZoneInformation(&tzinfo);
185
186   if (tzid == TIME_ZONE_ID_INVALID)
187     data->time_buffer.tm_isdst = -1;
188   else 
189     data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
190
191   return &data->time_buffer;
192 }
193
194 /*********************************************************************
195  *      gmtime (MSVCRT.@)
196  */
197 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT_time_t* secs)
198 {
199   thread_data_t * const data = msvcrt_get_thread_data();
200   int i;
201   FILETIME ft;
202   SYSTEMTIME st;
203
204   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
205
206   ft.dwHighDateTime = (UINT)(time >> 32);
207   ft.dwLowDateTime  = (UINT)time;
208
209   FileTimeToSystemTime(&ft, &st);
210
211   if (st.wYear < 1970) return NULL;
212
213   data->time_buffer.tm_sec  = st.wSecond;
214   data->time_buffer.tm_min  = st.wMinute;
215   data->time_buffer.tm_hour = st.wHour;
216   data->time_buffer.tm_mday = st.wDay;
217   data->time_buffer.tm_year = st.wYear - 1900;
218   data->time_buffer.tm_mon  = st.wMonth - 1;
219   data->time_buffer.tm_wday = st.wDayOfWeek;
220   for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
221     data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
222   }
223
224   data->time_buffer.tm_yday += st.wDay - 1;
225   data->time_buffer.tm_isdst = 0;
226
227   return &data->time_buffer;
228 }
229
230 /**********************************************************************
231  *              _strdate (MSVCRT.@)
232  */
233 char* CDECL _strdate(char* date)
234 {
235   LPCSTR format = "MM'/'dd'/'yy";
236
237   GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
238
239   return date;
240 }
241
242 /**********************************************************************
243  *              _wstrdate (MSVCRT.@)
244  */
245 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
246 {
247   static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
248
249   GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
250
251   return date;
252 }
253
254 /*********************************************************************
255  *              _strtime (MSVCRT.@)
256  */
257 char* CDECL _strtime(char* time)
258 {
259   LPCSTR format = "HH':'mm':'ss";
260
261   GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9); 
262
263   return time;
264 }
265
266 /*********************************************************************
267  *              _wstrtime (MSVCRT.@)
268  */
269 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
270 {
271   static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
272
273   GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
274
275   return time;
276 }
277
278 /*********************************************************************
279  *              clock (MSVCRT.@)
280  */
281 MSVCRT_clock_t CDECL MSVCRT_clock(void)
282 {
283   FILETIME ftc, fte, ftk, ftu;
284   ULONGLONG utime, ktime;
285  
286   MSVCRT_clock_t clock;
287
288   GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
289
290   ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
291   utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
292
293   clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
294
295   return clock;
296 }
297
298 /*********************************************************************
299  *              difftime (MSVCRT.@)
300  */
301 double CDECL MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
302 {
303   return (double)(time1 - time2);
304 }
305
306 /*********************************************************************
307  *              _ftime (MSVCRT.@)
308  */
309 void CDECL _ftime(struct MSVCRT__timeb *buf)
310 {
311   TIME_ZONE_INFORMATION tzinfo;
312   FILETIME ft;
313   ULONGLONG time;
314
315   DWORD tzid = GetTimeZoneInformation(&tzinfo);
316   GetSystemTimeAsFileTime(&ft);
317
318   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
319
320   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
321   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
322   buf->timezone = tzinfo.Bias +
323       ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
324       ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
325   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
326 }
327
328 /*********************************************************************
329  *              time (MSVCRT.@)
330  */
331 MSVCRT_time_t CDECL MSVCRT_time(MSVCRT_time_t* buf)
332 {
333   MSVCRT_time_t curtime;
334   struct MSVCRT__timeb tb;
335
336   _ftime(&tb);
337
338   curtime = tb.time;
339   return buf ? *buf = curtime : curtime;
340 }
341
342 /*********************************************************************
343  *              _daylight (MSVCRT.@)
344  */
345 int MSVCRT___daylight = 0;
346
347 /*********************************************************************
348  *              __p_daylight (MSVCRT.@)
349  */
350 int * CDECL MSVCRT___p__daylight(void)
351 {
352         return &MSVCRT___daylight;
353 }
354
355 /*********************************************************************
356  *              _dstbias (MSVCRT.@)
357  */
358 int MSVCRT__dstbias = 0;
359
360 /*********************************************************************
361  *              __p_dstbias (MSVCRT.@)
362  */
363 int * CDECL __p__dstbias(void)
364 {
365     return &MSVCRT__dstbias;
366 }
367
368 /*********************************************************************
369  *              _timezone (MSVCRT.@)
370  */
371 long MSVCRT___timezone = 0;
372
373 /*********************************************************************
374  *              __p_timezone (MSVCRT.@)
375  */
376 long * CDECL MSVCRT___p__timezone(void)
377 {
378         return &MSVCRT___timezone;
379 }
380
381 /*********************************************************************
382  *              _tzname (MSVCRT.@)
383  * NOTES
384  *  Some apps (notably Mozilla) insist on writing to these, so the buffer
385  *  must be large enough.  The size is picked based on observation of
386  *  Windows XP.
387  */
388 static char tzname_std[64] = "";
389 static char tzname_dst[64] = "";
390 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
391
392 /*********************************************************************
393  *              __p_tzname (MSVCRT.@)
394  */
395 char ** CDECL __p__tzname(void)
396 {
397         return MSVCRT__tzname;
398 }
399
400 /*********************************************************************
401  *              _tzset (MSVCRT.@)
402  */
403 void CDECL MSVCRT__tzset(void)
404 {
405     tzset();
406 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
407     MSVCRT___daylight = daylight;
408     MSVCRT___timezone = timezone;
409 #else
410     {
411         static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
412         time_t t;
413         struct tm *tmp;
414         long zone_january, zone_july;
415
416         t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
417         tmp = localtime(&t);
418         zone_january = -tmp->tm_gmtoff;
419         t += seconds_in_year / 2;
420         tmp = localtime(&t);
421         zone_july = -tmp->tm_gmtoff;
422         MSVCRT___daylight = (zone_january != zone_july);
423         MSVCRT___timezone = max(zone_january, zone_july);
424     }
425 #endif
426     lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
427     tzname_std[sizeof(tzname_std) - 1] = '\0';
428     lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
429     tzname_dst[sizeof(tzname_dst) - 1] = '\0';
430 }
431
432 /*********************************************************************
433  *              strftime (MSVCRT.@)
434  */
435 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
436                                      const struct MSVCRT_tm *mstm )
437 {
438     struct tm tm;
439
440     msvcrt_tm_to_unix( &tm, mstm );
441     return strftime( str, max, format, &tm );
442 }
443
444 /*********************************************************************
445  *              wcsftime (MSVCRT.@)
446  */
447 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
448                                      const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
449 {
450     char *s, *fmt;
451     MSVCRT_size_t len;
452
453     TRACE("%p %d %s %p\n", str, max, debugstr_w(format), mstm );
454
455     len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
456     if (!(fmt = MSVCRT_malloc( len ))) return 0;
457     WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
458
459     if ((s = MSVCRT_malloc( max*4 )))
460     {
461         struct tm tm;
462         msvcrt_tm_to_unix( &tm, mstm );
463         if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
464         len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
465         if (len) len--;
466         MSVCRT_free( s );
467     }
468     else len = 0;
469
470     MSVCRT_free( fmt );
471     return len;
472 }
473
474 /*********************************************************************
475  *              asctime (MSVCRT.@)
476  */
477 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
478 {
479     thread_data_t *data = msvcrt_get_thread_data();
480     struct tm tm;
481
482     msvcrt_tm_to_unix( &tm, mstm );
483
484     if (!data->asctime_buffer)
485         data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
486
487     /* FIXME: may want to map from Unix codepage to CP_ACP */
488 #ifdef HAVE_ASCTIME_R
489     asctime_r( &tm, data->asctime_buffer );
490 #else
491     strcpy( data->asctime_buffer, asctime(&tm) );
492 #endif
493     return data->asctime_buffer;
494 }
495
496 /*********************************************************************
497  *              _wasctime (MSVCRT.@)
498  */
499 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
500 {
501     thread_data_t *data = msvcrt_get_thread_data();
502     struct tm tm;
503     char buffer[30];
504
505     msvcrt_tm_to_unix( &tm, mstm );
506
507     if (!data->wasctime_buffer)
508         data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
509 #ifdef HAVE_ASCTIME_R
510     asctime_r( &tm, buffer );
511 #else
512     strcpy( buffer, asctime(&tm) );
513 #endif
514     MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
515     return data->wasctime_buffer;
516 }
517
518 /*********************************************************************
519  *              ctime (MSVCRT.@)
520  */
521 char * CDECL MSVCRT_ctime(const MSVCRT_time_t *time)
522 {
523     return MSVCRT_asctime( MSVCRT_localtime(time) );
524 }
525
526 /*********************************************************************
527  *              _wctime (MSVCRT.@)
528  */
529 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT_time_t *time)
530 {
531     return MSVCRT__wasctime( MSVCRT_localtime(time) );
532 }