Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / msvcrt / time.c
blob2c08185548a88e045f1f536857241456b35172a0
1 /*
2 * msvcrt.dll date/time functions
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
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.
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.
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
25 #include "config.h"
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>
34 #include "msvcrt.h"
35 #include "mtdll.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
43 static const int MonthLengths[2][12] =
45 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
46 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
49 static inline int IsLeapYear(int Year)
51 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
54 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
56 memset( dest, 0, sizeof(*dest) );
57 dest->tm_sec = src->tm_sec;
58 dest->tm_min = src->tm_min;
59 dest->tm_hour = src->tm_hour;
60 dest->tm_mday = src->tm_mday;
61 dest->tm_mon = src->tm_mon;
62 dest->tm_year = src->tm_year;
63 dest->tm_wday = src->tm_wday;
64 dest->tm_yday = src->tm_yday;
65 dest->tm_isdst = src->tm_isdst;
68 static inline void unix_tm_to_msvcrt( struct MSVCRT_tm *dest, const struct tm *src )
70 memset( dest, 0, sizeof(*dest) );
71 dest->tm_sec = src->tm_sec;
72 dest->tm_min = src->tm_min;
73 dest->tm_hour = src->tm_hour;
74 dest->tm_mday = src->tm_mday;
75 dest->tm_mon = src->tm_mon;
76 dest->tm_year = src->tm_year;
77 dest->tm_wday = src->tm_wday;
78 dest->tm_yday = src->tm_yday;
79 dest->tm_isdst = src->tm_isdst;
82 static inline void write_invalid_msvcrt_tm( struct MSVCRT_tm *tm )
84 tm->tm_sec = -1;
85 tm->tm_min = -1;
86 tm->tm_hour = -1;
87 tm->tm_mday = -1;
88 tm->tm_mon = -1;
89 tm->tm_year = -1;
90 tm->tm_wday = -1;
91 tm->tm_yday = -1;
92 tm->tm_isdst = -1;
95 #define SECSPERDAY 86400
96 /* 1601 to 1970 is 369 years plus 89 leap days */
97 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
98 #define TICKSPERSEC 10000000
99 #define TICKSPERMSEC 10000
100 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
102 /**********************************************************************
103 * _mktime64 (MSVCRT.@)
105 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
107 time_t secs;
108 struct tm tm;
110 msvcrt_tm_to_unix( &tm, mstm );
111 secs = mktime( &tm );
112 unix_tm_to_msvcrt( mstm, &tm );
114 return secs < 0 ? -1 : secs;
117 /**********************************************************************
118 * _mktime32 (MSVCRT.@)
120 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
122 return MSVCRT__mktime64( mstm );
125 /**********************************************************************
126 * mktime (MSVCRT.@)
128 #ifdef _WIN64
129 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
131 return MSVCRT__mktime64( mstm );
133 #else
134 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
136 return MSVCRT__mktime32( mstm );
138 #endif
140 /**********************************************************************
141 * _mkgmtime64 (MSVCRT.@)
143 * time->tm_isdst value is ignored
145 MSVCRT___time64_t CDECL MSVCRT__mkgmtime64(struct MSVCRT_tm *time)
147 SYSTEMTIME st;
148 FILETIME ft;
149 MSVCRT___time64_t ret;
150 int i;
152 st.wMilliseconds = 0;
153 st.wSecond = time->tm_sec;
154 st.wMinute = time->tm_min;
155 st.wHour = time->tm_hour;
156 st.wDay = time->tm_mday;
157 st.wMonth = time->tm_mon+1;
158 st.wYear = time->tm_year+1900;
160 if(!SystemTimeToFileTime(&st, &ft))
161 return -1;
163 FileTimeToSystemTime(&ft, &st);
164 time->tm_wday = st.wDayOfWeek;
166 for(i=time->tm_yday=0; i<st.wMonth-1; i++)
167 time->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
168 time->tm_yday += st.wDay-1;
170 ret = ((MSVCRT___time64_t)ft.dwHighDateTime<<32)+ft.dwLowDateTime;
171 ret = (ret-TICKS_1601_TO_1970)/TICKSPERSEC;
172 return ret;
175 /**********************************************************************
176 * _mkgmtime32 (MSVCRT.@)
178 MSVCRT___time32_t CDECL MSVCRT__mkgmtime32(struct MSVCRT_tm *time)
180 return MSVCRT__mkgmtime64(time);
183 /**********************************************************************
184 * _mkgmtime (MSVCRT.@)
186 #ifdef _WIN64
187 MSVCRT___time64_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
189 return MSVCRT__mkgmtime64(time);
191 #else
192 MSVCRT___time32_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
194 return MSVCRT__mkgmtime32(time);
196 #endif
198 /*********************************************************************
199 * _localtime64 (MSVCRT.@)
201 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
203 struct tm *tm;
204 thread_data_t *data;
205 time_t seconds = *secs;
207 if (seconds < 0) return NULL;
209 _mlock(_TIME_LOCK);
210 if (!(tm = localtime( &seconds))) {
211 _munlock(_TIME_LOCK);
212 return NULL;
215 data = msvcrt_get_thread_data();
216 if(!data->time_buffer)
217 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
219 unix_tm_to_msvcrt( data->time_buffer, tm );
220 _munlock(_TIME_LOCK);
222 return data->time_buffer;
225 /*********************************************************************
226 * _localtime64_s (MSVCRT.@)
228 int CDECL _localtime64_s(struct MSVCRT_tm *time, const MSVCRT___time64_t *secs)
230 struct tm *tm;
231 time_t seconds;
233 if (!time || !secs || *secs < 0 || *secs > _MAX__TIME64_T)
235 if (time)
236 write_invalid_msvcrt_tm(time);
238 *MSVCRT__errno() = MSVCRT_EINVAL;
239 return MSVCRT_EINVAL;
242 seconds = *secs;
244 _mlock(_TIME_LOCK);
245 if (!(tm = localtime(&seconds)))
247 _munlock(_TIME_LOCK);
248 *MSVCRT__errno() = MSVCRT_EINVAL;
249 return MSVCRT_EINVAL;
252 unix_tm_to_msvcrt(time, tm);
253 _munlock(_TIME_LOCK);
254 return 0;
257 /*********************************************************************
258 * _localtime32 (MSVCRT.@)
260 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
262 MSVCRT___time64_t secs64 = *secs;
263 return MSVCRT__localtime64( &secs64 );
266 /*********************************************************************
267 * _localtime32_s (MSVCRT.@)
269 int CDECL _localtime32_s(struct MSVCRT_tm *time, const MSVCRT___time32_t *secs)
271 MSVCRT___time64_t secs64;
273 if (!time || !secs || *secs < 0)
275 if (time)
276 write_invalid_msvcrt_tm(time);
278 *MSVCRT__errno() = MSVCRT_EINVAL;
279 return MSVCRT_EINVAL;
282 secs64 = *secs;
283 return _localtime64_s(time, &secs64);
286 /*********************************************************************
287 * localtime (MSVCRT.@)
289 #ifdef _WIN64
290 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
292 return MSVCRT__localtime64( secs );
294 #else
295 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
297 return MSVCRT__localtime32( secs );
299 #endif
301 /*********************************************************************
302 * _gmtime64 (MSVCRT.@)
304 int CDECL MSVCRT__gmtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
306 int i;
307 FILETIME ft;
308 SYSTEMTIME st;
309 ULONGLONG time;
311 if (!res || !secs || *secs < 0) {
312 if (res) {
313 write_invalid_msvcrt_tm(res);
316 *MSVCRT__errno() = MSVCRT_EINVAL;
317 return MSVCRT_EINVAL;
320 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
322 ft.dwHighDateTime = (UINT)(time >> 32);
323 ft.dwLowDateTime = (UINT)time;
325 FileTimeToSystemTime(&ft, &st);
327 res->tm_sec = st.wSecond;
328 res->tm_min = st.wMinute;
329 res->tm_hour = st.wHour;
330 res->tm_mday = st.wDay;
331 res->tm_year = st.wYear - 1900;
332 res->tm_mon = st.wMonth - 1;
333 res->tm_wday = st.wDayOfWeek;
334 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++) {
335 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
338 res->tm_yday += st.wDay - 1;
339 res->tm_isdst = 0;
341 return 0;
344 /*********************************************************************
345 * _gmtime64 (MSVCRT.@)
347 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t *secs)
349 thread_data_t * const data = msvcrt_get_thread_data();
351 if(!data->time_buffer)
352 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
354 if(MSVCRT__gmtime64_s(data->time_buffer, secs))
355 return NULL;
356 return data->time_buffer;
359 /*********************************************************************
360 * _gmtime32_s (MSVCRT.@)
362 int CDECL MSVCRT__gmtime32_s(struct MSVCRT_tm *res, const MSVCRT___time32_t *secs)
364 MSVCRT___time64_t secs64;
366 if(secs) {
367 secs64 = *secs;
368 return MSVCRT__gmtime64_s(res, &secs64);
370 return MSVCRT__gmtime64_s(res, NULL);
373 /*********************************************************************
374 * _gmtime32 (MSVCRT.@)
376 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
378 MSVCRT___time64_t secs64;
380 if(!secs)
381 return NULL;
383 secs64 = *secs;
384 return MSVCRT__gmtime64( &secs64 );
387 /*********************************************************************
388 * gmtime (MSVCRT.@)
390 #ifdef _WIN64
391 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
393 return MSVCRT__gmtime64( secs );
395 #else
396 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
398 return MSVCRT__gmtime32( secs );
400 #endif
402 /**********************************************************************
403 * _strdate (MSVCRT.@)
405 char* CDECL MSVCRT__strdate(char* date)
407 static const char format[] = "MM'/'dd'/'yy";
409 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
411 return date;
414 /**********************************************************************
415 * _strdate_s (MSVCRT.@)
417 int CDECL _strdate_s(char* date, MSVCRT_size_t size)
419 if(date && size)
420 date[0] = '\0';
422 if(!date) {
423 *MSVCRT__errno() = MSVCRT_EINVAL;
424 return MSVCRT_EINVAL;
427 if(size < 9) {
428 *MSVCRT__errno() = MSVCRT_ERANGE;
429 return MSVCRT_ERANGE;
432 MSVCRT__strdate(date);
433 return 0;
436 /**********************************************************************
437 * _wstrdate (MSVCRT.@)
439 MSVCRT_wchar_t* CDECL MSVCRT__wstrdate(MSVCRT_wchar_t* date)
441 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
443 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
445 return date;
448 /**********************************************************************
449 * _wstrdate_s (MSVCRT.@)
451 int CDECL _wstrdate_s(MSVCRT_wchar_t* date, MSVCRT_size_t size)
453 if(date && size)
454 date[0] = '\0';
456 if(!date) {
457 *MSVCRT__errno() = MSVCRT_EINVAL;
458 return MSVCRT_EINVAL;
461 if(size < 9) {
462 *MSVCRT__errno() = MSVCRT_ERANGE;
463 return MSVCRT_ERANGE;
466 MSVCRT__wstrdate(date);
467 return 0;
470 /*********************************************************************
471 * _strtime (MSVCRT.@)
473 char* CDECL MSVCRT__strtime(char* time)
475 static const char format[] = "HH':'mm':'ss";
477 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
479 return time;
482 /*********************************************************************
483 * _strtime_s (MSVCRT.@)
485 int CDECL _strtime_s(char* time, MSVCRT_size_t size)
487 if(time && size)
488 time[0] = '\0';
490 if(!time) {
491 *MSVCRT__errno() = MSVCRT_EINVAL;
492 return MSVCRT_EINVAL;
495 if(size < 9) {
496 *MSVCRT__errno() = MSVCRT_ERANGE;
497 return MSVCRT_ERANGE;
500 MSVCRT__strtime(time);
501 return 0;
504 /*********************************************************************
505 * _wstrtime (MSVCRT.@)
507 MSVCRT_wchar_t* CDECL MSVCRT__wstrtime(MSVCRT_wchar_t* time)
509 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
511 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
513 return time;
516 /*********************************************************************
517 * _wstrtime_s (MSVCRT.@)
519 int CDECL _wstrtime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size)
521 if(time && size)
522 time[0] = '\0';
524 if(!time) {
525 *MSVCRT__errno() = MSVCRT_EINVAL;
526 return MSVCRT_EINVAL;
529 if(size < 9) {
530 *MSVCRT__errno() = MSVCRT_ERANGE;
531 return MSVCRT_ERANGE;
534 MSVCRT__wstrtime(time);
535 return 0;
538 /*********************************************************************
539 * clock (MSVCRT.@)
541 MSVCRT_clock_t CDECL MSVCRT_clock(void)
543 FILETIME ftc, fte, ftk, ftu;
544 ULONGLONG utime, ktime;
546 MSVCRT_clock_t clock;
548 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
550 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
551 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
553 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
555 return clock;
558 /*********************************************************************
559 * _difftime64 (MSVCRT.@)
561 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
563 return (double)(time1 - time2);
566 /*********************************************************************
567 * _difftime32 (MSVCRT.@)
569 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
571 return (double)(time1 - time2);
574 /*********************************************************************
575 * difftime (MSVCRT.@)
577 #ifdef _WIN64
578 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
580 return MSVCRT__difftime64( time1, time2 );
582 #else
583 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
585 return MSVCRT__difftime32( time1, time2 );
587 #endif
589 /*********************************************************************
590 * _ftime64 (MSVCRT.@)
592 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
594 TIME_ZONE_INFORMATION tzinfo;
595 FILETIME ft;
596 ULONGLONG time;
598 DWORD tzid = GetTimeZoneInformation(&tzinfo);
599 GetSystemTimeAsFileTime(&ft);
601 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
603 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
604 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
605 buf->timezone = tzinfo.Bias +
606 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
607 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
608 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
611 /*********************************************************************
612 * _ftime64_s (MSVCRT.@)
614 int CDECL MSVCRT__ftime64_s(struct MSVCRT___timeb64 *buf)
616 if (!MSVCRT_CHECK_PMT( buf != NULL )) return MSVCRT_EINVAL;
617 MSVCRT__ftime64(buf);
618 return 0;
621 /*********************************************************************
622 * _ftime32 (MSVCRT.@)
624 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
626 struct MSVCRT___timeb64 buf64;
628 MSVCRT__ftime64( &buf64 );
629 buf->time = buf64.time;
630 buf->millitm = buf64.millitm;
631 buf->timezone = buf64.timezone;
632 buf->dstflag = buf64.dstflag;
635 /*********************************************************************
636 * _ftime32_s (MSVCRT.@)
638 int CDECL MSVCRT__ftime32_s(struct MSVCRT___timeb32 *buf)
640 if (!MSVCRT_CHECK_PMT( buf != NULL )) return MSVCRT_EINVAL;
641 MSVCRT__ftime32(buf);
642 return 0;
645 /*********************************************************************
646 * _ftime (MSVCRT.@)
648 #ifdef _WIN64
649 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
651 MSVCRT__ftime64( buf );
653 #else
654 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
656 MSVCRT__ftime32( buf );
658 #endif
660 /*********************************************************************
661 * _time64 (MSVCRT.@)
663 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
665 MSVCRT___time64_t curtime;
666 struct MSVCRT___timeb64 tb;
668 MSVCRT__ftime64(&tb);
670 curtime = tb.time;
671 return buf ? *buf = curtime : curtime;
674 /*********************************************************************
675 * _time32 (MSVCRT.@)
677 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
679 MSVCRT___time32_t curtime;
680 struct MSVCRT___timeb64 tb;
682 MSVCRT__ftime64(&tb);
684 curtime = tb.time;
685 return buf ? *buf = curtime : curtime;
688 /*********************************************************************
689 * time (MSVCRT.@)
691 #ifdef _WIN64
692 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
694 return MSVCRT__time64( buf );
696 #else
697 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
699 return MSVCRT__time32( buf );
701 #endif
703 /*********************************************************************
704 * _daylight (MSVCRT.@)
706 int MSVCRT___daylight = 0;
708 /*********************************************************************
709 * __p_daylight (MSVCRT.@)
711 int * CDECL MSVCRT___p__daylight(void)
713 return &MSVCRT___daylight;
716 /*********************************************************************
717 * _dstbias (MSVCRT.@)
719 int MSVCRT__dstbias = 0;
721 /*********************************************************************
722 * __p_dstbias (MSVCRT.@)
724 int * CDECL __p__dstbias(void)
726 return &MSVCRT__dstbias;
729 /*********************************************************************
730 * _timezone (MSVCRT.@)
732 MSVCRT_long MSVCRT___timezone = 0;
734 /*********************************************************************
735 * __p_timezone (MSVCRT.@)
737 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
739 return &MSVCRT___timezone;
742 /*********************************************************************
743 * _tzname (MSVCRT.@)
744 * NOTES
745 * Some apps (notably Mozilla) insist on writing to these, so the buffer
746 * must be large enough. The size is picked based on observation of
747 * Windows XP.
749 static char tzname_std[64] = "PST";
750 static char tzname_dst[64] = "PDT";
751 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
753 /*********************************************************************
754 * _get_tzname (MSVCRT.@)
756 int CDECL MSVCRT__get_tzname(MSVCRT_size_t *ret, char *buf, MSVCRT_size_t bufsize, int index)
758 char *timezone;
760 switch(index)
762 case 0:
763 timezone = tzname_std;
764 break;
765 case 1:
766 timezone = tzname_dst;
767 break;
768 default:
769 *MSVCRT__errno() = MSVCRT_EINVAL;
770 return MSVCRT_EINVAL;
773 if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
775 *MSVCRT__errno() = MSVCRT_EINVAL;
776 return MSVCRT_EINVAL;
779 *ret = strlen(timezone)+1;
780 if(!buf && !bufsize)
781 return 0;
783 strcpy(buf, timezone);
784 return 0;
787 /*********************************************************************
788 * __p_tzname (MSVCRT.@)
790 char ** CDECL __p__tzname(void)
792 return MSVCRT__tzname;
795 /*********************************************************************
796 * _tzset (MSVCRT.@)
798 void CDECL MSVCRT__tzset(void)
800 tzset();
801 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
802 MSVCRT___daylight = daylight;
803 MSVCRT___timezone = timezone;
804 #else
806 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
807 time_t t;
808 struct tm *tmp;
809 int zone_january, zone_july;
811 _mlock(_TIME_LOCK);
812 t = (time(NULL) / seconds_in_year) * seconds_in_year;
813 tmp = localtime(&t);
814 zone_january = -tmp->tm_gmtoff;
815 t += seconds_in_year / 2;
816 tmp = localtime(&t);
817 zone_july = -tmp->tm_gmtoff;
818 _munlock(_TIME_LOCK);
820 MSVCRT___daylight = (zone_january != zone_july);
821 MSVCRT___timezone = max(zone_january, zone_july);
823 #endif
824 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
825 tzname_std[sizeof(tzname_std) - 1] = '\0';
826 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
827 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
830 static inline BOOL strftime_date(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
831 BOOL alternate, const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
833 char *format;
834 SYSTEMTIME st;
835 MSVCRT_size_t ret;
837 st.wYear = mstm->tm_year + 1900;
838 st.wMonth = mstm->tm_mon + 1;
839 st.wDayOfWeek = mstm->tm_wday;
840 st.wDay = mstm->tm_mday;
841 st.wHour = mstm->tm_hour;
842 st.wMinute = mstm->tm_min;
843 st.wSecond = mstm->tm_sec;
844 st.wMilliseconds = 0;
846 format = alternate ? time_data->str.names.date : time_data->str.names.short_date;
847 ret = GetDateFormatA(time_data->lcid, 0, &st, format, NULL, 0);
848 if(ret && ret<max-*pos)
849 ret = GetDateFormatA(time_data->lcid, 0, &st, format, str+*pos, max-*pos);
850 if(!ret) {
851 *str = 0;
852 *MSVCRT__errno() = MSVCRT_EINVAL;
853 return FALSE;
854 }else if(ret > max-*pos) {
855 *str = 0;
856 *MSVCRT__errno() = MSVCRT_ERANGE;
857 return FALSE;
859 *pos += ret-1;
860 return TRUE;
863 static inline BOOL strftime_time(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
864 const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
866 SYSTEMTIME st;
867 MSVCRT_size_t ret;
869 st.wYear = mstm->tm_year + 1900;
870 st.wMonth = mstm->tm_mon + 1;
871 st.wDayOfWeek = mstm->tm_wday;
872 st.wDay = mstm->tm_mday;
873 st.wHour = mstm->tm_hour;
874 st.wMinute = mstm->tm_min;
875 st.wSecond = mstm->tm_sec;
876 st.wMilliseconds = 0;
878 ret = GetTimeFormatA(time_data->lcid, 0, &st, time_data->str.names.time, NULL, 0);
879 if(ret && ret<max-*pos)
880 ret = GetTimeFormatA(time_data->lcid, 0, &st, time_data->str.names.time,
881 str+*pos, max-*pos);
882 if(!ret) {
883 *str = 0;
884 *MSVCRT__errno() = MSVCRT_EINVAL;
885 return FALSE;
886 }else if(ret > max-*pos) {
887 *str = 0;
888 *MSVCRT__errno() = MSVCRT_ERANGE;
889 return FALSE;
891 *pos += ret-1;
892 return TRUE;
895 static inline BOOL strftime_str(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max, char *src)
897 MSVCRT_size_t len = strlen(src);
898 if(len > max-*pos) {
899 *str = 0;
900 *MSVCRT__errno() = MSVCRT_ERANGE;
901 return FALSE;
904 memcpy(str+*pos, src, len);
905 *pos += len;
906 return TRUE;
909 static inline BOOL strftime_int(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
910 int src, int prec, int l, int h)
912 MSVCRT_size_t len;
914 if(src<l || src>h) {
915 *str = 0;
916 *MSVCRT__errno() = MSVCRT_EINVAL;
917 return FALSE;
920 len = MSVCRT__snprintf(str+*pos, max-*pos, "%0*d", prec, src);
921 if(len == -1) {
922 *str = 0;
923 *MSVCRT__errno() = MSVCRT_ERANGE;
924 return FALSE;
927 *pos += len;
928 return TRUE;
931 /*********************************************************************
932 * _Strftime (MSVCRT.@)
934 MSVCRT_size_t CDECL _Strftime(char *str, MSVCRT_size_t max, const char *format,
935 const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
937 MSVCRT_size_t ret, tmp;
938 BOOL alternate;
940 TRACE("(%p %ld %s %p %p)\n", str, max, format, mstm, time_data);
942 if(!str || !format) {
943 if(str && max)
944 *str = 0;
945 *MSVCRT__errno() = MSVCRT_EINVAL;
946 return 0;
949 if(!time_data)
950 time_data = get_locinfo()->lc_time_curr;
952 for(ret=0; *format && ret<max; format++) {
953 if(*format != '%') {
954 str[ret++] = *format;
955 continue;
958 format++;
959 if(*format == '#') {
960 alternate = TRUE;
961 format++;
962 }else {
963 alternate = FALSE;
966 if(!mstm)
967 goto einval_error;
969 switch(*format) {
970 case 'c':
971 if(!strftime_date(str, &ret, max, alternate, mstm, time_data))
972 return 0;
973 if(ret < max)
974 str[ret++] = ' ';
975 if(!strftime_time(str, &ret, max, mstm, time_data))
976 return 0;
977 break;
978 case 'x':
979 if(!strftime_date(str, &ret, max, alternate, mstm, time_data))
980 return 0;
981 break;
982 case 'X':
983 if(!strftime_time(str, &ret, max, mstm, time_data))
984 return 0;
985 break;
986 case 'a':
987 if(mstm->tm_wday<0 || mstm->tm_wday>6)
988 goto einval_error;
989 if(!strftime_str(str, &ret, max, time_data->str.names.short_wday[mstm->tm_wday]))
990 return 0;
991 break;
992 case 'A':
993 if(mstm->tm_wday<0 || mstm->tm_wday>6)
994 goto einval_error;
995 if(!strftime_str(str, &ret, max, time_data->str.names.wday[mstm->tm_wday]))
996 return 0;
997 break;
998 case 'b':
999 if(mstm->tm_mon<0 || mstm->tm_mon>11)
1000 goto einval_error;
1001 if(!strftime_str(str, &ret, max, time_data->str.names.short_mon[mstm->tm_mon]))
1002 return 0;
1003 break;
1004 case 'B':
1005 if(mstm->tm_mon<0 || mstm->tm_mon>11)
1006 goto einval_error;
1007 if(!strftime_str(str, &ret, max, time_data->str.names.mon[mstm->tm_mon]))
1008 return 0;
1009 break;
1010 case 'd':
1011 if(!strftime_int(str, &ret, max, mstm->tm_mday, alternate ? 0 : 2, 0, 31))
1012 return 0;
1013 break;
1014 case 'H':
1015 if(!strftime_int(str, &ret, max, mstm->tm_hour, alternate ? 0 : 2, 0, 23))
1016 return 0;
1017 break;
1018 case 'I':
1019 tmp = mstm->tm_hour;
1020 if(tmp > 12)
1021 tmp -= 12;
1022 else if(!tmp)
1023 tmp = 12;
1024 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 1, 12))
1025 return 0;
1026 break;
1027 case 'j':
1028 if(!strftime_int(str, &ret, max, mstm->tm_yday+1, alternate ? 0 : 3, 1, 366))
1029 return 0;
1030 break;
1031 case 'm':
1032 if(!strftime_int(str, &ret, max, mstm->tm_mon+1, alternate ? 0 : 2, 1, 12))
1033 return 0;
1034 break;
1035 case 'M':
1036 if(!strftime_int(str, &ret, max, mstm->tm_min, alternate ? 0 : 2, 0, 59))
1037 return 0;
1038 break;
1039 case 'p':
1040 if(mstm->tm_hour<0 || mstm->tm_hour>23)
1041 goto einval_error;
1042 if(!strftime_str(str, &ret, max, mstm->tm_hour<12 ?
1043 time_data->str.names.am : time_data->str.names.pm))
1044 return 0;
1045 break;
1046 case 'S':
1047 if(!strftime_int(str, &ret, max, mstm->tm_sec, alternate ? 0 : 2, 0, 59))
1048 return 0;
1049 break;
1050 case 'w':
1051 if(!strftime_int(str, &ret, max, mstm->tm_wday, 0, 0, 6))
1052 return 0;
1053 break;
1054 case 'y':
1055 if(!strftime_int(str, &ret, max, mstm->tm_year%100, alternate ? 0 : 2, 0, 99))
1056 return 0;
1057 break;
1058 case 'Y':
1059 tmp = 1900+mstm->tm_year;
1060 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 4, 0, 9999))
1061 return 0;
1062 break;
1063 case 'z':
1064 case 'Z':
1065 MSVCRT__tzset();
1066 if(MSVCRT__get_tzname(&tmp, str+ret, max-ret, mstm->tm_isdst ? 1 : 0))
1067 return 0;
1068 ret += tmp;
1069 break;
1070 case 'U':
1071 case 'W':
1072 if(mstm->tm_wday<0 || mstm->tm_wday>6 || mstm->tm_yday<0 || mstm->tm_yday>365)
1073 goto einval_error;
1074 if(*format == 'U')
1075 tmp = mstm->tm_wday;
1076 else if(!mstm->tm_wday)
1077 tmp = 6;
1078 else
1079 tmp = mstm->tm_wday-1;
1081 tmp = mstm->tm_yday/7 + (tmp<=mstm->tm_yday%7);
1082 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 0, 53))
1083 return 0;
1084 break;
1085 case '%':
1086 str[ret++] = '%';
1087 break;
1088 default:
1089 WARN("unknown format %c\n", *format);
1090 goto einval_error;
1094 if(ret == max) {
1095 if(max)
1096 *str = 0;
1097 *MSVCRT__errno() = MSVCRT_ERANGE;
1098 return 0;
1101 str[ret] = 0;
1102 return ret;
1104 einval_error:
1105 *str = 0;
1106 *MSVCRT__errno() = MSVCRT_EINVAL;
1107 return 0;
1110 /*********************************************************************
1111 * strftime (MSVCRT.@)
1113 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
1114 const struct MSVCRT_tm *mstm )
1116 return _Strftime(str, max, format, mstm, NULL);
1119 /*********************************************************************
1120 * wcsftime (MSVCRT.@)
1122 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
1123 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
1125 char *s, *fmt;
1126 MSVCRT_size_t len;
1128 TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
1130 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
1131 if (!(fmt = MSVCRT_malloc( len ))) return 0;
1132 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
1134 if ((s = MSVCRT_malloc( max*4 )))
1136 if (!MSVCRT_strftime( s, max*4, fmt, mstm )) s[0] = 0;
1137 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
1138 if (len) len--;
1139 MSVCRT_free( s );
1141 else len = 0;
1143 MSVCRT_free( fmt );
1144 return len;
1147 static char* asctime_buf(char *buf, const struct MSVCRT_tm *mstm)
1149 static const char wday[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
1150 static const char month[12][4] = {"Jan", "Feb", "Mar", "Apr", "May",
1151 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
1153 if (mstm->tm_sec<0 || mstm->tm_sec>59
1154 || mstm->tm_min<0 || mstm->tm_min>59
1155 || mstm->tm_hour<0 || mstm->tm_hour>23
1156 || mstm->tm_mon<0 || mstm->tm_mon>11
1157 || mstm->tm_wday<0 || mstm->tm_wday>6
1158 || mstm->tm_year<0 || mstm->tm_mday<0
1159 || mstm->tm_mday>MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon]) {
1160 *MSVCRT__errno() = MSVCRT_EINVAL;
1161 return NULL;
1164 MSVCRT__snprintf(buf, 26, "%s %s %02d %02d:%02d:%02d %c%03d\n", wday[mstm->tm_wday],
1165 month[mstm->tm_mon], mstm->tm_mday, mstm->tm_hour, mstm->tm_min,
1166 mstm->tm_sec, '1'+(mstm->tm_year+900)/1000, (900+mstm->tm_year)%1000);
1167 return buf;
1170 /*********************************************************************
1171 * asctime (MSVCRT.@)
1173 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
1175 thread_data_t *data = msvcrt_get_thread_data();
1177 /* asctime returns date in format that always has exactly 26 characters */
1178 if (!data->asctime_buffer) {
1179 data->asctime_buffer = MSVCRT_malloc(26);
1180 if (!data->asctime_buffer) {
1181 *MSVCRT__errno() = MSVCRT_ENOMEM;
1182 return NULL;
1186 return asctime_buf(data->asctime_buffer, mstm);
1189 /*********************************************************************
1190 * asctime_s (MSVCRT.@)
1192 int CDECL MSVCRT_asctime_s(char* time, MSVCRT_size_t size, const struct MSVCRT_tm *mstm)
1194 if (!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1195 if (size) time[0] = 0;
1196 if (!MSVCRT_CHECK_PMT(size >= 26)) return MSVCRT_EINVAL;
1197 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return MSVCRT_EINVAL;
1198 if (!MSVCRT_CHECK_PMT(mstm->tm_sec >= 0 && mstm->tm_sec < 60)) return MSVCRT_EINVAL;
1199 if (!MSVCRT_CHECK_PMT(mstm->tm_min >= 0 && mstm->tm_min < 60)) return MSVCRT_EINVAL;
1200 if (!MSVCRT_CHECK_PMT(mstm->tm_hour >= 0 && mstm->tm_hour < 24)) return MSVCRT_EINVAL;
1201 if (!MSVCRT_CHECK_PMT(mstm->tm_mon >= 0 && mstm->tm_mon < 12)) return MSVCRT_EINVAL;
1202 if (!MSVCRT_CHECK_PMT(mstm->tm_wday >= 0 && mstm->tm_wday < 7)) return MSVCRT_EINVAL;
1203 if (!MSVCRT_CHECK_PMT(mstm->tm_year >= 0)) return MSVCRT_EINVAL;
1204 if (!MSVCRT_CHECK_PMT(mstm->tm_mday >= 0)) return MSVCRT_EINVAL;
1205 if (!MSVCRT_CHECK_PMT(mstm->tm_mday <= MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon])) return MSVCRT_EINVAL;
1207 asctime_buf(time, mstm);
1208 return 0;
1211 /*********************************************************************
1212 * _wasctime (MSVCRT.@)
1214 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
1216 thread_data_t *data = msvcrt_get_thread_data();
1217 char buffer[26];
1219 if(!data->wasctime_buffer) {
1220 data->wasctime_buffer = MSVCRT_malloc(26*sizeof(MSVCRT_wchar_t));
1221 if(!data->wasctime_buffer) {
1222 *MSVCRT__errno() = MSVCRT_ENOMEM;
1223 return NULL;
1227 if(!asctime_buf(buffer, mstm))
1228 return NULL;
1230 MultiByteToWideChar(CP_ACP, 0, buffer, -1, data->wasctime_buffer, 26);
1231 return data->wasctime_buffer;
1234 /*********************************************************************
1235 * _wasctime_s (MSVCRT.@)
1237 int CDECL MSVCRT__wasctime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size, const struct MSVCRT_tm *mstm)
1239 char buffer[26];
1240 int ret;
1242 if (!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1243 if (size) time[0] = 0;
1244 if (!MSVCRT_CHECK_PMT(size >= 26)) return MSVCRT_EINVAL;
1245 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return MSVCRT_EINVAL;
1247 ret = MSVCRT_asctime_s(buffer, sizeof(buffer), mstm);
1248 if(!ret)
1249 return ret;
1250 MultiByteToWideChar(CP_ACP, 0, buffer, -1, time, size);
1251 return 0;
1254 /*********************************************************************
1255 * _ctime64 (MSVCRT.@)
1257 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
1259 struct MSVCRT_tm *t;
1260 t = MSVCRT__localtime64( time );
1261 if (!t) return NULL;
1262 return MSVCRT_asctime( t );
1265 /*********************************************************************
1266 * _ctime64_s (MSVCRT.@)
1268 int CDECL MSVCRT__ctime64_s(char *res, MSVCRT_size_t len, const MSVCRT___time64_t *time)
1270 struct MSVCRT_tm *t;
1272 if (!MSVCRT_CHECK_PMT( res != NULL )) return MSVCRT_EINVAL;
1273 if (!MSVCRT_CHECK_PMT( len >= 26 )) return MSVCRT_EINVAL;
1274 res[0] = '\0';
1275 if (!MSVCRT_CHECK_PMT( time != NULL )) return MSVCRT_EINVAL;
1276 if (!MSVCRT_CHECK_PMT( *time > 0 )) return MSVCRT_EINVAL;
1278 t = MSVCRT__localtime64( time );
1279 strcpy( res, MSVCRT_asctime( t ) );
1280 return 0;
1283 /*********************************************************************
1284 * _ctime32 (MSVCRT.@)
1286 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
1288 struct MSVCRT_tm *t;
1289 t = MSVCRT__localtime32( time );
1290 if (!t) return NULL;
1291 return MSVCRT_asctime( t );
1294 /*********************************************************************
1295 * _ctime32_s (MSVCRT.@)
1297 int CDECL MSVCRT__ctime32_s(char *res, MSVCRT_size_t len, const MSVCRT___time32_t *time)
1299 struct MSVCRT_tm *t;
1301 if (!MSVCRT_CHECK_PMT( res != NULL )) return MSVCRT_EINVAL;
1302 if (!MSVCRT_CHECK_PMT( len >= 26 )) return MSVCRT_EINVAL;
1303 res[0] = '\0';
1304 if (!MSVCRT_CHECK_PMT( time != NULL )) return MSVCRT_EINVAL;
1305 if (!MSVCRT_CHECK_PMT( *time > 0 )) return MSVCRT_EINVAL;
1307 t = MSVCRT__localtime32( time );
1308 strcpy( res, MSVCRT_asctime( t ) );
1309 return 0;
1312 /*********************************************************************
1313 * ctime (MSVCRT.@)
1315 #ifdef _WIN64
1316 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
1318 return MSVCRT__ctime64( time );
1320 #else
1321 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
1323 return MSVCRT__ctime32( time );
1325 #endif
1327 /*********************************************************************
1328 * _wctime64 (MSVCRT.@)
1330 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
1332 return MSVCRT__wasctime( MSVCRT__localtime64(time) );
1335 /*********************************************************************
1336 * _wctime32 (MSVCRT.@)
1338 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
1340 return MSVCRT__wasctime( MSVCRT__localtime32(time) );
1343 /*********************************************************************
1344 * _wctime (MSVCRT.@)
1346 #ifdef _WIN64
1347 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
1349 return MSVCRT__wctime64( time );
1351 #else
1352 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
1354 return MSVCRT__wctime32( time );
1356 #endif