cmd: Reorder global vars declarations/definitions to improve readability.
[wine/multimedia.git] / dlls / msvcrt / time.c
blobbf87711cfd52cbf46500223b24035b7b8aef7017
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 if(!data->time_buffer)
216 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
218 unix_tm_to_msvcrt( data->time_buffer, tm );
219 _munlock(_TIME_LOCK);
221 return data->time_buffer;
224 /*********************************************************************
225 * _localtime64_s (MSVCRT.@)
227 int CDECL _localtime64_s(struct MSVCRT_tm *time, const MSVCRT___time64_t *secs)
229 struct tm *tm;
230 time_t seconds;
232 if (!time || !secs || *secs < 0 || *secs > _MAX__TIME64_T)
234 if (time)
235 write_invalid_msvcrt_tm(time);
237 *MSVCRT__errno() = MSVCRT_EINVAL;
238 return MSVCRT_EINVAL;
241 seconds = *secs;
243 _mlock(_TIME_LOCK);
244 if (!(tm = localtime(&seconds)))
246 _munlock(_TIME_LOCK);
247 *MSVCRT__errno() = MSVCRT_EINVAL;
248 return MSVCRT_EINVAL;
251 unix_tm_to_msvcrt(time, tm);
252 _munlock(_TIME_LOCK);
253 return 0;
256 /*********************************************************************
257 * _localtime32 (MSVCRT.@)
259 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
261 MSVCRT___time64_t secs64 = *secs;
262 return MSVCRT__localtime64( &secs64 );
265 /*********************************************************************
266 * _localtime32_s (MSVCRT.@)
268 int CDECL _localtime32_s(struct MSVCRT_tm *time, const MSVCRT___time32_t *secs)
270 MSVCRT___time64_t secs64;
272 if (!time || !secs || *secs < 0)
274 if (time)
275 write_invalid_msvcrt_tm(time);
277 *MSVCRT__errno() = MSVCRT_EINVAL;
278 return MSVCRT_EINVAL;
281 secs64 = *secs;
282 return _localtime64_s(time, &secs64);
285 /*********************************************************************
286 * localtime (MSVCRT.@)
288 #ifdef _WIN64
289 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
291 return MSVCRT__localtime64( secs );
293 #else
294 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
296 return MSVCRT__localtime32( secs );
298 #endif
300 /*********************************************************************
301 * _gmtime64 (MSVCRT.@)
303 int CDECL MSVCRT__gmtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
305 int i;
306 FILETIME ft;
307 SYSTEMTIME st;
308 ULONGLONG time;
310 if (!res || !secs || *secs < 0) {
311 if (res) {
312 write_invalid_msvcrt_tm(res);
315 *MSVCRT__errno() = MSVCRT_EINVAL;
316 return MSVCRT_EINVAL;
319 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
321 ft.dwHighDateTime = (UINT)(time >> 32);
322 ft.dwLowDateTime = (UINT)time;
324 FileTimeToSystemTime(&ft, &st);
326 res->tm_sec = st.wSecond;
327 res->tm_min = st.wMinute;
328 res->tm_hour = st.wHour;
329 res->tm_mday = st.wDay;
330 res->tm_year = st.wYear - 1900;
331 res->tm_mon = st.wMonth - 1;
332 res->tm_wday = st.wDayOfWeek;
333 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++) {
334 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
337 res->tm_yday += st.wDay - 1;
338 res->tm_isdst = 0;
340 return 0;
343 /*********************************************************************
344 * _gmtime64 (MSVCRT.@)
346 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t *secs)
348 thread_data_t * const data = msvcrt_get_thread_data();
350 if(!data->time_buffer)
351 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
353 if(MSVCRT__gmtime64_s(data->time_buffer, secs))
354 return NULL;
355 return data->time_buffer;
358 /*********************************************************************
359 * _gmtime32_s (MSVCRT.@)
361 int CDECL MSVCRT__gmtime32_s(struct MSVCRT_tm *res, const MSVCRT___time32_t *secs)
363 MSVCRT___time64_t secs64;
365 if(secs) {
366 secs64 = *secs;
367 return MSVCRT__gmtime64_s(res, &secs64);
369 return MSVCRT__gmtime64_s(res, NULL);
372 /*********************************************************************
373 * _gmtime32 (MSVCRT.@)
375 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
377 MSVCRT___time64_t secs64;
379 if(!secs)
380 return NULL;
382 secs64 = *secs;
383 return MSVCRT__gmtime64( &secs64 );
386 /*********************************************************************
387 * gmtime (MSVCRT.@)
389 #ifdef _WIN64
390 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
392 return MSVCRT__gmtime64( secs );
394 #else
395 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
397 return MSVCRT__gmtime32( secs );
399 #endif
401 /**********************************************************************
402 * _strdate (MSVCRT.@)
404 char* CDECL MSVCRT__strdate(char* date)
406 static const char format[] = "MM'/'dd'/'yy";
408 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
410 return date;
413 /**********************************************************************
414 * _strdate_s (MSVCRT.@)
416 int CDECL _strdate_s(char* date, MSVCRT_size_t size)
418 if(date && size)
419 date[0] = '\0';
421 if(!date) {
422 *MSVCRT__errno() = MSVCRT_EINVAL;
423 return MSVCRT_EINVAL;
426 if(size < 9) {
427 *MSVCRT__errno() = MSVCRT_ERANGE;
428 return MSVCRT_ERANGE;
431 MSVCRT__strdate(date);
432 return 0;
435 /**********************************************************************
436 * _wstrdate (MSVCRT.@)
438 MSVCRT_wchar_t* CDECL MSVCRT__wstrdate(MSVCRT_wchar_t* date)
440 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
442 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
444 return date;
447 /**********************************************************************
448 * _wstrdate_s (MSVCRT.@)
450 int CDECL _wstrdate_s(MSVCRT_wchar_t* date, MSVCRT_size_t size)
452 if(date && size)
453 date[0] = '\0';
455 if(!date) {
456 *MSVCRT__errno() = MSVCRT_EINVAL;
457 return MSVCRT_EINVAL;
460 if(size < 9) {
461 *MSVCRT__errno() = MSVCRT_ERANGE;
462 return MSVCRT_ERANGE;
465 MSVCRT__wstrdate(date);
466 return 0;
469 /*********************************************************************
470 * _strtime (MSVCRT.@)
472 char* CDECL MSVCRT__strtime(char* time)
474 static const char format[] = "HH':'mm':'ss";
476 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
478 return time;
481 /*********************************************************************
482 * _strtime_s (MSVCRT.@)
484 int CDECL _strtime_s(char* time, MSVCRT_size_t size)
486 if(time && size)
487 time[0] = '\0';
489 if(!time) {
490 *MSVCRT__errno() = MSVCRT_EINVAL;
491 return MSVCRT_EINVAL;
494 if(size < 9) {
495 *MSVCRT__errno() = MSVCRT_ERANGE;
496 return MSVCRT_ERANGE;
499 MSVCRT__strtime(time);
500 return 0;
503 /*********************************************************************
504 * _wstrtime (MSVCRT.@)
506 MSVCRT_wchar_t* CDECL MSVCRT__wstrtime(MSVCRT_wchar_t* time)
508 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
510 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
512 return time;
515 /*********************************************************************
516 * _wstrtime_s (MSVCRT.@)
518 int CDECL _wstrtime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size)
520 if(time && size)
521 time[0] = '\0';
523 if(!time) {
524 *MSVCRT__errno() = MSVCRT_EINVAL;
525 return MSVCRT_EINVAL;
528 if(size < 9) {
529 *MSVCRT__errno() = MSVCRT_ERANGE;
530 return MSVCRT_ERANGE;
533 MSVCRT__wstrtime(time);
534 return 0;
537 /*********************************************************************
538 * clock (MSVCRT.@)
540 MSVCRT_clock_t CDECL MSVCRT_clock(void)
542 FILETIME ftc, fte, ftk, ftu;
543 ULONGLONG utime, ktime;
545 MSVCRT_clock_t clock;
547 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
549 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
550 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
552 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
554 return clock;
557 /*********************************************************************
558 * _difftime64 (MSVCRT.@)
560 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
562 return (double)(time1 - time2);
565 /*********************************************************************
566 * _difftime32 (MSVCRT.@)
568 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
570 return (double)(time1 - time2);
573 /*********************************************************************
574 * difftime (MSVCRT.@)
576 #ifdef _WIN64
577 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
579 return MSVCRT__difftime64( time1, time2 );
581 #else
582 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
584 return MSVCRT__difftime32( time1, time2 );
586 #endif
588 /*********************************************************************
589 * _ftime64 (MSVCRT.@)
591 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
593 TIME_ZONE_INFORMATION tzinfo;
594 FILETIME ft;
595 ULONGLONG time;
597 DWORD tzid = GetTimeZoneInformation(&tzinfo);
598 GetSystemTimeAsFileTime(&ft);
600 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
602 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
603 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
604 buf->timezone = tzinfo.Bias +
605 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
606 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
607 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
610 /*********************************************************************
611 * _ftime64_s (MSVCRT.@)
613 int CDECL MSVCRT__ftime64_s(struct MSVCRT___timeb64 *buf)
615 if( !MSVCRT_CHECK_PMT( buf != NULL ) )
617 *MSVCRT__errno() = MSVCRT_EINVAL;
618 return MSVCRT_EINVAL;
620 MSVCRT__ftime64(buf);
621 return 0;
624 /*********************************************************************
625 * _ftime32 (MSVCRT.@)
627 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
629 struct MSVCRT___timeb64 buf64;
631 MSVCRT__ftime64( &buf64 );
632 buf->time = buf64.time;
633 buf->millitm = buf64.millitm;
634 buf->timezone = buf64.timezone;
635 buf->dstflag = buf64.dstflag;
638 /*********************************************************************
639 * _ftime32_s (MSVCRT.@)
641 int CDECL MSVCRT__ftime32_s(struct MSVCRT___timeb32 *buf)
643 if( !MSVCRT_CHECK_PMT( buf != NULL ) )
645 *MSVCRT__errno() = MSVCRT_EINVAL;
646 return MSVCRT_EINVAL;
648 MSVCRT__ftime32(buf);
649 return 0;
652 /*********************************************************************
653 * _ftime (MSVCRT.@)
655 #ifdef _WIN64
656 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
658 MSVCRT__ftime64( buf );
660 #else
661 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
663 MSVCRT__ftime32( buf );
665 #endif
667 /*********************************************************************
668 * _time64 (MSVCRT.@)
670 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
672 MSVCRT___time64_t curtime;
673 struct MSVCRT___timeb64 tb;
675 MSVCRT__ftime64(&tb);
677 curtime = tb.time;
678 return buf ? *buf = curtime : curtime;
681 /*********************************************************************
682 * _time32 (MSVCRT.@)
684 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
686 MSVCRT___time32_t curtime;
687 struct MSVCRT___timeb64 tb;
689 MSVCRT__ftime64(&tb);
691 curtime = tb.time;
692 return buf ? *buf = curtime : curtime;
695 /*********************************************************************
696 * time (MSVCRT.@)
698 #ifdef _WIN64
699 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
701 return MSVCRT__time64( buf );
703 #else
704 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
706 return MSVCRT__time32( buf );
708 #endif
710 /*********************************************************************
711 * _daylight (MSVCRT.@)
713 int MSVCRT___daylight = 0;
715 /*********************************************************************
716 * __p_daylight (MSVCRT.@)
718 int * CDECL MSVCRT___p__daylight(void)
720 return &MSVCRT___daylight;
723 /*********************************************************************
724 * _dstbias (MSVCRT.@)
726 int MSVCRT__dstbias = 0;
728 /*********************************************************************
729 * __p_dstbias (MSVCRT.@)
731 int * CDECL __p__dstbias(void)
733 return &MSVCRT__dstbias;
736 /*********************************************************************
737 * _timezone (MSVCRT.@)
739 MSVCRT_long MSVCRT___timezone = 0;
741 /*********************************************************************
742 * __p_timezone (MSVCRT.@)
744 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
746 return &MSVCRT___timezone;
749 /*********************************************************************
750 * _tzname (MSVCRT.@)
751 * NOTES
752 * Some apps (notably Mozilla) insist on writing to these, so the buffer
753 * must be large enough. The size is picked based on observation of
754 * Windows XP.
756 static char tzname_std[64] = "PST";
757 static char tzname_dst[64] = "PDT";
758 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
760 /*********************************************************************
761 * _get_tzname (MSVCRT.@)
763 int CDECL MSVCRT__get_tzname(MSVCRT_size_t *ret, char *buf, MSVCRT_size_t bufsize, int index)
765 char *timezone;
767 switch(index)
769 case 0:
770 timezone = tzname_std;
771 break;
772 case 1:
773 timezone = tzname_dst;
774 break;
775 default:
776 *MSVCRT__errno() = MSVCRT_EINVAL;
777 return MSVCRT_EINVAL;
780 if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
782 *MSVCRT__errno() = MSVCRT_EINVAL;
783 return MSVCRT_EINVAL;
786 *ret = strlen(timezone)+1;
787 if(!buf && !bufsize)
788 return 0;
790 strcpy(buf, timezone);
791 return 0;
794 /*********************************************************************
795 * __p_tzname (MSVCRT.@)
797 char ** CDECL __p__tzname(void)
799 return MSVCRT__tzname;
802 /*********************************************************************
803 * _tzset (MSVCRT.@)
805 void CDECL MSVCRT__tzset(void)
807 tzset();
808 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
809 MSVCRT___daylight = daylight;
810 MSVCRT___timezone = timezone;
811 #else
813 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
814 time_t t;
815 struct tm *tmp;
816 int zone_january, zone_july;
818 _mlock(_TIME_LOCK);
819 t = (time(NULL) / seconds_in_year) * seconds_in_year;
820 tmp = localtime(&t);
821 zone_january = -tmp->tm_gmtoff;
822 t += seconds_in_year / 2;
823 tmp = localtime(&t);
824 zone_july = -tmp->tm_gmtoff;
825 _munlock(_TIME_LOCK);
827 MSVCRT___daylight = (zone_january != zone_july);
828 MSVCRT___timezone = max(zone_january, zone_july);
830 #endif
831 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
832 tzname_std[sizeof(tzname_std) - 1] = '\0';
833 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
834 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
837 /*********************************************************************
838 * strftime (MSVCRT.@)
840 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
841 const struct MSVCRT_tm *mstm )
843 struct tm tm;
845 msvcrt_tm_to_unix( &tm, mstm );
846 return strftime( str, max, format, &tm );
849 /*********************************************************************
850 * wcsftime (MSVCRT.@)
852 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
853 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
855 char *s, *fmt;
856 MSVCRT_size_t len;
858 TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
860 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
861 if (!(fmt = MSVCRT_malloc( len ))) return 0;
862 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
864 if ((s = MSVCRT_malloc( max*4 )))
866 struct tm tm;
867 msvcrt_tm_to_unix( &tm, mstm );
868 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
869 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
870 if (len) len--;
871 MSVCRT_free( s );
873 else len = 0;
875 MSVCRT_free( fmt );
876 return len;
879 /*********************************************************************
880 * asctime (MSVCRT.@)
882 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
884 char bufferA[30];
885 WCHAR bufferW[30];
887 thread_data_t *data = msvcrt_get_thread_data();
888 struct tm tm;
890 msvcrt_tm_to_unix( &tm, mstm );
892 if (!data->asctime_buffer)
893 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
895 #ifdef HAVE_ASCTIME_R
896 asctime_r( &tm, bufferA );
897 #else
898 strcpy( bufferA, asctime(&tm) );
899 #endif
900 MultiByteToWideChar( CP_UNIXCP, 0, bufferA, -1, bufferW, 30 );
901 WideCharToMultiByte( CP_ACP, 0, bufferW, -1, data->asctime_buffer, 30, NULL, NULL );
902 return data->asctime_buffer;
905 /*********************************************************************
906 * _wasctime (MSVCRT.@)
908 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
910 thread_data_t *data = msvcrt_get_thread_data();
911 struct tm tm;
912 char buffer[30];
914 msvcrt_tm_to_unix( &tm, mstm );
916 if (!data->wasctime_buffer)
917 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
918 #ifdef HAVE_ASCTIME_R
919 asctime_r( &tm, buffer );
920 #else
921 strcpy( buffer, asctime(&tm) );
922 #endif
923 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
924 return data->wasctime_buffer;
927 /*********************************************************************
928 * _ctime64 (MSVCRT.@)
930 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
932 struct MSVCRT_tm *t;
933 t = MSVCRT__localtime64( time );
934 if (!t) return NULL;
935 return MSVCRT_asctime( t );
938 /*********************************************************************
939 * _ctime64_s (MSVCRT.@)
941 int CDECL MSVCRT__ctime64_s(char *res, MSVCRT_size_t len, const MSVCRT___time64_t *time)
943 struct MSVCRT_tm *t;
944 if( !MSVCRT_CHECK_PMT( res != NULL ) || !MSVCRT_CHECK_PMT( len >= 26 ) )
946 *MSVCRT__errno() = MSVCRT_EINVAL;
947 return MSVCRT_EINVAL;
949 res[0] = '\0';
950 if( !MSVCRT_CHECK_PMT( time != NULL ) || !MSVCRT_CHECK_PMT( *time > 0 ) )
952 *MSVCRT__errno() = MSVCRT_EINVAL;
953 return MSVCRT_EINVAL;
955 t = MSVCRT__localtime64( time );
956 strcpy( res, MSVCRT_asctime( t ) );
957 return 0;
960 /*********************************************************************
961 * _ctime32 (MSVCRT.@)
963 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
965 struct MSVCRT_tm *t;
966 t = MSVCRT__localtime32( time );
967 if (!t) return NULL;
968 return MSVCRT_asctime( t );
971 /*********************************************************************
972 * _ctime32_s (MSVCRT.@)
974 int CDECL MSVCRT__ctime32_s(char *res, MSVCRT_size_t len, const MSVCRT___time32_t *time)
976 struct MSVCRT_tm *t;
977 if( !MSVCRT_CHECK_PMT( res != NULL ) || !MSVCRT_CHECK_PMT( len >= 26 ) )
979 *MSVCRT__errno() = MSVCRT_EINVAL;
980 return MSVCRT_EINVAL;
982 res[0] = '\0';
983 if( !MSVCRT_CHECK_PMT( time != NULL ) || !MSVCRT_CHECK_PMT( *time > 0 ) )
985 *MSVCRT__errno() = MSVCRT_EINVAL;
986 return MSVCRT_EINVAL;
988 t = MSVCRT__localtime32( time );
989 strcpy( res, MSVCRT_asctime( t ) );
990 return 0;
993 /*********************************************************************
994 * ctime (MSVCRT.@)
996 #ifdef _WIN64
997 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
999 return MSVCRT__ctime64( time );
1001 #else
1002 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
1004 return MSVCRT__ctime32( time );
1006 #endif
1008 /*********************************************************************
1009 * _wctime64 (MSVCRT.@)
1011 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
1013 return MSVCRT__wasctime( MSVCRT__localtime64(time) );
1016 /*********************************************************************
1017 * _wctime32 (MSVCRT.@)
1019 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
1021 return MSVCRT__wasctime( MSVCRT__localtime32(time) );
1024 /*********************************************************************
1025 * _wctime (MSVCRT.@)
1027 #ifdef _WIN64
1028 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
1030 return MSVCRT__wctime64( time );
1032 #else
1033 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
1035 return MSVCRT__wctime32( time );
1037 #endif