msvcrt: Added _strdate_s and _wstrdate_s implementation.
[wine] / dlls / msvcrt / time.c
1 /*
2  * msvcrt.dll date/time functions
3  *
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
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26
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>
33
34 #include "msvcrt.h"
35 #include "mtdll.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "wine/debug.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41
42 static const int MonthLengths[2][12] =
43 {
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 }
46 };
47
48 static inline int IsLeapYear(int Year)
49 {
50     return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
51 }
52
53 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
54 {
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;
65 }
66
67 static inline void unix_tm_to_msvcrt( struct MSVCRT_tm *dest, const struct tm *src )
68 {
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;
79 }
80
81 #define SECSPERDAY        86400
82 /* 1601 to 1970 is 369 years plus 89 leap days */
83 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
84 #define TICKSPERSEC       10000000
85 #define TICKSPERMSEC      10000
86 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
87
88 /**********************************************************************
89  *              _mktime64 (MSVCRT.@)
90  */
91 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
92 {
93     time_t secs;
94     struct tm tm;
95
96     msvcrt_tm_to_unix( &tm, mstm );
97     secs = mktime( &tm );
98     unix_tm_to_msvcrt( mstm, &tm );
99
100     return secs < 0 ? -1 : secs;
101 }
102
103 /**********************************************************************
104  *              _mktime32 (MSVCRT.@)
105  */
106 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
107 {
108     return MSVCRT__mktime64( mstm );
109 }
110
111 /**********************************************************************
112  *              mktime (MSVCRT.@)
113  */
114 #ifdef _WIN64
115 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
116 {
117     return MSVCRT__mktime64( mstm );
118 }
119 #else
120 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
121 {
122     return MSVCRT__mktime32( mstm );
123 }
124 #endif
125
126 /**********************************************************************
127  *              _mkgmtime64 (MSVCRT.@)
128  *
129  * time->tm_isdst value is ignored
130  */
131 MSVCRT___time64_t CDECL MSVCRT__mkgmtime64(struct MSVCRT_tm *time)
132 {
133     SYSTEMTIME st;
134     FILETIME ft;
135     MSVCRT___time64_t ret;
136     int i;
137
138     st.wMilliseconds = 0;
139     st.wSecond = time->tm_sec;
140     st.wMinute = time->tm_min;
141     st.wHour = time->tm_hour;
142     st.wDay = time->tm_mday;
143     st.wMonth = time->tm_mon+1;
144     st.wYear = time->tm_year+1900;
145
146     if(!SystemTimeToFileTime(&st, &ft))
147         return -1;
148
149     FileTimeToSystemTime(&ft, &st);
150     time->tm_wday = st.wDayOfWeek;
151
152     for(i=time->tm_yday=0; i<st.wMonth-1; i++)
153         time->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
154     time->tm_yday += st.wDay-1;
155
156     ret = ((MSVCRT___time64_t)ft.dwHighDateTime<<32)+ft.dwLowDateTime;
157     ret = (ret-TICKS_1601_TO_1970)/TICKSPERSEC;
158     return ret;
159 }
160
161 /**********************************************************************
162  *              _mkgmtime32 (MSVCRT.@)
163  */
164 MSVCRT___time32_t CDECL MSVCRT__mkgmtime32(struct MSVCRT_tm *time)
165 {
166     return MSVCRT__mkgmtime64(time);
167 }
168
169 /**********************************************************************
170  *              _mkgmtime (MSVCRT.@)
171  */
172 #ifdef _WIN64
173 MSVCRT___time64_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
174 {
175     return MSVCRT__mkgmtime64(time);
176 }
177 #else
178 MSVCRT___time32_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
179 {
180     return MSVCRT__mkgmtime32(time);
181 }
182 #endif
183
184 /*********************************************************************
185  *      _localtime64 (MSVCRT.@)
186  */
187 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
188 {
189     struct tm *tm;
190     thread_data_t *data;
191     time_t seconds = *secs;
192
193     if (seconds < 0) return NULL;
194
195     _mlock(_TIME_LOCK);
196     if (!(tm = localtime( &seconds))) {
197         _munlock(_TIME_LOCK);
198         return NULL;
199     }
200
201     data = msvcrt_get_thread_data();
202     unix_tm_to_msvcrt( &data->time_buffer, tm );
203     _munlock(_TIME_LOCK);
204
205     return &data->time_buffer;
206 }
207
208 /*********************************************************************
209  *      _localtime32 (MSVCRT.@)
210  */
211 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
212 {
213     MSVCRT___time64_t secs64 = *secs;
214     return MSVCRT__localtime64( &secs64 );
215 }
216
217 /*********************************************************************
218  *      localtime (MSVCRT.@)
219  */
220 #ifdef _WIN64
221 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
222 {
223     return MSVCRT__localtime64( secs );
224 }
225 #else
226 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
227 {
228     return MSVCRT__localtime32( secs );
229 }
230 #endif
231
232 /*********************************************************************
233  *      _gmtime64 (MSVCRT.@)
234  */
235 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t* secs)
236 {
237   thread_data_t * const data = msvcrt_get_thread_data();
238   int i;
239   FILETIME ft;
240   SYSTEMTIME st;
241
242   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
243
244   ft.dwHighDateTime = (UINT)(time >> 32);
245   ft.dwLowDateTime  = (UINT)time;
246
247   FileTimeToSystemTime(&ft, &st);
248
249   if (st.wYear < 1970) return NULL;
250
251   data->time_buffer.tm_sec  = st.wSecond;
252   data->time_buffer.tm_min  = st.wMinute;
253   data->time_buffer.tm_hour = st.wHour;
254   data->time_buffer.tm_mday = st.wDay;
255   data->time_buffer.tm_year = st.wYear - 1900;
256   data->time_buffer.tm_mon  = st.wMonth - 1;
257   data->time_buffer.tm_wday = st.wDayOfWeek;
258   for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
259     data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
260   }
261
262   data->time_buffer.tm_yday += st.wDay - 1;
263   data->time_buffer.tm_isdst = 0;
264
265   return &data->time_buffer;
266 }
267
268 /*********************************************************************
269  *      _gmtime32 (MSVCRT.@)
270  */
271 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
272 {
273     MSVCRT___time64_t secs64 = *secs;
274     return MSVCRT__gmtime64( &secs64 );
275 }
276
277 /*********************************************************************
278  *      gmtime (MSVCRT.@)
279  */
280 #ifdef _WIN64
281 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
282 {
283     return MSVCRT__gmtime64( secs );
284 }
285 #else
286 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
287 {
288     return MSVCRT__gmtime32( secs );
289 }
290 #endif
291
292 /**********************************************************************
293  *              _strdate (MSVCRT.@)
294  */
295 char* CDECL _strdate(char* date)
296 {
297   static const char format[] = "MM'/'dd'/'yy";
298
299   GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
300
301   return date;
302 }
303
304 /**********************************************************************
305  *              _strdate_s (MSVCRT.@)
306  */
307 int CDECL _strdate_s(char* date, MSVCRT_size_t size)
308 {
309     if(date && size)
310         date[0] = '\0';
311
312     if(!date) {
313         *MSVCRT__errno() = MSVCRT_EINVAL;
314         return MSVCRT_EINVAL;
315     }
316
317     if(size < 9) {
318         *MSVCRT__errno() = MSVCRT_ERANGE;
319         return MSVCRT_ERANGE;
320     }
321
322     _strdate(date);
323     return 0;
324 }
325
326 /**********************************************************************
327  *              _wstrdate (MSVCRT.@)
328  */
329 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
330 {
331   static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
332
333   GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
334
335   return date;
336 }
337
338 /**********************************************************************
339  *              _wstrdate_s (MSVCRT.@)
340  */
341 int CDECL _wstrdate_s(MSVCRT_wchar_t* date, MSVCRT_size_t size)
342 {
343     if(date && size)
344         date[0] = '\0';
345
346     if(!date) {
347         *MSVCRT__errno() = MSVCRT_EINVAL;
348         return MSVCRT_EINVAL;
349     }
350
351     if(size < 9) {
352         *MSVCRT__errno() = MSVCRT_ERANGE;
353         return MSVCRT_ERANGE;
354     }
355
356     _wstrdate(date);
357     return 0;
358 }
359
360 /*********************************************************************
361  *              _strtime (MSVCRT.@)
362  */
363 char* CDECL _strtime(char* time)
364 {
365   static const char format[] = "HH':'mm':'ss";
366
367   GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9); 
368
369   return time;
370 }
371
372 /*********************************************************************
373  *              _strtime_s (MSVCRT.@)
374  */
375 int CDECL _strtime_s(char* time, MSVCRT_size_t size)
376 {
377     if(time && size)
378         time[0] = '\0';
379
380     if(!time) {
381         *MSVCRT__errno() = MSVCRT_EINVAL;
382         return MSVCRT_EINVAL;
383     }
384
385     if(size < 9) {
386         *MSVCRT__errno() = MSVCRT_ERANGE;
387         return MSVCRT_ERANGE;
388     }
389
390     _strtime(time);
391     return 0;
392 }
393
394 /*********************************************************************
395  *              _wstrtime (MSVCRT.@)
396  */
397 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
398 {
399   static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
400
401   GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
402
403   return time;
404 }
405
406 /*********************************************************************
407  *              _wstrtime_s (MSVCRT.@)
408  */
409 int CDECL _wstrtime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size)
410 {
411     if(time && size)
412         time[0] = '\0';
413
414     if(!time) {
415         *MSVCRT__errno() = MSVCRT_EINVAL;
416         return MSVCRT_EINVAL;
417     }
418
419     if(size < 9) {
420         *MSVCRT__errno() = MSVCRT_ERANGE;
421         return MSVCRT_ERANGE;
422     }
423
424     _wstrtime(time);
425     return 0;
426 }
427
428 /*********************************************************************
429  *              clock (MSVCRT.@)
430  */
431 MSVCRT_clock_t CDECL MSVCRT_clock(void)
432 {
433   FILETIME ftc, fte, ftk, ftu;
434   ULONGLONG utime, ktime;
435  
436   MSVCRT_clock_t clock;
437
438   GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
439
440   ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
441   utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
442
443   clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
444
445   return clock;
446 }
447
448 /*********************************************************************
449  *              _difftime64 (MSVCRT.@)
450  */
451 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
452 {
453   return (double)(time1 - time2);
454 }
455
456 /*********************************************************************
457  *              _difftime32 (MSVCRT.@)
458  */
459 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
460 {
461   return (double)(time1 - time2);
462 }
463
464 /*********************************************************************
465  *              difftime (MSVCRT.@)
466  */
467 #ifdef _WIN64
468 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
469 {
470     return MSVCRT__difftime64( time1, time2 );
471 }
472 #else
473 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
474 {
475     return MSVCRT__difftime32( time1, time2 );
476 }
477 #endif
478
479 /*********************************************************************
480  *              _ftime64 (MSVCRT.@)
481  */
482 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
483 {
484   TIME_ZONE_INFORMATION tzinfo;
485   FILETIME ft;
486   ULONGLONG time;
487
488   DWORD tzid = GetTimeZoneInformation(&tzinfo);
489   GetSystemTimeAsFileTime(&ft);
490
491   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
492
493   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
494   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
495   buf->timezone = tzinfo.Bias +
496       ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
497       ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
498   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
499 }
500
501 /*********************************************************************
502  *              _ftime32 (MSVCRT.@)
503  */
504 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
505 {
506     struct MSVCRT___timeb64 buf64;
507
508     MSVCRT__ftime64( &buf64 );
509     buf->time     = buf64.time;
510     buf->millitm  = buf64.millitm;
511     buf->timezone = buf64.timezone;
512     buf->dstflag  = buf64.dstflag;
513 }
514
515 /*********************************************************************
516  *              _ftime (MSVCRT.@)
517  */
518 #ifdef _WIN64
519 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
520 {
521     return MSVCRT__ftime64( buf );
522 }
523 #else
524 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
525 {
526     return MSVCRT__ftime32( buf );
527 }
528 #endif
529
530 /*********************************************************************
531  *              _time64 (MSVCRT.@)
532  */
533 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
534 {
535     MSVCRT___time64_t curtime;
536     struct MSVCRT___timeb64 tb;
537
538     MSVCRT__ftime64(&tb);
539
540     curtime = tb.time;
541     return buf ? *buf = curtime : curtime;
542 }
543
544 /*********************************************************************
545  *              _time32 (MSVCRT.@)
546  */
547 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
548 {
549     MSVCRT___time32_t curtime;
550     struct MSVCRT___timeb64 tb;
551
552     MSVCRT__ftime64(&tb);
553
554     curtime = tb.time;
555     return buf ? *buf = curtime : curtime;
556 }
557
558 /*********************************************************************
559  *              time (MSVCRT.@)
560  */
561 #ifdef _WIN64
562 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
563 {
564     return MSVCRT__time64( buf );
565 }
566 #else
567 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
568 {
569     return MSVCRT__time32( buf );
570 }
571 #endif
572
573 /*********************************************************************
574  *              _daylight (MSVCRT.@)
575  */
576 int MSVCRT___daylight = 0;
577
578 /*********************************************************************
579  *              __p_daylight (MSVCRT.@)
580  */
581 int * CDECL MSVCRT___p__daylight(void)
582 {
583         return &MSVCRT___daylight;
584 }
585
586 /*********************************************************************
587  *              _dstbias (MSVCRT.@)
588  */
589 int MSVCRT__dstbias = 0;
590
591 /*********************************************************************
592  *              __p_dstbias (MSVCRT.@)
593  */
594 int * CDECL __p__dstbias(void)
595 {
596     return &MSVCRT__dstbias;
597 }
598
599 /*********************************************************************
600  *              _timezone (MSVCRT.@)
601  */
602 MSVCRT_long MSVCRT___timezone = 0;
603
604 /*********************************************************************
605  *              __p_timezone (MSVCRT.@)
606  */
607 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
608 {
609         return &MSVCRT___timezone;
610 }
611
612 /*********************************************************************
613  *              _tzname (MSVCRT.@)
614  * NOTES
615  *  Some apps (notably Mozilla) insist on writing to these, so the buffer
616  *  must be large enough.  The size is picked based on observation of
617  *  Windows XP.
618  */
619 static char tzname_std[64] = "";
620 static char tzname_dst[64] = "";
621 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
622
623 /*********************************************************************
624  *              __p_tzname (MSVCRT.@)
625  */
626 char ** CDECL __p__tzname(void)
627 {
628         return MSVCRT__tzname;
629 }
630
631 /*********************************************************************
632  *              _tzset (MSVCRT.@)
633  */
634 void CDECL MSVCRT__tzset(void)
635 {
636     tzset();
637 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
638     MSVCRT___daylight = daylight;
639     MSVCRT___timezone = timezone;
640 #else
641     {
642         static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
643         time_t t;
644         struct tm *tmp;
645         int zone_january, zone_july;
646
647         _mlock(_TIME_LOCK);
648         t = (time(NULL) / seconds_in_year) * seconds_in_year;
649         tmp = localtime(&t);
650         zone_january = -tmp->tm_gmtoff;
651         t += seconds_in_year / 2;
652         tmp = localtime(&t);
653         zone_july = -tmp->tm_gmtoff;
654         _munlock(_TIME_LOCK);
655
656         MSVCRT___daylight = (zone_january != zone_july);
657         MSVCRT___timezone = max(zone_january, zone_july);
658     }
659 #endif
660     lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
661     tzname_std[sizeof(tzname_std) - 1] = '\0';
662     lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
663     tzname_dst[sizeof(tzname_dst) - 1] = '\0';
664 }
665
666 /*********************************************************************
667  *              strftime (MSVCRT.@)
668  */
669 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
670                                      const struct MSVCRT_tm *mstm )
671 {
672     struct tm tm;
673
674     msvcrt_tm_to_unix( &tm, mstm );
675     return strftime( str, max, format, &tm );
676 }
677
678 /*********************************************************************
679  *              wcsftime (MSVCRT.@)
680  */
681 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
682                                      const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
683 {
684     char *s, *fmt;
685     MSVCRT_size_t len;
686
687     TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
688
689     len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
690     if (!(fmt = MSVCRT_malloc( len ))) return 0;
691     WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
692
693     if ((s = MSVCRT_malloc( max*4 )))
694     {
695         struct tm tm;
696         msvcrt_tm_to_unix( &tm, mstm );
697         if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
698         len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
699         if (len) len--;
700         MSVCRT_free( s );
701     }
702     else len = 0;
703
704     MSVCRT_free( fmt );
705     return len;
706 }
707
708 /*********************************************************************
709  *              asctime (MSVCRT.@)
710  */
711 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
712 {
713     thread_data_t *data = msvcrt_get_thread_data();
714     struct tm tm;
715
716     msvcrt_tm_to_unix( &tm, mstm );
717
718     if (!data->asctime_buffer)
719         data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
720
721     /* FIXME: may want to map from Unix codepage to CP_ACP */
722 #ifdef HAVE_ASCTIME_R
723     asctime_r( &tm, data->asctime_buffer );
724 #else
725     strcpy( data->asctime_buffer, asctime(&tm) );
726 #endif
727     return data->asctime_buffer;
728 }
729
730 /*********************************************************************
731  *              _wasctime (MSVCRT.@)
732  */
733 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
734 {
735     thread_data_t *data = msvcrt_get_thread_data();
736     struct tm tm;
737     char buffer[30];
738
739     msvcrt_tm_to_unix( &tm, mstm );
740
741     if (!data->wasctime_buffer)
742         data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
743 #ifdef HAVE_ASCTIME_R
744     asctime_r( &tm, buffer );
745 #else
746     strcpy( buffer, asctime(&tm) );
747 #endif
748     MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
749     return data->wasctime_buffer;
750 }
751
752 /*********************************************************************
753  *              _ctime64 (MSVCRT.@)
754  */
755 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
756 {
757     struct MSVCRT_tm *t;
758     t = MSVCRT__localtime64( time );
759     if (!t) return NULL;
760     return MSVCRT_asctime( t );
761 }
762
763 /*********************************************************************
764  *              _ctime32 (MSVCRT.@)
765  */
766 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
767 {
768     struct MSVCRT_tm *t;
769     t = MSVCRT__localtime32( time );
770     if (!t) return NULL;
771     return MSVCRT_asctime( t );
772 }
773
774 /*********************************************************************
775  *              ctime (MSVCRT.@)
776  */
777 #ifdef _WIN64
778 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
779 {
780     return MSVCRT__ctime64( time );
781 }
782 #else
783 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
784 {
785     return MSVCRT__ctime32( time );
786 }
787 #endif
788
789 /*********************************************************************
790  *              _wctime64 (MSVCRT.@)
791  */
792 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
793 {
794     return MSVCRT__wasctime( MSVCRT__localtime64(time) );
795 }
796
797 /*********************************************************************
798  *              _wctime32 (MSVCRT.@)
799  */
800 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
801 {
802     return MSVCRT__wasctime( MSVCRT__localtime32(time) );
803 }
804
805 /*********************************************************************
806  *              _wctime (MSVCRT.@)
807  */
808 #ifdef _WIN64
809 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
810 {
811     return MSVCRT__wctime64( time );
812 }
813 #else
814 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
815 {
816     return MSVCRT__wctime32( time );
817 }
818 #endif