user32: Disable assert() for the mingw build since mingw gets confused trying to...
[wine.git] / dlls / msvcrt / time.c
blob3b7bb71abb5b623d819d54cd803059fcb5892888
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 "winbase.h"
36 #include "winnls.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 static const int MonthLengths[2][12] =
43 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
44 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
47 static inline int IsLeapYear(int Year)
49 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
52 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
54 memset( dest, 0, sizeof(*dest) );
55 dest->tm_sec = src->tm_sec;
56 dest->tm_min = src->tm_min;
57 dest->tm_hour = src->tm_hour;
58 dest->tm_mday = src->tm_mday;
59 dest->tm_mon = src->tm_mon;
60 dest->tm_year = src->tm_year;
61 dest->tm_wday = src->tm_wday;
62 dest->tm_yday = src->tm_yday;
63 dest->tm_isdst = src->tm_isdst;
66 static inline void unix_tm_to_msvcrt( struct MSVCRT_tm *dest, const struct tm *src )
68 memset( dest, 0, sizeof(*dest) );
69 dest->tm_sec = src->tm_sec;
70 dest->tm_min = src->tm_min;
71 dest->tm_hour = src->tm_hour;
72 dest->tm_mday = src->tm_mday;
73 dest->tm_mon = src->tm_mon;
74 dest->tm_year = src->tm_year;
75 dest->tm_wday = src->tm_wday;
76 dest->tm_yday = src->tm_yday;
77 dest->tm_isdst = src->tm_isdst;
80 #define SECSPERDAY 86400
81 /* 1601 to 1970 is 369 years plus 89 leap days */
82 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
83 #define TICKSPERSEC 10000000
84 #define TICKSPERMSEC 10000
85 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
87 /**********************************************************************
88 * _mktime64 (MSVCRT.@)
90 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
92 time_t secs;
93 struct tm tm;
95 msvcrt_tm_to_unix( &tm, mstm );
96 secs = mktime( &tm );
97 unix_tm_to_msvcrt( mstm, &tm );
99 return secs < 0 ? -1 : secs;
102 /**********************************************************************
103 * _mktime32 (MSVCRT.@)
105 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
107 return MSVCRT__mktime64( mstm );
110 /**********************************************************************
111 * mktime (MSVCRT.@)
113 #ifdef _WIN64
114 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
116 return MSVCRT__mktime64( mstm );
118 #else
119 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
121 return MSVCRT__mktime32( mstm );
123 #endif
125 /*********************************************************************
126 * _localtime64 (MSVCRT.@)
128 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
130 struct tm tm;
131 thread_data_t *data;
132 time_t seconds = *secs;
134 if (seconds < 0) return NULL;
136 if (!localtime_r( &seconds, &tm )) return NULL;
138 data = msvcrt_get_thread_data();
139 unix_tm_to_msvcrt( &data->time_buffer, &tm );
141 return &data->time_buffer;
144 /*********************************************************************
145 * _localtime32 (MSVCRT.@)
147 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
149 MSVCRT___time64_t secs64 = *secs;
150 return MSVCRT__localtime64( &secs64 );
153 /*********************************************************************
154 * localtime (MSVCRT.@)
156 #ifdef _WIN64
157 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
159 return MSVCRT__localtime64( secs );
161 #else
162 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
164 return MSVCRT__localtime32( secs );
166 #endif
168 /*********************************************************************
169 * _gmtime64 (MSVCRT.@)
171 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t* secs)
173 thread_data_t * const data = msvcrt_get_thread_data();
174 int i;
175 FILETIME ft;
176 SYSTEMTIME st;
178 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
180 ft.dwHighDateTime = (UINT)(time >> 32);
181 ft.dwLowDateTime = (UINT)time;
183 FileTimeToSystemTime(&ft, &st);
185 if (st.wYear < 1970) return NULL;
187 data->time_buffer.tm_sec = st.wSecond;
188 data->time_buffer.tm_min = st.wMinute;
189 data->time_buffer.tm_hour = st.wHour;
190 data->time_buffer.tm_mday = st.wDay;
191 data->time_buffer.tm_year = st.wYear - 1900;
192 data->time_buffer.tm_mon = st.wMonth - 1;
193 data->time_buffer.tm_wday = st.wDayOfWeek;
194 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
195 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
198 data->time_buffer.tm_yday += st.wDay - 1;
199 data->time_buffer.tm_isdst = 0;
201 return &data->time_buffer;
204 /*********************************************************************
205 * _gmtime32 (MSVCRT.@)
207 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
209 MSVCRT___time64_t secs64 = *secs;
210 return MSVCRT__gmtime64( &secs64 );
213 /*********************************************************************
214 * gmtime (MSVCRT.@)
216 #ifdef _WIN64
217 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
219 return MSVCRT__gmtime64( secs );
221 #else
222 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
224 return MSVCRT__gmtime32( secs );
226 #endif
228 /**********************************************************************
229 * _strdate (MSVCRT.@)
231 char* CDECL _strdate(char* date)
233 static const char format[] = "MM'/'dd'/'yy";
235 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
237 return date;
240 /**********************************************************************
241 * _wstrdate (MSVCRT.@)
243 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
245 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
247 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
249 return date;
252 /*********************************************************************
253 * _strtime (MSVCRT.@)
255 char* CDECL _strtime(char* time)
257 static const char format[] = "HH':'mm':'ss";
259 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
261 return time;
264 /*********************************************************************
265 * _wstrtime (MSVCRT.@)
267 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
269 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
271 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
273 return time;
276 /*********************************************************************
277 * clock (MSVCRT.@)
279 MSVCRT_clock_t CDECL MSVCRT_clock(void)
281 FILETIME ftc, fte, ftk, ftu;
282 ULONGLONG utime, ktime;
284 MSVCRT_clock_t clock;
286 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
288 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
289 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
291 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
293 return clock;
296 /*********************************************************************
297 * _difftime64 (MSVCRT.@)
299 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
301 return (double)(time1 - time2);
304 /*********************************************************************
305 * _difftime32 (MSVCRT.@)
307 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
309 return (double)(time1 - time2);
312 /*********************************************************************
313 * difftime (MSVCRT.@)
315 #ifdef _WIN64
316 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
318 return MSVCRT__difftime64( time1, time2 );
320 #else
321 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
323 return MSVCRT__difftime32( time1, time2 );
325 #endif
327 /*********************************************************************
328 * _ftime64 (MSVCRT.@)
330 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
332 TIME_ZONE_INFORMATION tzinfo;
333 FILETIME ft;
334 ULONGLONG time;
336 DWORD tzid = GetTimeZoneInformation(&tzinfo);
337 GetSystemTimeAsFileTime(&ft);
339 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
341 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
342 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
343 buf->timezone = tzinfo.Bias +
344 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
345 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
346 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
349 /*********************************************************************
350 * _ftime32 (MSVCRT.@)
352 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
354 struct MSVCRT___timeb64 buf64;
356 MSVCRT__ftime64( &buf64 );
357 buf->time = buf64.time;
358 buf->millitm = buf64.millitm;
359 buf->timezone = buf64.timezone;
360 buf->dstflag = buf64.dstflag;
363 /*********************************************************************
364 * _ftime (MSVCRT.@)
366 #ifdef _WIN64
367 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
369 return MSVCRT__ftime64( buf );
371 #else
372 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
374 return MSVCRT__ftime32( buf );
376 #endif
378 /*********************************************************************
379 * _time64 (MSVCRT.@)
381 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
383 MSVCRT___time64_t curtime;
384 struct MSVCRT___timeb64 tb;
386 MSVCRT__ftime64(&tb);
388 curtime = tb.time;
389 return buf ? *buf = curtime : curtime;
392 /*********************************************************************
393 * _time32 (MSVCRT.@)
395 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
397 MSVCRT___time32_t curtime;
398 struct MSVCRT___timeb64 tb;
400 MSVCRT__ftime64(&tb);
402 curtime = tb.time;
403 return buf ? *buf = curtime : curtime;
406 /*********************************************************************
407 * time (MSVCRT.@)
409 #ifdef _WIN64
410 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
412 return MSVCRT__time64( buf );
414 #else
415 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
417 return MSVCRT__time32( buf );
419 #endif
421 /*********************************************************************
422 * _daylight (MSVCRT.@)
424 int MSVCRT___daylight = 0;
426 /*********************************************************************
427 * __p_daylight (MSVCRT.@)
429 int * CDECL MSVCRT___p__daylight(void)
431 return &MSVCRT___daylight;
434 /*********************************************************************
435 * _dstbias (MSVCRT.@)
437 int MSVCRT__dstbias = 0;
439 /*********************************************************************
440 * __p_dstbias (MSVCRT.@)
442 int * CDECL __p__dstbias(void)
444 return &MSVCRT__dstbias;
447 /*********************************************************************
448 * _timezone (MSVCRT.@)
450 MSVCRT_long MSVCRT___timezone = 0;
452 /*********************************************************************
453 * __p_timezone (MSVCRT.@)
455 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
457 return &MSVCRT___timezone;
460 /*********************************************************************
461 * _tzname (MSVCRT.@)
462 * NOTES
463 * Some apps (notably Mozilla) insist on writing to these, so the buffer
464 * must be large enough. The size is picked based on observation of
465 * Windows XP.
467 static char tzname_std[64] = "";
468 static char tzname_dst[64] = "";
469 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
471 /*********************************************************************
472 * __p_tzname (MSVCRT.@)
474 char ** CDECL __p__tzname(void)
476 return MSVCRT__tzname;
479 /*********************************************************************
480 * _tzset (MSVCRT.@)
482 void CDECL MSVCRT__tzset(void)
484 tzset();
485 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
486 MSVCRT___daylight = daylight;
487 MSVCRT___timezone = timezone;
488 #else
490 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
491 time_t t;
492 struct tm *tmp;
493 int zone_january, zone_july;
495 t = (time(NULL) / seconds_in_year) * seconds_in_year;
496 tmp = localtime(&t);
497 zone_january = -tmp->tm_gmtoff;
498 t += seconds_in_year / 2;
499 tmp = localtime(&t);
500 zone_july = -tmp->tm_gmtoff;
501 MSVCRT___daylight = (zone_january != zone_july);
502 MSVCRT___timezone = max(zone_january, zone_july);
504 #endif
505 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
506 tzname_std[sizeof(tzname_std) - 1] = '\0';
507 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
508 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
511 /*********************************************************************
512 * strftime (MSVCRT.@)
514 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
515 const struct MSVCRT_tm *mstm )
517 struct tm tm;
519 msvcrt_tm_to_unix( &tm, mstm );
520 return strftime( str, max, format, &tm );
523 /*********************************************************************
524 * wcsftime (MSVCRT.@)
526 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
527 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
529 char *s, *fmt;
530 MSVCRT_size_t len;
532 TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
534 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
535 if (!(fmt = MSVCRT_malloc( len ))) return 0;
536 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
538 if ((s = MSVCRT_malloc( max*4 )))
540 struct tm tm;
541 msvcrt_tm_to_unix( &tm, mstm );
542 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
543 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
544 if (len) len--;
545 MSVCRT_free( s );
547 else len = 0;
549 MSVCRT_free( fmt );
550 return len;
553 /*********************************************************************
554 * asctime (MSVCRT.@)
556 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
558 thread_data_t *data = msvcrt_get_thread_data();
559 struct tm tm;
561 msvcrt_tm_to_unix( &tm, mstm );
563 if (!data->asctime_buffer)
564 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
566 /* FIXME: may want to map from Unix codepage to CP_ACP */
567 #ifdef HAVE_ASCTIME_R
568 asctime_r( &tm, data->asctime_buffer );
569 #else
570 strcpy( data->asctime_buffer, asctime(&tm) );
571 #endif
572 return data->asctime_buffer;
575 /*********************************************************************
576 * _wasctime (MSVCRT.@)
578 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
580 thread_data_t *data = msvcrt_get_thread_data();
581 struct tm tm;
582 char buffer[30];
584 msvcrt_tm_to_unix( &tm, mstm );
586 if (!data->wasctime_buffer)
587 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
588 #ifdef HAVE_ASCTIME_R
589 asctime_r( &tm, buffer );
590 #else
591 strcpy( buffer, asctime(&tm) );
592 #endif
593 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
594 return data->wasctime_buffer;
597 /*********************************************************************
598 * _ctime64 (MSVCRT.@)
600 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
602 struct MSVCRT_tm *t;
603 t = MSVCRT__localtime64( time );
604 if (!t) return NULL;
605 return MSVCRT_asctime( t );
608 /*********************************************************************
609 * _ctime32 (MSVCRT.@)
611 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
613 struct MSVCRT_tm *t;
614 t = MSVCRT__localtime32( time );
615 if (!t) return NULL;
616 return MSVCRT_asctime( t );
619 /*********************************************************************
620 * ctime (MSVCRT.@)
622 #ifdef _WIN64
623 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
625 return MSVCRT__ctime64( time );
627 #else
628 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
630 return MSVCRT__ctime32( time );
632 #endif
634 /*********************************************************************
635 * _wctime64 (MSVCRT.@)
637 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
639 return MSVCRT__wasctime( MSVCRT__localtime64(time) );
642 /*********************************************************************
643 * _wctime32 (MSVCRT.@)
645 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
647 return MSVCRT__wasctime( MSVCRT__localtime32(time) );
650 /*********************************************************************
651 * _wctime (MSVCRT.@)
653 #ifdef _WIN64
654 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
656 return MSVCRT__wctime64( time );
658 #else
659 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
661 return MSVCRT__wctime32( time );
663 #endif