msvcrt: Added _cscanf_s(_l) implementation.
[wine.git] / dlls / msvcrt / time.c
blobd5b02274e8430cc636038704fad8463957261c40
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 #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)
88 /**********************************************************************
89 * _mktime64 (MSVCRT.@)
91 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
93 time_t secs;
94 struct tm tm;
96 msvcrt_tm_to_unix( &tm, mstm );
97 secs = mktime( &tm );
98 unix_tm_to_msvcrt( mstm, &tm );
100 return secs < 0 ? -1 : secs;
103 /**********************************************************************
104 * _mktime32 (MSVCRT.@)
106 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
108 return MSVCRT__mktime64( mstm );
111 /**********************************************************************
112 * mktime (MSVCRT.@)
114 #ifdef _WIN64
115 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
117 return MSVCRT__mktime64( mstm );
119 #else
120 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
122 return MSVCRT__mktime32( mstm );
124 #endif
126 /*********************************************************************
127 * _localtime64 (MSVCRT.@)
129 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
131 struct tm *tm;
132 thread_data_t *data;
133 time_t seconds = *secs;
135 if (seconds < 0) return NULL;
137 _mlock(_TIME_LOCK);
138 if (!(tm = localtime( &seconds))) {
139 _munlock(_TIME_LOCK);
140 return NULL;
143 data = msvcrt_get_thread_data();
144 unix_tm_to_msvcrt( &data->time_buffer, tm );
145 _munlock(_TIME_LOCK);
147 return &data->time_buffer;
150 /*********************************************************************
151 * _localtime32 (MSVCRT.@)
153 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
155 MSVCRT___time64_t secs64 = *secs;
156 return MSVCRT__localtime64( &secs64 );
159 /*********************************************************************
160 * localtime (MSVCRT.@)
162 #ifdef _WIN64
163 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
165 return MSVCRT__localtime64( secs );
167 #else
168 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
170 return MSVCRT__localtime32( secs );
172 #endif
174 /*********************************************************************
175 * _gmtime64 (MSVCRT.@)
177 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t* secs)
179 thread_data_t * const data = msvcrt_get_thread_data();
180 int i;
181 FILETIME ft;
182 SYSTEMTIME st;
184 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
186 ft.dwHighDateTime = (UINT)(time >> 32);
187 ft.dwLowDateTime = (UINT)time;
189 FileTimeToSystemTime(&ft, &st);
191 if (st.wYear < 1970) return NULL;
193 data->time_buffer.tm_sec = st.wSecond;
194 data->time_buffer.tm_min = st.wMinute;
195 data->time_buffer.tm_hour = st.wHour;
196 data->time_buffer.tm_mday = st.wDay;
197 data->time_buffer.tm_year = st.wYear - 1900;
198 data->time_buffer.tm_mon = st.wMonth - 1;
199 data->time_buffer.tm_wday = st.wDayOfWeek;
200 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
201 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
204 data->time_buffer.tm_yday += st.wDay - 1;
205 data->time_buffer.tm_isdst = 0;
207 return &data->time_buffer;
210 /*********************************************************************
211 * _gmtime32 (MSVCRT.@)
213 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
215 MSVCRT___time64_t secs64 = *secs;
216 return MSVCRT__gmtime64( &secs64 );
219 /*********************************************************************
220 * gmtime (MSVCRT.@)
222 #ifdef _WIN64
223 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
225 return MSVCRT__gmtime64( secs );
227 #else
228 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
230 return MSVCRT__gmtime32( secs );
232 #endif
234 /**********************************************************************
235 * _strdate (MSVCRT.@)
237 char* CDECL _strdate(char* date)
239 static const char format[] = "MM'/'dd'/'yy";
241 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
243 return date;
246 /**********************************************************************
247 * _wstrdate (MSVCRT.@)
249 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
251 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
253 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
255 return date;
258 /*********************************************************************
259 * _strtime (MSVCRT.@)
261 char* CDECL _strtime(char* time)
263 static const char format[] = "HH':'mm':'ss";
265 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
267 return time;
270 /*********************************************************************
271 * _wstrtime (MSVCRT.@)
273 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
275 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
277 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
279 return time;
282 /*********************************************************************
283 * clock (MSVCRT.@)
285 MSVCRT_clock_t CDECL MSVCRT_clock(void)
287 FILETIME ftc, fte, ftk, ftu;
288 ULONGLONG utime, ktime;
290 MSVCRT_clock_t clock;
292 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
294 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
295 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
297 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
299 return clock;
302 /*********************************************************************
303 * _difftime64 (MSVCRT.@)
305 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
307 return (double)(time1 - time2);
310 /*********************************************************************
311 * _difftime32 (MSVCRT.@)
313 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
315 return (double)(time1 - time2);
318 /*********************************************************************
319 * difftime (MSVCRT.@)
321 #ifdef _WIN64
322 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
324 return MSVCRT__difftime64( time1, time2 );
326 #else
327 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
329 return MSVCRT__difftime32( time1, time2 );
331 #endif
333 /*********************************************************************
334 * _ftime64 (MSVCRT.@)
336 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
338 TIME_ZONE_INFORMATION tzinfo;
339 FILETIME ft;
340 ULONGLONG time;
342 DWORD tzid = GetTimeZoneInformation(&tzinfo);
343 GetSystemTimeAsFileTime(&ft);
345 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
347 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
348 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
349 buf->timezone = tzinfo.Bias +
350 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
351 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
352 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
355 /*********************************************************************
356 * _ftime32 (MSVCRT.@)
358 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
360 struct MSVCRT___timeb64 buf64;
362 MSVCRT__ftime64( &buf64 );
363 buf->time = buf64.time;
364 buf->millitm = buf64.millitm;
365 buf->timezone = buf64.timezone;
366 buf->dstflag = buf64.dstflag;
369 /*********************************************************************
370 * _ftime (MSVCRT.@)
372 #ifdef _WIN64
373 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
375 return MSVCRT__ftime64( buf );
377 #else
378 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
380 return MSVCRT__ftime32( buf );
382 #endif
384 /*********************************************************************
385 * _time64 (MSVCRT.@)
387 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
389 MSVCRT___time64_t curtime;
390 struct MSVCRT___timeb64 tb;
392 MSVCRT__ftime64(&tb);
394 curtime = tb.time;
395 return buf ? *buf = curtime : curtime;
398 /*********************************************************************
399 * _time32 (MSVCRT.@)
401 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
403 MSVCRT___time32_t curtime;
404 struct MSVCRT___timeb64 tb;
406 MSVCRT__ftime64(&tb);
408 curtime = tb.time;
409 return buf ? *buf = curtime : curtime;
412 /*********************************************************************
413 * time (MSVCRT.@)
415 #ifdef _WIN64
416 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
418 return MSVCRT__time64( buf );
420 #else
421 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
423 return MSVCRT__time32( buf );
425 #endif
427 /*********************************************************************
428 * _daylight (MSVCRT.@)
430 int MSVCRT___daylight = 0;
432 /*********************************************************************
433 * __p_daylight (MSVCRT.@)
435 int * CDECL MSVCRT___p__daylight(void)
437 return &MSVCRT___daylight;
440 /*********************************************************************
441 * _dstbias (MSVCRT.@)
443 int MSVCRT__dstbias = 0;
445 /*********************************************************************
446 * __p_dstbias (MSVCRT.@)
448 int * CDECL __p__dstbias(void)
450 return &MSVCRT__dstbias;
453 /*********************************************************************
454 * _timezone (MSVCRT.@)
456 MSVCRT_long MSVCRT___timezone = 0;
458 /*********************************************************************
459 * __p_timezone (MSVCRT.@)
461 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
463 return &MSVCRT___timezone;
466 /*********************************************************************
467 * _tzname (MSVCRT.@)
468 * NOTES
469 * Some apps (notably Mozilla) insist on writing to these, so the buffer
470 * must be large enough. The size is picked based on observation of
471 * Windows XP.
473 static char tzname_std[64] = "";
474 static char tzname_dst[64] = "";
475 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
477 /*********************************************************************
478 * __p_tzname (MSVCRT.@)
480 char ** CDECL __p__tzname(void)
482 return MSVCRT__tzname;
485 /*********************************************************************
486 * _tzset (MSVCRT.@)
488 void CDECL MSVCRT__tzset(void)
490 tzset();
491 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
492 MSVCRT___daylight = daylight;
493 MSVCRT___timezone = timezone;
494 #else
496 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
497 time_t t;
498 struct tm *tmp;
499 int zone_january, zone_july;
501 _mlock(_TIME_LOCK);
502 t = (time(NULL) / seconds_in_year) * seconds_in_year;
503 tmp = localtime(&t);
504 zone_january = -tmp->tm_gmtoff;
505 t += seconds_in_year / 2;
506 tmp = localtime(&t);
507 zone_july = -tmp->tm_gmtoff;
508 _munlock(_TIME_LOCK);
510 MSVCRT___daylight = (zone_january != zone_july);
511 MSVCRT___timezone = max(zone_january, zone_july);
513 #endif
514 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
515 tzname_std[sizeof(tzname_std) - 1] = '\0';
516 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
517 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
520 /*********************************************************************
521 * strftime (MSVCRT.@)
523 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
524 const struct MSVCRT_tm *mstm )
526 struct tm tm;
528 msvcrt_tm_to_unix( &tm, mstm );
529 return strftime( str, max, format, &tm );
532 /*********************************************************************
533 * wcsftime (MSVCRT.@)
535 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
536 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
538 char *s, *fmt;
539 MSVCRT_size_t len;
541 TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
543 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
544 if (!(fmt = MSVCRT_malloc( len ))) return 0;
545 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
547 if ((s = MSVCRT_malloc( max*4 )))
549 struct tm tm;
550 msvcrt_tm_to_unix( &tm, mstm );
551 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
552 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
553 if (len) len--;
554 MSVCRT_free( s );
556 else len = 0;
558 MSVCRT_free( fmt );
559 return len;
562 /*********************************************************************
563 * asctime (MSVCRT.@)
565 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
567 thread_data_t *data = msvcrt_get_thread_data();
568 struct tm tm;
570 msvcrt_tm_to_unix( &tm, mstm );
572 if (!data->asctime_buffer)
573 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
575 /* FIXME: may want to map from Unix codepage to CP_ACP */
576 #ifdef HAVE_ASCTIME_R
577 asctime_r( &tm, data->asctime_buffer );
578 #else
579 strcpy( data->asctime_buffer, asctime(&tm) );
580 #endif
581 return data->asctime_buffer;
584 /*********************************************************************
585 * _wasctime (MSVCRT.@)
587 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
589 thread_data_t *data = msvcrt_get_thread_data();
590 struct tm tm;
591 char buffer[30];
593 msvcrt_tm_to_unix( &tm, mstm );
595 if (!data->wasctime_buffer)
596 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
597 #ifdef HAVE_ASCTIME_R
598 asctime_r( &tm, buffer );
599 #else
600 strcpy( buffer, asctime(&tm) );
601 #endif
602 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
603 return data->wasctime_buffer;
606 /*********************************************************************
607 * _ctime64 (MSVCRT.@)
609 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
611 struct MSVCRT_tm *t;
612 t = MSVCRT__localtime64( time );
613 if (!t) return NULL;
614 return MSVCRT_asctime( t );
617 /*********************************************************************
618 * _ctime32 (MSVCRT.@)
620 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
622 struct MSVCRT_tm *t;
623 t = MSVCRT__localtime32( time );
624 if (!t) return NULL;
625 return MSVCRT_asctime( t );
628 /*********************************************************************
629 * ctime (MSVCRT.@)
631 #ifdef _WIN64
632 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
634 return MSVCRT__ctime64( time );
636 #else
637 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
639 return MSVCRT__ctime32( time );
641 #endif
643 /*********************************************************************
644 * _wctime64 (MSVCRT.@)
646 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
648 return MSVCRT__wasctime( MSVCRT__localtime64(time) );
651 /*********************************************************************
652 * _wctime32 (MSVCRT.@)
654 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
656 return MSVCRT__wasctime( MSVCRT__localtime32(time) );
659 /*********************************************************************
660 * _wctime (MSVCRT.@)
662 #ifdef _WIN64
663 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
665 return MSVCRT__wctime64( time );
667 #else
668 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
670 return MSVCRT__wctime32( time );
672 #endif