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
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
38 static const int MonthLengths[2][12] =
40 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
41 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
44 static inline int IsLeapYear(int Year)
46 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
49 static inline void write_invalid_msvcrt_tm( struct MSVCRT_tm *tm )
62 /*********************************************************************
63 * _daylight (MSVCRT.@)
65 int MSVCRT___daylight = 1;
67 /*********************************************************************
68 * _timezone (MSVCRT.@)
70 MSVCRT_long MSVCRT___timezone = 28800;
72 /*********************************************************************
75 int MSVCRT__dstbias = -3600;
77 /*********************************************************************
80 * Some apps (notably Mozilla) insist on writing to these, so the buffer
81 * must be large enough.
83 static char tzname_std[64] = "PST";
84 static char tzname_dst[64] = "PDT";
85 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
87 static TIME_ZONE_INFORMATION tzi = {0};
88 /*********************************************************************
91 void CDECL MSVCRT__tzset(void)
93 char *tz = MSVCRT_getenv("TZ");
98 BOOL neg_zone = FALSE;
100 memset(&tzi, 0, sizeof(tzi));
102 /* Parse timezone information: tzn[+|-]hh[:mm[:ss]][dzn] */
103 lstrcpynA(MSVCRT__tzname[0], tz, 3);
109 }else if(*tz == '+') {
112 MSVCRT___timezone = strtol(tz, &tz, 10)*3600;
114 MSVCRT___timezone += strtol(tz+1, &tz, 10)*60;
116 MSVCRT___timezone += strtol(tz+1, &tz, 10);
119 MSVCRT___timezone = -MSVCRT___timezone;
121 MSVCRT___daylight = *tz;
122 lstrcpynA(MSVCRT__tzname[1], tz, 3);
123 }else if(GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID) {
124 MSVCRT___timezone = tzi.Bias*60;
125 if(tzi.StandardDate.wMonth)
126 MSVCRT___timezone += tzi.StandardBias*60;
128 if(tzi.DaylightDate.wMonth) {
129 MSVCRT___daylight = 1;
130 MSVCRT__dstbias = (tzi.DaylightBias-tzi.StandardBias)*60;
132 MSVCRT___daylight = 0;
136 if(!WideCharToMultiByte(CP_ACP, 0, tzi.StandardName, -1, MSVCRT__tzname[0],
137 sizeof(tzname_std), NULL, &error) || error)
138 *MSVCRT__tzname[0] = 0;
139 if(!WideCharToMultiByte(CP_ACP, 0, tzi.DaylightName, -1, MSVCRT__tzname[1],
140 sizeof(tzname_dst), NULL, &error) || error)
141 *MSVCRT__tzname[0] = 0;
143 _munlock(_TIME_LOCK);
146 static void _tzset_init(void)
148 static BOOL init = FALSE;
156 _munlock(_TIME_LOCK);
160 static BOOL is_dst(const SYSTEMTIME *st)
162 TIME_ZONE_INFORMATION tmp;
165 if(!MSVCRT___daylight)
168 if(tzi.DaylightDate.wMonth) {
170 }else if(st->wYear >= 2007) {
171 memset(&tmp, 0, sizeof(tmp));
172 tmp.StandardDate.wMonth = 11;
173 tmp.StandardDate.wDay = 1;
174 tmp.StandardDate.wHour = 2;
175 tmp.DaylightDate.wMonth = 3;
176 tmp.DaylightDate.wDay = 2;
177 tmp.DaylightDate.wHour = 2;
179 memset(&tmp, 0, sizeof(tmp));
180 tmp.StandardDate.wMonth = 10;
181 tmp.StandardDate.wDay = 5;
182 tmp.StandardDate.wHour = 2;
183 tmp.DaylightDate.wMonth = 4;
184 tmp.DaylightDate.wDay = 1;
185 tmp.DaylightDate.wHour = 2;
189 tmp.StandardBias = 0;
190 tmp.DaylightBias = MSVCRT__dstbias/60;
191 if(!SystemTimeToTzSpecificLocalTime(&tmp, st, &out))
194 return memcmp(st, &out, sizeof(SYSTEMTIME));
197 #define SECSPERDAY 86400
198 /* 1601 to 1970 is 369 years plus 89 leap days */
199 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
200 #define TICKSPERSEC 10000000
201 #define TICKSPERMSEC 10000
202 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
204 static MSVCRT___time64_t mktime_helper(struct MSVCRT_tm *mstm, BOOL local)
208 MSVCRT___time64_t ret = 0;
210 BOOL use_dst = FALSE;
212 ret = mstm->tm_year + mstm->tm_mon/12;
214 if(mstm->tm_mon < 0) {
219 if(ret<70 || ret>1100) {
220 *MSVCRT__errno() = MSVCRT_EINVAL;
224 memset(&st, 0, sizeof(SYSTEMTIME));
226 st.wMonth = mstm->tm_mon+1;
229 if(!SystemTimeToFileTime(&st, &ft)) {
230 *MSVCRT__errno() = MSVCRT_EINVAL;
234 ret = ((MSVCRT___time64_t)ft.dwHighDateTime<<32)+ft.dwLowDateTime;
235 ret += (MSVCRT___time64_t)mstm->tm_sec*TICKSPERSEC;
236 ret += (MSVCRT___time64_t)mstm->tm_min*60*TICKSPERSEC;
237 ret += (MSVCRT___time64_t)mstm->tm_hour*60*60*TICKSPERSEC;
238 ret += (MSVCRT___time64_t)(mstm->tm_mday-1)*SECSPERDAY*TICKSPERSEC;
240 ft.dwLowDateTime = ret & 0xffffffff;
241 ft.dwHighDateTime = ret >> 32;
242 FileTimeToSystemTime(&ft, &st);
246 use_dst = is_dst(&st);
247 if((mstm->tm_isdst<=-1 && use_dst) || (mstm->tm_isdst>=1)) {
250 ret += (MSVCRT___time64_t)MSVCRT__dstbias*TICKSPERSEC;
252 ft.dwLowDateTime = ret & 0xffffffff;
253 ft.dwHighDateTime = ret >> 32;
254 FileTimeToSystemTime(&ft, &tmp);
262 }else if(mstm->tm_isdst==0 && use_dst) {
263 ret -= (MSVCRT___time64_t)MSVCRT__dstbias*TICKSPERSEC;
264 ft.dwLowDateTime = ret & 0xffffffff;
265 ft.dwHighDateTime = ret >> 32;
266 FileTimeToSystemTime(&ft, &st);
267 ret += (MSVCRT___time64_t)MSVCRT__dstbias*TICKSPERSEC;
269 ret += (MSVCRT___time64_t)MSVCRT___timezone*TICKSPERSEC;
272 mstm->tm_sec = st.wSecond;
273 mstm->tm_min = st.wMinute;
274 mstm->tm_hour = st.wHour;
275 mstm->tm_mday = st.wDay;
276 mstm->tm_mon = st.wMonth-1;
277 mstm->tm_year = st.wYear-1900;
278 mstm->tm_wday = st.wDayOfWeek;
279 for(i=mstm->tm_yday=0; i<st.wMonth-1; i++)
280 mstm->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
281 mstm->tm_yday += st.wDay-1;
282 mstm->tm_isdst = use_dst ? 1 : 0;
284 if(ret < TICKS_1601_TO_1970) {
285 *MSVCRT__errno() = MSVCRT_EINVAL;
288 ret = (ret-TICKS_1601_TO_1970)/TICKSPERSEC;
292 /**********************************************************************
293 * _mktime64 (MSVCRT.@)
295 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
297 return mktime_helper(mstm, TRUE);
300 /**********************************************************************
301 * _mktime32 (MSVCRT.@)
303 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
305 MSVCRT___time64_t ret = MSVCRT__mktime64( mstm );
306 return ret == (MSVCRT___time32_t)ret ? ret : -1;
309 /**********************************************************************
313 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
315 return MSVCRT__mktime64( mstm );
318 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
320 return MSVCRT__mktime32( mstm );
324 /**********************************************************************
325 * _mkgmtime64 (MSVCRT.@)
327 * time->tm_isdst value is ignored
329 MSVCRT___time64_t CDECL MSVCRT__mkgmtime64(struct MSVCRT_tm *time)
331 return mktime_helper(time, FALSE);
334 /**********************************************************************
335 * _mkgmtime32 (MSVCRT.@)
337 MSVCRT___time32_t CDECL MSVCRT__mkgmtime32(struct MSVCRT_tm *time)
339 MSVCRT___time64_t ret = MSVCRT__mkgmtime64(time);
340 return ret == (MSVCRT___time32_t)ret ? ret : -1;
343 /**********************************************************************
344 * _mkgmtime (MSVCRT.@)
347 MSVCRT___time64_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
349 return MSVCRT__mkgmtime64(time);
352 MSVCRT___time32_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
354 return MSVCRT__mkgmtime32(time);
358 /*********************************************************************
359 * _localtime64_s (MSVCRT.@)
361 int CDECL _localtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
368 if (!res || !secs || *secs < 0 || *secs > _MAX__TIME64_T)
371 write_invalid_msvcrt_tm(res);
373 *MSVCRT__errno() = MSVCRT_EINVAL;
374 return MSVCRT_EINVAL;
378 time = (*secs - MSVCRT___timezone) * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
380 ft.dwHighDateTime = (UINT)(time >> 32);
381 ft.dwLowDateTime = (UINT)time;
382 FileTimeToSystemTime(&ft, &st);
384 res->tm_isdst = is_dst(&st) ? 1 : 0;
386 time -= MSVCRT__dstbias * (ULONGLONG)TICKSPERSEC;
387 ft.dwHighDateTime = (UINT)(time >> 32);
388 ft.dwLowDateTime = (UINT)time;
389 FileTimeToSystemTime(&ft, &st);
392 res->tm_sec = st.wSecond;
393 res->tm_min = st.wMinute;
394 res->tm_hour = st.wHour;
395 res->tm_mday = st.wDay;
396 res->tm_year = st.wYear - 1900;
397 res->tm_mon = st.wMonth - 1;
398 res->tm_wday = st.wDayOfWeek;
399 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++)
400 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
401 res->tm_yday += st.wDay - 1;
406 /*********************************************************************
407 * _localtime64 (MSVCRT.@)
409 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
411 thread_data_t *data = msvcrt_get_thread_data();
413 if(!data->time_buffer)
414 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
416 if(_localtime64_s(data->time_buffer, secs))
418 return data->time_buffer;
421 /*********************************************************************
422 * _localtime32 (MSVCRT.@)
424 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
426 MSVCRT___time64_t secs64;
432 return MSVCRT__localtime64( &secs64 );
435 /*********************************************************************
436 * _localtime32_s (MSVCRT.@)
438 int CDECL _localtime32_s(struct MSVCRT_tm *time, const MSVCRT___time32_t *secs)
440 MSVCRT___time64_t secs64;
442 if (!time || !secs || *secs < 0)
445 write_invalid_msvcrt_tm(time);
447 *MSVCRT__errno() = MSVCRT_EINVAL;
448 return MSVCRT_EINVAL;
452 return _localtime64_s(time, &secs64);
455 /*********************************************************************
456 * localtime (MSVCRT.@)
459 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
461 return MSVCRT__localtime64( secs );
464 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
466 return MSVCRT__localtime32( secs );
470 /*********************************************************************
471 * _gmtime64 (MSVCRT.@)
473 int CDECL MSVCRT__gmtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
480 if (!res || !secs || *secs < 0 || *secs > _MAX__TIME64_T) {
482 write_invalid_msvcrt_tm(res);
485 *MSVCRT__errno() = MSVCRT_EINVAL;
486 return MSVCRT_EINVAL;
489 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
491 ft.dwHighDateTime = (UINT)(time >> 32);
492 ft.dwLowDateTime = (UINT)time;
494 FileTimeToSystemTime(&ft, &st);
496 res->tm_sec = st.wSecond;
497 res->tm_min = st.wMinute;
498 res->tm_hour = st.wHour;
499 res->tm_mday = st.wDay;
500 res->tm_year = st.wYear - 1900;
501 res->tm_mon = st.wMonth - 1;
502 res->tm_wday = st.wDayOfWeek;
503 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++) {
504 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
507 res->tm_yday += st.wDay - 1;
513 /*********************************************************************
514 * _gmtime64 (MSVCRT.@)
516 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t *secs)
518 thread_data_t * const data = msvcrt_get_thread_data();
520 if(!data->time_buffer)
521 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
523 if(MSVCRT__gmtime64_s(data->time_buffer, secs))
525 return data->time_buffer;
528 /*********************************************************************
529 * _gmtime32_s (MSVCRT.@)
531 int CDECL MSVCRT__gmtime32_s(struct MSVCRT_tm *res, const MSVCRT___time32_t *secs)
533 MSVCRT___time64_t secs64;
537 return MSVCRT__gmtime64_s(res, &secs64);
539 return MSVCRT__gmtime64_s(res, NULL);
542 /*********************************************************************
543 * _gmtime32 (MSVCRT.@)
545 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
547 MSVCRT___time64_t secs64;
553 return MSVCRT__gmtime64( &secs64 );
556 /*********************************************************************
560 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
562 return MSVCRT__gmtime64( secs );
565 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
567 return MSVCRT__gmtime32( secs );
571 /**********************************************************************
572 * _strdate (MSVCRT.@)
574 char* CDECL MSVCRT__strdate(char* date)
576 static const char format[] = "MM'/'dd'/'yy";
578 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
583 /**********************************************************************
584 * _strdate_s (MSVCRT.@)
586 int CDECL _strdate_s(char* date, MSVCRT_size_t size)
592 *MSVCRT__errno() = MSVCRT_EINVAL;
593 return MSVCRT_EINVAL;
597 *MSVCRT__errno() = MSVCRT_ERANGE;
598 return MSVCRT_ERANGE;
601 MSVCRT__strdate(date);
605 /**********************************************************************
606 * _wstrdate (MSVCRT.@)
608 MSVCRT_wchar_t* CDECL MSVCRT__wstrdate(MSVCRT_wchar_t* date)
610 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
612 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
617 /**********************************************************************
618 * _wstrdate_s (MSVCRT.@)
620 int CDECL _wstrdate_s(MSVCRT_wchar_t* date, MSVCRT_size_t size)
626 *MSVCRT__errno() = MSVCRT_EINVAL;
627 return MSVCRT_EINVAL;
631 *MSVCRT__errno() = MSVCRT_ERANGE;
632 return MSVCRT_ERANGE;
635 MSVCRT__wstrdate(date);
639 /*********************************************************************
640 * _strtime (MSVCRT.@)
642 char* CDECL MSVCRT__strtime(char* time)
644 static const char format[] = "HH':'mm':'ss";
646 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
651 /*********************************************************************
652 * _strtime_s (MSVCRT.@)
654 int CDECL _strtime_s(char* time, MSVCRT_size_t size)
660 *MSVCRT__errno() = MSVCRT_EINVAL;
661 return MSVCRT_EINVAL;
665 *MSVCRT__errno() = MSVCRT_ERANGE;
666 return MSVCRT_ERANGE;
669 MSVCRT__strtime(time);
673 /*********************************************************************
674 * _wstrtime (MSVCRT.@)
676 MSVCRT_wchar_t* CDECL MSVCRT__wstrtime(MSVCRT_wchar_t* time)
678 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
680 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
685 /*********************************************************************
686 * _wstrtime_s (MSVCRT.@)
688 int CDECL _wstrtime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size)
694 *MSVCRT__errno() = MSVCRT_EINVAL;
695 return MSVCRT_EINVAL;
699 *MSVCRT__errno() = MSVCRT_ERANGE;
700 return MSVCRT_ERANGE;
703 MSVCRT__wstrtime(time);
707 /*********************************************************************
710 MSVCRT_clock_t CDECL MSVCRT_clock(void)
712 FILETIME ftc, fte, ftk, ftu;
713 ULONGLONG utime, ktime;
715 MSVCRT_clock_t clock;
717 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
719 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
720 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
722 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
727 /*********************************************************************
728 * _difftime64 (MSVCRT.@)
730 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
732 return (double)(time1 - time2);
735 /*********************************************************************
736 * _difftime32 (MSVCRT.@)
738 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
740 return (double)(time1 - time2);
743 /*********************************************************************
744 * difftime (MSVCRT.@)
747 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
749 return MSVCRT__difftime64( time1, time2 );
752 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
754 return MSVCRT__difftime32( time1, time2 );
758 /*********************************************************************
759 * _ftime64 (MSVCRT.@)
761 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
763 TIME_ZONE_INFORMATION tzinfo;
767 DWORD tzid = GetTimeZoneInformation(&tzinfo);
768 GetSystemTimeAsFileTime(&ft);
770 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
772 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
773 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
774 buf->timezone = tzinfo.Bias +
775 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
776 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
777 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
780 /*********************************************************************
781 * _ftime64_s (MSVCRT.@)
783 int CDECL MSVCRT__ftime64_s(struct MSVCRT___timeb64 *buf)
785 if (!MSVCRT_CHECK_PMT( buf != NULL )) return MSVCRT_EINVAL;
786 MSVCRT__ftime64(buf);
790 /*********************************************************************
791 * _ftime32 (MSVCRT.@)
793 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
795 struct MSVCRT___timeb64 buf64;
797 MSVCRT__ftime64( &buf64 );
798 buf->time = buf64.time;
799 buf->millitm = buf64.millitm;
800 buf->timezone = buf64.timezone;
801 buf->dstflag = buf64.dstflag;
804 /*********************************************************************
805 * _ftime32_s (MSVCRT.@)
807 int CDECL MSVCRT__ftime32_s(struct MSVCRT___timeb32 *buf)
809 if (!MSVCRT_CHECK_PMT( buf != NULL )) return MSVCRT_EINVAL;
810 MSVCRT__ftime32(buf);
814 /*********************************************************************
818 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
820 MSVCRT__ftime64( buf );
823 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
825 MSVCRT__ftime32( buf );
829 /*********************************************************************
832 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
834 MSVCRT___time64_t curtime;
835 struct MSVCRT___timeb64 tb;
837 MSVCRT__ftime64(&tb);
840 return buf ? *buf = curtime : curtime;
843 /*********************************************************************
846 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
848 MSVCRT___time32_t curtime;
849 struct MSVCRT___timeb64 tb;
851 MSVCRT__ftime64(&tb);
854 return buf ? *buf = curtime : curtime;
857 /*********************************************************************
861 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
863 return MSVCRT__time64( buf );
866 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
868 return MSVCRT__time32( buf );
872 /*********************************************************************
873 * __p_daylight (MSVCRT.@)
875 int * CDECL MSVCRT___p__daylight(void)
877 return &MSVCRT___daylight;
880 /*********************************************************************
881 * __p_dstbias (MSVCRT.@)
883 int * CDECL __p__dstbias(void)
885 return &MSVCRT__dstbias;
888 /*********************************************************************
889 * __p_timezone (MSVCRT.@)
891 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
893 return &MSVCRT___timezone;
896 /*********************************************************************
897 * _get_tzname (MSVCRT.@)
899 int CDECL MSVCRT__get_tzname(MSVCRT_size_t *ret, char *buf, MSVCRT_size_t bufsize, int index)
906 timezone = tzname_std;
909 timezone = tzname_dst;
912 *MSVCRT__errno() = MSVCRT_EINVAL;
913 return MSVCRT_EINVAL;
916 if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
918 *MSVCRT__errno() = MSVCRT_EINVAL;
919 return MSVCRT_EINVAL;
922 *ret = strlen(timezone)+1;
926 strcpy(buf, timezone);
930 /*********************************************************************
931 * __p_tzname (MSVCRT.@)
933 char ** CDECL __p__tzname(void)
935 return MSVCRT__tzname;
938 static inline BOOL strftime_date(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
939 BOOL alternate, const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
945 st.wYear = mstm->tm_year + 1900;
946 st.wMonth = mstm->tm_mon + 1;
947 st.wDayOfWeek = mstm->tm_wday;
948 st.wDay = mstm->tm_mday;
949 st.wHour = mstm->tm_hour;
950 st.wMinute = mstm->tm_min;
951 st.wSecond = mstm->tm_sec;
952 st.wMilliseconds = 0;
954 format = alternate ? time_data->str.names.date : time_data->str.names.short_date;
955 ret = GetDateFormatA(time_data->lcid, 0, &st, format, NULL, 0);
956 if(ret && ret<max-*pos)
957 ret = GetDateFormatA(time_data->lcid, 0, &st, format, str+*pos, max-*pos);
960 *MSVCRT__errno() = MSVCRT_EINVAL;
962 }else if(ret > max-*pos) {
964 *MSVCRT__errno() = MSVCRT_ERANGE;
971 static inline BOOL strftime_time(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
972 const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
977 st.wYear = mstm->tm_year + 1900;
978 st.wMonth = mstm->tm_mon + 1;
979 st.wDayOfWeek = mstm->tm_wday;
980 st.wDay = mstm->tm_mday;
981 st.wHour = mstm->tm_hour;
982 st.wMinute = mstm->tm_min;
983 st.wSecond = mstm->tm_sec;
984 st.wMilliseconds = 0;
986 ret = GetTimeFormatA(time_data->lcid, 0, &st, time_data->str.names.time, NULL, 0);
987 if(ret && ret<max-*pos)
988 ret = GetTimeFormatA(time_data->lcid, 0, &st, time_data->str.names.time,
992 *MSVCRT__errno() = MSVCRT_EINVAL;
994 }else if(ret > max-*pos) {
996 *MSVCRT__errno() = MSVCRT_ERANGE;
1003 static inline BOOL strftime_str(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max, char *src)
1005 MSVCRT_size_t len = strlen(src);
1006 if(len > max-*pos) {
1008 *MSVCRT__errno() = MSVCRT_ERANGE;
1012 memcpy(str+*pos, src, len);
1017 static inline BOOL strftime_int(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
1018 int src, int prec, int l, int h)
1022 if(src<l || src>h) {
1024 *MSVCRT__errno() = MSVCRT_EINVAL;
1028 len = MSVCRT__snprintf(str+*pos, max-*pos, "%0*d", prec, src);
1031 *MSVCRT__errno() = MSVCRT_ERANGE;
1039 /*********************************************************************
1040 * _Strftime (MSVCRT.@)
1042 MSVCRT_size_t CDECL _Strftime(char *str, MSVCRT_size_t max, const char *format,
1043 const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
1045 MSVCRT_size_t ret, tmp;
1048 TRACE("(%p %ld %s %p %p)\n", str, max, format, mstm, time_data);
1050 if(!str || !format) {
1053 *MSVCRT__errno() = MSVCRT_EINVAL;
1058 time_data = get_locinfo()->lc_time_curr;
1060 for(ret=0; *format && ret<max; format++) {
1061 if(*format != '%') {
1062 str[ret++] = *format;
1067 if(*format == '#') {
1079 if(!strftime_date(str, &ret, max, alternate, mstm, time_data))
1083 if(!strftime_time(str, &ret, max, mstm, time_data))
1087 if(!strftime_date(str, &ret, max, alternate, mstm, time_data))
1091 if(!strftime_time(str, &ret, max, mstm, time_data))
1095 if(mstm->tm_wday<0 || mstm->tm_wday>6)
1097 if(!strftime_str(str, &ret, max, time_data->str.names.short_wday[mstm->tm_wday]))
1101 if(mstm->tm_wday<0 || mstm->tm_wday>6)
1103 if(!strftime_str(str, &ret, max, time_data->str.names.wday[mstm->tm_wday]))
1107 if(mstm->tm_mon<0 || mstm->tm_mon>11)
1109 if(!strftime_str(str, &ret, max, time_data->str.names.short_mon[mstm->tm_mon]))
1113 if(mstm->tm_mon<0 || mstm->tm_mon>11)
1115 if(!strftime_str(str, &ret, max, time_data->str.names.mon[mstm->tm_mon]))
1119 if(!strftime_int(str, &ret, max, mstm->tm_mday, alternate ? 0 : 2, 0, 31))
1123 if(!strftime_int(str, &ret, max, mstm->tm_hour, alternate ? 0 : 2, 0, 23))
1127 tmp = mstm->tm_hour;
1132 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 1, 12))
1136 if(!strftime_int(str, &ret, max, mstm->tm_yday+1, alternate ? 0 : 3, 1, 366))
1140 if(!strftime_int(str, &ret, max, mstm->tm_mon+1, alternate ? 0 : 2, 1, 12))
1144 if(!strftime_int(str, &ret, max, mstm->tm_min, alternate ? 0 : 2, 0, 59))
1148 if(mstm->tm_hour<0 || mstm->tm_hour>23)
1150 if(!strftime_str(str, &ret, max, mstm->tm_hour<12 ?
1151 time_data->str.names.am : time_data->str.names.pm))
1155 if(!strftime_int(str, &ret, max, mstm->tm_sec, alternate ? 0 : 2, 0, 59))
1159 if(!strftime_int(str, &ret, max, mstm->tm_wday, 0, 0, 6))
1163 if(!strftime_int(str, &ret, max, mstm->tm_year%100, alternate ? 0 : 2, 0, 99))
1167 tmp = 1900+mstm->tm_year;
1168 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 4, 0, 9999))
1174 if(MSVCRT__get_tzname(&tmp, str+ret, max-ret, mstm->tm_isdst ? 1 : 0))
1180 if(mstm->tm_wday<0 || mstm->tm_wday>6 || mstm->tm_yday<0 || mstm->tm_yday>365)
1183 tmp = mstm->tm_wday;
1184 else if(!mstm->tm_wday)
1187 tmp = mstm->tm_wday-1;
1189 tmp = mstm->tm_yday/7 + (tmp<=mstm->tm_yday%7);
1190 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 0, 53))
1197 WARN("unknown format %c\n", *format);
1205 *MSVCRT__errno() = MSVCRT_ERANGE;
1214 *MSVCRT__errno() = MSVCRT_EINVAL;
1218 /*********************************************************************
1219 * strftime (MSVCRT.@)
1221 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
1222 const struct MSVCRT_tm *mstm )
1224 return _Strftime(str, max, format, mstm, NULL);
1227 /*********************************************************************
1228 * wcsftime (MSVCRT.@)
1230 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
1231 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
1236 TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
1238 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
1239 if (!(fmt = MSVCRT_malloc( len ))) return 0;
1240 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
1242 if ((s = MSVCRT_malloc( max*4 )))
1244 if (!MSVCRT_strftime( s, max*4, fmt, mstm )) s[0] = 0;
1245 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
1255 static char* asctime_buf(char *buf, const struct MSVCRT_tm *mstm)
1257 static const char wday[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
1258 static const char month[12][4] = {"Jan", "Feb", "Mar", "Apr", "May",
1259 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
1261 if (mstm->tm_sec<0 || mstm->tm_sec>59
1262 || mstm->tm_min<0 || mstm->tm_min>59
1263 || mstm->tm_hour<0 || mstm->tm_hour>23
1264 || mstm->tm_mon<0 || mstm->tm_mon>11
1265 || mstm->tm_wday<0 || mstm->tm_wday>6
1266 || mstm->tm_year<0 || mstm->tm_mday<0
1267 || mstm->tm_mday>MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon]) {
1268 *MSVCRT__errno() = MSVCRT_EINVAL;
1272 MSVCRT__snprintf(buf, 26, "%s %s %02d %02d:%02d:%02d %c%03d\n", wday[mstm->tm_wday],
1273 month[mstm->tm_mon], mstm->tm_mday, mstm->tm_hour, mstm->tm_min,
1274 mstm->tm_sec, '1'+(mstm->tm_year+900)/1000, (900+mstm->tm_year)%1000);
1278 /*********************************************************************
1279 * asctime (MSVCRT.@)
1281 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
1283 thread_data_t *data = msvcrt_get_thread_data();
1285 /* asctime returns date in format that always has exactly 26 characters */
1286 if (!data->asctime_buffer) {
1287 data->asctime_buffer = MSVCRT_malloc(26);
1288 if (!data->asctime_buffer) {
1289 *MSVCRT__errno() = MSVCRT_ENOMEM;
1294 return asctime_buf(data->asctime_buffer, mstm);
1297 /*********************************************************************
1298 * asctime_s (MSVCRT.@)
1300 int CDECL MSVCRT_asctime_s(char* time, MSVCRT_size_t size, const struct MSVCRT_tm *mstm)
1302 if (!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1303 if (size) time[0] = 0;
1304 if (!MSVCRT_CHECK_PMT(size >= 26)) return MSVCRT_EINVAL;
1305 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return MSVCRT_EINVAL;
1306 if (!MSVCRT_CHECK_PMT(mstm->tm_sec >= 0 && mstm->tm_sec < 60)) return MSVCRT_EINVAL;
1307 if (!MSVCRT_CHECK_PMT(mstm->tm_min >= 0 && mstm->tm_min < 60)) return MSVCRT_EINVAL;
1308 if (!MSVCRT_CHECK_PMT(mstm->tm_hour >= 0 && mstm->tm_hour < 24)) return MSVCRT_EINVAL;
1309 if (!MSVCRT_CHECK_PMT(mstm->tm_mon >= 0 && mstm->tm_mon < 12)) return MSVCRT_EINVAL;
1310 if (!MSVCRT_CHECK_PMT(mstm->tm_wday >= 0 && mstm->tm_wday < 7)) return MSVCRT_EINVAL;
1311 if (!MSVCRT_CHECK_PMT(mstm->tm_year >= 0)) return MSVCRT_EINVAL;
1312 if (!MSVCRT_CHECK_PMT(mstm->tm_mday >= 0)) return MSVCRT_EINVAL;
1313 if (!MSVCRT_CHECK_PMT(mstm->tm_mday <= MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon])) return MSVCRT_EINVAL;
1315 asctime_buf(time, mstm);
1319 /*********************************************************************
1320 * _wasctime (MSVCRT.@)
1322 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
1324 thread_data_t *data = msvcrt_get_thread_data();
1327 if(!data->wasctime_buffer) {
1328 data->wasctime_buffer = MSVCRT_malloc(26*sizeof(MSVCRT_wchar_t));
1329 if(!data->wasctime_buffer) {
1330 *MSVCRT__errno() = MSVCRT_ENOMEM;
1335 if(!asctime_buf(buffer, mstm))
1338 MultiByteToWideChar(CP_ACP, 0, buffer, -1, data->wasctime_buffer, 26);
1339 return data->wasctime_buffer;
1342 /*********************************************************************
1343 * _wasctime_s (MSVCRT.@)
1345 int CDECL MSVCRT__wasctime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size, const struct MSVCRT_tm *mstm)
1350 if (!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1351 if (size) time[0] = 0;
1352 if (!MSVCRT_CHECK_PMT(size >= 26)) return MSVCRT_EINVAL;
1353 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return MSVCRT_EINVAL;
1355 ret = MSVCRT_asctime_s(buffer, sizeof(buffer), mstm);
1358 MultiByteToWideChar(CP_ACP, 0, buffer, -1, time, size);
1362 /*********************************************************************
1363 * _ctime64 (MSVCRT.@)
1365 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
1367 struct MSVCRT_tm *t;
1368 t = MSVCRT__localtime64( time );
1369 if (!t) return NULL;
1370 return MSVCRT_asctime( t );
1373 /*********************************************************************
1374 * _ctime64_s (MSVCRT.@)
1376 int CDECL MSVCRT__ctime64_s(char *res, MSVCRT_size_t len, const MSVCRT___time64_t *time)
1378 struct MSVCRT_tm *t;
1380 if (!MSVCRT_CHECK_PMT( res != NULL )) return MSVCRT_EINVAL;
1381 if (!MSVCRT_CHECK_PMT( len >= 26 )) return MSVCRT_EINVAL;
1383 if (!MSVCRT_CHECK_PMT( time != NULL )) return MSVCRT_EINVAL;
1384 if (!MSVCRT_CHECK_PMT( *time > 0 )) return MSVCRT_EINVAL;
1386 t = MSVCRT__localtime64( time );
1387 strcpy( res, MSVCRT_asctime( t ) );
1391 /*********************************************************************
1392 * _ctime32 (MSVCRT.@)
1394 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
1396 struct MSVCRT_tm *t;
1397 t = MSVCRT__localtime32( time );
1398 if (!t) return NULL;
1399 return MSVCRT_asctime( t );
1402 /*********************************************************************
1403 * _ctime32_s (MSVCRT.@)
1405 int CDECL MSVCRT__ctime32_s(char *res, MSVCRT_size_t len, const MSVCRT___time32_t *time)
1407 struct MSVCRT_tm *t;
1409 if (!MSVCRT_CHECK_PMT( res != NULL )) return MSVCRT_EINVAL;
1410 if (!MSVCRT_CHECK_PMT( len >= 26 )) return MSVCRT_EINVAL;
1412 if (!MSVCRT_CHECK_PMT( time != NULL )) return MSVCRT_EINVAL;
1413 if (!MSVCRT_CHECK_PMT( *time > 0 )) return MSVCRT_EINVAL;
1415 t = MSVCRT__localtime32( time );
1416 strcpy( res, MSVCRT_asctime( t ) );
1420 /*********************************************************************
1424 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
1426 return MSVCRT__ctime64( time );
1429 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
1431 return MSVCRT__ctime32( time );
1435 /*********************************************************************
1436 * _wctime64 (MSVCRT.@)
1438 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
1440 return MSVCRT__wasctime( MSVCRT__localtime64(time) );
1443 /*********************************************************************
1444 * _wctime32 (MSVCRT.@)
1446 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
1448 return MSVCRT__wasctime( MSVCRT__localtime32(time) );
1451 /*********************************************************************
1452 * _wctime (MSVCRT.@)
1455 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
1457 return MSVCRT__wctime64( time );
1460 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
1462 return MSVCRT__wctime32( time );