msvcrt: Synchronize spec files across dlls.
[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 static inline void write_invalid_msvcrt_tm( struct MSVCRT_tm *tm )
82 {
83     tm->tm_sec = -1;
84     tm->tm_min = -1;
85     tm->tm_hour = -1;
86     tm->tm_mday = -1;
87     tm->tm_mon = -1;
88     tm->tm_year = -1;
89     tm->tm_wday = -1;
90     tm->tm_yday = -1;
91     tm->tm_isdst = -1;
92 }
93
94 #define SECSPERDAY        86400
95 /* 1601 to 1970 is 369 years plus 89 leap days */
96 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
97 #define TICKSPERSEC       10000000
98 #define TICKSPERMSEC      10000
99 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
100
101 /**********************************************************************
102  *              _mktime64 (MSVCRT.@)
103  */
104 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
105 {
106     time_t secs;
107     struct tm tm;
108
109     msvcrt_tm_to_unix( &tm, mstm );
110     secs = mktime( &tm );
111     unix_tm_to_msvcrt( mstm, &tm );
112
113     return secs < 0 ? -1 : secs;
114 }
115
116 /**********************************************************************
117  *              _mktime32 (MSVCRT.@)
118  */
119 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
120 {
121     return MSVCRT__mktime64( mstm );
122 }
123
124 /**********************************************************************
125  *              mktime (MSVCRT.@)
126  */
127 #ifdef _WIN64
128 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
129 {
130     return MSVCRT__mktime64( mstm );
131 }
132 #else
133 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
134 {
135     return MSVCRT__mktime32( mstm );
136 }
137 #endif
138
139 /**********************************************************************
140  *              _mkgmtime64 (MSVCRT.@)
141  *
142  * time->tm_isdst value is ignored
143  */
144 MSVCRT___time64_t CDECL MSVCRT__mkgmtime64(struct MSVCRT_tm *time)
145 {
146     SYSTEMTIME st;
147     FILETIME ft;
148     MSVCRT___time64_t ret;
149     int i;
150
151     st.wMilliseconds = 0;
152     st.wSecond = time->tm_sec;
153     st.wMinute = time->tm_min;
154     st.wHour = time->tm_hour;
155     st.wDay = time->tm_mday;
156     st.wMonth = time->tm_mon+1;
157     st.wYear = time->tm_year+1900;
158
159     if(!SystemTimeToFileTime(&st, &ft))
160         return -1;
161
162     FileTimeToSystemTime(&ft, &st);
163     time->tm_wday = st.wDayOfWeek;
164
165     for(i=time->tm_yday=0; i<st.wMonth-1; i++)
166         time->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
167     time->tm_yday += st.wDay-1;
168
169     ret = ((MSVCRT___time64_t)ft.dwHighDateTime<<32)+ft.dwLowDateTime;
170     ret = (ret-TICKS_1601_TO_1970)/TICKSPERSEC;
171     return ret;
172 }
173
174 /**********************************************************************
175  *              _mkgmtime32 (MSVCRT.@)
176  */
177 MSVCRT___time32_t CDECL MSVCRT__mkgmtime32(struct MSVCRT_tm *time)
178 {
179     return MSVCRT__mkgmtime64(time);
180 }
181
182 /**********************************************************************
183  *              _mkgmtime (MSVCRT.@)
184  */
185 #ifdef _WIN64
186 MSVCRT___time64_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
187 {
188     return MSVCRT__mkgmtime64(time);
189 }
190 #else
191 MSVCRT___time32_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
192 {
193     return MSVCRT__mkgmtime32(time);
194 }
195 #endif
196
197 /*********************************************************************
198  *      _localtime64 (MSVCRT.@)
199  */
200 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
201 {
202     struct tm *tm;
203     thread_data_t *data;
204     time_t seconds = *secs;
205
206     if (seconds < 0) return NULL;
207
208     _mlock(_TIME_LOCK);
209     if (!(tm = localtime( &seconds))) {
210         _munlock(_TIME_LOCK);
211         return NULL;
212     }
213
214     data = msvcrt_get_thread_data();
215     unix_tm_to_msvcrt( &data->time_buffer, tm );
216     _munlock(_TIME_LOCK);
217
218     return &data->time_buffer;
219 }
220
221 /*********************************************************************
222  *      _localtime64_s (MSVCRT.@)
223  */
224 int CDECL _localtime64_s(struct MSVCRT_tm *time, const MSVCRT___time64_t *secs)
225 {
226     struct tm *tm;
227     time_t seconds;
228
229     if (!time || !secs || *secs < 0 || *secs > _MAX__TIME64_T)
230     {
231         if (time)
232             write_invalid_msvcrt_tm(time);
233
234         *MSVCRT__errno() = MSVCRT_EINVAL;
235         return MSVCRT_EINVAL;
236     }
237
238     seconds = *secs;
239
240     _mlock(_TIME_LOCK);
241     if (!(tm = localtime(&seconds)))
242     {
243         _munlock(_TIME_LOCK);
244         *MSVCRT__errno() = MSVCRT_EINVAL;
245         return MSVCRT_EINVAL;
246     }
247
248     unix_tm_to_msvcrt(time, tm);
249     _munlock(_TIME_LOCK);
250     return 0;
251 }
252
253 /*********************************************************************
254  *      _localtime32 (MSVCRT.@)
255  */
256 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
257 {
258     MSVCRT___time64_t secs64 = *secs;
259     return MSVCRT__localtime64( &secs64 );
260 }
261
262 /*********************************************************************
263  *      _localtime32_s (MSVCRT.@)
264  */
265 int CDECL _localtime32_s(struct MSVCRT_tm *time, const MSVCRT___time32_t *secs)
266 {
267     MSVCRT___time64_t secs64;
268
269     if (!time || !secs || *secs < 0)
270     {
271         if (time)
272             write_invalid_msvcrt_tm(time);
273
274         *MSVCRT__errno() = MSVCRT_EINVAL;
275         return MSVCRT_EINVAL;
276     }
277
278     secs64 = *secs;
279     return _localtime64_s(time, &secs64);
280 }
281
282 /*********************************************************************
283  *      localtime (MSVCRT.@)
284  */
285 #ifdef _WIN64
286 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
287 {
288     return MSVCRT__localtime64( secs );
289 }
290 #else
291 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
292 {
293     return MSVCRT__localtime32( secs );
294 }
295 #endif
296
297 /*********************************************************************
298  *      _gmtime64 (MSVCRT.@)
299  */
300 int CDECL MSVCRT__gmtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
301 {
302     int i;
303     FILETIME ft;
304     SYSTEMTIME st;
305     ULONGLONG time;
306
307     if (!res || !secs || *secs < 0) {
308         if (res) {
309             write_invalid_msvcrt_tm(res);
310         }
311
312         *MSVCRT__errno() = MSVCRT_EINVAL;
313         return MSVCRT_EINVAL;
314     }
315
316     time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
317
318     ft.dwHighDateTime = (UINT)(time >> 32);
319     ft.dwLowDateTime  = (UINT)time;
320
321     FileTimeToSystemTime(&ft, &st);
322
323     res->tm_sec  = st.wSecond;
324     res->tm_min  = st.wMinute;
325     res->tm_hour = st.wHour;
326     res->tm_mday = st.wDay;
327     res->tm_year = st.wYear - 1900;
328     res->tm_mon  = st.wMonth - 1;
329     res->tm_wday = st.wDayOfWeek;
330     for (i = res->tm_yday = 0; i < st.wMonth - 1; i++) {
331         res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
332     }
333
334     res->tm_yday += st.wDay - 1;
335     res->tm_isdst = 0;
336
337     return 0;
338 }
339
340 /*********************************************************************
341  *      _gmtime64 (MSVCRT.@)
342  */
343 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t *secs)
344 {
345     thread_data_t * const data = msvcrt_get_thread_data();
346
347     if(MSVCRT__gmtime64_s(&data->time_buffer, secs))
348         return NULL;
349     return &data->time_buffer;
350 }
351
352 /*********************************************************************
353  *      _gmtime32_s (MSVCRT.@)
354  */
355 int CDECL MSVCRT__gmtime32_s(struct MSVCRT_tm *res, const MSVCRT___time32_t *secs)
356 {
357     MSVCRT___time64_t secs64;
358
359     if(secs) {
360         secs64 = *secs;
361         return MSVCRT__gmtime64_s(res, &secs64);
362     }
363     return MSVCRT__gmtime64_s(res, NULL);
364 }
365
366 /*********************************************************************
367  *      _gmtime32 (MSVCRT.@)
368  */
369 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
370 {
371     MSVCRT___time64_t secs64;
372
373     if(!secs)
374         return NULL;
375
376     secs64 = *secs;
377     return MSVCRT__gmtime64( &secs64 );
378 }
379
380 /*********************************************************************
381  *      gmtime (MSVCRT.@)
382  */
383 #ifdef _WIN64
384 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
385 {
386     return MSVCRT__gmtime64( secs );
387 }
388 #else
389 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
390 {
391     return MSVCRT__gmtime32( secs );
392 }
393 #endif
394
395 /**********************************************************************
396  *              _strdate (MSVCRT.@)
397  */
398 char* CDECL _strdate(char* date)
399 {
400   static const char format[] = "MM'/'dd'/'yy";
401
402   GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
403
404   return date;
405 }
406
407 /**********************************************************************
408  *              _strdate_s (MSVCRT.@)
409  */
410 int CDECL _strdate_s(char* date, MSVCRT_size_t size)
411 {
412     if(date && size)
413         date[0] = '\0';
414
415     if(!date) {
416         *MSVCRT__errno() = MSVCRT_EINVAL;
417         return MSVCRT_EINVAL;
418     }
419
420     if(size < 9) {
421         *MSVCRT__errno() = MSVCRT_ERANGE;
422         return MSVCRT_ERANGE;
423     }
424
425     _strdate(date);
426     return 0;
427 }
428
429 /**********************************************************************
430  *              _wstrdate (MSVCRT.@)
431  */
432 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
433 {
434   static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
435
436   GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
437
438   return date;
439 }
440
441 /**********************************************************************
442  *              _wstrdate_s (MSVCRT.@)
443  */
444 int CDECL _wstrdate_s(MSVCRT_wchar_t* date, MSVCRT_size_t size)
445 {
446     if(date && size)
447         date[0] = '\0';
448
449     if(!date) {
450         *MSVCRT__errno() = MSVCRT_EINVAL;
451         return MSVCRT_EINVAL;
452     }
453
454     if(size < 9) {
455         *MSVCRT__errno() = MSVCRT_ERANGE;
456         return MSVCRT_ERANGE;
457     }
458
459     _wstrdate(date);
460     return 0;
461 }
462
463 /*********************************************************************
464  *              _strtime (MSVCRT.@)
465  */
466 char* CDECL _strtime(char* time)
467 {
468   static const char format[] = "HH':'mm':'ss";
469
470   GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9); 
471
472   return time;
473 }
474
475 /*********************************************************************
476  *              _strtime_s (MSVCRT.@)
477  */
478 int CDECL _strtime_s(char* time, MSVCRT_size_t size)
479 {
480     if(time && size)
481         time[0] = '\0';
482
483     if(!time) {
484         *MSVCRT__errno() = MSVCRT_EINVAL;
485         return MSVCRT_EINVAL;
486     }
487
488     if(size < 9) {
489         *MSVCRT__errno() = MSVCRT_ERANGE;
490         return MSVCRT_ERANGE;
491     }
492
493     _strtime(time);
494     return 0;
495 }
496
497 /*********************************************************************
498  *              _wstrtime (MSVCRT.@)
499  */
500 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
501 {
502   static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
503
504   GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
505
506   return time;
507 }
508
509 /*********************************************************************
510  *              _wstrtime_s (MSVCRT.@)
511  */
512 int CDECL _wstrtime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size)
513 {
514     if(time && size)
515         time[0] = '\0';
516
517     if(!time) {
518         *MSVCRT__errno() = MSVCRT_EINVAL;
519         return MSVCRT_EINVAL;
520     }
521
522     if(size < 9) {
523         *MSVCRT__errno() = MSVCRT_ERANGE;
524         return MSVCRT_ERANGE;
525     }
526
527     _wstrtime(time);
528     return 0;
529 }
530
531 /*********************************************************************
532  *              clock (MSVCRT.@)
533  */
534 MSVCRT_clock_t CDECL MSVCRT_clock(void)
535 {
536   FILETIME ftc, fte, ftk, ftu;
537   ULONGLONG utime, ktime;
538  
539   MSVCRT_clock_t clock;
540
541   GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
542
543   ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
544   utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
545
546   clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
547
548   return clock;
549 }
550
551 /*********************************************************************
552  *              _difftime64 (MSVCRT.@)
553  */
554 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
555 {
556   return (double)(time1 - time2);
557 }
558
559 /*********************************************************************
560  *              _difftime32 (MSVCRT.@)
561  */
562 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
563 {
564   return (double)(time1 - time2);
565 }
566
567 /*********************************************************************
568  *              difftime (MSVCRT.@)
569  */
570 #ifdef _WIN64
571 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
572 {
573     return MSVCRT__difftime64( time1, time2 );
574 }
575 #else
576 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
577 {
578     return MSVCRT__difftime32( time1, time2 );
579 }
580 #endif
581
582 /*********************************************************************
583  *              _ftime64 (MSVCRT.@)
584  */
585 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
586 {
587   TIME_ZONE_INFORMATION tzinfo;
588   FILETIME ft;
589   ULONGLONG time;
590
591   DWORD tzid = GetTimeZoneInformation(&tzinfo);
592   GetSystemTimeAsFileTime(&ft);
593
594   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
595
596   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
597   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
598   buf->timezone = tzinfo.Bias +
599       ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
600       ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
601   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
602 }
603
604 /*********************************************************************
605  *              _ftime64_s (MSVCRT.@)
606  */
607 int CDECL MSVCRT__ftime64_s(struct MSVCRT___timeb64 *buf)
608 {
609     if( !MSVCRT_CHECK_PMT( buf != NULL ) )
610     {
611         *MSVCRT__errno() = MSVCRT_EINVAL;
612         return MSVCRT_EINVAL;
613     }
614     MSVCRT__ftime64(buf);
615     return 0;
616 }
617
618 /*********************************************************************
619  *              _ftime32 (MSVCRT.@)
620  */
621 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
622 {
623     struct MSVCRT___timeb64 buf64;
624
625     MSVCRT__ftime64( &buf64 );
626     buf->time     = buf64.time;
627     buf->millitm  = buf64.millitm;
628     buf->timezone = buf64.timezone;
629     buf->dstflag  = buf64.dstflag;
630 }
631
632 /*********************************************************************
633  *              _ftime32_s (MSVCRT.@)
634  */
635 int CDECL MSVCRT__ftime32_s(struct MSVCRT___timeb32 *buf)
636 {
637     if( !MSVCRT_CHECK_PMT( buf != NULL ) )
638     {
639         *MSVCRT__errno() = MSVCRT_EINVAL;
640         return MSVCRT_EINVAL;
641     }
642     MSVCRT__ftime32(buf);
643     return 0;
644 }
645
646 /*********************************************************************
647  *              _ftime (MSVCRT.@)
648  */
649 #ifdef _WIN64
650 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
651 {
652     return MSVCRT__ftime64( buf );
653 }
654 #else
655 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
656 {
657     return MSVCRT__ftime32( buf );
658 }
659 #endif
660
661 /*********************************************************************
662  *              _time64 (MSVCRT.@)
663  */
664 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
665 {
666     MSVCRT___time64_t curtime;
667     struct MSVCRT___timeb64 tb;
668
669     MSVCRT__ftime64(&tb);
670
671     curtime = tb.time;
672     return buf ? *buf = curtime : curtime;
673 }
674
675 /*********************************************************************
676  *              _time32 (MSVCRT.@)
677  */
678 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
679 {
680     MSVCRT___time32_t curtime;
681     struct MSVCRT___timeb64 tb;
682
683     MSVCRT__ftime64(&tb);
684
685     curtime = tb.time;
686     return buf ? *buf = curtime : curtime;
687 }
688
689 /*********************************************************************
690  *              time (MSVCRT.@)
691  */
692 #ifdef _WIN64
693 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
694 {
695     return MSVCRT__time64( buf );
696 }
697 #else
698 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
699 {
700     return MSVCRT__time32( buf );
701 }
702 #endif
703
704 /*********************************************************************
705  *              _daylight (MSVCRT.@)
706  */
707 int MSVCRT___daylight = 0;
708
709 /*********************************************************************
710  *              __p_daylight (MSVCRT.@)
711  */
712 int * CDECL MSVCRT___p__daylight(void)
713 {
714         return &MSVCRT___daylight;
715 }
716
717 /*********************************************************************
718  *              _dstbias (MSVCRT.@)
719  */
720 int MSVCRT__dstbias = 0;
721
722 /*********************************************************************
723  *              __p_dstbias (MSVCRT.@)
724  */
725 int * CDECL __p__dstbias(void)
726 {
727     return &MSVCRT__dstbias;
728 }
729
730 /*********************************************************************
731  *              _timezone (MSVCRT.@)
732  */
733 MSVCRT_long MSVCRT___timezone = 0;
734
735 /*********************************************************************
736  *              __p_timezone (MSVCRT.@)
737  */
738 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
739 {
740         return &MSVCRT___timezone;
741 }
742
743 /*********************************************************************
744  *              _tzname (MSVCRT.@)
745  * NOTES
746  *  Some apps (notably Mozilla) insist on writing to these, so the buffer
747  *  must be large enough.  The size is picked based on observation of
748  *  Windows XP.
749  */
750 static char tzname_std[64] = "PST";
751 static char tzname_dst[64] = "PDT";
752 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
753
754 /*********************************************************************
755  *              _get_tzname (MSVCRT.@)
756  */
757 int CDECL MSVCRT__get_tzname(MSVCRT_size_t *ret, char *buf, MSVCRT_size_t bufsize, int index)
758 {
759     char *timezone;
760
761     switch(index)
762     {
763     case 0:
764         timezone = tzname_std;
765         break;
766     case 1:
767         timezone = tzname_dst;
768         break;
769     default:
770         *MSVCRT__errno() = MSVCRT_EINVAL;
771         return MSVCRT_EINVAL;
772     }
773
774     if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
775     {
776         *MSVCRT__errno() = MSVCRT_EINVAL;
777         return MSVCRT_EINVAL;
778     }
779
780     *ret = strlen(timezone)+1;
781     if(!buf && !bufsize)
782         return 0;
783
784     strcpy(buf, timezone);
785     return 0;
786 }
787
788 /*********************************************************************
789  *              __p_tzname (MSVCRT.@)
790  */
791 char ** CDECL __p__tzname(void)
792 {
793         return MSVCRT__tzname;
794 }
795
796 /*********************************************************************
797  *              _tzset (MSVCRT.@)
798  */
799 void CDECL MSVCRT__tzset(void)
800 {
801     tzset();
802 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
803     MSVCRT___daylight = daylight;
804     MSVCRT___timezone = timezone;
805 #else
806     {
807         static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
808         time_t t;
809         struct tm *tmp;
810         int zone_january, zone_july;
811
812         _mlock(_TIME_LOCK);
813         t = (time(NULL) / seconds_in_year) * seconds_in_year;
814         tmp = localtime(&t);
815         zone_january = -tmp->tm_gmtoff;
816         t += seconds_in_year / 2;
817         tmp = localtime(&t);
818         zone_july = -tmp->tm_gmtoff;
819         _munlock(_TIME_LOCK);
820
821         MSVCRT___daylight = (zone_january != zone_july);
822         MSVCRT___timezone = max(zone_january, zone_july);
823     }
824 #endif
825     lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
826     tzname_std[sizeof(tzname_std) - 1] = '\0';
827     lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
828     tzname_dst[sizeof(tzname_dst) - 1] = '\0';
829 }
830
831 /*********************************************************************
832  *              strftime (MSVCRT.@)
833  */
834 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
835                                      const struct MSVCRT_tm *mstm )
836 {
837     struct tm tm;
838
839     msvcrt_tm_to_unix( &tm, mstm );
840     return strftime( str, max, format, &tm );
841 }
842
843 /*********************************************************************
844  *              wcsftime (MSVCRT.@)
845  */
846 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
847                                      const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
848 {
849     char *s, *fmt;
850     MSVCRT_size_t len;
851
852     TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
853
854     len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
855     if (!(fmt = MSVCRT_malloc( len ))) return 0;
856     WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
857
858     if ((s = MSVCRT_malloc( max*4 )))
859     {
860         struct tm tm;
861         msvcrt_tm_to_unix( &tm, mstm );
862         if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
863         len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
864         if (len) len--;
865         MSVCRT_free( s );
866     }
867     else len = 0;
868
869     MSVCRT_free( fmt );
870     return len;
871 }
872
873 /*********************************************************************
874  *              asctime (MSVCRT.@)
875  */
876 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
877 {
878     char bufferA[30];
879     WCHAR bufferW[30];
880
881     thread_data_t *data = msvcrt_get_thread_data();
882     struct tm tm;
883
884     msvcrt_tm_to_unix( &tm, mstm );
885
886     if (!data->asctime_buffer)
887         data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
888
889 #ifdef HAVE_ASCTIME_R
890     asctime_r( &tm, bufferA );
891 #else
892     strcpy( bufferA, asctime(&tm) );
893 #endif
894     MultiByteToWideChar( CP_UNIXCP, 0, bufferA, -1, bufferW, 30 );
895     WideCharToMultiByte( CP_ACP, 0, bufferW, -1, data->asctime_buffer, 30, NULL, NULL );
896     return data->asctime_buffer;
897 }
898
899 /*********************************************************************
900  *              _wasctime (MSVCRT.@)
901  */
902 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
903 {
904     thread_data_t *data = msvcrt_get_thread_data();
905     struct tm tm;
906     char buffer[30];
907
908     msvcrt_tm_to_unix( &tm, mstm );
909
910     if (!data->wasctime_buffer)
911         data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
912 #ifdef HAVE_ASCTIME_R
913     asctime_r( &tm, buffer );
914 #else
915     strcpy( buffer, asctime(&tm) );
916 #endif
917     MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
918     return data->wasctime_buffer;
919 }
920
921 /*********************************************************************
922  *              _ctime64 (MSVCRT.@)
923  */
924 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
925 {
926     struct MSVCRT_tm *t;
927     t = MSVCRT__localtime64( time );
928     if (!t) return NULL;
929     return MSVCRT_asctime( t );
930 }
931
932 /*********************************************************************
933  *              _ctime64_s (MSVCRT.@)
934  */
935 int CDECL MSVCRT__ctime64_s(char *res, MSVCRT_size_t len, const MSVCRT___time64_t *time)
936 {
937     struct MSVCRT_tm *t;
938     if( !MSVCRT_CHECK_PMT( res != NULL ) || !MSVCRT_CHECK_PMT( len >= 26 ) )
939     {
940         *MSVCRT__errno() = MSVCRT_EINVAL;
941         return MSVCRT_EINVAL;
942     }
943     res[0] = '\0';
944     if( !MSVCRT_CHECK_PMT( time != NULL ) || !MSVCRT_CHECK_PMT( *time > 0 ) )
945     {
946         *MSVCRT__errno() = MSVCRT_EINVAL;
947         return MSVCRT_EINVAL;
948     }
949     t = MSVCRT__localtime64( time );
950     strcpy( res, MSVCRT_asctime( t ) );
951     return 0;
952 }
953
954 /*********************************************************************
955  *              _ctime32 (MSVCRT.@)
956  */
957 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
958 {
959     struct MSVCRT_tm *t;
960     t = MSVCRT__localtime32( time );
961     if (!t) return NULL;
962     return MSVCRT_asctime( t );
963 }
964
965 /*********************************************************************
966  *              _ctime32_s (MSVCRT.@)
967  */
968 int CDECL MSVCRT__ctime32_s(char *res, MSVCRT_size_t len, const MSVCRT___time32_t *time)
969 {
970     struct MSVCRT_tm *t;
971     if( !MSVCRT_CHECK_PMT( res != NULL ) || !MSVCRT_CHECK_PMT( len >= 26 ) )
972     {
973         *MSVCRT__errno() = MSVCRT_EINVAL;
974         return MSVCRT_EINVAL;
975     }
976     res[0] = '\0';
977     if( !MSVCRT_CHECK_PMT( time != NULL ) || !MSVCRT_CHECK_PMT( *time > 0 ) )
978     {
979         *MSVCRT__errno() = MSVCRT_EINVAL;
980         return MSVCRT_EINVAL;
981     }
982     t = MSVCRT__localtime32( time );
983     strcpy( res, MSVCRT_asctime( t ) );
984     return 0;
985 }
986
987 /*********************************************************************
988  *              ctime (MSVCRT.@)
989  */
990 #ifdef _WIN64
991 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
992 {
993     return MSVCRT__ctime64( time );
994 }
995 #else
996 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
997 {
998     return MSVCRT__ctime32( time );
999 }
1000 #endif
1001
1002 /*********************************************************************
1003  *              _wctime64 (MSVCRT.@)
1004  */
1005 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
1006 {
1007     return MSVCRT__wasctime( MSVCRT__localtime64(time) );
1008 }
1009
1010 /*********************************************************************
1011  *              _wctime32 (MSVCRT.@)
1012  */
1013 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
1014 {
1015     return MSVCRT__wasctime( MSVCRT__localtime32(time) );
1016 }
1017
1018 /*********************************************************************
1019  *              _wctime (MSVCRT.@)
1020  */
1021 #ifdef _WIN64
1022 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
1023 {
1024     return MSVCRT__wctime64( time );
1025 }
1026 #else
1027 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
1028 {
1029     return MSVCRT__wctime32( time );
1030 }
1031 #endif