msvcrt: Change strtod_l implementation.
[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 "mtdll.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41
42 static const int MonthLengths[2][12] =
43 {
44     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
45     { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
46 };
47
48 static inline int IsLeapYear(int Year)
49 {
50     return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
51 }
52
53 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
54 {
55     memset( dest, 0, sizeof(*dest) );
56     dest->tm_sec   = src->tm_sec;
57     dest->tm_min   = src->tm_min;
58     dest->tm_hour  = src->tm_hour;
59     dest->tm_mday  = src->tm_mday;
60     dest->tm_mon   = src->tm_mon;
61     dest->tm_year  = src->tm_year;
62     dest->tm_wday  = src->tm_wday;
63     dest->tm_yday  = src->tm_yday;
64     dest->tm_isdst = src->tm_isdst;
65 }
66
67 static inline void unix_tm_to_msvcrt( struct MSVCRT_tm *dest, const struct tm *src )
68 {
69     memset( dest, 0, sizeof(*dest) );
70     dest->tm_sec   = src->tm_sec;
71     dest->tm_min   = src->tm_min;
72     dest->tm_hour  = src->tm_hour;
73     dest->tm_mday  = src->tm_mday;
74     dest->tm_mon   = src->tm_mon;
75     dest->tm_year  = src->tm_year;
76     dest->tm_wday  = src->tm_wday;
77     dest->tm_yday  = src->tm_yday;
78     dest->tm_isdst = src->tm_isdst;
79 }
80
81 #define SECSPERDAY        86400
82 /* 1601 to 1970 is 369 years plus 89 leap days */
83 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
84 #define TICKSPERSEC       10000000
85 #define TICKSPERMSEC      10000
86 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
87
88 /**********************************************************************
89  *              _mktime64 (MSVCRT.@)
90  */
91 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
92 {
93     time_t secs;
94     struct tm tm;
95
96     msvcrt_tm_to_unix( &tm, mstm );
97     secs = mktime( &tm );
98     unix_tm_to_msvcrt( mstm, &tm );
99
100     return secs < 0 ? -1 : secs;
101 }
102
103 /**********************************************************************
104  *              _mktime32 (MSVCRT.@)
105  */
106 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
107 {
108     return MSVCRT__mktime64( mstm );
109 }
110
111 /**********************************************************************
112  *              mktime (MSVCRT.@)
113  */
114 #ifdef _WIN64
115 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
116 {
117     return MSVCRT__mktime64( mstm );
118 }
119 #else
120 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
121 {
122     return MSVCRT__mktime32( mstm );
123 }
124 #endif
125
126 /*********************************************************************
127  *      _localtime64 (MSVCRT.@)
128  */
129 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
130 {
131     struct tm *tm;
132     thread_data_t *data;
133     time_t seconds = *secs;
134
135     if (seconds < 0) return NULL;
136
137     _mlock(_TIME_LOCK);
138     if (!(tm = localtime( &seconds))) {
139         _munlock(_TIME_LOCK);
140         return NULL;
141     }
142
143     data = msvcrt_get_thread_data();
144     unix_tm_to_msvcrt( &data->time_buffer, tm );
145     _munlock(_TIME_LOCK);
146
147     return &data->time_buffer;
148 }
149
150 /*********************************************************************
151  *      _localtime32 (MSVCRT.@)
152  */
153 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
154 {
155     MSVCRT___time64_t secs64 = *secs;
156     return MSVCRT__localtime64( &secs64 );
157 }
158
159 /*********************************************************************
160  *      localtime (MSVCRT.@)
161  */
162 #ifdef _WIN64
163 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
164 {
165     return MSVCRT__localtime64( secs );
166 }
167 #else
168 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
169 {
170     return MSVCRT__localtime32( secs );
171 }
172 #endif
173
174 /*********************************************************************
175  *      _gmtime64 (MSVCRT.@)
176  */
177 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t* secs)
178 {
179   thread_data_t * const data = msvcrt_get_thread_data();
180   int i;
181   FILETIME ft;
182   SYSTEMTIME st;
183
184   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
185
186   ft.dwHighDateTime = (UINT)(time >> 32);
187   ft.dwLowDateTime  = (UINT)time;
188
189   FileTimeToSystemTime(&ft, &st);
190
191   if (st.wYear < 1970) return NULL;
192
193   data->time_buffer.tm_sec  = st.wSecond;
194   data->time_buffer.tm_min  = st.wMinute;
195   data->time_buffer.tm_hour = st.wHour;
196   data->time_buffer.tm_mday = st.wDay;
197   data->time_buffer.tm_year = st.wYear - 1900;
198   data->time_buffer.tm_mon  = st.wMonth - 1;
199   data->time_buffer.tm_wday = st.wDayOfWeek;
200   for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
201     data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
202   }
203
204   data->time_buffer.tm_yday += st.wDay - 1;
205   data->time_buffer.tm_isdst = 0;
206
207   return &data->time_buffer;
208 }
209
210 /*********************************************************************
211  *      _gmtime32 (MSVCRT.@)
212  */
213 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
214 {
215     MSVCRT___time64_t secs64 = *secs;
216     return MSVCRT__gmtime64( &secs64 );
217 }
218
219 /*********************************************************************
220  *      gmtime (MSVCRT.@)
221  */
222 #ifdef _WIN64
223 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
224 {
225     return MSVCRT__gmtime64( secs );
226 }
227 #else
228 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
229 {
230     return MSVCRT__gmtime32( secs );
231 }
232 #endif
233
234 /**********************************************************************
235  *              _strdate (MSVCRT.@)
236  */
237 char* CDECL _strdate(char* date)
238 {
239   static const char format[] = "MM'/'dd'/'yy";
240
241   GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
242
243   return date;
244 }
245
246 /**********************************************************************
247  *              _wstrdate (MSVCRT.@)
248  */
249 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
250 {
251   static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
252
253   GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
254
255   return date;
256 }
257
258 /*********************************************************************
259  *              _strtime (MSVCRT.@)
260  */
261 char* CDECL _strtime(char* time)
262 {
263   static const char format[] = "HH':'mm':'ss";
264
265   GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9); 
266
267   return time;
268 }
269
270 /*********************************************************************
271  *              _wstrtime (MSVCRT.@)
272  */
273 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
274 {
275   static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
276
277   GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
278
279   return time;
280 }
281
282 /*********************************************************************
283  *              clock (MSVCRT.@)
284  */
285 MSVCRT_clock_t CDECL MSVCRT_clock(void)
286 {
287   FILETIME ftc, fte, ftk, ftu;
288   ULONGLONG utime, ktime;
289  
290   MSVCRT_clock_t clock;
291
292   GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
293
294   ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
295   utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
296
297   clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
298
299   return clock;
300 }
301
302 /*********************************************************************
303  *              _difftime64 (MSVCRT.@)
304  */
305 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
306 {
307   return (double)(time1 - time2);
308 }
309
310 /*********************************************************************
311  *              _difftime32 (MSVCRT.@)
312  */
313 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
314 {
315   return (double)(time1 - time2);
316 }
317
318 /*********************************************************************
319  *              difftime (MSVCRT.@)
320  */
321 #ifdef _WIN64
322 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
323 {
324     return MSVCRT__difftime64( time1, time2 );
325 }
326 #else
327 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
328 {
329     return MSVCRT__difftime32( time1, time2 );
330 }
331 #endif
332
333 /*********************************************************************
334  *              _ftime64 (MSVCRT.@)
335  */
336 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
337 {
338   TIME_ZONE_INFORMATION tzinfo;
339   FILETIME ft;
340   ULONGLONG time;
341
342   DWORD tzid = GetTimeZoneInformation(&tzinfo);
343   GetSystemTimeAsFileTime(&ft);
344
345   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
346
347   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
348   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
349   buf->timezone = tzinfo.Bias +
350       ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
351       ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
352   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
353 }
354
355 /*********************************************************************
356  *              _ftime32 (MSVCRT.@)
357  */
358 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
359 {
360     struct MSVCRT___timeb64 buf64;
361
362     MSVCRT__ftime64( &buf64 );
363     buf->time     = buf64.time;
364     buf->millitm  = buf64.millitm;
365     buf->timezone = buf64.timezone;
366     buf->dstflag  = buf64.dstflag;
367 }
368
369 /*********************************************************************
370  *              _ftime (MSVCRT.@)
371  */
372 #ifdef _WIN64
373 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
374 {
375     return MSVCRT__ftime64( buf );
376 }
377 #else
378 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
379 {
380     return MSVCRT__ftime32( buf );
381 }
382 #endif
383
384 /*********************************************************************
385  *              _time64 (MSVCRT.@)
386  */
387 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
388 {
389     MSVCRT___time64_t curtime;
390     struct MSVCRT___timeb64 tb;
391
392     MSVCRT__ftime64(&tb);
393
394     curtime = tb.time;
395     return buf ? *buf = curtime : curtime;
396 }
397
398 /*********************************************************************
399  *              _time32 (MSVCRT.@)
400  */
401 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
402 {
403     MSVCRT___time32_t curtime;
404     struct MSVCRT___timeb64 tb;
405
406     MSVCRT__ftime64(&tb);
407
408     curtime = tb.time;
409     return buf ? *buf = curtime : curtime;
410 }
411
412 /*********************************************************************
413  *              time (MSVCRT.@)
414  */
415 #ifdef _WIN64
416 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
417 {
418     return MSVCRT__time64( buf );
419 }
420 #else
421 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
422 {
423     return MSVCRT__time32( buf );
424 }
425 #endif
426
427 /*********************************************************************
428  *              _daylight (MSVCRT.@)
429  */
430 int MSVCRT___daylight = 0;
431
432 /*********************************************************************
433  *              __p_daylight (MSVCRT.@)
434  */
435 int * CDECL MSVCRT___p__daylight(void)
436 {
437         return &MSVCRT___daylight;
438 }
439
440 /*********************************************************************
441  *              _dstbias (MSVCRT.@)
442  */
443 int MSVCRT__dstbias = 0;
444
445 /*********************************************************************
446  *              __p_dstbias (MSVCRT.@)
447  */
448 int * CDECL __p__dstbias(void)
449 {
450     return &MSVCRT__dstbias;
451 }
452
453 /*********************************************************************
454  *              _timezone (MSVCRT.@)
455  */
456 MSVCRT_long MSVCRT___timezone = 0;
457
458 /*********************************************************************
459  *              __p_timezone (MSVCRT.@)
460  */
461 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
462 {
463         return &MSVCRT___timezone;
464 }
465
466 /*********************************************************************
467  *              _tzname (MSVCRT.@)
468  * NOTES
469  *  Some apps (notably Mozilla) insist on writing to these, so the buffer
470  *  must be large enough.  The size is picked based on observation of
471  *  Windows XP.
472  */
473 static char tzname_std[64] = "";
474 static char tzname_dst[64] = "";
475 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
476
477 /*********************************************************************
478  *              __p_tzname (MSVCRT.@)
479  */
480 char ** CDECL __p__tzname(void)
481 {
482         return MSVCRT__tzname;
483 }
484
485 /*********************************************************************
486  *              _tzset (MSVCRT.@)
487  */
488 void CDECL MSVCRT__tzset(void)
489 {
490     tzset();
491 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
492     MSVCRT___daylight = daylight;
493     MSVCRT___timezone = timezone;
494 #else
495     {
496         static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
497         time_t t;
498         struct tm *tmp;
499         int zone_january, zone_july;
500
501         _mlock(_TIME_LOCK);
502         t = (time(NULL) / seconds_in_year) * seconds_in_year;
503         tmp = localtime(&t);
504         zone_january = -tmp->tm_gmtoff;
505         t += seconds_in_year / 2;
506         tmp = localtime(&t);
507         zone_july = -tmp->tm_gmtoff;
508         _munlock(_TIME_LOCK);
509
510         MSVCRT___daylight = (zone_january != zone_july);
511         MSVCRT___timezone = max(zone_january, zone_july);
512     }
513 #endif
514     lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
515     tzname_std[sizeof(tzname_std) - 1] = '\0';
516     lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
517     tzname_dst[sizeof(tzname_dst) - 1] = '\0';
518 }
519
520 /*********************************************************************
521  *              strftime (MSVCRT.@)
522  */
523 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
524                                      const struct MSVCRT_tm *mstm )
525 {
526     struct tm tm;
527
528     msvcrt_tm_to_unix( &tm, mstm );
529     return strftime( str, max, format, &tm );
530 }
531
532 /*********************************************************************
533  *              wcsftime (MSVCRT.@)
534  */
535 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
536                                      const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
537 {
538     char *s, *fmt;
539     MSVCRT_size_t len;
540
541     TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
542
543     len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
544     if (!(fmt = MSVCRT_malloc( len ))) return 0;
545     WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
546
547     if ((s = MSVCRT_malloc( max*4 )))
548     {
549         struct tm tm;
550         msvcrt_tm_to_unix( &tm, mstm );
551         if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
552         len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
553         if (len) len--;
554         MSVCRT_free( s );
555     }
556     else len = 0;
557
558     MSVCRT_free( fmt );
559     return len;
560 }
561
562 /*********************************************************************
563  *              asctime (MSVCRT.@)
564  */
565 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
566 {
567     thread_data_t *data = msvcrt_get_thread_data();
568     struct tm tm;
569
570     msvcrt_tm_to_unix( &tm, mstm );
571
572     if (!data->asctime_buffer)
573         data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
574
575     /* FIXME: may want to map from Unix codepage to CP_ACP */
576 #ifdef HAVE_ASCTIME_R
577     asctime_r( &tm, data->asctime_buffer );
578 #else
579     strcpy( data->asctime_buffer, asctime(&tm) );
580 #endif
581     return data->asctime_buffer;
582 }
583
584 /*********************************************************************
585  *              _wasctime (MSVCRT.@)
586  */
587 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
588 {
589     thread_data_t *data = msvcrt_get_thread_data();
590     struct tm tm;
591     char buffer[30];
592
593     msvcrt_tm_to_unix( &tm, mstm );
594
595     if (!data->wasctime_buffer)
596         data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
597 #ifdef HAVE_ASCTIME_R
598     asctime_r( &tm, buffer );
599 #else
600     strcpy( buffer, asctime(&tm) );
601 #endif
602     MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
603     return data->wasctime_buffer;
604 }
605
606 /*********************************************************************
607  *              _ctime64 (MSVCRT.@)
608  */
609 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
610 {
611     struct MSVCRT_tm *t;
612     t = MSVCRT__localtime64( time );
613     if (!t) return NULL;
614     return MSVCRT_asctime( t );
615 }
616
617 /*********************************************************************
618  *              _ctime32 (MSVCRT.@)
619  */
620 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
621 {
622     struct MSVCRT_tm *t;
623     t = MSVCRT__localtime32( time );
624     if (!t) return NULL;
625     return MSVCRT_asctime( t );
626 }
627
628 /*********************************************************************
629  *              ctime (MSVCRT.@)
630  */
631 #ifdef _WIN64
632 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
633 {
634     return MSVCRT__ctime64( time );
635 }
636 #else
637 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
638 {
639     return MSVCRT__ctime32( time );
640 }
641 #endif
642
643 /*********************************************************************
644  *              _wctime64 (MSVCRT.@)
645  */
646 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
647 {
648     return MSVCRT__wasctime( MSVCRT__localtime64(time) );
649 }
650
651 /*********************************************************************
652  *              _wctime32 (MSVCRT.@)
653  */
654 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
655 {
656     return MSVCRT__wasctime( MSVCRT__localtime32(time) );
657 }
658
659 /*********************************************************************
660  *              _wctime (MSVCRT.@)
661  */
662 #ifdef _WIN64
663 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
664 {
665     return MSVCRT__wctime64( time );
666 }
667 #else
668 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
669 {
670     return MSVCRT__wctime32( time );
671 }
672 #endif