clock: Update Korean resource.
[wine/multimedia.git] / dlls / msvcrt / time.c
blob41501566f23a0828650509360445103af68211c3
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"
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
42 static const int MonthLengths[2][12] =
44 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
45 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
48 static inline int IsLeapYear(int Year)
50 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
53 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
55 memset( dest, 0, sizeof(*dest) );
56 dest->tm_sec = src->tm_sec;
57 dest->tm_min = src->tm_min;
58 dest->tm_hour = src->tm_hour;
59 dest->tm_mday = src->tm_mday;
60 dest->tm_mon = src->tm_mon;
61 dest->tm_year = src->tm_year;
62 dest->tm_wday = src->tm_wday;
63 dest->tm_yday = src->tm_yday;
64 dest->tm_isdst = src->tm_isdst;
67 static inline void unix_tm_to_msvcrt( struct MSVCRT_tm *dest, const struct tm *src )
69 memset( dest, 0, sizeof(*dest) );
70 dest->tm_sec = src->tm_sec;
71 dest->tm_min = src->tm_min;
72 dest->tm_hour = src->tm_hour;
73 dest->tm_mday = src->tm_mday;
74 dest->tm_mon = src->tm_mon;
75 dest->tm_year = src->tm_year;
76 dest->tm_wday = src->tm_wday;
77 dest->tm_yday = src->tm_yday;
78 dest->tm_isdst = src->tm_isdst;
81 static inline void write_invalid_msvcrt_tm( struct MSVCRT_tm *tm )
83 tm->tm_sec = -1;
84 tm->tm_min = -1;
85 tm->tm_hour = -1;
86 tm->tm_mday = -1;
87 tm->tm_mon = -1;
88 tm->tm_year = -1;
89 tm->tm_wday = -1;
90 tm->tm_yday = -1;
91 tm->tm_isdst = -1;
94 #define SECSPERDAY 86400
95 /* 1601 to 1970 is 369 years plus 89 leap days */
96 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
97 #define TICKSPERSEC 10000000
98 #define TICKSPERMSEC 10000
99 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
101 /**********************************************************************
102 * _mktime64 (MSVCRT.@)
104 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
106 time_t secs;
107 struct tm tm;
109 msvcrt_tm_to_unix( &tm, mstm );
110 secs = mktime( &tm );
111 unix_tm_to_msvcrt( mstm, &tm );
113 return secs < 0 ? -1 : secs;
116 /**********************************************************************
117 * _mktime32 (MSVCRT.@)
119 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
121 return MSVCRT__mktime64( mstm );
124 /**********************************************************************
125 * mktime (MSVCRT.@)
127 #ifdef _WIN64
128 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
130 return MSVCRT__mktime64( mstm );
132 #else
133 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
135 return MSVCRT__mktime32( mstm );
137 #endif
139 /**********************************************************************
140 * _mkgmtime64 (MSVCRT.@)
142 * time->tm_isdst value is ignored
144 MSVCRT___time64_t CDECL MSVCRT__mkgmtime64(struct MSVCRT_tm *time)
146 SYSTEMTIME st;
147 FILETIME ft;
148 MSVCRT___time64_t ret;
149 int i;
151 st.wMilliseconds = 0;
152 st.wSecond = time->tm_sec;
153 st.wMinute = time->tm_min;
154 st.wHour = time->tm_hour;
155 st.wDay = time->tm_mday;
156 st.wMonth = time->tm_mon+1;
157 st.wYear = time->tm_year+1900;
159 if(!SystemTimeToFileTime(&st, &ft))
160 return -1;
162 FileTimeToSystemTime(&ft, &st);
163 time->tm_wday = st.wDayOfWeek;
165 for(i=time->tm_yday=0; i<st.wMonth-1; i++)
166 time->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
167 time->tm_yday += st.wDay-1;
169 ret = ((MSVCRT___time64_t)ft.dwHighDateTime<<32)+ft.dwLowDateTime;
170 ret = (ret-TICKS_1601_TO_1970)/TICKSPERSEC;
171 return ret;
174 /**********************************************************************
175 * _mkgmtime32 (MSVCRT.@)
177 MSVCRT___time32_t CDECL MSVCRT__mkgmtime32(struct MSVCRT_tm *time)
179 return MSVCRT__mkgmtime64(time);
182 /**********************************************************************
183 * _mkgmtime (MSVCRT.@)
185 #ifdef _WIN64
186 MSVCRT___time64_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
188 return MSVCRT__mkgmtime64(time);
190 #else
191 MSVCRT___time32_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
193 return MSVCRT__mkgmtime32(time);
195 #endif
197 /*********************************************************************
198 * _localtime64 (MSVCRT.@)
200 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
202 struct tm *tm;
203 thread_data_t *data;
204 time_t seconds = *secs;
206 if (seconds < 0) return NULL;
208 _mlock(_TIME_LOCK);
209 if (!(tm = localtime( &seconds))) {
210 _munlock(_TIME_LOCK);
211 return NULL;
214 data = msvcrt_get_thread_data();
215 unix_tm_to_msvcrt( &data->time_buffer, tm );
216 _munlock(_TIME_LOCK);
218 return &data->time_buffer;
221 /*********************************************************************
222 * _localtime64_s (MSVCRT.@)
224 int CDECL _localtime64_s(struct MSVCRT_tm *time, const MSVCRT___time64_t *secs)
226 struct tm *tm;
227 time_t seconds;
229 if (!time || !secs || *secs < 0 || *secs > _MAX__TIME64_T)
231 if (time)
232 write_invalid_msvcrt_tm(time);
234 *MSVCRT__errno() = MSVCRT_EINVAL;
235 return MSVCRT_EINVAL;
238 seconds = *secs;
240 _mlock(_TIME_LOCK);
241 if (!(tm = localtime(&seconds)))
243 _munlock(_TIME_LOCK);
244 *MSVCRT__errno() = MSVCRT_EINVAL;
245 return MSVCRT_EINVAL;
248 unix_tm_to_msvcrt(time, tm);
249 _munlock(_TIME_LOCK);
250 return 0;
253 /*********************************************************************
254 * _localtime32 (MSVCRT.@)
256 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
258 MSVCRT___time64_t secs64 = *secs;
259 return MSVCRT__localtime64( &secs64 );
262 /*********************************************************************
263 * _localtime32_s (MSVCRT.@)
265 int CDECL _localtime32_s(struct MSVCRT_tm *time, const MSVCRT___time32_t *secs)
267 MSVCRT___time64_t secs64;
269 if (!time || !secs || *secs < 0)
271 if (time)
272 write_invalid_msvcrt_tm(time);
274 *MSVCRT__errno() = MSVCRT_EINVAL;
275 return MSVCRT_EINVAL;
278 secs64 = *secs;
279 return _localtime64_s(time, &secs64);
282 /*********************************************************************
283 * localtime (MSVCRT.@)
285 #ifdef _WIN64
286 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
288 return MSVCRT__localtime64( secs );
290 #else
291 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
293 return MSVCRT__localtime32( secs );
295 #endif
297 /*********************************************************************
298 * _gmtime64 (MSVCRT.@)
300 int CDECL MSVCRT__gmtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
302 int i;
303 FILETIME ft;
304 SYSTEMTIME st;
305 ULONGLONG time;
307 if (!res || !secs || *secs < 0) {
308 if (res) {
309 write_invalid_msvcrt_tm(res);
312 *MSVCRT__errno() = MSVCRT_EINVAL;
313 return MSVCRT_EINVAL;
316 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
318 ft.dwHighDateTime = (UINT)(time >> 32);
319 ft.dwLowDateTime = (UINT)time;
321 FileTimeToSystemTime(&ft, &st);
323 res->tm_sec = st.wSecond;
324 res->tm_min = st.wMinute;
325 res->tm_hour = st.wHour;
326 res->tm_mday = st.wDay;
327 res->tm_year = st.wYear - 1900;
328 res->tm_mon = st.wMonth - 1;
329 res->tm_wday = st.wDayOfWeek;
330 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++) {
331 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
334 res->tm_yday += st.wDay - 1;
335 res->tm_isdst = 0;
337 return 0;
340 /*********************************************************************
341 * _gmtime64 (MSVCRT.@)
343 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t *secs)
345 thread_data_t * const data = msvcrt_get_thread_data();
347 if(MSVCRT__gmtime64_s(&data->time_buffer, secs))
348 return NULL;
349 return &data->time_buffer;
352 /*********************************************************************
353 * _gmtime32_s (MSVCRT.@)
355 int CDECL MSVCRT__gmtime32_s(struct MSVCRT_tm *res, const MSVCRT___time32_t *secs)
357 MSVCRT___time64_t secs64;
359 if(secs) {
360 secs64 = *secs;
361 return MSVCRT__gmtime64_s(res, &secs64);
363 return MSVCRT__gmtime64_s(res, NULL);
366 /*********************************************************************
367 * _gmtime32 (MSVCRT.@)
369 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
371 MSVCRT___time64_t secs64;
373 if(!secs)
374 return NULL;
376 secs64 = *secs;
377 return MSVCRT__gmtime64( &secs64 );
380 /*********************************************************************
381 * gmtime (MSVCRT.@)
383 #ifdef _WIN64
384 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
386 return MSVCRT__gmtime64( secs );
388 #else
389 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
391 return MSVCRT__gmtime32( secs );
393 #endif
395 /**********************************************************************
396 * _strdate (MSVCRT.@)
398 char* CDECL _strdate(char* date)
400 static const char format[] = "MM'/'dd'/'yy";
402 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
404 return date;
407 /**********************************************************************
408 * _strdate_s (MSVCRT.@)
410 int CDECL _strdate_s(char* date, MSVCRT_size_t size)
412 if(date && size)
413 date[0] = '\0';
415 if(!date) {
416 *MSVCRT__errno() = MSVCRT_EINVAL;
417 return MSVCRT_EINVAL;
420 if(size < 9) {
421 *MSVCRT__errno() = MSVCRT_ERANGE;
422 return MSVCRT_ERANGE;
425 _strdate(date);
426 return 0;
429 /**********************************************************************
430 * _wstrdate (MSVCRT.@)
432 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
434 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
436 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
438 return date;
441 /**********************************************************************
442 * _wstrdate_s (MSVCRT.@)
444 int CDECL _wstrdate_s(MSVCRT_wchar_t* date, MSVCRT_size_t size)
446 if(date && size)
447 date[0] = '\0';
449 if(!date) {
450 *MSVCRT__errno() = MSVCRT_EINVAL;
451 return MSVCRT_EINVAL;
454 if(size < 9) {
455 *MSVCRT__errno() = MSVCRT_ERANGE;
456 return MSVCRT_ERANGE;
459 _wstrdate(date);
460 return 0;
463 /*********************************************************************
464 * _strtime (MSVCRT.@)
466 char* CDECL _strtime(char* time)
468 static const char format[] = "HH':'mm':'ss";
470 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
472 return time;
475 /*********************************************************************
476 * _strtime_s (MSVCRT.@)
478 int CDECL _strtime_s(char* time, MSVCRT_size_t size)
480 if(time && size)
481 time[0] = '\0';
483 if(!time) {
484 *MSVCRT__errno() = MSVCRT_EINVAL;
485 return MSVCRT_EINVAL;
488 if(size < 9) {
489 *MSVCRT__errno() = MSVCRT_ERANGE;
490 return MSVCRT_ERANGE;
493 _strtime(time);
494 return 0;
497 /*********************************************************************
498 * _wstrtime (MSVCRT.@)
500 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
502 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
504 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
506 return time;
509 /*********************************************************************
510 * _wstrtime_s (MSVCRT.@)
512 int CDECL _wstrtime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size)
514 if(time && size)
515 time[0] = '\0';
517 if(!time) {
518 *MSVCRT__errno() = MSVCRT_EINVAL;
519 return MSVCRT_EINVAL;
522 if(size < 9) {
523 *MSVCRT__errno() = MSVCRT_ERANGE;
524 return MSVCRT_ERANGE;
527 _wstrtime(time);
528 return 0;
531 /*********************************************************************
532 * clock (MSVCRT.@)
534 MSVCRT_clock_t CDECL MSVCRT_clock(void)
536 FILETIME ftc, fte, ftk, ftu;
537 ULONGLONG utime, ktime;
539 MSVCRT_clock_t clock;
541 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
543 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
544 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
546 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
548 return clock;
551 /*********************************************************************
552 * _difftime64 (MSVCRT.@)
554 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
556 return (double)(time1 - time2);
559 /*********************************************************************
560 * _difftime32 (MSVCRT.@)
562 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
564 return (double)(time1 - time2);
567 /*********************************************************************
568 * difftime (MSVCRT.@)
570 #ifdef _WIN64
571 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
573 return MSVCRT__difftime64( time1, time2 );
575 #else
576 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
578 return MSVCRT__difftime32( time1, time2 );
580 #endif
582 /*********************************************************************
583 * _ftime64 (MSVCRT.@)
585 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
587 TIME_ZONE_INFORMATION tzinfo;
588 FILETIME ft;
589 ULONGLONG time;
591 DWORD tzid = GetTimeZoneInformation(&tzinfo);
592 GetSystemTimeAsFileTime(&ft);
594 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
596 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
597 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
598 buf->timezone = tzinfo.Bias +
599 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
600 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
601 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
604 /*********************************************************************
605 * _ftime32 (MSVCRT.@)
607 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
609 struct MSVCRT___timeb64 buf64;
611 MSVCRT__ftime64( &buf64 );
612 buf->time = buf64.time;
613 buf->millitm = buf64.millitm;
614 buf->timezone = buf64.timezone;
615 buf->dstflag = buf64.dstflag;
618 /*********************************************************************
619 * _ftime (MSVCRT.@)
621 #ifdef _WIN64
622 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
624 return MSVCRT__ftime64( buf );
626 #else
627 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
629 return MSVCRT__ftime32( buf );
631 #endif
633 /*********************************************************************
634 * _time64 (MSVCRT.@)
636 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
638 MSVCRT___time64_t curtime;
639 struct MSVCRT___timeb64 tb;
641 MSVCRT__ftime64(&tb);
643 curtime = tb.time;
644 return buf ? *buf = curtime : curtime;
647 /*********************************************************************
648 * _time32 (MSVCRT.@)
650 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
652 MSVCRT___time32_t curtime;
653 struct MSVCRT___timeb64 tb;
655 MSVCRT__ftime64(&tb);
657 curtime = tb.time;
658 return buf ? *buf = curtime : curtime;
661 /*********************************************************************
662 * time (MSVCRT.@)
664 #ifdef _WIN64
665 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
667 return MSVCRT__time64( buf );
669 #else
670 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
672 return MSVCRT__time32( buf );
674 #endif
676 /*********************************************************************
677 * _daylight (MSVCRT.@)
679 int MSVCRT___daylight = 0;
681 /*********************************************************************
682 * __p_daylight (MSVCRT.@)
684 int * CDECL MSVCRT___p__daylight(void)
686 return &MSVCRT___daylight;
689 /*********************************************************************
690 * _dstbias (MSVCRT.@)
692 int MSVCRT__dstbias = 0;
694 /*********************************************************************
695 * __p_dstbias (MSVCRT.@)
697 int * CDECL __p__dstbias(void)
699 return &MSVCRT__dstbias;
702 /*********************************************************************
703 * _timezone (MSVCRT.@)
705 MSVCRT_long MSVCRT___timezone = 0;
707 /*********************************************************************
708 * __p_timezone (MSVCRT.@)
710 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
712 return &MSVCRT___timezone;
715 /*********************************************************************
716 * _tzname (MSVCRT.@)
717 * NOTES
718 * Some apps (notably Mozilla) insist on writing to these, so the buffer
719 * must be large enough. The size is picked based on observation of
720 * Windows XP.
722 static char tzname_std[64] = "PST";
723 static char tzname_dst[64] = "PDT";
724 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
726 /*********************************************************************
727 * _get_tzname (MSVCRT.@)
729 int CDECL MSVCRT__get_tzname(MSVCRT_size_t *ret, char *buf, MSVCRT_size_t bufsize, int index)
731 char *timezone;
733 switch(index)
735 case 0:
736 timezone = tzname_std;
737 break;
738 case 1:
739 timezone = tzname_dst;
740 break;
741 default:
742 *MSVCRT__errno() = MSVCRT_EINVAL;
743 return MSVCRT_EINVAL;
746 if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
748 *MSVCRT__errno() = MSVCRT_EINVAL;
749 return MSVCRT_EINVAL;
752 *ret = strlen(timezone)+1;
753 if(!buf && !bufsize)
754 return 0;
756 strcpy(buf, timezone);
757 return 0;
760 /*********************************************************************
761 * __p_tzname (MSVCRT.@)
763 char ** CDECL __p__tzname(void)
765 return MSVCRT__tzname;
768 /*********************************************************************
769 * _tzset (MSVCRT.@)
771 void CDECL MSVCRT__tzset(void)
773 tzset();
774 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
775 MSVCRT___daylight = daylight;
776 MSVCRT___timezone = timezone;
777 #else
779 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
780 time_t t;
781 struct tm *tmp;
782 int zone_january, zone_july;
784 _mlock(_TIME_LOCK);
785 t = (time(NULL) / seconds_in_year) * seconds_in_year;
786 tmp = localtime(&t);
787 zone_january = -tmp->tm_gmtoff;
788 t += seconds_in_year / 2;
789 tmp = localtime(&t);
790 zone_july = -tmp->tm_gmtoff;
791 _munlock(_TIME_LOCK);
793 MSVCRT___daylight = (zone_january != zone_july);
794 MSVCRT___timezone = max(zone_january, zone_july);
796 #endif
797 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
798 tzname_std[sizeof(tzname_std) - 1] = '\0';
799 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
800 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
803 /*********************************************************************
804 * strftime (MSVCRT.@)
806 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
807 const struct MSVCRT_tm *mstm )
809 struct tm tm;
811 msvcrt_tm_to_unix( &tm, mstm );
812 return strftime( str, max, format, &tm );
815 /*********************************************************************
816 * wcsftime (MSVCRT.@)
818 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
819 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
821 char *s, *fmt;
822 MSVCRT_size_t len;
824 TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
826 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
827 if (!(fmt = MSVCRT_malloc( len ))) return 0;
828 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
830 if ((s = MSVCRT_malloc( max*4 )))
832 struct tm tm;
833 msvcrt_tm_to_unix( &tm, mstm );
834 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
835 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
836 if (len) len--;
837 MSVCRT_free( s );
839 else len = 0;
841 MSVCRT_free( fmt );
842 return len;
845 /*********************************************************************
846 * asctime (MSVCRT.@)
848 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
850 char bufferA[30];
851 WCHAR bufferW[30];
853 thread_data_t *data = msvcrt_get_thread_data();
854 struct tm tm;
856 msvcrt_tm_to_unix( &tm, mstm );
858 if (!data->asctime_buffer)
859 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
861 #ifdef HAVE_ASCTIME_R
862 asctime_r( &tm, bufferA );
863 #else
864 strcpy( bufferA, asctime(&tm) );
865 #endif
866 MultiByteToWideChar( CP_UNIXCP, 0, bufferA, -1, bufferW, 30 );
867 WideCharToMultiByte( CP_ACP, 0, bufferW, -1, data->asctime_buffer, 30, NULL, NULL );
868 return data->asctime_buffer;
871 /*********************************************************************
872 * _wasctime (MSVCRT.@)
874 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
876 thread_data_t *data = msvcrt_get_thread_data();
877 struct tm tm;
878 char buffer[30];
880 msvcrt_tm_to_unix( &tm, mstm );
882 if (!data->wasctime_buffer)
883 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
884 #ifdef HAVE_ASCTIME_R
885 asctime_r( &tm, buffer );
886 #else
887 strcpy( buffer, asctime(&tm) );
888 #endif
889 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
890 return data->wasctime_buffer;
893 /*********************************************************************
894 * _ctime64 (MSVCRT.@)
896 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
898 struct MSVCRT_tm *t;
899 t = MSVCRT__localtime64( time );
900 if (!t) return NULL;
901 return MSVCRT_asctime( t );
904 /*********************************************************************
905 * _ctime64_s (MSVCRT.@)
907 int CDECL MSVCRT__ctime64_s(char *res, MSVCRT_size_t len, const MSVCRT___time64_t *time)
909 struct MSVCRT_tm *t;
910 if( !MSVCRT_CHECK_PMT( res != NULL ) || !MSVCRT_CHECK_PMT( len >= 26 ) )
912 *MSVCRT__errno() = MSVCRT_EINVAL;
913 return MSVCRT_EINVAL;
915 res[0] = '\0';
916 if( !MSVCRT_CHECK_PMT( time != NULL ) || !MSVCRT_CHECK_PMT( *time > 0 ) )
918 *MSVCRT__errno() = MSVCRT_EINVAL;
919 return MSVCRT_EINVAL;
921 t = MSVCRT__localtime64( time );
922 strcpy( res, MSVCRT_asctime( t ) );
923 return 0;
926 /*********************************************************************
927 * _ctime32 (MSVCRT.@)
929 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
931 struct MSVCRT_tm *t;
932 t = MSVCRT__localtime32( time );
933 if (!t) return NULL;
934 return MSVCRT_asctime( t );
937 /*********************************************************************
938 * _ctime32_s (MSVCRT.@)
940 int CDECL MSVCRT__ctime32_s(char *res, MSVCRT_size_t len, const MSVCRT___time32_t *time)
942 struct MSVCRT_tm *t;
943 if( !MSVCRT_CHECK_PMT( res != NULL ) || !MSVCRT_CHECK_PMT( len >= 26 ) )
945 *MSVCRT__errno() = MSVCRT_EINVAL;
946 return MSVCRT_EINVAL;
948 res[0] = '\0';
949 if( !MSVCRT_CHECK_PMT( time != NULL ) || !MSVCRT_CHECK_PMT( *time > 0 ) )
951 *MSVCRT__errno() = MSVCRT_EINVAL;
952 return MSVCRT_EINVAL;
954 t = MSVCRT__localtime32( time );
955 strcpy( res, MSVCRT_asctime( t ) );
956 return 0;
959 /*********************************************************************
960 * ctime (MSVCRT.@)
962 #ifdef _WIN64
963 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
965 return MSVCRT__ctime64( time );
967 #else
968 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
970 return MSVCRT__ctime32( time );
972 #endif
974 /*********************************************************************
975 * _wctime64 (MSVCRT.@)
977 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
979 return MSVCRT__wasctime( MSVCRT__localtime64(time) );
982 /*********************************************************************
983 * _wctime32 (MSVCRT.@)
985 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
987 return MSVCRT__wasctime( MSVCRT__localtime32(time) );
990 /*********************************************************************
991 * _wctime (MSVCRT.@)
993 #ifdef _WIN64
994 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
996 return MSVCRT__wctime64( time );
998 #else
999 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
1001 return MSVCRT__wctime32( time );
1003 #endif