schedsvc: Print an error if the service couldn't start monitoring the tasks directory.
[wine.git] / dlls / msvcrt / time.c
blob861b2eddb5e1ab8a30c08f18997f716ef7a65443
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 #include <stdlib.h>
29 #include "msvcrt.h"
30 #include "mtdll.h"
31 #include "winbase.h"
32 #include "winnls.h"
33 #include "winternl.h"
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 static const int MonthLengths[2][12] =
41 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
42 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
45 static inline BOOL IsLeapYear(int Year)
47 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
50 static inline void write_invalid_msvcrt_tm( struct MSVCRT_tm *tm )
52 tm->tm_sec = -1;
53 tm->tm_min = -1;
54 tm->tm_hour = -1;
55 tm->tm_mday = -1;
56 tm->tm_mon = -1;
57 tm->tm_year = -1;
58 tm->tm_wday = -1;
59 tm->tm_yday = -1;
60 tm->tm_isdst = -1;
63 /*********************************************************************
64 * _daylight (MSVCRT.@)
66 int MSVCRT___daylight = 1;
68 /*********************************************************************
69 * _timezone (MSVCRT.@)
71 MSVCRT_long MSVCRT___timezone = 28800;
73 /*********************************************************************
74 * _dstbias (MSVCRT.@)
76 int MSVCRT__dstbias = -3600;
78 /*********************************************************************
79 * _tzname (MSVCRT.@)
80 * NOTES
81 * Some apps (notably Mozilla) insist on writing to these, so the buffer
82 * must be large enough.
84 static char tzname_std[64] = "PST";
85 static char tzname_dst[64] = "PDT";
86 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
88 static TIME_ZONE_INFORMATION tzi = {0};
89 /*********************************************************************
90 * _tzset (MSVCRT.@)
92 void CDECL MSVCRT__tzset(void)
94 char *tz = MSVCRT_getenv("TZ");
95 BOOL error;
97 _mlock(_TIME_LOCK);
98 if(tz && tz[0]) {
99 BOOL neg_zone = FALSE;
101 memset(&tzi, 0, sizeof(tzi));
103 /* Parse timezone information: tzn[+|-]hh[:mm[:ss]][dzn] */
104 lstrcpynA(MSVCRT__tzname[0], tz, 3);
105 tz += 3;
107 if(*tz == '-') {
108 neg_zone = TRUE;
109 tz++;
110 }else if(*tz == '+') {
111 tz++;
113 MSVCRT___timezone = strtol(tz, &tz, 10)*3600;
114 if(*tz == ':') {
115 MSVCRT___timezone += strtol(tz+1, &tz, 10)*60;
116 if(*tz == ':')
117 MSVCRT___timezone += strtol(tz+1, &tz, 10);
119 if(neg_zone)
120 MSVCRT___timezone = -MSVCRT___timezone;
122 MSVCRT___daylight = *tz;
123 lstrcpynA(MSVCRT__tzname[1], tz, 3);
124 }else if(GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID) {
125 MSVCRT___timezone = tzi.Bias*60;
126 if(tzi.StandardDate.wMonth)
127 MSVCRT___timezone += tzi.StandardBias*60;
129 if(tzi.DaylightDate.wMonth) {
130 MSVCRT___daylight = 1;
131 MSVCRT__dstbias = (tzi.DaylightBias-tzi.StandardBias)*60;
132 }else {
133 MSVCRT___daylight = 0;
134 MSVCRT__dstbias = 0;
137 if(!WideCharToMultiByte(CP_ACP, 0, tzi.StandardName, -1, MSVCRT__tzname[0],
138 sizeof(tzname_std), NULL, &error) || error)
139 *MSVCRT__tzname[0] = 0;
140 if(!WideCharToMultiByte(CP_ACP, 0, tzi.DaylightName, -1, MSVCRT__tzname[1],
141 sizeof(tzname_dst), NULL, &error) || error)
142 *MSVCRT__tzname[0] = 0;
144 _munlock(_TIME_LOCK);
147 static void _tzset_init(void)
149 static BOOL init = FALSE;
151 if(!init) {
152 _mlock(_TIME_LOCK);
153 if(!init) {
154 MSVCRT__tzset();
155 init = TRUE;
157 _munlock(_TIME_LOCK);
161 static BOOL is_dst(const SYSTEMTIME *st)
163 TIME_ZONE_INFORMATION tmp;
164 SYSTEMTIME out;
166 if(!MSVCRT___daylight)
167 return FALSE;
169 if(tzi.DaylightDate.wMonth) {
170 tmp = tzi;
171 }else if(st->wYear >= 2007) {
172 memset(&tmp, 0, sizeof(tmp));
173 tmp.StandardDate.wMonth = 11;
174 tmp.StandardDate.wDay = 1;
175 tmp.StandardDate.wHour = 2;
176 tmp.DaylightDate.wMonth = 3;
177 tmp.DaylightDate.wDay = 2;
178 tmp.DaylightDate.wHour = 2;
179 }else {
180 memset(&tmp, 0, sizeof(tmp));
181 tmp.StandardDate.wMonth = 10;
182 tmp.StandardDate.wDay = 5;
183 tmp.StandardDate.wHour = 2;
184 tmp.DaylightDate.wMonth = 4;
185 tmp.DaylightDate.wDay = 1;
186 tmp.DaylightDate.wHour = 2;
189 tmp.Bias = 0;
190 tmp.StandardBias = 0;
191 tmp.DaylightBias = MSVCRT__dstbias/60;
192 if(!SystemTimeToTzSpecificLocalTime(&tmp, st, &out))
193 return FALSE;
195 return memcmp(st, &out, sizeof(SYSTEMTIME));
198 #define SECSPERDAY 86400
199 /* 1601 to 1970 is 369 years plus 89 leap days */
200 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
201 #define TICKSPERSEC 10000000
202 #define TICKSPERMSEC 10000
203 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
205 static MSVCRT___time64_t mktime_helper(struct MSVCRT_tm *mstm, BOOL local)
207 SYSTEMTIME st;
208 FILETIME ft;
209 MSVCRT___time64_t ret = 0;
210 int i;
211 BOOL use_dst = FALSE;
213 ret = mstm->tm_year + mstm->tm_mon/12;
214 mstm->tm_mon %= 12;
215 if(mstm->tm_mon < 0) {
216 mstm->tm_mon += 12;
217 ret--;
220 if(ret<70 || ret>1100) {
221 *MSVCRT__errno() = MSVCRT_EINVAL;
222 return -1;
225 memset(&st, 0, sizeof(SYSTEMTIME));
226 st.wDay = 1;
227 st.wMonth = mstm->tm_mon+1;
228 st.wYear = ret+1900;
230 if(!SystemTimeToFileTime(&st, &ft)) {
231 *MSVCRT__errno() = MSVCRT_EINVAL;
232 return -1;
235 ret = ((MSVCRT___time64_t)ft.dwHighDateTime<<32)+ft.dwLowDateTime;
236 ret += (MSVCRT___time64_t)mstm->tm_sec*TICKSPERSEC;
237 ret += (MSVCRT___time64_t)mstm->tm_min*60*TICKSPERSEC;
238 ret += (MSVCRT___time64_t)mstm->tm_hour*60*60*TICKSPERSEC;
239 ret += (MSVCRT___time64_t)(mstm->tm_mday-1)*SECSPERDAY*TICKSPERSEC;
241 ft.dwLowDateTime = ret & 0xffffffff;
242 ft.dwHighDateTime = ret >> 32;
243 FileTimeToSystemTime(&ft, &st);
245 if(local) {
246 _tzset_init();
247 use_dst = is_dst(&st);
248 if((mstm->tm_isdst<=-1 && use_dst) || (mstm->tm_isdst>=1)) {
249 SYSTEMTIME tmp;
251 ret += (MSVCRT___time64_t)MSVCRT__dstbias*TICKSPERSEC;
253 ft.dwLowDateTime = ret & 0xffffffff;
254 ft.dwHighDateTime = ret >> 32;
255 FileTimeToSystemTime(&ft, &tmp);
257 if(!is_dst(&tmp)) {
258 st = tmp;
259 use_dst = FALSE;
260 }else {
261 use_dst = TRUE;
263 }else if(mstm->tm_isdst==0 && use_dst) {
264 ret -= (MSVCRT___time64_t)MSVCRT__dstbias*TICKSPERSEC;
265 ft.dwLowDateTime = ret & 0xffffffff;
266 ft.dwHighDateTime = ret >> 32;
267 FileTimeToSystemTime(&ft, &st);
268 ret += (MSVCRT___time64_t)MSVCRT__dstbias*TICKSPERSEC;
270 ret += (MSVCRT___time64_t)MSVCRT___timezone*TICKSPERSEC;
273 mstm->tm_sec = st.wSecond;
274 mstm->tm_min = st.wMinute;
275 mstm->tm_hour = st.wHour;
276 mstm->tm_mday = st.wDay;
277 mstm->tm_mon = st.wMonth-1;
278 mstm->tm_year = st.wYear-1900;
279 mstm->tm_wday = st.wDayOfWeek;
280 for(i=mstm->tm_yday=0; i<st.wMonth-1; i++)
281 mstm->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
282 mstm->tm_yday += st.wDay-1;
283 mstm->tm_isdst = use_dst ? 1 : 0;
285 if(ret < TICKS_1601_TO_1970) {
286 *MSVCRT__errno() = MSVCRT_EINVAL;
287 return -1;
289 ret = (ret-TICKS_1601_TO_1970)/TICKSPERSEC;
290 return ret;
293 /**********************************************************************
294 * _mktime64 (MSVCRT.@)
296 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
298 return mktime_helper(mstm, TRUE);
301 /**********************************************************************
302 * _mktime32 (MSVCRT.@)
304 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
306 MSVCRT___time64_t ret = MSVCRT__mktime64( mstm );
307 return ret == (MSVCRT___time32_t)ret ? ret : -1;
310 /**********************************************************************
311 * mktime (MSVCRT.@)
313 #ifdef _WIN64
314 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
316 return MSVCRT__mktime64( mstm );
318 #else
319 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
321 return MSVCRT__mktime32( mstm );
323 #endif
325 /**********************************************************************
326 * _mkgmtime64 (MSVCRT.@)
328 * time->tm_isdst value is ignored
330 MSVCRT___time64_t CDECL MSVCRT__mkgmtime64(struct MSVCRT_tm *time)
332 return mktime_helper(time, FALSE);
335 /**********************************************************************
336 * _mkgmtime32 (MSVCRT.@)
338 MSVCRT___time32_t CDECL MSVCRT__mkgmtime32(struct MSVCRT_tm *time)
340 MSVCRT___time64_t ret = MSVCRT__mkgmtime64(time);
341 return ret == (MSVCRT___time32_t)ret ? ret : -1;
344 /**********************************************************************
345 * _mkgmtime (MSVCRT.@)
347 #ifdef _WIN64
348 MSVCRT___time64_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
350 return MSVCRT__mkgmtime64(time);
352 #else
353 MSVCRT___time32_t CDECL MSVCRT__mkgmtime(struct MSVCRT_tm *time)
355 return MSVCRT__mkgmtime32(time);
357 #endif
359 /*********************************************************************
360 * _localtime64_s (MSVCRT.@)
362 int CDECL _localtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
364 int i;
365 FILETIME ft;
366 SYSTEMTIME st;
367 ULONGLONG time;
369 if (!res || !secs || *secs < 0 || *secs > _MAX__TIME64_T)
371 if (res)
372 write_invalid_msvcrt_tm(res);
374 *MSVCRT__errno() = MSVCRT_EINVAL;
375 return MSVCRT_EINVAL;
378 _tzset_init();
379 time = (*secs - MSVCRT___timezone) * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
381 ft.dwHighDateTime = (UINT)(time >> 32);
382 ft.dwLowDateTime = (UINT)time;
383 FileTimeToSystemTime(&ft, &st);
385 res->tm_isdst = is_dst(&st) ? 1 : 0;
386 if(res->tm_isdst) {
387 time -= MSVCRT__dstbias * (ULONGLONG)TICKSPERSEC;
388 ft.dwHighDateTime = (UINT)(time >> 32);
389 ft.dwLowDateTime = (UINT)time;
390 FileTimeToSystemTime(&ft, &st);
393 res->tm_sec = st.wSecond;
394 res->tm_min = st.wMinute;
395 res->tm_hour = st.wHour;
396 res->tm_mday = st.wDay;
397 res->tm_year = st.wYear - 1900;
398 res->tm_mon = st.wMonth - 1;
399 res->tm_wday = st.wDayOfWeek;
400 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++)
401 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
402 res->tm_yday += st.wDay - 1;
404 return 0;
407 /*********************************************************************
408 * _localtime64 (MSVCRT.@)
410 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
412 thread_data_t *data = msvcrt_get_thread_data();
414 if(!data->time_buffer)
415 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
417 if(_localtime64_s(data->time_buffer, secs))
418 return NULL;
419 return data->time_buffer;
422 /*********************************************************************
423 * _localtime32 (MSVCRT.@)
425 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
427 MSVCRT___time64_t secs64;
429 if(!secs)
430 return NULL;
432 secs64 = *secs;
433 return MSVCRT__localtime64( &secs64 );
436 /*********************************************************************
437 * _localtime32_s (MSVCRT.@)
439 int CDECL _localtime32_s(struct MSVCRT_tm *time, const MSVCRT___time32_t *secs)
441 MSVCRT___time64_t secs64;
443 if (!time || !secs || *secs < 0)
445 if (time)
446 write_invalid_msvcrt_tm(time);
448 *MSVCRT__errno() = MSVCRT_EINVAL;
449 return MSVCRT_EINVAL;
452 secs64 = *secs;
453 return _localtime64_s(time, &secs64);
456 /*********************************************************************
457 * localtime (MSVCRT.@)
459 #ifdef _WIN64
460 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
462 return MSVCRT__localtime64( secs );
464 #else
465 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
467 return MSVCRT__localtime32( secs );
469 #endif
471 /*********************************************************************
472 * _gmtime64 (MSVCRT.@)
474 int CDECL MSVCRT__gmtime64_s(struct MSVCRT_tm *res, const MSVCRT___time64_t *secs)
476 int i;
477 FILETIME ft;
478 SYSTEMTIME st;
479 ULONGLONG time;
481 if (!res || !secs || *secs < 0 || *secs > _MAX__TIME64_T) {
482 if (res) {
483 write_invalid_msvcrt_tm(res);
486 *MSVCRT__errno() = MSVCRT_EINVAL;
487 return MSVCRT_EINVAL;
490 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
492 ft.dwHighDateTime = (UINT)(time >> 32);
493 ft.dwLowDateTime = (UINT)time;
495 FileTimeToSystemTime(&ft, &st);
497 res->tm_sec = st.wSecond;
498 res->tm_min = st.wMinute;
499 res->tm_hour = st.wHour;
500 res->tm_mday = st.wDay;
501 res->tm_year = st.wYear - 1900;
502 res->tm_mon = st.wMonth - 1;
503 res->tm_wday = st.wDayOfWeek;
504 for (i = res->tm_yday = 0; i < st.wMonth - 1; i++) {
505 res->tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
508 res->tm_yday += st.wDay - 1;
509 res->tm_isdst = 0;
511 return 0;
514 /*********************************************************************
515 * _gmtime64 (MSVCRT.@)
517 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t *secs)
519 thread_data_t * const data = msvcrt_get_thread_data();
521 if(!data->time_buffer)
522 data->time_buffer = MSVCRT_malloc(sizeof(struct MSVCRT_tm));
524 if(MSVCRT__gmtime64_s(data->time_buffer, secs))
525 return NULL;
526 return data->time_buffer;
529 /*********************************************************************
530 * _gmtime32_s (MSVCRT.@)
532 int CDECL MSVCRT__gmtime32_s(struct MSVCRT_tm *res, const MSVCRT___time32_t *secs)
534 MSVCRT___time64_t secs64;
536 if(secs) {
537 secs64 = *secs;
538 return MSVCRT__gmtime64_s(res, &secs64);
540 return MSVCRT__gmtime64_s(res, NULL);
543 /*********************************************************************
544 * _gmtime32 (MSVCRT.@)
546 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
548 MSVCRT___time64_t secs64;
550 if(!secs)
551 return NULL;
553 secs64 = *secs;
554 return MSVCRT__gmtime64( &secs64 );
557 /*********************************************************************
558 * gmtime (MSVCRT.@)
560 #ifdef _WIN64
561 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
563 return MSVCRT__gmtime64( secs );
565 #else
566 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
568 return MSVCRT__gmtime32( secs );
570 #endif
572 /**********************************************************************
573 * _strdate (MSVCRT.@)
575 char* CDECL MSVCRT__strdate(char* date)
577 static const char format[] = "MM'/'dd'/'yy";
579 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
581 return date;
584 /**********************************************************************
585 * _strdate_s (MSVCRT.@)
587 int CDECL _strdate_s(char* date, MSVCRT_size_t size)
589 if(date && size)
590 date[0] = '\0';
592 if(!date) {
593 *MSVCRT__errno() = MSVCRT_EINVAL;
594 return MSVCRT_EINVAL;
597 if(size < 9) {
598 *MSVCRT__errno() = MSVCRT_ERANGE;
599 return MSVCRT_ERANGE;
602 MSVCRT__strdate(date);
603 return 0;
606 /**********************************************************************
607 * _wstrdate (MSVCRT.@)
609 MSVCRT_wchar_t* CDECL MSVCRT__wstrdate(MSVCRT_wchar_t* date)
611 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
613 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
615 return date;
618 /**********************************************************************
619 * _wstrdate_s (MSVCRT.@)
621 int CDECL _wstrdate_s(MSVCRT_wchar_t* date, MSVCRT_size_t size)
623 if(date && size)
624 date[0] = '\0';
626 if(!date) {
627 *MSVCRT__errno() = MSVCRT_EINVAL;
628 return MSVCRT_EINVAL;
631 if(size < 9) {
632 *MSVCRT__errno() = MSVCRT_ERANGE;
633 return MSVCRT_ERANGE;
636 MSVCRT__wstrdate(date);
637 return 0;
640 /*********************************************************************
641 * _strtime (MSVCRT.@)
643 char* CDECL MSVCRT__strtime(char* time)
645 static const char format[] = "HH':'mm':'ss";
647 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
649 return time;
652 /*********************************************************************
653 * _strtime_s (MSVCRT.@)
655 int CDECL _strtime_s(char* time, MSVCRT_size_t size)
657 if(time && size)
658 time[0] = '\0';
660 if(!time) {
661 *MSVCRT__errno() = MSVCRT_EINVAL;
662 return MSVCRT_EINVAL;
665 if(size < 9) {
666 *MSVCRT__errno() = MSVCRT_ERANGE;
667 return MSVCRT_ERANGE;
670 MSVCRT__strtime(time);
671 return 0;
674 /*********************************************************************
675 * _wstrtime (MSVCRT.@)
677 MSVCRT_wchar_t* CDECL MSVCRT__wstrtime(MSVCRT_wchar_t* time)
679 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
681 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
683 return time;
686 /*********************************************************************
687 * _wstrtime_s (MSVCRT.@)
689 int CDECL _wstrtime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size)
691 if(time && size)
692 time[0] = '\0';
694 if(!time) {
695 *MSVCRT__errno() = MSVCRT_EINVAL;
696 return MSVCRT_EINVAL;
699 if(size < 9) {
700 *MSVCRT__errno() = MSVCRT_ERANGE;
701 return MSVCRT_ERANGE;
704 MSVCRT__wstrtime(time);
705 return 0;
708 /*********************************************************************
709 * clock (MSVCRT.@)
711 MSVCRT_clock_t CDECL MSVCRT_clock(void)
713 static LONGLONG start_time;
714 LARGE_INTEGER systime;
716 if(!start_time) {
717 KERNEL_USER_TIMES pti;
719 /* while Linux's clock returns user time, Windows' clock
720 * returns wall-clock time from process start. cache the
721 * process start time since it won't change and to avoid
722 * wineserver round-trip overhead */
723 if(NtQueryInformationProcess(GetCurrentProcess(), ProcessTimes, &pti, sizeof(pti), NULL))
724 return -1;
725 start_time = pti.CreateTime.QuadPart;
728 NtQuerySystemTime(&systime);
729 return (systime.QuadPart - start_time) * MSVCRT_CLOCKS_PER_SEC / TICKSPERSEC;
732 /*********************************************************************
733 * _difftime64 (MSVCRT.@)
735 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
737 return (double)(time1 - time2);
740 /*********************************************************************
741 * _difftime32 (MSVCRT.@)
743 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
745 return (double)(time1 - time2);
748 /*********************************************************************
749 * difftime (MSVCRT.@)
751 #ifdef _WIN64
752 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
754 return MSVCRT__difftime64( time1, time2 );
756 #else
757 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
759 return MSVCRT__difftime32( time1, time2 );
761 #endif
763 /*********************************************************************
764 * _ftime64 (MSVCRT.@)
766 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
768 TIME_ZONE_INFORMATION tzinfo;
769 FILETIME ft;
770 ULONGLONG time;
772 DWORD tzid = GetTimeZoneInformation(&tzinfo);
773 GetSystemTimeAsFileTime(&ft);
775 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
777 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
778 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
779 buf->timezone = tzinfo.Bias +
780 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
781 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
782 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
785 /*********************************************************************
786 * _ftime64_s (MSVCRT.@)
788 int CDECL MSVCRT__ftime64_s(struct MSVCRT___timeb64 *buf)
790 if (!MSVCRT_CHECK_PMT( buf != NULL )) return MSVCRT_EINVAL;
791 MSVCRT__ftime64(buf);
792 return 0;
795 /*********************************************************************
796 * _ftime32 (MSVCRT.@)
798 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
800 struct MSVCRT___timeb64 buf64;
802 MSVCRT__ftime64( &buf64 );
803 buf->time = buf64.time;
804 buf->millitm = buf64.millitm;
805 buf->timezone = buf64.timezone;
806 buf->dstflag = buf64.dstflag;
809 /*********************************************************************
810 * _ftime32_s (MSVCRT.@)
812 int CDECL MSVCRT__ftime32_s(struct MSVCRT___timeb32 *buf)
814 if (!MSVCRT_CHECK_PMT( buf != NULL )) return MSVCRT_EINVAL;
815 MSVCRT__ftime32(buf);
816 return 0;
819 /*********************************************************************
820 * _ftime (MSVCRT.@)
822 #ifdef _WIN64
823 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
825 MSVCRT__ftime64( buf );
827 #else
828 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
830 MSVCRT__ftime32( buf );
832 #endif
834 /*********************************************************************
835 * _time64 (MSVCRT.@)
837 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
839 MSVCRT___time64_t curtime;
840 struct MSVCRT___timeb64 tb;
842 MSVCRT__ftime64(&tb);
844 curtime = tb.time;
845 return buf ? *buf = curtime : curtime;
848 /*********************************************************************
849 * _time32 (MSVCRT.@)
851 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
853 MSVCRT___time32_t curtime;
854 struct MSVCRT___timeb64 tb;
856 MSVCRT__ftime64(&tb);
858 curtime = tb.time;
859 return buf ? *buf = curtime : curtime;
862 /*********************************************************************
863 * time (MSVCRT.@)
865 #ifdef _WIN64
866 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
868 return MSVCRT__time64( buf );
870 #else
871 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
873 return MSVCRT__time32( buf );
875 #endif
877 /*********************************************************************
878 * __p__daylight (MSVCRT.@)
880 int * CDECL MSVCRT___p__daylight(void)
882 return &MSVCRT___daylight;
885 /*********************************************************************
886 * __p__dstbias (MSVCRT.@)
888 int * CDECL MSVCRT___p__dstbias(void)
890 return &MSVCRT__dstbias;
893 #if _MSVCR_VER >= 80
894 /*********************************************************************
895 * _get_dstbias (MSVCR80.@)
897 int CDECL MSVCRT__get_dstbias(int *seconds)
899 if (!MSVCRT_CHECK_PMT(seconds != NULL)) return MSVCRT_EINVAL;
900 *seconds = MSVCRT__dstbias;
901 return 0;
903 #endif
905 /*********************************************************************
906 * __p__timezone (MSVCRT.@)
908 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
910 return &MSVCRT___timezone;
913 /*********************************************************************
914 * _get_tzname (MSVCRT.@)
916 int CDECL MSVCRT__get_tzname(MSVCRT_size_t *ret, char *buf, MSVCRT_size_t bufsize, int index)
918 char *timezone;
920 switch(index)
922 case 0:
923 timezone = tzname_std;
924 break;
925 case 1:
926 timezone = tzname_dst;
927 break;
928 default:
929 *MSVCRT__errno() = MSVCRT_EINVAL;
930 return MSVCRT_EINVAL;
933 if(!ret || (!buf && bufsize > 0) || (buf && !bufsize))
935 *MSVCRT__errno() = MSVCRT_EINVAL;
936 return MSVCRT_EINVAL;
939 *ret = strlen(timezone)+1;
940 if(!buf && !bufsize)
941 return 0;
943 strcpy(buf, timezone);
944 return 0;
947 /*********************************************************************
948 * __p_tzname (MSVCRT.@)
950 char ** CDECL __p__tzname(void)
952 return MSVCRT__tzname;
955 static inline BOOL strftime_date(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
956 BOOL alternate, const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
958 char *format;
959 SYSTEMTIME st;
960 MSVCRT_size_t ret;
961 LCID lcid;
963 st.wYear = mstm->tm_year + 1900;
964 st.wMonth = mstm->tm_mon + 1;
965 st.wDayOfWeek = mstm->tm_wday;
966 st.wDay = mstm->tm_mday;
967 st.wHour = mstm->tm_hour;
968 st.wMinute = mstm->tm_min;
969 st.wSecond = mstm->tm_sec;
970 st.wMilliseconds = 0;
972 #if _MSVCR_VER < 110
973 lcid = time_data->lcid;
974 #else
975 lcid = LocaleNameToLCID(time_data->locname, 0);
976 #endif
978 format = alternate ? time_data->str.names.date : time_data->str.names.short_date;
979 ret = GetDateFormatA(lcid, 0, &st, format, NULL, 0);
980 if(ret && ret<max-*pos)
981 ret = GetDateFormatA(lcid, 0, &st, format, str+*pos, max-*pos);
982 if(!ret) {
983 *str = 0;
984 *MSVCRT__errno() = MSVCRT_EINVAL;
985 return FALSE;
986 }else if(ret > max-*pos) {
987 *str = 0;
988 *MSVCRT__errno() = MSVCRT_ERANGE;
989 return FALSE;
991 *pos += ret-1;
992 return TRUE;
995 static inline BOOL strftime_time(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
996 const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
998 SYSTEMTIME st;
999 MSVCRT_size_t ret;
1000 LCID lcid;
1002 st.wYear = mstm->tm_year + 1900;
1003 st.wMonth = mstm->tm_mon + 1;
1004 st.wDayOfWeek = mstm->tm_wday;
1005 st.wDay = mstm->tm_mday;
1006 st.wHour = mstm->tm_hour;
1007 st.wMinute = mstm->tm_min;
1008 st.wSecond = mstm->tm_sec;
1009 st.wMilliseconds = 0;
1011 #if _MSVCR_VER < 110
1012 lcid = time_data->lcid;
1013 #else
1014 lcid = LocaleNameToLCID(time_data->locname, 0);
1015 #endif
1017 ret = GetTimeFormatA(lcid, 0, &st, time_data->str.names.time, NULL, 0);
1018 if(ret && ret<max-*pos)
1019 ret = GetTimeFormatA(lcid, 0, &st, time_data->str.names.time,
1020 str+*pos, max-*pos);
1021 if(!ret) {
1022 *str = 0;
1023 *MSVCRT__errno() = MSVCRT_EINVAL;
1024 return FALSE;
1025 }else if(ret > max-*pos) {
1026 *str = 0;
1027 *MSVCRT__errno() = MSVCRT_ERANGE;
1028 return FALSE;
1030 *pos += ret-1;
1031 return TRUE;
1034 static inline BOOL strftime_str(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max, char *src)
1036 MSVCRT_size_t len = strlen(src);
1037 if(len > max-*pos) {
1038 *str = 0;
1039 *MSVCRT__errno() = MSVCRT_ERANGE;
1040 return FALSE;
1043 memcpy(str+*pos, src, len);
1044 *pos += len;
1045 return TRUE;
1048 static inline BOOL strftime_int(char *str, MSVCRT_size_t *pos, MSVCRT_size_t max,
1049 int src, int prec, int l, int h)
1051 MSVCRT_size_t len;
1053 if(src<l || src>h) {
1054 *str = 0;
1055 *MSVCRT__errno() = MSVCRT_EINVAL;
1056 return FALSE;
1059 len = MSVCRT__snprintf(str+*pos, max-*pos, "%0*d", prec, src);
1060 if(len == -1) {
1061 *str = 0;
1062 *MSVCRT__errno() = MSVCRT_ERANGE;
1063 return FALSE;
1066 *pos += len;
1067 return TRUE;
1070 static MSVCRT_size_t strftime_helper(char *str, MSVCRT_size_t max, const char *format,
1071 const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data, MSVCRT__locale_t loc)
1073 MSVCRT_size_t ret, tmp;
1074 BOOL alternate;
1076 TRACE("(%p %ld %s %p %p)\n", str, max, format, mstm, time_data);
1078 if(!str || !format) {
1079 if(str && max)
1080 *str = 0;
1081 *MSVCRT__errno() = MSVCRT_EINVAL;
1082 return 0;
1085 if(!time_data)
1086 time_data = loc ? loc->locinfo->lc_time_curr : get_locinfo()->lc_time_curr;
1088 for(ret=0; *format && ret<max; format++) {
1089 if(*format != '%') {
1090 if(MSVCRT__isleadbyte_l((unsigned char)*format, loc)) {
1091 str[ret++] = *(format++);
1092 if(ret == max) continue;
1093 if(!str[ret]) goto einval_error;
1095 str[ret++] = *format;
1096 continue;
1099 format++;
1100 if(*format == '#') {
1101 alternate = TRUE;
1102 format++;
1103 }else {
1104 alternate = FALSE;
1107 if(!mstm)
1108 goto einval_error;
1110 switch(*format) {
1111 case 'c':
1112 if(!strftime_date(str, &ret, max, alternate, mstm, time_data))
1113 return 0;
1114 if(ret < max)
1115 str[ret++] = ' ';
1116 if(!strftime_time(str, &ret, max, mstm, time_data))
1117 return 0;
1118 break;
1119 case 'x':
1120 if(!strftime_date(str, &ret, max, alternate, mstm, time_data))
1121 return 0;
1122 break;
1123 case 'X':
1124 if(!strftime_time(str, &ret, max, mstm, time_data))
1125 return 0;
1126 break;
1127 case 'a':
1128 if(mstm->tm_wday<0 || mstm->tm_wday>6)
1129 goto einval_error;
1130 if(!strftime_str(str, &ret, max, time_data->str.names.short_wday[mstm->tm_wday]))
1131 return 0;
1132 break;
1133 case 'A':
1134 if(mstm->tm_wday<0 || mstm->tm_wday>6)
1135 goto einval_error;
1136 if(!strftime_str(str, &ret, max, time_data->str.names.wday[mstm->tm_wday]))
1137 return 0;
1138 break;
1139 case 'b':
1140 if(mstm->tm_mon<0 || mstm->tm_mon>11)
1141 goto einval_error;
1142 if(!strftime_str(str, &ret, max, time_data->str.names.short_mon[mstm->tm_mon]))
1143 return 0;
1144 break;
1145 case 'B':
1146 if(mstm->tm_mon<0 || mstm->tm_mon>11)
1147 goto einval_error;
1148 if(!strftime_str(str, &ret, max, time_data->str.names.mon[mstm->tm_mon]))
1149 return 0;
1150 break;
1151 case 'd':
1152 if(!strftime_int(str, &ret, max, mstm->tm_mday, alternate ? 0 : 2, 0, 31))
1153 return 0;
1154 break;
1155 case 'H':
1156 if(!strftime_int(str, &ret, max, mstm->tm_hour, alternate ? 0 : 2, 0, 23))
1157 return 0;
1158 break;
1159 case 'I':
1160 tmp = mstm->tm_hour;
1161 if(tmp > 12)
1162 tmp -= 12;
1163 else if(!tmp)
1164 tmp = 12;
1165 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 1, 12))
1166 return 0;
1167 break;
1168 case 'j':
1169 if(!strftime_int(str, &ret, max, mstm->tm_yday+1, alternate ? 0 : 3, 1, 366))
1170 return 0;
1171 break;
1172 case 'm':
1173 if(!strftime_int(str, &ret, max, mstm->tm_mon+1, alternate ? 0 : 2, 1, 12))
1174 return 0;
1175 break;
1176 case 'M':
1177 if(!strftime_int(str, &ret, max, mstm->tm_min, alternate ? 0 : 2, 0, 59))
1178 return 0;
1179 break;
1180 case 'p':
1181 if(mstm->tm_hour<0 || mstm->tm_hour>23)
1182 goto einval_error;
1183 if(!strftime_str(str, &ret, max, mstm->tm_hour<12 ?
1184 time_data->str.names.am : time_data->str.names.pm))
1185 return 0;
1186 break;
1187 case 'S':
1188 if(!strftime_int(str, &ret, max, mstm->tm_sec, alternate ? 0 : 2, 0, 59))
1189 return 0;
1190 break;
1191 case 'w':
1192 if(!strftime_int(str, &ret, max, mstm->tm_wday, 0, 0, 6))
1193 return 0;
1194 break;
1195 case 'y':
1196 if(!strftime_int(str, &ret, max, mstm->tm_year%100, alternate ? 0 : 2, 0, 99))
1197 return 0;
1198 break;
1199 case 'Y':
1200 tmp = 1900+mstm->tm_year;
1201 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 4, 0, 9999))
1202 return 0;
1203 break;
1204 case 'z':
1205 case 'Z':
1206 MSVCRT__tzset();
1207 if(MSVCRT__get_tzname(&tmp, str+ret, max-ret, mstm->tm_isdst ? 1 : 0))
1208 return 0;
1209 ret += tmp-1;
1210 break;
1211 case 'U':
1212 case 'W':
1213 if(mstm->tm_wday<0 || mstm->tm_wday>6 || mstm->tm_yday<0 || mstm->tm_yday>365)
1214 goto einval_error;
1215 if(*format == 'U')
1216 tmp = mstm->tm_wday;
1217 else if(!mstm->tm_wday)
1218 tmp = 6;
1219 else
1220 tmp = mstm->tm_wday-1;
1222 tmp = mstm->tm_yday/7 + (tmp<=mstm->tm_yday%7);
1223 if(!strftime_int(str, &ret, max, tmp, alternate ? 0 : 2, 0, 53))
1224 return 0;
1225 break;
1226 case '%':
1227 str[ret++] = '%';
1228 break;
1229 default:
1230 WARN("unknown format %c\n", *format);
1231 goto einval_error;
1235 if(ret == max) {
1236 if(max)
1237 *str = 0;
1238 *MSVCRT__errno() = MSVCRT_ERANGE;
1239 return 0;
1242 str[ret] = 0;
1243 return ret;
1245 einval_error:
1246 *str = 0;
1247 *MSVCRT__errno() = MSVCRT_EINVAL;
1248 return 0;
1251 #if _MSVCR_VER >= 80
1252 /********************************************************************
1253 * _strftime_l (MSVCR80.@)
1255 MSVCRT_size_t CDECL MSVCRT__strftime_l( char *str, MSVCRT_size_t max, const char *format,
1256 const struct MSVCRT_tm *mstm, MSVCRT__locale_t loc )
1258 return strftime_helper(str, max, format, mstm, NULL, loc);
1260 #endif
1262 /*********************************************************************
1263 * _Strftime (MSVCRT.@)
1265 MSVCRT_size_t CDECL _Strftime(char *str, MSVCRT_size_t max, const char *format,
1266 const struct MSVCRT_tm *mstm, MSVCRT___lc_time_data *time_data)
1268 return strftime_helper(str, max, format, mstm, time_data, NULL);
1271 /*********************************************************************
1272 * strftime (MSVCRT.@)
1274 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
1275 const struct MSVCRT_tm *mstm )
1277 return strftime_helper(str, max, format, mstm, NULL, NULL);
1280 static MSVCRT_size_t wcsftime_helper( MSVCRT_wchar_t *str, MSVCRT_size_t max,
1281 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm,
1282 MSVCRT___lc_time_data *time_data, MSVCRT__locale_t loc )
1284 char *s, *fmt;
1285 MSVCRT_size_t len;
1287 TRACE("%p %ld %s %p %p %p\n", str, max, debugstr_w(format), mstm, time_data, loc);
1289 len = MSVCRT__wcstombs_l( NULL, format, 0, loc ) + 1;
1290 if (!(fmt = MSVCRT_malloc( len ))) return 0;
1291 MSVCRT__wcstombs_l(fmt, format, len, loc);
1293 if ((s = MSVCRT_malloc( max*4 )))
1295 if (!strftime_helper( s, max*4, fmt, mstm, time_data, loc )) s[0] = 0;
1296 len = MSVCRT__mbstowcs_l( str, s, max, loc );
1297 MSVCRT_free( s );
1299 else len = 0;
1301 MSVCRT_free( fmt );
1302 return len;
1305 /*********************************************************************
1306 * _wcsftime_l (MSVCRT.@)
1308 MSVCRT_size_t CDECL MSVCRT__wcsftime_l( MSVCRT_wchar_t *str, MSVCRT_size_t max,
1309 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm, MSVCRT__locale_t loc )
1311 return wcsftime_helper(str, max, format, mstm, NULL, loc);
1314 /*********************************************************************
1315 * wcsftime (MSVCRT.@)
1317 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
1318 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
1320 return wcsftime_helper(str, max, format, mstm, NULL, NULL);
1323 #if _MSVCR_VER >= 110
1324 /*********************************************************************
1325 * _Wcsftime (MSVCR110.@)
1327 MSVCRT_size_t CDECL _Wcsftime(MSVCRT_wchar_t *str, MSVCRT_size_t max,
1328 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm,
1329 MSVCRT___lc_time_data *time_data)
1331 return wcsftime_helper(str, max, format, mstm, time_data, NULL);
1333 #endif
1335 static char* asctime_buf(char *buf, const struct MSVCRT_tm *mstm)
1337 static const char wday[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
1338 static const char month[12][4] = {"Jan", "Feb", "Mar", "Apr", "May",
1339 "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
1341 if (!mstm || mstm->tm_sec<0 || mstm->tm_sec>59
1342 || mstm->tm_min<0 || mstm->tm_min>59
1343 || mstm->tm_hour<0 || mstm->tm_hour>23
1344 || mstm->tm_mon<0 || mstm->tm_mon>11
1345 || mstm->tm_wday<0 || mstm->tm_wday>6
1346 || mstm->tm_year<0 || mstm->tm_mday<0
1347 || mstm->tm_mday>MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon]) {
1348 *MSVCRT__errno() = MSVCRT_EINVAL;
1349 return NULL;
1352 #if _MSVCR_VER>=140
1353 /* C89 (4.12.3.1) uses space-padding for day of month. */
1354 MSVCRT__snprintf(buf, 26, "%s %s %2d %02d:%02d:%02d %c%03d\n", wday[mstm->tm_wday],
1355 month[mstm->tm_mon], mstm->tm_mday, mstm->tm_hour, mstm->tm_min,
1356 mstm->tm_sec, '1'+(mstm->tm_year+900)/1000, (900+mstm->tm_year)%1000);
1357 #else
1358 MSVCRT__snprintf(buf, 26, "%s %s %02d %02d:%02d:%02d %c%03d\n", wday[mstm->tm_wday],
1359 month[mstm->tm_mon], mstm->tm_mday, mstm->tm_hour, mstm->tm_min,
1360 mstm->tm_sec, '1'+(mstm->tm_year+900)/1000, (900+mstm->tm_year)%1000);
1361 #endif
1362 return buf;
1365 /*********************************************************************
1366 * asctime (MSVCRT.@)
1368 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
1370 thread_data_t *data = msvcrt_get_thread_data();
1372 /* asctime returns date in format that always has exactly 26 characters */
1373 if (!data->asctime_buffer) {
1374 data->asctime_buffer = MSVCRT_malloc(26);
1375 if (!data->asctime_buffer) {
1376 *MSVCRT__errno() = MSVCRT_ENOMEM;
1377 return NULL;
1381 return asctime_buf(data->asctime_buffer, mstm);
1384 /*********************************************************************
1385 * asctime_s (MSVCRT.@)
1387 int CDECL MSVCRT_asctime_s(char* time, MSVCRT_size_t size, const struct MSVCRT_tm *mstm)
1389 if (!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1390 if (size) time[0] = 0;
1391 if (!MSVCRT_CHECK_PMT(size >= 26)) return MSVCRT_EINVAL;
1392 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return MSVCRT_EINVAL;
1393 if (!MSVCRT_CHECK_PMT(mstm->tm_sec >= 0 && mstm->tm_sec < 60)) return MSVCRT_EINVAL;
1394 if (!MSVCRT_CHECK_PMT(mstm->tm_min >= 0 && mstm->tm_min < 60)) return MSVCRT_EINVAL;
1395 if (!MSVCRT_CHECK_PMT(mstm->tm_hour >= 0 && mstm->tm_hour < 24)) return MSVCRT_EINVAL;
1396 if (!MSVCRT_CHECK_PMT(mstm->tm_mon >= 0 && mstm->tm_mon < 12)) return MSVCRT_EINVAL;
1397 if (!MSVCRT_CHECK_PMT(mstm->tm_wday >= 0 && mstm->tm_wday < 7)) return MSVCRT_EINVAL;
1398 if (!MSVCRT_CHECK_PMT(mstm->tm_year >= 0)) return MSVCRT_EINVAL;
1399 if (!MSVCRT_CHECK_PMT(mstm->tm_mday >= 0)) return MSVCRT_EINVAL;
1400 if (!MSVCRT_CHECK_PMT(mstm->tm_mday <= MonthLengths[IsLeapYear(1900+mstm->tm_year)][mstm->tm_mon])) return MSVCRT_EINVAL;
1402 asctime_buf(time, mstm);
1403 return 0;
1406 /*********************************************************************
1407 * _wasctime (MSVCRT.@)
1409 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
1411 thread_data_t *data = msvcrt_get_thread_data();
1412 char buffer[26];
1414 if(!data->wasctime_buffer) {
1415 data->wasctime_buffer = MSVCRT_malloc(26*sizeof(MSVCRT_wchar_t));
1416 if(!data->wasctime_buffer) {
1417 *MSVCRT__errno() = MSVCRT_ENOMEM;
1418 return NULL;
1422 if(!asctime_buf(buffer, mstm))
1423 return NULL;
1425 MultiByteToWideChar(CP_ACP, 0, buffer, -1, data->wasctime_buffer, 26);
1426 return data->wasctime_buffer;
1429 /*********************************************************************
1430 * _wasctime_s (MSVCRT.@)
1432 int CDECL MSVCRT__wasctime_s(MSVCRT_wchar_t* time, MSVCRT_size_t size, const struct MSVCRT_tm *mstm)
1434 char buffer[26];
1435 int ret;
1437 if (!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1438 if (size) time[0] = 0;
1439 if (!MSVCRT_CHECK_PMT(size >= 26)) return MSVCRT_EINVAL;
1440 if (!MSVCRT_CHECK_PMT(mstm != NULL)) return MSVCRT_EINVAL;
1442 ret = MSVCRT_asctime_s(buffer, sizeof(buffer), mstm);
1443 if(ret)
1444 return ret;
1445 MultiByteToWideChar(CP_ACP, 0, buffer, -1, time, size);
1446 return 0;
1449 /*********************************************************************
1450 * _ctime64 (MSVCRT.@)
1452 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
1454 struct MSVCRT_tm *t;
1455 t = MSVCRT__localtime64( time );
1456 if (!t) return NULL;
1457 return MSVCRT_asctime( t );
1460 /*********************************************************************
1461 * _ctime64_s (MSVCRT.@)
1463 int CDECL MSVCRT__ctime64_s(char *res, MSVCRT_size_t len, const MSVCRT___time64_t *time)
1465 struct MSVCRT_tm *t;
1467 if (!MSVCRT_CHECK_PMT( res != NULL )) return MSVCRT_EINVAL;
1468 if (!MSVCRT_CHECK_PMT( len >= 26 )) return MSVCRT_EINVAL;
1469 res[0] = '\0';
1470 if (!MSVCRT_CHECK_PMT( time != NULL )) return MSVCRT_EINVAL;
1471 if (!MSVCRT_CHECK_PMT( *time > 0 )) return MSVCRT_EINVAL;
1473 t = MSVCRT__localtime64( time );
1474 strcpy( res, MSVCRT_asctime( t ) );
1475 return 0;
1478 /*********************************************************************
1479 * _ctime32 (MSVCRT.@)
1481 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
1483 struct MSVCRT_tm *t;
1484 t = MSVCRT__localtime32( time );
1485 if (!t) return NULL;
1486 return MSVCRT_asctime( t );
1489 /*********************************************************************
1490 * _ctime32_s (MSVCRT.@)
1492 int CDECL MSVCRT__ctime32_s(char *res, MSVCRT_size_t len, const MSVCRT___time32_t *time)
1494 struct MSVCRT_tm *t;
1496 if (!MSVCRT_CHECK_PMT( res != NULL )) return MSVCRT_EINVAL;
1497 if (!MSVCRT_CHECK_PMT( len >= 26 )) return MSVCRT_EINVAL;
1498 res[0] = '\0';
1499 if (!MSVCRT_CHECK_PMT( time != NULL )) return MSVCRT_EINVAL;
1500 if (!MSVCRT_CHECK_PMT( *time > 0 )) return MSVCRT_EINVAL;
1502 t = MSVCRT__localtime32( time );
1503 strcpy( res, MSVCRT_asctime( t ) );
1504 return 0;
1507 /*********************************************************************
1508 * ctime (MSVCRT.@)
1510 #ifdef _WIN64
1511 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
1513 return MSVCRT__ctime64( time );
1515 #else
1516 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
1518 return MSVCRT__ctime32( time );
1520 #endif
1522 /*********************************************************************
1523 * _wctime64 (MSVCRT.@)
1525 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
1527 return MSVCRT__wasctime( MSVCRT__localtime64(time) );
1530 /*********************************************************************
1531 * _wctime32 (MSVCRT.@)
1533 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
1535 return MSVCRT__wasctime( MSVCRT__localtime32(time) );
1538 /*********************************************************************
1539 * _wctime (MSVCRT.@)
1541 #ifdef _WIN64
1542 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
1544 return MSVCRT__wctime64( time );
1546 #else
1547 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
1549 return MSVCRT__wctime32( time );
1551 #endif
1553 /*********************************************************************
1554 * _wctime64_s (MSVCRT.@)
1556 int CDECL MSVCRT__wctime64_s(MSVCRT_wchar_t *buf,
1557 MSVCRT_size_t size, const MSVCRT___time64_t *time)
1559 struct MSVCRT_tm tm;
1560 int ret;
1562 if(!MSVCRT_CHECK_PMT(buf != NULL)) return MSVCRT_EINVAL;
1563 if(!MSVCRT_CHECK_PMT(size != 0)) return MSVCRT_EINVAL;
1564 buf[0] = 0;
1565 if(!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1566 if(!MSVCRT_CHECK_PMT(*time >= 0)) return MSVCRT_EINVAL;
1567 if(!MSVCRT_CHECK_PMT(*time <= _MAX__TIME64_T)) return MSVCRT_EINVAL;
1569 ret = _localtime64_s(&tm, time);
1570 if(ret != 0)
1571 return ret;
1573 return MSVCRT__wasctime_s(buf, size, &tm);
1576 /*********************************************************************
1577 * _wctime32_s (MSVCRT.@)
1579 int CDECL MSVCRT__wctime32_s(MSVCRT_wchar_t *buf, MSVCRT_size_t size,
1580 const MSVCRT___time32_t *time)
1582 struct MSVCRT_tm tm;
1583 int ret;
1585 if(!MSVCRT_CHECK_PMT(buf != NULL)) return MSVCRT_EINVAL;
1586 if(!MSVCRT_CHECK_PMT(size != 0)) return MSVCRT_EINVAL;
1587 buf[0] = 0;
1588 if(!MSVCRT_CHECK_PMT(time != NULL)) return MSVCRT_EINVAL;
1589 if(!MSVCRT_CHECK_PMT(*time >= 0)) return MSVCRT_EINVAL;
1591 ret = _localtime32_s(&tm, time);
1592 if(ret != 0)
1593 return ret;
1595 return MSVCRT__wasctime_s(buf, size, &tm);
1598 #if _MSVCR_VER >= 80
1600 /*********************************************************************
1601 * _get_timezone (MSVCR80.@)
1603 int CDECL _get_timezone(LONG *timezone)
1605 if(!MSVCRT_CHECK_PMT(timezone != NULL)) return MSVCRT_EINVAL;
1607 *timezone = MSVCRT___timezone;
1608 return 0;
1611 /*********************************************************************
1612 * _get_daylight (MSVCR80.@)
1614 int CDECL _get_daylight(int *hours)
1616 if(!MSVCRT_CHECK_PMT(hours != NULL)) return MSVCRT_EINVAL;
1618 *hours = MSVCRT___daylight;
1619 return 0;
1622 #endif /* _MSVCR_VER >= 80 */
1624 #if _MSVCR_VER >= 140
1626 #define TIME_UTC 1
1628 struct _timespec32
1630 MSVCRT___time32_t tv_sec;
1631 LONG tv_nsec;
1634 struct _timespec64
1636 MSVCRT___time64_t tv_sec;
1637 LONG tv_nsec;
1640 /*********************************************************************
1641 * _timespec64_get (UCRTBASE.@)
1643 int CDECL _timespec64_get(struct _timespec64 *ts, int base)
1645 ULONGLONG time;
1646 FILETIME ft;
1648 if(!MSVCRT_CHECK_PMT(ts != NULL)) return 0;
1649 if(base != TIME_UTC) return 0;
1651 GetSystemTimePreciseAsFileTime(&ft);
1652 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
1654 ts->tv_sec = time / TICKSPERSEC - SECS_1601_TO_1970;
1655 ts->tv_nsec = time % TICKSPERSEC * 100;
1656 return base;
1659 /*********************************************************************
1660 * _timespec32_get (UCRTBASE.@)
1662 int CDECL _timespec32_get(struct _timespec32 *ts, int base)
1664 struct _timespec64 ts64;
1666 if(_timespec64_get(&ts64, base) != base)
1667 return 0;
1668 if(ts64.tv_sec != (MSVCRT___time32_t)ts64.tv_sec)
1669 return 0;
1671 ts->tv_sec = ts64.tv_sec;
1672 ts->tv_nsec = ts64.tv_nsec;
1673 return base;
1675 #endif /* _MSVCR_VER >= 140 */