include: Fix __dmb declaration.
[wine.git] / dlls / msvcrt / time.c
blob327d8e086e026624ede08a6796eba28f64b44a2c
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 <locale.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <sys/timeb.h>
30 #include "msvcrt.h"
31 #include "mtdll.h"
32 #include "winbase.h"
33 #include "winnls.h"
34 #include "winternl.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 #undef _ctime32
40 #undef _difftime32
41 #undef _gmtime32
42 #undef _localtime32
43 #undef _mktime32
44 #undef _time32
45 #undef _wctime32
47 BOOL WINAPI GetDaylightFlag(void);
49 static LONGLONG init_time;
51 void msvcrt_init_clock(void)
53 LARGE_INTEGER systime;
55 NtQuerySystemTime(&systime);
56 init_time = systime.QuadPart;
59 static const int MonthLengths[2][12] =
61 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
62 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
65 #if _MSVCR_VER>=140
66 static const int MAX_SECONDS = 60;
67 #else
68 static const int MAX_SECONDS = 59;
69 #endif
71 static inline BOOL IsLeapYear(int Year)
73 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
76 static inline void write_invalid_msvcrt_tm( struct tm *tm )
78 tm->tm_sec = -1;
79 tm->tm_min = -1;
80 tm->tm_hour = -1;
81 tm->tm_mday = -1;
82 tm->tm_mon = -1;
83 tm->tm_year = -1;
84 tm->tm_wday = -1;
85 tm->tm_yday = -1;
86 tm->tm_isdst = -1;
89 /*********************************************************************
90 * _daylight (MSVCRT.@)
92 int MSVCRT___daylight = 1;
94 /*********************************************************************
95 * _timezone (MSVCRT.@)
97 __msvcrt_long MSVCRT___timezone = 28800;
99 /*********************************************************************
100 * _dstbias (MSVCRT.@)
102 __msvcrt_long MSVCRT__dstbias = -3600;
104 /*********************************************************************
105 * _tzname (MSVCRT.@)
106 * NOTES
107 * Some apps (notably Mozilla) insist on writing to these, so the buffer
108 * must be large enough.
110 static char tzname_std[64] = "PST";
111 static char tzname_dst[64] = "PDT";
112 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
114 static TIME_ZONE_INFORMATION tzi = {0};
115 /*********************************************************************
116 * _tzset (MSVCRT.@)
118 void CDECL _tzset(void)
120 char *tz = getenv("TZ");
121 BOOL error;
123 _lock(_TIME_LOCK);
124 if(tz && tz[0]) {
125 BOOL neg_zone = FALSE;
127 memset(&tzi, 0, sizeof(tzi));
129 /* Parse timezone information: tzn[+|-]hh[:mm[:ss]][dzn] */
130 lstrcpynA(MSVCRT__tzname[0], tz, 3);
131 tz += 3;
133 if(*tz == '-') {
134 neg_zone = TRUE;
135 tz++;
136 }else if(*tz == '+') {
137 tz++;
139 MSVCRT___timezone = strtol(tz, &tz, 10)*3600;
140 if(*tz == ':') {
141 MSVCRT___timezone += strtol(tz+1, &tz, 10)*60;
142 if(*tz == ':')
143 MSVCRT___timezone += strtol(tz+1, &tz, 10);
145 if(neg_zone)
146 MSVCRT___timezone = -MSVCRT___timezone;
148 MSVCRT___daylight = *tz;
149 lstrcpynA(MSVCRT__tzname[1], tz, 3);
150 }else if(GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID) {
151 MSVCRT___timezone = tzi.Bias*60;
152 if(tzi.StandardDate.wMonth)
153 MSVCRT___timezone += tzi.StandardBias*60;
155 if(tzi.DaylightDate.wMonth) {
156 MSVCRT___daylight = 1;
157 MSVCRT__dstbias = (tzi.DaylightBias-tzi.StandardBias)*60;
158 }else {
159 MSVCRT___daylight = 0;
160 MSVCRT__dstbias = 0;
163 if(!WideCharToMultiByte(CP_ACP, 0, tzi.StandardName, -1, MSVCRT__tzname[0],
164 sizeof(tzname_std), NULL, &error) || error)
165 *MSVCRT__tzname[0] = 0;
166 if(!WideCharToMultiByte(CP_ACP, 0, tzi.DaylightName, -1, MSVCRT__tzname[1],
167 sizeof(tzname_dst), NULL, &error) || error)
168 *MSVCRT__tzname[0] = 0;
170 _unlock(_TIME_LOCK);
173 static void _tzset_init(void)
175 static BOOL init = FALSE;
177 if(!init) {
178 _lock(_TIME_LOCK);
179 if(!init) {
180 _tzset();
181 init = TRUE;
183 _unlock(_TIME_LOCK);
187 static BOOL is_dst(const SYSTEMTIME *st)
189 TIME_ZONE_INFORMATION tmp;
190 SYSTEMTIME out;
192 if(!MSVCRT___daylight)
193 return FALSE;
195 if(tzi.DaylightDate.wMonth) {
196 tmp = tzi;
197 }else if(st->wYear >= 2007) {
198 memset(&tmp, 0, sizeof(tmp));
199 tmp.StandardDate.wMonth = 11;
200 tmp.StandardDate.wDay = 1;
201 tmp.StandardDate.wHour = 2;
202 tmp.DaylightDate.wMonth = 3;
203 tmp.DaylightDate.wDay = 2;
204 tmp.DaylightDate.wHour = 2;
205 }else {
206 memset(&tmp, 0, sizeof(tmp));
207 tmp.StandardDate.wMonth = 10;
208 tmp.StandardDate.wDay = 5;
209 tmp.StandardDate.wHour = 2;
210 tmp.DaylightDate.wMonth = 4;
211 tmp.DaylightDate.wDay = 1;
212 tmp.DaylightDate.wHour = 2;
215 tmp.Bias = 0;
216 tmp.StandardBias = 0;
217 tmp.DaylightBias = MSVCRT__dstbias/60;
218 if(!SystemTimeToTzSpecificLocalTime(&tmp, st, &out))
219 return FALSE;
221 return memcmp(st, &out, sizeof(SYSTEMTIME));
224 #define SECSPERDAY 86400
225 /* 1601 to 1970 is 369 years plus 89 leap days */
226 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
227 #define TICKSPERSEC 10000000
228 #define TICKSPERMSEC 10000
229 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
231 static __time64_t mktime_helper(struct tm *mstm, BOOL local)
233 SYSTEMTIME st;
234 FILETIME ft;
235 __time64_t ret = 0;
236 int i;
237 BOOL use_dst = FALSE;
239 ret = mstm->tm_year + mstm->tm_mon/12;
240 mstm->tm_mon %= 12;
241 if(mstm->tm_mon < 0) {
242 mstm->tm_mon += 12;
243 ret--;
246 if(ret<70 || ret>1100) {
247 *_errno() = EINVAL;
248 return -1;
251 memset(&st, 0, sizeof(SYSTEMTIME));
252 st.wDay = 1;
253 st.wMonth = mstm->tm_mon+1;
254 st.wYear = ret+1900;
256 if(!SystemTimeToFileTime(&st, &ft)) {
257 *_errno() = EINVAL;
258 return -1;
261 ret = ((__time64_t)ft.dwHighDateTime<<32)+ft.dwLowDateTime;
262 ret += (__time64_t)mstm->tm_sec*TICKSPERSEC;
263 ret += (__time64_t)mstm->tm_min*60*TICKSPERSEC;
264 ret += (__time64_t)mstm->tm_hour*60*60*TICKSPERSEC;
265 ret += (__time64_t)(mstm->tm_mday-1)*SECSPERDAY*TICKSPERSEC;
267 ft.dwLowDateTime = ret & 0xffffffff;
268 ft.dwHighDateTime = ret >> 32;
269 FileTimeToSystemTime(&ft, &st);
271 if(local) {
272 _tzset_init();
273 use_dst = is_dst(&st);
274 if((mstm->tm_isdst<=-1 && use_dst) || (mstm->tm_isdst>=1)) {
275 SYSTEMTIME tmp;
277 ret += (__time64_t)MSVCRT__dstbias*TICKSPERSEC;
279 ft.dwLowDateTime = ret & 0xffffffff;
280 ft.dwHighDateTime = ret >> 32;
281 FileTimeToSystemTime(&ft, &tmp);
283 if(!is_dst(&tmp)) {
284 st = tmp;
285 use_dst = FALSE;
286 }else {
287 use_dst = TRUE;
289 }else if(mstm->tm_isdst==0 && use_dst) {
290 ret -= (__time64_t)MSVCRT__dstbias*TICKSPERSEC;
291 ft.dwLowDateTime = ret & 0xffffffff;
292 ft.dwHighDateTime = ret >> 32;
293 FileTimeToSystemTime(&ft, &st);
294 ret += (__time64_t)MSVCRT__dstbias*TICKSPERSEC;
296 ret += (__time64_t)MSVCRT___timezone*TICKSPERSEC;
299 mstm->tm_sec = st.wSecond;
300 mstm->tm_min = st.wMinute;
301 mstm->tm_hour = st.wHour;
302 mstm->tm_mday = st.wDay;
303 mstm->tm_mon = st.wMonth-1;
304 mstm->tm_year = st.wYear-1900;
305 mstm->tm_wday = st.wDayOfWeek;
306 for(i=mstm->tm_yday=0; i<st.wMonth-1; i++)
307 mstm->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
308 mstm->tm_yday += st.wDay-1;
309 mstm->tm_isdst = use_dst ? 1 : 0;
311 if(ret < TICKS_1601_TO_1970) {
312 *_errno() = EINVAL;
313 return -1;
315 ret = (ret-TICKS_1601_TO_1970)/TICKSPERSEC;
316 return ret;
319 /**********************************************************************
320 * _mktime64 (MSVCRT.@)
322 __time64_t CDECL _mktime64(struct tm *mstm)
324 return mktime_helper(mstm, TRUE);
327 /**********************************************************************
328 * _mktime32 (MSVCRT.@)
330 __time32_t CDECL _mktime32(struct tm *mstm)
332 __time64_t ret = _mktime64( mstm );
333 return ret == (__time32_t)ret ? ret : -1;
336 /**********************************************************************
337 * _mkgmtime64 (MSVCRT.@)
339 * time->tm_isdst value is ignored
341 __time64_t CDECL _mkgmtime64(struct tm *time)
343 return mktime_helper(time, FALSE);
346 /**********************************************************************
347 * _mkgmtime32 (MSVCRT.@)
349 __time32_t CDECL _mkgmtime32(struct tm *time)
351 __time64_t ret = _mkgmtime64(time);
352 return ret == (__time32_t)ret ? ret : -1;
355 /*********************************************************************
356 * _localtime64_s (MSVCRT.@)
358 int CDECL _localtime64_s(struct tm *res, const __time64_t *secs)
360 int i;
361 FILETIME ft;
362 SYSTEMTIME st;
363 ULONGLONG time;
365 if (!res || !secs || *secs < 0 || *secs > _MAX__TIME64_T)
367 if (res)
368 write_invalid_msvcrt_tm(res);
370 *_errno() = EINVAL;
371 return EINVAL;
374 _tzset_init();
375 time = (*secs - MSVCRT___timezone) * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
377 ft.dwHighDateTime = (UINT)(time >> 32);
378 ft.dwLowDateTime = (UINT)time;
379 FileTimeToSystemTime(&ft, &st);
381 res->tm_isdst = is_dst(&st) ? 1 : 0;
382 if(res->tm_isdst) {
383 time -= MSVCRT__dstbias * (ULONGLONG)TICKSPERSEC;
384 ft.dwHighDateTime = (UINT)(time >> 32);
385 ft.dwLowDateTime = (UINT)time;
386 FileTimeToSystemTime(&ft, &st);
389 res->tm_sec = st.wSecond;
390 res->tm_min = st.wMinute;
391 res->tm_hour = st.wHour;
392 res->tm_mday = st.wDay;
393 res->tm_year = st.wYear - 1900;
394 res->tm_mon = st.wMonth - 1;
395 res->tm_wday = st.wDayOfWeek;
396 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++)
397 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
398 res->tm_yday += st.wDay - 1;
400 return 0;
403 /*********************************************************************
404 * _localtime64 (MSVCRT.@)
406 struct tm* CDECL _localtime64(const __time64_t* secs)
408 thread_data_t *data = msvcrt_get_thread_data();
410 if(!data->time_buffer)
411 data->time_buffer = malloc(sizeof(struct tm));
413 if(_localtime64_s(data->time_buffer, secs))
414 return NULL;
415 return data->time_buffer;
418 /*********************************************************************
419 * _localtime32 (MSVCRT.@)
421 struct tm* CDECL _localtime32(const __time32_t* secs)
423 __time64_t secs64;
425 if(!secs)
426 return NULL;
428 secs64 = *secs;
429 return _localtime64( &secs64 );
432 /*********************************************************************
433 * _localtime32_s (MSVCRT.@)
435 int CDECL _localtime32_s(struct tm *time, const __time32_t *secs)
437 __time64_t secs64;
439 if (!time || !secs || *secs < 0)
441 if (time)
442 write_invalid_msvcrt_tm(time);
444 *_errno() = EINVAL;
445 return EINVAL;
448 secs64 = *secs;
449 return _localtime64_s(time, &secs64);
452 /*********************************************************************
453 * _gmtime64 (MSVCRT.@)
455 int CDECL _gmtime64_s(struct tm *res, const __time64_t *secs)
457 int i;
458 FILETIME ft;
459 SYSTEMTIME st;
460 ULONGLONG time;
462 if (!res || !secs || *secs < 0 || *secs > _MAX__TIME64_T) {
463 if (res) {
464 write_invalid_msvcrt_tm(res);
467 *_errno() = EINVAL;
468 return EINVAL;
471 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
473 ft.dwHighDateTime = (UINT)(time >> 32);
474 ft.dwLowDateTime = (UINT)time;
476 FileTimeToSystemTime(&ft, &st);
478 res->tm_sec = st.wSecond;
479 res->tm_min = st.wMinute;
480 res->tm_hour = st.wHour;
481 res->tm_mday = st.wDay;
482 res->tm_year = st.wYear - 1900;
483 res->tm_mon = st.wMonth - 1;
484 res->tm_wday = st.wDayOfWeek;
485 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++) {
486 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
489 res->tm_yday += st.wDay - 1;
490 res->tm_isdst = 0;
492 return 0;
495 /*********************************************************************
496 * _gmtime64 (MSVCRT.@)
498 struct tm* CDECL _gmtime64(const __time64_t *secs)
500 thread_data_t * const data = msvcrt_get_thread_data();
502 if(!data->time_buffer)
503 data->time_buffer = malloc(sizeof(struct tm));
505 if(_gmtime64_s(data->time_buffer, secs))
506 return NULL;
507 return data->time_buffer;
510 /*********************************************************************
511 * _gmtime32_s (MSVCRT.@)
513 int CDECL _gmtime32_s(struct tm *res, const __time32_t *secs)
515 __time64_t secs64;
517 if(secs) {
518 secs64 = *secs;
519 return _gmtime64_s(res, &secs64);
521 return _gmtime64_s(res, NULL);
524 /*********************************************************************
525 * _gmtime32 (MSVCRT.@)
527 struct tm* CDECL _gmtime32(const __time32_t* secs)
529 __time64_t secs64;
531 if(!secs)
532 return NULL;
534 secs64 = *secs;
535 return _gmtime64( &secs64 );
538 /**********************************************************************
539 * _strdate (MSVCRT.@)
541 char* CDECL _strdate(char* date)
543 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, "MM'/'dd'/'yy", date, 9);
544 return date;
547 /**********************************************************************
548 * _strdate_s (MSVCRT.@)
550 int CDECL _strdate_s(char* date, size_t size)
552 if(date && size)
553 date[0] = '\0';
555 if(!date) {
556 *_errno() = EINVAL;
557 return EINVAL;
560 if(size < 9) {
561 *_errno() = ERANGE;
562 return ERANGE;
565 _strdate(date);
566 return 0;
569 /**********************************************************************
570 * _wstrdate (MSVCRT.@)
572 wchar_t* CDECL _wstrdate(wchar_t* date)
574 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, L"MM'/'dd'/'yy", date, 9);
575 return date;
578 /**********************************************************************
579 * _wstrdate_s (MSVCRT.@)
581 int CDECL _wstrdate_s(wchar_t* date, size_t size)
583 if(date && size)
584 date[0] = '\0';
586 if(!date) {
587 *_errno() = EINVAL;
588 return EINVAL;
591 if(size < 9) {
592 *_errno() = ERANGE;
593 return ERANGE;
596 _wstrdate(date);
597 return 0;
600 /*********************************************************************
601 * _strtime (MSVCRT.@)
603 char* CDECL _strtime(char* time)
605 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, "HH':'mm':'ss", time, 9);
606 return time;
609 /*********************************************************************
610 * _strtime_s (MSVCRT.@)
612 int CDECL _strtime_s(char* time, size_t size)
614 if(time && size)
615 time[0] = '\0';
617 if(!time) {
618 *_errno() = EINVAL;
619 return EINVAL;
622 if(size < 9) {
623 *_errno() = ERANGE;
624 return ERANGE;
627 _strtime(time);
628 return 0;
631 /*********************************************************************
632 * _wstrtime (MSVCRT.@)
634 wchar_t* CDECL _wstrtime(wchar_t* time)
636 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, L"HH':'mm':'ss", time, 9);
637 return time;
640 /*********************************************************************
641 * _wstrtime_s (MSVCRT.@)
643 int CDECL _wstrtime_s(wchar_t* time, size_t size)
645 if(time && size)
646 time[0] = '\0';
648 if(!time) {
649 *_errno() = EINVAL;
650 return EINVAL;
653 if(size < 9) {
654 *_errno() = ERANGE;
655 return ERANGE;
658 _wstrtime(time);
659 return 0;
662 /*********************************************************************
663 * clock (MSVCRT.@)
665 clock_t CDECL clock(void)
667 LARGE_INTEGER systime;
669 NtQuerySystemTime(&systime);
670 return (systime.QuadPart - init_time) / (TICKSPERSEC / CLOCKS_PER_SEC);
673 /*********************************************************************
674 * _difftime64 (MSVCRT.@)
676 double CDECL _difftime64(__time64_t time1, __time64_t time2)
678 return (double)(time1 - time2);
681 /*********************************************************************
682 * _difftime32 (MSVCRT.@)
684 double CDECL _difftime32(__time32_t time1, __time32_t time2)
686 return (double)(time1 - time2);
689 /*********************************************************************
690 * _ftime64 (MSVCRT.@)
692 void CDECL _ftime64(struct __timeb64 *buf)
694 FILETIME ft;
695 ULONGLONG time;
697 _tzset_init();
699 GetSystemTimeAsFileTime(&ft);
701 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
703 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
704 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
705 buf->timezone = MSVCRT___timezone / 60;
706 buf->dstflag = GetDaylightFlag();
709 /*********************************************************************
710 * _ftime64_s (MSVCRT.@)
712 int CDECL _ftime64_s(struct __timeb64 *buf)
714 if (!MSVCRT_CHECK_PMT( buf != NULL )) return EINVAL;
715 _ftime64(buf);
716 return 0;
719 /*********************************************************************
720 * _ftime32 (MSVCRT.@)
722 void CDECL _ftime32(struct __timeb32 *buf)
724 struct __timeb64 buf64;
726 _ftime64( &buf64 );
727 buf->time = buf64.time;
728 buf->millitm = buf64.millitm;
729 buf->timezone = buf64.timezone;
730 buf->dstflag = buf64.dstflag;
733 /*********************************************************************
734 * _ftime32_s (MSVCRT.@)
736 int CDECL _ftime32_s(struct __timeb32 *buf)
738 if (!MSVCRT_CHECK_PMT( buf != NULL )) return EINVAL;
739 _ftime32(buf);
740 return 0;
743 /*********************************************************************
744 * _time64 (MSVCRT.@)
746 __time64_t CDECL _time64(__time64_t *buf)
748 __time64_t curtime;
749 struct __timeb64 tb;
751 _ftime64(&tb);
753 curtime = tb.time;
754 return buf ? *buf = curtime : curtime;
757 /*********************************************************************
758 * _time32 (MSVCRT.@)
760 __time32_t CDECL _time32(__time32_t *buf)
762 __time32_t curtime;
763 struct __timeb64 tb;
765 _ftime64(&tb);
767 curtime = tb.time;
768 return buf ? *buf = curtime : curtime;
771 /*********************************************************************
772 * __p__daylight (MSVCRT.@)
774 int * CDECL __p__daylight(void)
776 return &MSVCRT___daylight;
779 /*********************************************************************
780 * __p__dstbias (MSVCRT.@)
782 __msvcrt_long * CDECL __p__dstbias(void)
784 return &MSVCRT__dstbias;
787 #if _MSVCR_VER >= 80
788 /*********************************************************************
789 * _get_dstbias (MSVCR80.@)
791 int CDECL _get_dstbias(int *seconds)
793 if (!MSVCRT_CHECK_PMT(seconds != NULL)) return EINVAL;
794 *seconds = MSVCRT__dstbias;
795 return 0;
797 #endif
799 /*********************************************************************
800 * __p__timezone (MSVCRT.@)
802 __msvcrt_long * CDECL __p__timezone(void)
804 return &MSVCRT___timezone;
807 /*********************************************************************
808 * _get_tzname (MSVCRT.@)
810 int CDECL _get_tzname(size_t *ret, char *buf, size_t bufsize, int index)
812 char *timezone;
814 switch(index)
816 case 0:
817 timezone = tzname_std;
818 break;
819 case 1:
820 timezone = tzname_dst;
821 break;
822 default:
823 *_errno() = EINVAL;
824 return EINVAL;
827 if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
829 *_errno() = EINVAL;
830 return EINVAL;
833 *ret = strlen(timezone)+1;
834 if(!buf && !bufsize)
835 return 0;
836 if(*ret > bufsize)
838 buf[0] = 0;
839 return ERANGE;
842 strcpy(buf, timezone);
843 return 0;
846 /*********************************************************************
847 * __p_tzname (MSVCRT.@)
849 char ** CDECL __p__tzname(void)
851 return MSVCRT__tzname;
854 #if _MSVCR_VER <= 90
855 #define STRFTIME_CHAR char
856 #define STRFTIME_TD(td, name) td->str.names.name
857 #else
858 #define STRFTIME_CHAR wchar_t
859 #define STRFTIME_TD(td, name) td->wstr.names.name
860 #endif
862 #define strftime_str(a,b,c,d) strftime_nstr(a,b,c,d,SIZE_MAX)
863 static inline BOOL strftime_nstr(STRFTIME_CHAR *str, size_t *pos,
864 size_t max, const STRFTIME_CHAR *src, size_t len)
866 while(*src && len)
868 if(*pos >= max) {
869 *str = 0;
870 *_errno() = ERANGE;
871 return FALSE;
874 str[*pos] = *src;
875 src++;
876 *pos += 1;
877 len--;
879 return TRUE;
882 static inline BOOL strftime_int(STRFTIME_CHAR *str, size_t *pos, size_t max,
883 int src, int prec, int l, int h)
885 size_t len;
887 if(!MSVCRT_CHECK_PMT(src>=l && src<=h)) {
888 *str = 0;
889 return FALSE;
892 #if _MSVCR_VER <= 90
893 len = _snprintf(str+*pos, max-*pos, "%0*d", prec, src);
894 #else
895 len = _snwprintf(str+*pos, max-*pos, L"%0*d", prec, src);
896 #endif
897 if(len == -1) {
898 *str = 0;
899 *_errno() = ERANGE;
900 return FALSE;
903 *pos += len;
904 return TRUE;
907 static inline BOOL strftime_format(STRFTIME_CHAR *str, size_t *pos, size_t max,
908 const struct tm *mstm, __lc_time_data *time_data, const STRFTIME_CHAR *format)
910 size_t count;
911 BOOL ret = TRUE;
913 while(*format && ret)
915 count = 1;
916 while(format[0] == format[count]) count++;
918 switch(*format) {
919 case '\'':
920 if(count % 2 == 0) break;
922 format += count;
923 count = 0;
924 while(format[count] && format[count] != '\'') count++;
926 ret = strftime_nstr(str, pos, max, format, count);
927 if(!ret) return FALSE;
928 if(format[count] == '\'') count++;
929 break;
930 case 'd':
931 if(count > 2)
933 if(!MSVCRT_CHECK_PMT(mstm->tm_wday>=0 && mstm->tm_wday<=6))
935 *str = 0;
936 return FALSE;
939 switch(count) {
940 case 1:
941 case 2:
942 ret = strftime_int(str, pos, max, mstm->tm_mday, count==1 ? 0 : 2, 1, 31);
943 break;
944 case 3:
945 ret = strftime_str(str, pos, max, STRFTIME_TD(time_data, short_wday)[mstm->tm_wday]);
946 break;
947 default:
948 ret = strftime_nstr(str, pos, max, format, count-4);
949 if(ret)
950 ret = strftime_str(str, pos, max, STRFTIME_TD(time_data, wday)[mstm->tm_wday]);
951 break;
953 break;
954 case 'M':
955 if(count > 2)
957 if(!MSVCRT_CHECK_PMT(mstm->tm_mon>=0 && mstm->tm_mon<=11))
959 *str = 0;
960 return FALSE;
963 switch(count) {
964 case 1:
965 case 2:
966 ret = strftime_int(str, pos, max, mstm->tm_mon+1, count==1 ? 0 : 2, 1, 12);
967 break;
968 case 3:
969 ret = strftime_str(str, pos, max, STRFTIME_TD(time_data, short_mon)[mstm->tm_mon]);
970 break;
971 default:
972 ret = strftime_nstr(str, pos, max, format, count-4);
973 if(ret)
974 ret = strftime_str(str, pos, max, STRFTIME_TD(time_data, mon)[mstm->tm_mon]);
975 break;
977 break;
978 case 'y':
979 if(count > 1)
981 #if _MSVCR_VER>=140
982 if(!MSVCRT_CHECK_PMT(mstm->tm_year >= -1900 && mstm->tm_year <= 8099))
983 #else
984 if(!MSVCRT_CHECK_PMT(mstm->tm_year >= 0))
985 #endif
987 *str = 0;
988 return FALSE;
992 switch(count) {
993 case 1:
994 ret = strftime_nstr(str, pos, max, format, 1);
995 break;
996 case 2:
997 case 3:
998 ret = strftime_nstr(str, pos, max, format, count-2);
999 if(ret)
1000 ret = strftime_int(str, pos, max, (mstm->tm_year+1900)%100, 2, 0, 99);
1001 break;
1002 default:
1003 ret = strftime_nstr(str, pos, max, format, count-4);
1004 if(ret)
1005 ret = strftime_int(str, pos, max, mstm->tm_year+1900, 4, 0, 9999);
1006 break;
1008 break;
1009 case 'h':
1010 if(!MSVCRT_CHECK_PMT(mstm->tm_hour>=0 && mstm->tm_hour<=23))
1012 *str = 0;
1013 return FALSE;
1015 if(count > 2)
1016 ret = strftime_nstr(str, pos, max, format, count-2);
1017 if(ret)
1018 ret = strftime_int(str, pos, max, (mstm->tm_hour + 11) % 12 + 1,
1019 count == 1 ? 0 : 2, 1, 12);
1020 break;
1021 case 'H':
1022 if(count > 2)
1023 ret = strftime_nstr(str, pos, max, format, count-2);
1024 if(ret)
1025 ret = strftime_int(str, pos, max, mstm->tm_hour, count == 1 ? 0 : 2, 0, 23);
1026 break;
1027 case 'm':
1028 if(count > 2)
1029 ret = strftime_nstr(str, pos, max, format, count-2);
1030 if(ret)
1031 ret = strftime_int(str, pos, max, mstm->tm_min, count == 1 ? 0 : 2, 0, 59);
1032 break;
1033 case 's':
1034 if(count > 2)
1035 ret = strftime_nstr(str, pos, max, format, count-2);
1036 if(ret)
1037 ret = strftime_int(str, pos, max, mstm->tm_sec, count == 1 ? 0 : 2, 0, MAX_SECONDS);
1038 break;
1039 case 'a':
1040 case 'A':
1041 case 't':
1042 if(!MSVCRT_CHECK_PMT(mstm->tm_hour>=0 && mstm->tm_hour<=23))
1044 *str = 0;
1045 return FALSE;
1047 ret = strftime_nstr(str, pos, max,
1048 mstm->tm_hour < 12 ? STRFTIME_TD(time_data, am) : STRFTIME_TD(time_data, pm),
1049 (*format == 't' && count == 1) ? 1 : SIZE_MAX);
1050 break;
1051 default:
1052 ret = strftime_nstr(str, pos, max, format, count);
1053 break;
1055 format += count;
1058 return ret;
1061 #if _MSVCR_VER>=140
1062 static inline BOOL strftime_tzdiff(STRFTIME_CHAR *str, size_t *pos, size_t max, BOOL is_dst)
1064 __msvcrt_long tz = MSVCRT___timezone + (is_dst ? MSVCRT__dstbias : 0);
1065 char sign;
1067 if(tz < 0) {
1068 sign = '+';
1069 tz = -tz;
1070 }else {
1071 sign = '-';
1074 if(*pos < max)
1075 str[(*pos)++] = sign;
1076 if(!strftime_int(str, pos, max, tz/60/60, 2, 0, 99))
1077 return FALSE;
1078 return strftime_int(str, pos, max, tz/60%60, 2, 0, 59);
1080 #endif
1082 static size_t strftime_impl(STRFTIME_CHAR *str, size_t max,
1083 const STRFTIME_CHAR *format, const struct tm *mstm,
1084 __lc_time_data *time_data, _locale_t loc)
1086 size_t ret, tmp;
1087 BOOL alternate;
1088 int year = mstm ? mstm->tm_year + 1900 : -1;
1090 if(!str || !format) {
1091 if(str && max)
1092 *str = 0;
1093 *_errno() = EINVAL;
1094 return 0;
1097 if(!time_data)
1098 time_data = loc ? loc->locinfo->lc_time_curr : get_locinfo()->lc_time_curr;
1100 for(ret=0; *format && ret<max; format++) {
1101 if(*format != '%') {
1102 if(_isleadbyte_l((unsigned char)*format, loc)) {
1103 str[ret++] = *(format++);
1104 if(ret == max) continue;
1105 if(!MSVCRT_CHECK_PMT(str[ret]))
1106 goto einval_error;
1108 str[ret++] = *format;
1109 continue;
1112 format++;
1113 if(*format == '#') {
1114 alternate = TRUE;
1115 format++;
1116 }else {
1117 alternate = FALSE;
1120 if(!MSVCRT_CHECK_PMT(mstm))
1121 goto einval_error;
1123 switch(*format) {
1124 case 'c':
1125 #if _MSVCR_VER>=140
1126 if(time_data == &cloc_time_data && !alternate)
1128 tmp = strftime_impl(str+ret, max-ret, L"%a %b %e %T %Y", mstm, time_data, loc);
1129 if(!tmp)
1130 return 0;
1131 ret += tmp;
1132 break;
1134 #endif
1135 if(!strftime_format(str, &ret, max, mstm, time_data,
1136 alternate ? STRFTIME_TD(time_data, date) : STRFTIME_TD(time_data, short_date)))
1137 return 0;
1138 if(ret < max)
1139 str[ret++] = ' ';
1140 if(!strftime_format(str, &ret, max, mstm, time_data, STRFTIME_TD(time_data, time)))
1141 return 0;
1142 break;
1143 case 'x':
1144 if(!strftime_format(str, &ret, max, mstm, time_data,
1145 alternate ? STRFTIME_TD(time_data, date) : STRFTIME_TD(time_data, short_date)))
1146 return 0;
1147 break;
1148 case 'X':
1149 if(!strftime_format(str, &ret, max, mstm, time_data, STRFTIME_TD(time_data, time)))
1150 return 0;
1151 break;
1152 case 'a':
1153 if(!MSVCRT_CHECK_PMT(mstm->tm_wday>=0 && mstm->tm_wday<=6))
1154 goto einval_error;
1155 if(!strftime_str(str, &ret, max, STRFTIME_TD(time_data, short_wday)[mstm->tm_wday]))
1156 return 0;
1157 break;
1158 case 'A':
1159 if(!MSVCRT_CHECK_PMT(mstm->tm_wday>=0 && mstm->tm_wday<=6))
1160 goto einval_error;
1161 if(!strftime_str(str, &ret, max, STRFTIME_TD(time_data, wday)[mstm->tm_wday]))
1162 return 0;
1163 break;
1164 case 'b':
1165 #if _MSVCR_VER>=140
1166 case 'h':
1167 #endif
1168 if(!MSVCRT_CHECK_PMT(mstm->tm_mon>=0 && mstm->tm_mon<=11))
1169 goto einval_error;
1170 if(!strftime_str(str, &ret, max, STRFTIME_TD(time_data, short_mon)[mstm->tm_mon]))
1171 return 0;
1172 break;
1173 case 'B':
1174 if(!MSVCRT_CHECK_PMT(mstm->tm_mon>=0 && mstm->tm_mon<=11))
1175 goto einval_error;
1176 if(!strftime_str(str, &ret, max, STRFTIME_TD(time_data, mon)[mstm->tm_mon]))
1177 return 0;
1178 break;
1179 #if _MSVCR_VER>=140
1180 case 'C':
1181 if(!MSVCRT_CHECK_PMT(year>=0 && year<=9999))
1182 goto einval_error;
1183 if(!strftime_int(str, &ret, max, year/100, alternate ? 0 : 2, 0, 99))
1184 return 0;
1185 break;
1186 #endif
1187 case 'd':
1188 if(!strftime_int(str, &ret, max, mstm->tm_mday, alternate ? 0 : 2, 1, 31))
1189 return 0;
1190 break;
1191 #if _MSVCR_VER>=140
1192 case 'D':
1193 if(!MSVCRT_CHECK_PMT(year>=0 && year<=9999))
1194 goto einval_error;
1195 if(!strftime_int(str, &ret, max, mstm->tm_mon+1, alternate ? 0 : 2, 1, 12))
1196 return 0;
1197 if(ret < max)
1198 str[ret++] = '/';
1199 if(!strftime_int(str, &ret, max, mstm->tm_mday, alternate ? 0 : 2, 1, 31))
1200 return 0;
1201 if(ret < max)
1202 str[ret++] = '/';
1203 if(!strftime_int(str, &ret, max, year%100, alternate ? 0 : 2, 0, 99))
1204 return 0;
1205 break;
1206 case 'e':
1207 if(!strftime_int(str, &ret, max, mstm->tm_mday, alternate ? 0 : 2, 1, 31))
1208 return 0;
1209 if(!alternate && str[ret-2] == '0')
1210 str[ret-2] = ' ';
1211 break;
1212 case 'F':
1213 if(!strftime_int(str, &ret, max, year, alternate ? 0 : 4, 0, 9999))
1214 return 0;
1215 if(ret < max)
1216 str[ret++] = '-';
1217 if(!strftime_int(str, &ret, max, mstm->tm_mon+1, alternate ? 0 : 2, 1, 12))
1218 return 0;
1219 if(ret < max)
1220 str[ret++] = '-';
1221 if(!strftime_int(str, &ret, max, mstm->tm_mday, alternate ? 0 : 2, 1, 31))
1222 return 0;
1223 break;
1224 case 'g':
1225 case 'G':
1226 if(!MSVCRT_CHECK_PMT(year>=0 && year<=9999))
1227 goto einval_error;
1228 /* fall through */
1229 case 'V':
1231 int iso_year = year;
1232 int iso_days = mstm->tm_yday - (mstm->tm_wday ? mstm->tm_wday : 7) + 4;
1233 if (iso_days < 0)
1234 iso_days += 365 + IsLeapYear(--iso_year);
1235 else if(iso_days >= 365 + IsLeapYear(iso_year))
1236 iso_days -= 365 + IsLeapYear(iso_year++);
1238 if(*format == 'G') {
1239 if(!strftime_int(str, &ret, max, iso_year, 4, 0, 9999))
1240 return 0;
1241 } else if(*format == 'g') {
1242 if(!strftime_int(str, &ret, max, iso_year%100, 2, 0, 99))
1243 return 0;
1244 } else {
1245 if(!strftime_int(str, &ret, max, iso_days/7 + 1, alternate ? 0 : 2, 0, 53))
1246 return 0;
1248 break;
1250 #endif
1251 case 'H':
1252 if(!strftime_int(str, &ret, max, mstm->tm_hour, alternate ? 0 : 2, 0, 23))
1253 return 0;
1254 break;
1255 case 'I':
1256 if(!MSVCRT_CHECK_PMT(mstm->tm_hour>=0 && mstm->tm_hour<=23))
1257 goto einval_error;
1258 if(!strftime_int(str, &ret, max, (mstm->tm_hour + 11) % 12 + 1,
1259 alternate ? 0 : 2, 1, 12))
1260 return 0;
1261 break;
1262 case 'j':
1263 if(!strftime_int(str, &ret, max, mstm->tm_yday+1, alternate ? 0 : 3, 1, 366))
1264 return 0;
1265 break;
1266 case 'm':
1267 if(!strftime_int(str, &ret, max, mstm->tm_mon+1, alternate ? 0 : 2, 1, 12))
1268 return 0;
1269 break;
1270 case 'M':
1271 if(!strftime_int(str, &ret, max, mstm->tm_min, alternate ? 0 : 2, 0, 59))
1272 return 0;
1273 break;
1274 #if _MSVCR_VER>=140
1275 case 'n':
1276 str[ret++] = '\n';
1277 break;
1278 #endif
1279 case 'p':
1280 if(!MSVCRT_CHECK_PMT(mstm->tm_hour>=0 && mstm->tm_hour<=23))
1281 goto einval_error;
1282 if(!strftime_str(str, &ret, max, mstm->tm_hour<12 ?
1283 STRFTIME_TD(time_data, am) : STRFTIME_TD(time_data, pm)))
1284 return 0;
1285 break;
1286 #if _MSVCR_VER>=140
1287 case 'r':
1288 if(time_data == &cloc_time_data)
1290 if(!MSVCRT_CHECK_PMT(mstm->tm_hour>=0 && mstm->tm_hour<=23))
1291 goto einval_error;
1292 if(!strftime_int(str, &ret, max, (mstm->tm_hour + 11) % 12 + 1,
1293 alternate ? 0 : 2, 1, 12))
1294 return 0;
1295 if(ret < max)
1296 str[ret++] = ':';
1297 if(!strftime_int(str, &ret, max, mstm->tm_min, alternate ? 0 : 2, 0, 59))
1298 return 0;
1299 if(ret < max)
1300 str[ret++] = ':';
1301 if(!strftime_int(str, &ret, max, mstm->tm_sec, alternate ? 0 : 2, 0, MAX_SECONDS))
1302 return 0;
1303 if(ret < max)
1304 str[ret++] = ' ';
1305 if(!strftime_str(str, &ret, max, mstm->tm_hour<12 ?
1306 STRFTIME_TD(time_data, am) : STRFTIME_TD(time_data, pm)))
1307 return 0;
1309 else
1311 if(!strftime_format(str, &ret, max, mstm, time_data, STRFTIME_TD(time_data, time)))
1312 return 0;
1314 break;
1315 case 'R':
1316 if(!strftime_int(str, &ret, max, mstm->tm_hour, alternate ? 0 : 2, 0, 23))
1317 return 0;
1318 if(ret < max)
1319 str[ret++] = ':';
1320 if(!strftime_int(str, &ret, max, mstm->tm_min, alternate ? 0 : 2, 0, 59))
1321 return 0;
1322 break;
1323 #endif
1324 case 'S':
1325 if(!strftime_int(str, &ret, max, mstm->tm_sec, alternate ? 0 : 2, 0, MAX_SECONDS))
1326 return 0;
1327 break;
1328 #if _MSVCR_VER>=140
1329 case 't':
1330 str[ret++] = '\t';
1331 break;
1332 case 'T':
1333 if(!strftime_int(str, &ret, max, mstm->tm_hour, alternate ? 0 : 2, 0, 23))
1334 return 0;
1335 if(ret < max)
1336 str[ret++] = ':';
1337 if(!strftime_int(str, &ret, max, mstm->tm_min, alternate ? 0 : 2, 0, 59))
1338 return 0;
1339 if(ret < max)
1340 str[ret++] = ':';
1341 if(!strftime_int(str, &ret, max, mstm->tm_sec, alternate ? 0 : 2, 0, MAX_SECONDS))
1342 return 0;
1343 break;
1344 case 'u':
1345 if(!MSVCRT_CHECK_PMT(mstm->tm_wday>=0 && mstm->tm_wday<=6))
1346 goto einval_error;
1347 tmp = mstm->tm_wday ? mstm->tm_wday : 7;
1348 if(!strftime_int(str, &ret, max, tmp, 0, 1, 7))
1349 return 0;
1350 break;
1351 #endif
1352 case 'w':
1353 if(!strftime_int(str, &ret, max, mstm->tm_wday, 0, 0, 6))
1354 return 0;
1355 break;
1356 case 'y':
1357 #if _MSVCR_VER>=140
1358 if(!MSVCRT_CHECK_PMT(year>=0 && year<=9999))
1359 #else
1360 if(!MSVCRT_CHECK_PMT(year>=1900))
1361 #endif
1362 goto einval_error;
1363 if(!strftime_int(str, &ret, max, year%100, alternate ? 0 : 2, 0, 99))
1364 return 0;
1365 break;
1366 case 'Y':
1367 if(!strftime_int(str, &ret, max, year, alternate ? 0 : 4, 0, 9999))
1368 return 0;
1369 break;
1370 case 'z':
1371 #if _MSVCR_VER>=140
1372 _tzset();
1373 if(!strftime_tzdiff(str, &ret, max, mstm->tm_isdst))
1374 return 0;
1375 break;
1376 #endif
1377 case 'Z':
1378 _tzset();
1379 #if _MSVCR_VER <= 90
1380 if(_get_tzname(&tmp, str+ret, max-ret, mstm->tm_isdst ? 1 : 0))
1381 return 0;
1382 #else
1383 if(_mbstowcs_s_l(&tmp, str+ret, max-ret,
1384 mstm->tm_isdst ? tzname_dst : tzname_std,
1385 _TRUNCATE, loc) == STRUNCATE)
1386 ret = max;
1387 #endif
1388 ret += tmp-1;
1389 break;
1390 case 'U':
1391 case 'W':
1392 if(!MSVCRT_CHECK_PMT(mstm->tm_wday>=0 && mstm->tm_wday<=6))
1393 goto einval_error;
1394 if(!MSVCRT_CHECK_PMT(mstm->tm_yday>=0 && mstm->tm_yday<=365))
1395 goto einval_error;
1396 if(*format == 'U')
1397 tmp = mstm->tm_wday;
1398 else if(!mstm->tm_wday)
1399 tmp = 6;
1400 else
1401 tmp = mstm->tm_wday-1;
1403 tmp = mstm->tm_yday/7 + (tmp<=mstm->tm_yday%7);
1404 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 0, 53))
1405 return 0;
1406 break;
1407 case '%':
1408 str[ret++] = '%';
1409 break;
1410 default:
1411 WARN("unknown format %c\n", *format);
1412 MSVCRT_INVALID_PMT("unknown format", EINVAL);
1413 goto einval_error;
1417 if(ret == max) {
1418 if(max)
1419 *str = 0;
1420 *_errno() = ERANGE;
1421 return 0;
1424 str[ret] = 0;
1425 return ret;
1427 einval_error:
1428 *str = 0;
1429 return 0;
1432 static size_t strftime_helper(char *str, size_t max, const char *format,
1433 const struct tm *mstm, __lc_time_data *time_data, _locale_t loc)
1435 #if _MSVCR_VER <= 90
1436 TRACE("(%p %Iu %s %p %p %p)\n", str, max, format, mstm, time_data, loc);
1437 return strftime_impl(str, max, format, mstm, time_data, loc);
1438 #else
1439 wchar_t *s, *fmt;
1440 size_t len;
1441 int cp;
1443 TRACE("(%p %Iu %s %p %p %p)\n", str, max, format, mstm, time_data, loc);
1445 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
1446 if (!MSVCRT_CHECK_PMT(max != 0)) return 0;
1447 *str = 0;
1448 if (!MSVCRT_CHECK_PMT(format != NULL)) return 0;
1450 cp = (loc ? loc->locinfo : get_locinfo())->lc_id[LC_TIME].wCodePage;
1452 len = MultiByteToWideChar( cp, 0, format, -1, NULL, 0 );
1453 if (!len)
1455 *_errno() = EILSEQ;
1456 return 0;
1458 fmt = malloc( len*sizeof(wchar_t) );
1459 if (!fmt) return 0;
1460 MultiByteToWideChar( cp, 0, format, -1, fmt, len );
1462 if ((s = malloc( max*sizeof(wchar_t) )))
1464 len = strftime_impl( s, max, fmt, mstm, time_data, loc );
1465 if (len)
1467 len = WideCharToMultiByte( cp, 0, s, -1, str, max, NULL, NULL );
1468 if (len) len--;
1469 else *_errno() = EILSEQ;
1471 free( s );
1473 else len = 0;
1475 free( fmt );
1476 return len;
1477 #endif
1480 #if _MSVCR_VER >= 80
1481 /********************************************************************
1482 * _strftime_l (MSVCR80.@)
1484 size_t CDECL _strftime_l( char *str, size_t max, const char *format,
1485 const struct tm *mstm, _locale_t loc )
1487 return strftime_helper(str, max, format, mstm, NULL, loc);
1489 #endif
1491 /*********************************************************************
1492 * _Strftime (MSVCRT.@)
1494 size_t CDECL _Strftime(char *str, size_t max, const char *format,
1495 const struct tm *mstm, void *time_data)
1497 return strftime_helper(str, max, format, mstm, time_data, NULL);
1500 /*********************************************************************
1501 * strftime (MSVCRT.@)
1503 size_t CDECL strftime( char *str, size_t max, const char *format,
1504 const struct tm *mstm )
1506 return strftime_helper(str, max, format, mstm, NULL, NULL);
1509 static size_t wcsftime_helper( wchar_t *str, size_t max,
1510 const wchar_t *format, const struct tm *mstm,
1511 __lc_time_data *time_data, _locale_t loc )
1513 #if _MSVCR_VER <= 90
1514 char *s, *fmt;
1515 size_t len;
1517 TRACE("%p %Iu %s %p %p %p\n", str, max, debugstr_w(format), mstm, time_data, loc);
1519 len = _wcstombs_l( NULL, format, 0, loc ) + 1;
1520 if (!(fmt = malloc( len ))) return 0;
1521 _wcstombs_l(fmt, format, len, loc);
1523 if ((s = malloc( max*4 )))
1525 if (!strftime_impl( s, max*4, fmt, mstm, time_data, loc )) s[0] = 0;
1526 len = _mbstowcs_l( str, s, max, loc );
1527 free( s );
1529 else len = 0;
1531 free( fmt );
1532 return len;
1533 #else
1534 TRACE("%p %Iu %s %p %p %p\n", str, max, debugstr_w(format), mstm, time_data, loc);
1535 return strftime_impl(str, max, format, mstm, time_data, loc);
1536 #endif
1539 /*********************************************************************
1540 * _wcsftime_l (MSVCRT.@)
1542 size_t CDECL _wcsftime_l( wchar_t *str, size_t max,
1543 const wchar_t *format, const struct tm *mstm, _locale_t loc )
1545 return wcsftime_helper(str, max, format, mstm, NULL, loc);
1548 /*********************************************************************
1549 * wcsftime (MSVCRT.@)
1551 size_t CDECL wcsftime( wchar_t *str, size_t max,
1552 const wchar_t *format, const struct tm *mstm )
1554 return wcsftime_helper(str, max, format, mstm, NULL, NULL);
1557 #if _MSVCR_VER >= 110
1558 /*********************************************************************
1559 * _Wcsftime (MSVCR110.@)
1561 size_t CDECL _Wcsftime(wchar_t *str, size_t max,
1562 const wchar_t *format, const struct tm *mstm,
1563 __lc_time_data *time_data)
1565 return wcsftime_helper(str, max, format, mstm, time_data, NULL);
1567 #endif
1569 static char* asctime_buf(char *buf, const struct tm *mstm)
1571 static const char wday[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
1572 static const char month[12][4] = {"Jan", "Feb", "Mar", "Apr", "May",
1573 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
1575 if (!mstm || mstm->tm_sec<0 || mstm->tm_sec>59
1576 || mstm->tm_min<0 || mstm->tm_min>59
1577 || mstm->tm_hour<0 || mstm->tm_hour>23
1578 || mstm->tm_mon<0 || mstm->tm_mon>11
1579 || mstm->tm_wday<0 || mstm->tm_wday>6
1580 || mstm->tm_year<0 || mstm->tm_mday<0
1581 || mstm->tm_mday>MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon]) {
1582 *_errno() = EINVAL;
1583 return NULL;
1586 #if _MSVCR_VER>=140
1587 /* C89 (4.12.3.1) uses space-padding for day of month. */
1588 _snprintf(buf, 26, "%s %s %2d %02d:%02d:%02d %c%03d\n", wday[mstm->tm_wday],
1589 month[mstm->tm_mon], mstm->tm_mday, mstm->tm_hour, mstm->tm_min,
1590 mstm->tm_sec, '1'+(mstm->tm_year+900)/1000, (900+mstm->tm_year)%1000);
1591 #else
1592 _snprintf(buf, 26, "%s %s %02d %02d:%02d:%02d %c%03d\n", wday[mstm->tm_wday],
1593 month[mstm->tm_mon], mstm->tm_mday, mstm->tm_hour, mstm->tm_min,
1594 mstm->tm_sec, '1'+(mstm->tm_year+900)/1000, (900+mstm->tm_year)%1000);
1595 #endif
1596 return buf;
1599 /*********************************************************************
1600 * asctime (MSVCRT.@)
1602 char * CDECL asctime(const struct tm *mstm)
1604 thread_data_t *data = msvcrt_get_thread_data();
1606 /* asctime returns date in format that always has exactly 26 characters */
1607 if (!data->asctime_buffer) {
1608 data->asctime_buffer = malloc(26);
1609 if (!data->asctime_buffer) {
1610 *_errno() = ENOMEM;
1611 return NULL;
1615 return asctime_buf(data->asctime_buffer, mstm);
1618 /*********************************************************************
1619 * asctime_s (MSVCRT.@)
1621 int CDECL asctime_s(char* time, size_t size, const struct tm *mstm)
1623 if (!MSVCRT_CHECK_PMT(time != NULL)) return EINVAL;
1624 if (size) time[0] = 0;
1625 if (!MSVCRT_CHECK_PMT(size >= 26)) return EINVAL;
1626 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return EINVAL;
1627 if (!MSVCRT_CHECK_PMT(mstm->tm_sec >= 0 && mstm->tm_sec < 60)) return EINVAL;
1628 if (!MSVCRT_CHECK_PMT(mstm->tm_min >= 0 && mstm->tm_min < 60)) return EINVAL;
1629 if (!MSVCRT_CHECK_PMT(mstm->tm_hour >= 0 && mstm->tm_hour < 24)) return EINVAL;
1630 if (!MSVCRT_CHECK_PMT(mstm->tm_mon >= 0 && mstm->tm_mon < 12)) return EINVAL;
1631 if (!MSVCRT_CHECK_PMT(mstm->tm_wday >= 0 && mstm->tm_wday < 7)) return EINVAL;
1632 if (!MSVCRT_CHECK_PMT(mstm->tm_year >= 0)) return EINVAL;
1633 if (!MSVCRT_CHECK_PMT(mstm->tm_mday >= 0)) return EINVAL;
1634 if (!MSVCRT_CHECK_PMT(mstm->tm_mday <= MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon])) return EINVAL;
1636 asctime_buf(time, mstm);
1637 return 0;
1640 /*********************************************************************
1641 * _wasctime (MSVCRT.@)
1643 wchar_t * CDECL _wasctime(const struct tm *mstm)
1645 thread_data_t *data = msvcrt_get_thread_data();
1646 char buffer[26];
1648 if(!data->wasctime_buffer) {
1649 data->wasctime_buffer = malloc(26*sizeof(wchar_t));
1650 if(!data->wasctime_buffer) {
1651 *_errno() = ENOMEM;
1652 return NULL;
1656 if(!asctime_buf(buffer, mstm))
1657 return NULL;
1659 MultiByteToWideChar(CP_ACP, 0, buffer, -1, data->wasctime_buffer, 26);
1660 return data->wasctime_buffer;
1663 /*********************************************************************
1664 * _wasctime_s (MSVCRT.@)
1666 int CDECL _wasctime_s(wchar_t* time, size_t size, const struct tm *mstm)
1668 char buffer[26];
1669 int ret;
1671 if (!MSVCRT_CHECK_PMT(time != NULL)) return EINVAL;
1672 if (size) time[0] = 0;
1673 if (!MSVCRT_CHECK_PMT(size >= 26)) return EINVAL;
1674 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return EINVAL;
1676 ret = asctime_s(buffer, sizeof(buffer), mstm);
1677 if(ret)
1678 return ret;
1679 MultiByteToWideChar(CP_ACP, 0, buffer, -1, time, size);
1680 return 0;
1683 /*********************************************************************
1684 * _ctime64 (MSVCRT.@)
1686 char * CDECL _ctime64(const __time64_t *time)
1688 struct tm *t;
1689 t = _localtime64( time );
1690 if (!t) return NULL;
1691 return asctime( t );
1694 /*********************************************************************
1695 * _ctime64_s (MSVCRT.@)
1697 int CDECL _ctime64_s(char *res, size_t len, const __time64_t *time)
1699 struct tm *t;
1701 if (!MSVCRT_CHECK_PMT( res != NULL )) return EINVAL;
1702 if (!MSVCRT_CHECK_PMT( len >= 26 )) return EINVAL;
1703 res[0] = '\0';
1704 if (!MSVCRT_CHECK_PMT( time != NULL )) return EINVAL;
1705 if (!MSVCRT_CHECK_PMT( *time > 0 )) return EINVAL;
1707 t = _localtime64( time );
1708 strcpy( res, asctime( t ) );
1709 return 0;
1712 /*********************************************************************
1713 * _ctime32 (MSVCRT.@)
1715 char * CDECL _ctime32(const __time32_t *time)
1717 struct tm *t;
1718 t = _localtime32( time );
1719 if (!t) return NULL;
1720 return asctime( t );
1723 /*********************************************************************
1724 * _ctime32_s (MSVCRT.@)
1726 int CDECL _ctime32_s(char *res, size_t len, const __time32_t *time)
1728 struct tm *t;
1730 if (!MSVCRT_CHECK_PMT( res != NULL )) return EINVAL;
1731 if (!MSVCRT_CHECK_PMT( len >= 26 )) return EINVAL;
1732 res[0] = '\0';
1733 if (!MSVCRT_CHECK_PMT( time != NULL )) return EINVAL;
1734 if (!MSVCRT_CHECK_PMT( *time > 0 )) return EINVAL;
1736 t = _localtime32( time );
1737 strcpy( res, asctime( t ) );
1738 return 0;
1741 /*********************************************************************
1742 * _wctime64 (MSVCRT.@)
1744 wchar_t * CDECL _wctime64(const __time64_t *time)
1746 return _wasctime( _localtime64(time) );
1749 /*********************************************************************
1750 * _wctime32 (MSVCRT.@)
1752 wchar_t * CDECL _wctime32(const __time32_t *time)
1754 return _wasctime( _localtime32(time) );
1757 /*********************************************************************
1758 * _wctime64_s (MSVCRT.@)
1760 int CDECL _wctime64_s(wchar_t *buf,
1761 size_t size, const __time64_t *time)
1763 struct tm tm;
1764 int ret;
1766 if(!MSVCRT_CHECK_PMT(buf != NULL)) return EINVAL;
1767 if(!MSVCRT_CHECK_PMT(size != 0)) return EINVAL;
1768 buf[0] = 0;
1769 if(!MSVCRT_CHECK_PMT(time != NULL)) return EINVAL;
1770 if(!MSVCRT_CHECK_PMT(*time >= 0)) return EINVAL;
1771 if(!MSVCRT_CHECK_PMT(*time <= _MAX__TIME64_T)) return EINVAL;
1773 ret = _localtime64_s(&tm, time);
1774 if(ret != 0)
1775 return ret;
1777 return _wasctime_s(buf, size, &tm);
1780 /*********************************************************************
1781 * _wctime32_s (MSVCRT.@)
1783 int CDECL _wctime32_s(wchar_t *buf, size_t size,
1784 const __time32_t *time)
1786 struct tm tm;
1787 int ret;
1789 if(!MSVCRT_CHECK_PMT(buf != NULL)) return EINVAL;
1790 if(!MSVCRT_CHECK_PMT(size != 0)) return EINVAL;
1791 buf[0] = 0;
1792 if(!MSVCRT_CHECK_PMT(time != NULL)) return EINVAL;
1793 if(!MSVCRT_CHECK_PMT(*time >= 0)) return EINVAL;
1795 ret = _localtime32_s(&tm, time);
1796 if(ret != 0)
1797 return ret;
1799 return _wasctime_s(buf, size, &tm);
1802 #if _MSVCR_VER >= 80
1804 /*********************************************************************
1805 * _get_timezone (MSVCR80.@)
1807 int CDECL _get_timezone(LONG *timezone)
1809 if(!MSVCRT_CHECK_PMT(timezone != NULL)) return EINVAL;
1811 *timezone = MSVCRT___timezone;
1812 return 0;
1815 /*********************************************************************
1816 * _get_daylight (MSVCR80.@)
1818 int CDECL _get_daylight(int *hours)
1820 if(!MSVCRT_CHECK_PMT(hours != NULL)) return EINVAL;
1822 *hours = MSVCRT___daylight;
1823 return 0;
1826 #endif /* _MSVCR_VER >= 80 */
1828 #if _MSVCR_VER >= 140
1830 #define TIME_UTC 1
1832 struct _timespec32
1834 __time32_t tv_sec;
1835 LONG tv_nsec;
1838 struct _timespec64
1840 __time64_t tv_sec;
1841 LONG tv_nsec;
1844 /*********************************************************************
1845 * _timespec64_get (UCRTBASE.@)
1847 int CDECL _timespec64_get(struct _timespec64 *ts, int base)
1849 ULONGLONG time;
1850 FILETIME ft;
1852 if(!MSVCRT_CHECK_PMT(ts != NULL)) return 0;
1853 if(base != TIME_UTC) return 0;
1855 GetSystemTimePreciseAsFileTime(&ft);
1856 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
1858 ts->tv_sec = time / TICKSPERSEC - SECS_1601_TO_1970;
1859 ts->tv_nsec = time % TICKSPERSEC * 100;
1860 return base;
1863 /*********************************************************************
1864 * _timespec32_get (UCRTBASE.@)
1866 int CDECL _timespec32_get(struct _timespec32 *ts, int base)
1868 struct _timespec64 ts64;
1870 if(!MSVCRT_CHECK_PMT(ts != NULL)) return 0;
1871 if(base != TIME_UTC) return 0;
1873 if(_timespec64_get(&ts64, base) != base)
1874 return 0;
1875 if(ts64.tv_sec != (__time32_t)ts64.tv_sec)
1876 return 0;
1878 ts->tv_sec = ts64.tv_sec;
1879 ts->tv_nsec = ts64.tv_nsec;
1880 return base;
1882 #endif /* _MSVCR_VER >= 140 */