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
27 #define _POSIX_PTHREAD_SEMANTICS /* switch to a 2 arg style asctime_r on Solaris */
29 #ifdef HAVE_SYS_TIMES_H
30 # include <sys/times.h>
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
43 static const int MonthLengths
[2][12] =
45 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
46 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
49 static inline int IsLeapYear(int Year
)
51 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0);
54 static inline void msvcrt_tm_to_unix( struct tm
*dest
, const struct MSVCRT_tm
*src
)
56 memset( dest
, 0, sizeof(*dest
) );
57 dest
->tm_sec
= src
->tm_sec
;
58 dest
->tm_min
= src
->tm_min
;
59 dest
->tm_hour
= src
->tm_hour
;
60 dest
->tm_mday
= src
->tm_mday
;
61 dest
->tm_mon
= src
->tm_mon
;
62 dest
->tm_year
= src
->tm_year
;
63 dest
->tm_wday
= src
->tm_wday
;
64 dest
->tm_yday
= src
->tm_yday
;
65 dest
->tm_isdst
= src
->tm_isdst
;
68 static inline void unix_tm_to_msvcrt( struct MSVCRT_tm
*dest
, const struct tm
*src
)
70 memset( dest
, 0, sizeof(*dest
) );
71 dest
->tm_sec
= src
->tm_sec
;
72 dest
->tm_min
= src
->tm_min
;
73 dest
->tm_hour
= src
->tm_hour
;
74 dest
->tm_mday
= src
->tm_mday
;
75 dest
->tm_mon
= src
->tm_mon
;
76 dest
->tm_year
= src
->tm_year
;
77 dest
->tm_wday
= src
->tm_wday
;
78 dest
->tm_yday
= src
->tm_yday
;
79 dest
->tm_isdst
= src
->tm_isdst
;
82 static inline void write_invalid_msvcrt_tm( struct MSVCRT_tm
*tm
)
95 #define SECSPERDAY 86400
96 /* 1601 to 1970 is 369 years plus 89 leap days */
97 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
98 #define TICKSPERSEC 10000000
99 #define TICKSPERMSEC 10000
100 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
102 /**********************************************************************
103 * _mktime64 (MSVCRT.@)
105 MSVCRT___time64_t CDECL
MSVCRT__mktime64(struct MSVCRT_tm
*mstm
)
110 msvcrt_tm_to_unix( &tm
, mstm
);
111 secs
= mktime( &tm
);
112 unix_tm_to_msvcrt( mstm
, &tm
);
114 return secs
< 0 ? -1 : secs
;
117 /**********************************************************************
118 * _mktime32 (MSVCRT.@)
120 MSVCRT___time32_t CDECL
MSVCRT__mktime32(struct MSVCRT_tm
*mstm
)
122 return MSVCRT__mktime64( mstm
);
125 /**********************************************************************
129 MSVCRT___time64_t CDECL
MSVCRT_mktime(struct MSVCRT_tm
*mstm
)
131 return MSVCRT__mktime64( mstm
);
134 MSVCRT___time32_t CDECL
MSVCRT_mktime(struct MSVCRT_tm
*mstm
)
136 return MSVCRT__mktime32( mstm
);
140 /**********************************************************************
141 * _mkgmtime64 (MSVCRT.@)
143 * time->tm_isdst value is ignored
145 MSVCRT___time64_t CDECL
MSVCRT__mkgmtime64(struct MSVCRT_tm
*time
)
149 MSVCRT___time64_t ret
;
152 st
.wMilliseconds
= 0;
153 st
.wSecond
= time
->tm_sec
;
154 st
.wMinute
= time
->tm_min
;
155 st
.wHour
= time
->tm_hour
;
156 st
.wDay
= time
->tm_mday
;
157 st
.wMonth
= time
->tm_mon
+1;
158 st
.wYear
= time
->tm_year
+1900;
160 if(!SystemTimeToFileTime(&st
, &ft
))
163 FileTimeToSystemTime(&ft
, &st
);
164 time
->tm_wday
= st
.wDayOfWeek
;
166 for(i
=time
->tm_yday
=0; i
<st
.wMonth
-1; i
++)
167 time
->tm_yday
+= MonthLengths
[IsLeapYear(st
.wYear
)][i
];
168 time
->tm_yday
+= st
.wDay
-1;
170 ret
= ((MSVCRT___time64_t
)ft
.dwHighDateTime
<<32)+ft
.dwLowDateTime
;
171 ret
= (ret
-TICKS_1601_TO_1970
)/TICKSPERSEC
;
175 /**********************************************************************
176 * _mkgmtime32 (MSVCRT.@)
178 MSVCRT___time32_t CDECL
MSVCRT__mkgmtime32(struct MSVCRT_tm
*time
)
180 return MSVCRT__mkgmtime64(time
);
183 /**********************************************************************
184 * _mkgmtime (MSVCRT.@)
187 MSVCRT___time64_t CDECL
MSVCRT__mkgmtime(struct MSVCRT_tm
*time
)
189 return MSVCRT__mkgmtime64(time
);
192 MSVCRT___time32_t CDECL
MSVCRT__mkgmtime(struct MSVCRT_tm
*time
)
194 return MSVCRT__mkgmtime32(time
);
198 /*********************************************************************
199 * _localtime64 (MSVCRT.@)
201 struct MSVCRT_tm
* CDECL
MSVCRT__localtime64(const MSVCRT___time64_t
* secs
)
205 time_t seconds
= *secs
;
207 if (seconds
< 0) return NULL
;
210 if (!(tm
= localtime( &seconds
))) {
211 _munlock(_TIME_LOCK
);
215 data
= msvcrt_get_thread_data();
216 if(!data
->time_buffer
)
217 data
->time_buffer
= MSVCRT_malloc(sizeof(struct MSVCRT_tm
));
219 unix_tm_to_msvcrt( data
->time_buffer
, tm
);
220 _munlock(_TIME_LOCK
);
222 return data
->time_buffer
;
225 /*********************************************************************
226 * _localtime64_s (MSVCRT.@)
228 int CDECL
_localtime64_s(struct MSVCRT_tm
*time
, const MSVCRT___time64_t
*secs
)
233 if (!time
|| !secs
|| *secs
< 0 || *secs
> _MAX__TIME64_T
)
236 write_invalid_msvcrt_tm(time
);
238 *MSVCRT__errno() = MSVCRT_EINVAL
;
239 return MSVCRT_EINVAL
;
245 if (!(tm
= localtime(&seconds
)))
247 _munlock(_TIME_LOCK
);
248 *MSVCRT__errno() = MSVCRT_EINVAL
;
249 return MSVCRT_EINVAL
;
252 unix_tm_to_msvcrt(time
, tm
);
253 _munlock(_TIME_LOCK
);
257 /*********************************************************************
258 * _localtime32 (MSVCRT.@)
260 struct MSVCRT_tm
* CDECL
MSVCRT__localtime32(const MSVCRT___time32_t
* secs
)
262 MSVCRT___time64_t secs64
= *secs
;
263 return MSVCRT__localtime64( &secs64
);
266 /*********************************************************************
267 * _localtime32_s (MSVCRT.@)
269 int CDECL
_localtime32_s(struct MSVCRT_tm
*time
, const MSVCRT___time32_t
*secs
)
271 MSVCRT___time64_t secs64
;
273 if (!time
|| !secs
|| *secs
< 0)
276 write_invalid_msvcrt_tm(time
);
278 *MSVCRT__errno() = MSVCRT_EINVAL
;
279 return MSVCRT_EINVAL
;
283 return _localtime64_s(time
, &secs64
);
286 /*********************************************************************
287 * localtime (MSVCRT.@)
290 struct MSVCRT_tm
* CDECL
MSVCRT_localtime(const MSVCRT___time64_t
* secs
)
292 return MSVCRT__localtime64( secs
);
295 struct MSVCRT_tm
* CDECL
MSVCRT_localtime(const MSVCRT___time32_t
* secs
)
297 return MSVCRT__localtime32( secs
);
301 /*********************************************************************
302 * _gmtime64 (MSVCRT.@)
304 int CDECL
MSVCRT__gmtime64_s(struct MSVCRT_tm
*res
, const MSVCRT___time64_t
*secs
)
311 if (!res
|| !secs
|| *secs
< 0) {
313 write_invalid_msvcrt_tm(res
);
316 *MSVCRT__errno() = MSVCRT_EINVAL
;
317 return MSVCRT_EINVAL
;
320 time
= *secs
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
322 ft
.dwHighDateTime
= (UINT
)(time
>> 32);
323 ft
.dwLowDateTime
= (UINT
)time
;
325 FileTimeToSystemTime(&ft
, &st
);
327 res
->tm_sec
= st
.wSecond
;
328 res
->tm_min
= st
.wMinute
;
329 res
->tm_hour
= st
.wHour
;
330 res
->tm_mday
= st
.wDay
;
331 res
->tm_year
= st
.wYear
- 1900;
332 res
->tm_mon
= st
.wMonth
- 1;
333 res
->tm_wday
= st
.wDayOfWeek
;
334 for (i
= res
->tm_yday
= 0; i
< st
.wMonth
- 1; i
++) {
335 res
->tm_yday
+= MonthLengths
[IsLeapYear(st
.wYear
)][i
];
338 res
->tm_yday
+= st
.wDay
- 1;
344 /*********************************************************************
345 * _gmtime64 (MSVCRT.@)
347 struct MSVCRT_tm
* CDECL
MSVCRT__gmtime64(const MSVCRT___time64_t
*secs
)
349 thread_data_t
* const data
= msvcrt_get_thread_data();
351 if(!data
->time_buffer
)
352 data
->time_buffer
= MSVCRT_malloc(sizeof(struct MSVCRT_tm
));
354 if(MSVCRT__gmtime64_s(data
->time_buffer
, secs
))
356 return data
->time_buffer
;
359 /*********************************************************************
360 * _gmtime32_s (MSVCRT.@)
362 int CDECL
MSVCRT__gmtime32_s(struct MSVCRT_tm
*res
, const MSVCRT___time32_t
*secs
)
364 MSVCRT___time64_t secs64
;
368 return MSVCRT__gmtime64_s(res
, &secs64
);
370 return MSVCRT__gmtime64_s(res
, NULL
);
373 /*********************************************************************
374 * _gmtime32 (MSVCRT.@)
376 struct MSVCRT_tm
* CDECL
MSVCRT__gmtime32(const MSVCRT___time32_t
* secs
)
378 MSVCRT___time64_t secs64
;
384 return MSVCRT__gmtime64( &secs64
);
387 /*********************************************************************
391 struct MSVCRT_tm
* CDECL
MSVCRT_gmtime(const MSVCRT___time64_t
* secs
)
393 return MSVCRT__gmtime64( secs
);
396 struct MSVCRT_tm
* CDECL
MSVCRT_gmtime(const MSVCRT___time32_t
* secs
)
398 return MSVCRT__gmtime32( secs
);
402 /**********************************************************************
403 * _strdate (MSVCRT.@)
405 char* CDECL
MSVCRT__strdate(char* date
)
407 static const char format
[] = "MM'/'dd'/'yy";
409 GetDateFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
414 /**********************************************************************
415 * _strdate_s (MSVCRT.@)
417 int CDECL
_strdate_s(char* date
, MSVCRT_size_t size
)
423 *MSVCRT__errno() = MSVCRT_EINVAL
;
424 return MSVCRT_EINVAL
;
428 *MSVCRT__errno() = MSVCRT_ERANGE
;
429 return MSVCRT_ERANGE
;
432 MSVCRT__strdate(date
);
436 /**********************************************************************
437 * _wstrdate (MSVCRT.@)
439 MSVCRT_wchar_t
* CDECL
MSVCRT__wstrdate(MSVCRT_wchar_t
* date
)
441 static const WCHAR format
[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
443 GetDateFormatW(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
448 /**********************************************************************
449 * _wstrdate_s (MSVCRT.@)
451 int CDECL
_wstrdate_s(MSVCRT_wchar_t
* date
, MSVCRT_size_t size
)
457 *MSVCRT__errno() = MSVCRT_EINVAL
;
458 return MSVCRT_EINVAL
;
462 *MSVCRT__errno() = MSVCRT_ERANGE
;
463 return MSVCRT_ERANGE
;
466 MSVCRT__wstrdate(date
);
470 /*********************************************************************
471 * _strtime (MSVCRT.@)
473 char* CDECL
MSVCRT__strtime(char* time
)
475 static const char format
[] = "HH':'mm':'ss";
477 GetTimeFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, time
, 9);
482 /*********************************************************************
483 * _strtime_s (MSVCRT.@)
485 int CDECL
_strtime_s(char* time
, MSVCRT_size_t size
)
491 *MSVCRT__errno() = MSVCRT_EINVAL
;
492 return MSVCRT_EINVAL
;
496 *MSVCRT__errno() = MSVCRT_ERANGE
;
497 return MSVCRT_ERANGE
;
500 MSVCRT__strtime(time
);
504 /*********************************************************************
505 * _wstrtime (MSVCRT.@)
507 MSVCRT_wchar_t
* CDECL
MSVCRT__wstrtime(MSVCRT_wchar_t
* time
)
509 static const WCHAR format
[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
511 GetTimeFormatW(LOCALE_NEUTRAL
, 0, NULL
, format
, time
, 9);
516 /*********************************************************************
517 * _wstrtime_s (MSVCRT.@)
519 int CDECL
_wstrtime_s(MSVCRT_wchar_t
* time
, MSVCRT_size_t size
)
525 *MSVCRT__errno() = MSVCRT_EINVAL
;
526 return MSVCRT_EINVAL
;
530 *MSVCRT__errno() = MSVCRT_ERANGE
;
531 return MSVCRT_ERANGE
;
534 MSVCRT__wstrtime(time
);
538 /*********************************************************************
541 MSVCRT_clock_t CDECL
MSVCRT_clock(void)
543 FILETIME ftc
, fte
, ftk
, ftu
;
544 ULONGLONG utime
, ktime
;
546 MSVCRT_clock_t clock
;
548 GetProcessTimes(GetCurrentProcess(), &ftc
, &fte
, &ftk
, &ftu
);
550 ktime
= ((ULONGLONG
)ftk
.dwHighDateTime
<< 32) | ftk
.dwLowDateTime
;
551 utime
= ((ULONGLONG
)ftu
.dwHighDateTime
<< 32) | ftu
.dwLowDateTime
;
553 clock
= (utime
+ ktime
) / (TICKSPERSEC
/ MSVCRT_CLOCKS_PER_SEC
);
558 /*********************************************************************
559 * _difftime64 (MSVCRT.@)
561 double CDECL
MSVCRT__difftime64(MSVCRT___time64_t time1
, MSVCRT___time64_t time2
)
563 return (double)(time1
- time2
);
566 /*********************************************************************
567 * _difftime32 (MSVCRT.@)
569 double CDECL
MSVCRT__difftime32(MSVCRT___time32_t time1
, MSVCRT___time32_t time2
)
571 return (double)(time1
- time2
);
574 /*********************************************************************
575 * difftime (MSVCRT.@)
578 double CDECL
MSVCRT_difftime(MSVCRT___time64_t time1
, MSVCRT___time64_t time2
)
580 return MSVCRT__difftime64( time1
, time2
);
583 double CDECL
MSVCRT_difftime(MSVCRT___time32_t time1
, MSVCRT___time32_t time2
)
585 return MSVCRT__difftime32( time1
, time2
);
589 /*********************************************************************
590 * _ftime64 (MSVCRT.@)
592 void CDECL
MSVCRT__ftime64(struct MSVCRT___timeb64
*buf
)
594 TIME_ZONE_INFORMATION tzinfo
;
598 DWORD tzid
= GetTimeZoneInformation(&tzinfo
);
599 GetSystemTimeAsFileTime(&ft
);
601 time
= ((ULONGLONG
)ft
.dwHighDateTime
<< 32) | ft
.dwLowDateTime
;
603 buf
->time
= time
/ TICKSPERSEC
- SECS_1601_TO_1970
;
604 buf
->millitm
= (time
% TICKSPERSEC
) / TICKSPERMSEC
;
605 buf
->timezone
= tzinfo
.Bias
+
606 ( tzid
== TIME_ZONE_ID_STANDARD
? tzinfo
.StandardBias
:
607 ( tzid
== TIME_ZONE_ID_DAYLIGHT
? tzinfo
.DaylightBias
: 0 ));
608 buf
->dstflag
= (tzid
== TIME_ZONE_ID_DAYLIGHT
?1:0);
611 /*********************************************************************
612 * _ftime64_s (MSVCRT.@)
614 int CDECL
MSVCRT__ftime64_s(struct MSVCRT___timeb64
*buf
)
616 if( !MSVCRT_CHECK_PMT( buf
!= NULL
) )
618 *MSVCRT__errno() = MSVCRT_EINVAL
;
619 return MSVCRT_EINVAL
;
621 MSVCRT__ftime64(buf
);
625 /*********************************************************************
626 * _ftime32 (MSVCRT.@)
628 void CDECL
MSVCRT__ftime32(struct MSVCRT___timeb32
*buf
)
630 struct MSVCRT___timeb64 buf64
;
632 MSVCRT__ftime64( &buf64
);
633 buf
->time
= buf64
.time
;
634 buf
->millitm
= buf64
.millitm
;
635 buf
->timezone
= buf64
.timezone
;
636 buf
->dstflag
= buf64
.dstflag
;
639 /*********************************************************************
640 * _ftime32_s (MSVCRT.@)
642 int CDECL
MSVCRT__ftime32_s(struct MSVCRT___timeb32
*buf
)
644 if( !MSVCRT_CHECK_PMT( buf
!= NULL
) )
646 *MSVCRT__errno() = MSVCRT_EINVAL
;
647 return MSVCRT_EINVAL
;
649 MSVCRT__ftime32(buf
);
653 /*********************************************************************
657 void CDECL
MSVCRT__ftime(struct MSVCRT___timeb64
*buf
)
659 MSVCRT__ftime64( buf
);
662 void CDECL
MSVCRT__ftime(struct MSVCRT___timeb32
*buf
)
664 MSVCRT__ftime32( buf
);
668 /*********************************************************************
671 MSVCRT___time64_t CDECL
MSVCRT__time64(MSVCRT___time64_t
*buf
)
673 MSVCRT___time64_t curtime
;
674 struct MSVCRT___timeb64 tb
;
676 MSVCRT__ftime64(&tb
);
679 return buf
? *buf
= curtime
: curtime
;
682 /*********************************************************************
685 MSVCRT___time32_t CDECL
MSVCRT__time32(MSVCRT___time32_t
*buf
)
687 MSVCRT___time32_t curtime
;
688 struct MSVCRT___timeb64 tb
;
690 MSVCRT__ftime64(&tb
);
693 return buf
? *buf
= curtime
: curtime
;
696 /*********************************************************************
700 MSVCRT___time64_t CDECL
MSVCRT_time(MSVCRT___time64_t
* buf
)
702 return MSVCRT__time64( buf
);
705 MSVCRT___time32_t CDECL
MSVCRT_time(MSVCRT___time32_t
* buf
)
707 return MSVCRT__time32( buf
);
711 /*********************************************************************
712 * _daylight (MSVCRT.@)
714 int MSVCRT___daylight
= 0;
716 /*********************************************************************
717 * __p_daylight (MSVCRT.@)
719 int * CDECL
MSVCRT___p__daylight(void)
721 return &MSVCRT___daylight
;
724 /*********************************************************************
725 * _dstbias (MSVCRT.@)
727 int MSVCRT__dstbias
= 0;
729 /*********************************************************************
730 * __p_dstbias (MSVCRT.@)
732 int * CDECL
__p__dstbias(void)
734 return &MSVCRT__dstbias
;
737 /*********************************************************************
738 * _timezone (MSVCRT.@)
740 MSVCRT_long MSVCRT___timezone
= 0;
742 /*********************************************************************
743 * __p_timezone (MSVCRT.@)
745 MSVCRT_long
* CDECL
MSVCRT___p__timezone(void)
747 return &MSVCRT___timezone
;
750 /*********************************************************************
753 * Some apps (notably Mozilla) insist on writing to these, so the buffer
754 * must be large enough. The size is picked based on observation of
757 static char tzname_std
[64] = "PST";
758 static char tzname_dst
[64] = "PDT";
759 char *MSVCRT__tzname
[2] = { tzname_std
, tzname_dst
};
761 /*********************************************************************
762 * _get_tzname (MSVCRT.@)
764 int CDECL
MSVCRT__get_tzname(MSVCRT_size_t
*ret
, char *buf
, MSVCRT_size_t bufsize
, int index
)
771 timezone
= tzname_std
;
774 timezone
= tzname_dst
;
777 *MSVCRT__errno() = MSVCRT_EINVAL
;
778 return MSVCRT_EINVAL
;
781 if(!ret
|| (!buf
&& bufsize
> 0) || (buf
&& !bufsize
))
783 *MSVCRT__errno() = MSVCRT_EINVAL
;
784 return MSVCRT_EINVAL
;
787 *ret
= strlen(timezone
)+1;
791 strcpy(buf
, timezone
);
795 /*********************************************************************
796 * __p_tzname (MSVCRT.@)
798 char ** CDECL
__p__tzname(void)
800 return MSVCRT__tzname
;
803 /*********************************************************************
806 void CDECL
MSVCRT__tzset(void)
809 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
810 MSVCRT___daylight
= daylight
;
811 MSVCRT___timezone
= timezone
;
814 static const time_t seconds_in_year
= (365 * 24 + 6) * 3600;
817 int zone_january
, zone_july
;
820 t
= (time(NULL
) / seconds_in_year
) * seconds_in_year
;
822 zone_january
= -tmp
->tm_gmtoff
;
823 t
+= seconds_in_year
/ 2;
825 zone_july
= -tmp
->tm_gmtoff
;
826 _munlock(_TIME_LOCK
);
828 MSVCRT___daylight
= (zone_january
!= zone_july
);
829 MSVCRT___timezone
= max(zone_january
, zone_july
);
832 lstrcpynA(tzname_std
, tzname
[0], sizeof(tzname_std
));
833 tzname_std
[sizeof(tzname_std
) - 1] = '\0';
834 lstrcpynA(tzname_dst
, tzname
[1], sizeof(tzname_dst
));
835 tzname_dst
[sizeof(tzname_dst
) - 1] = '\0';
838 /*********************************************************************
839 * strftime (MSVCRT.@)
841 MSVCRT_size_t CDECL
MSVCRT_strftime( char *str
, MSVCRT_size_t max
, const char *format
,
842 const struct MSVCRT_tm
*mstm
)
846 msvcrt_tm_to_unix( &tm
, mstm
);
847 return strftime( str
, max
, format
, &tm
);
850 /*********************************************************************
851 * wcsftime (MSVCRT.@)
853 MSVCRT_size_t CDECL
MSVCRT_wcsftime( MSVCRT_wchar_t
*str
, MSVCRT_size_t max
,
854 const MSVCRT_wchar_t
*format
, const struct MSVCRT_tm
*mstm
)
859 TRACE("%p %ld %s %p\n", str
, max
, debugstr_w(format
), mstm
);
861 len
= WideCharToMultiByte( CP_UNIXCP
, 0, format
, -1, NULL
, 0, NULL
, NULL
);
862 if (!(fmt
= MSVCRT_malloc( len
))) return 0;
863 WideCharToMultiByte( CP_UNIXCP
, 0, format
, -1, fmt
, len
, NULL
, NULL
);
865 if ((s
= MSVCRT_malloc( max
*4 )))
868 msvcrt_tm_to_unix( &tm
, mstm
);
869 if (!strftime( s
, max
*4, fmt
, &tm
)) s
[0] = 0;
870 len
= MultiByteToWideChar( CP_UNIXCP
, 0, s
, -1, str
, max
);
880 /*********************************************************************
883 char * CDECL
MSVCRT_asctime(const struct MSVCRT_tm
*mstm
)
888 thread_data_t
*data
= msvcrt_get_thread_data();
891 msvcrt_tm_to_unix( &tm
, mstm
);
893 if (!data
->asctime_buffer
)
894 data
->asctime_buffer
= MSVCRT_malloc( 30 ); /* ought to be enough */
896 #ifdef HAVE_ASCTIME_R
897 asctime_r( &tm
, bufferA
);
899 strcpy( bufferA
, asctime(&tm
) );
901 MultiByteToWideChar( CP_UNIXCP
, 0, bufferA
, -1, bufferW
, 30 );
902 WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, data
->asctime_buffer
, 30, NULL
, NULL
);
903 return data
->asctime_buffer
;
906 /*********************************************************************
907 * asctime_s (MSVCRT.@)
909 int CDECL
MSVCRT_asctime_s(char* time
, MSVCRT_size_t size
, const struct MSVCRT_tm
*mstm
)
914 if (!MSVCRT_CHECK_PMT(time
!= NULL
) || !MSVCRT_CHECK_PMT(mstm
!= NULL
)) {
915 *MSVCRT__errno() = MSVCRT_EINVAL
;
916 return MSVCRT_EINVAL
;
919 asc
= MSVCRT_asctime(mstm
);
920 len
= strlen(asc
) + 1;
922 if(!MSVCRT_CHECK_PMT(size
>= len
)) {
923 *MSVCRT__errno() = MSVCRT_ERANGE
;
924 return MSVCRT_ERANGE
;
931 /*********************************************************************
932 * _wasctime (MSVCRT.@)
934 MSVCRT_wchar_t
* CDECL
MSVCRT__wasctime(const struct MSVCRT_tm
*mstm
)
936 thread_data_t
*data
= msvcrt_get_thread_data();
940 msvcrt_tm_to_unix( &tm
, mstm
);
942 if (!data
->wasctime_buffer
)
943 data
->wasctime_buffer
= MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t
) ); /* ought to be enough */
944 #ifdef HAVE_ASCTIME_R
945 asctime_r( &tm
, buffer
);
947 strcpy( buffer
, asctime(&tm
) );
949 MultiByteToWideChar( CP_UNIXCP
, 0, buffer
, -1, data
->wasctime_buffer
, 30 );
950 return data
->wasctime_buffer
;
953 /*********************************************************************
954 * _wasctime_s (MSVCRT.@)
956 int CDECL
MSVCRT__wasctime_s(MSVCRT_wchar_t
* time
, MSVCRT_size_t size
, const struct MSVCRT_tm
*mstm
)
961 if (!MSVCRT_CHECK_PMT(time
!= NULL
) || !MSVCRT_CHECK_PMT(mstm
!= NULL
)) {
962 *MSVCRT__errno() = MSVCRT_EINVAL
;
963 return MSVCRT_EINVAL
;
966 asc
= MSVCRT__wasctime(mstm
);
967 len
= (strlenW(asc
) + 1) * sizeof(WCHAR
);
969 if(!MSVCRT_CHECK_PMT(size
>= len
)) {
970 *MSVCRT__errno() = MSVCRT_ERANGE
;
971 return MSVCRT_ERANGE
;
978 /*********************************************************************
979 * _ctime64 (MSVCRT.@)
981 char * CDECL
MSVCRT__ctime64(const MSVCRT___time64_t
*time
)
984 t
= MSVCRT__localtime64( time
);
986 return MSVCRT_asctime( t
);
989 /*********************************************************************
990 * _ctime64_s (MSVCRT.@)
992 int CDECL
MSVCRT__ctime64_s(char *res
, MSVCRT_size_t len
, const MSVCRT___time64_t
*time
)
995 if( !MSVCRT_CHECK_PMT( res
!= NULL
) || !MSVCRT_CHECK_PMT( len
>= 26 ) )
997 *MSVCRT__errno() = MSVCRT_EINVAL
;
998 return MSVCRT_EINVAL
;
1001 if( !MSVCRT_CHECK_PMT( time
!= NULL
) || !MSVCRT_CHECK_PMT( *time
> 0 ) )
1003 *MSVCRT__errno() = MSVCRT_EINVAL
;
1004 return MSVCRT_EINVAL
;
1006 t
= MSVCRT__localtime64( time
);
1007 strcpy( res
, MSVCRT_asctime( t
) );
1011 /*********************************************************************
1012 * _ctime32 (MSVCRT.@)
1014 char * CDECL
MSVCRT__ctime32(const MSVCRT___time32_t
*time
)
1016 struct MSVCRT_tm
*t
;
1017 t
= MSVCRT__localtime32( time
);
1018 if (!t
) return NULL
;
1019 return MSVCRT_asctime( t
);
1022 /*********************************************************************
1023 * _ctime32_s (MSVCRT.@)
1025 int CDECL
MSVCRT__ctime32_s(char *res
, MSVCRT_size_t len
, const MSVCRT___time32_t
*time
)
1027 struct MSVCRT_tm
*t
;
1028 if( !MSVCRT_CHECK_PMT( res
!= NULL
) || !MSVCRT_CHECK_PMT( len
>= 26 ) )
1030 *MSVCRT__errno() = MSVCRT_EINVAL
;
1031 return MSVCRT_EINVAL
;
1034 if( !MSVCRT_CHECK_PMT( time
!= NULL
) || !MSVCRT_CHECK_PMT( *time
> 0 ) )
1036 *MSVCRT__errno() = MSVCRT_EINVAL
;
1037 return MSVCRT_EINVAL
;
1039 t
= MSVCRT__localtime32( time
);
1040 strcpy( res
, MSVCRT_asctime( t
) );
1044 /*********************************************************************
1048 char * CDECL
MSVCRT_ctime(const MSVCRT___time64_t
*time
)
1050 return MSVCRT__ctime64( time
);
1053 char * CDECL
MSVCRT_ctime(const MSVCRT___time32_t
*time
)
1055 return MSVCRT__ctime32( time
);
1059 /*********************************************************************
1060 * _wctime64 (MSVCRT.@)
1062 MSVCRT_wchar_t
* CDECL
MSVCRT__wctime64(const MSVCRT___time64_t
*time
)
1064 return MSVCRT__wasctime( MSVCRT__localtime64(time
) );
1067 /*********************************************************************
1068 * _wctime32 (MSVCRT.@)
1070 MSVCRT_wchar_t
* CDECL
MSVCRT__wctime32(const MSVCRT___time32_t
*time
)
1072 return MSVCRT__wasctime( MSVCRT__localtime32(time
) );
1075 /*********************************************************************
1076 * _wctime (MSVCRT.@)
1079 MSVCRT_wchar_t
* CDECL
MSVCRT__wctime(const MSVCRT___time64_t
*time
)
1081 return MSVCRT__wctime64( time
);
1084 MSVCRT_wchar_t
* CDECL
MSVCRT__wctime(const MSVCRT___time32_t
*time
)
1086 return MSVCRT__wctime32( time
);