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
27 #define _POSIX_PTHREAD_SEMANTICS /* switch to a 2 arg style asctime_r on Solaris */
29 #ifdef HAVE_SYS_TIMES_H
30 # include <sys/times.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
)
96 msvcrt_tm_to_unix( &tm
, mstm
);
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 /**********************************************************************
115 MSVCRT___time64_t CDECL
MSVCRT_mktime(struct MSVCRT_tm
*mstm
)
117 return MSVCRT__mktime64( mstm
);
120 MSVCRT___time32_t CDECL
MSVCRT_mktime(struct MSVCRT_tm
*mstm
)
122 return MSVCRT__mktime32( mstm
);
126 /*********************************************************************
127 * _localtime64 (MSVCRT.@)
129 struct MSVCRT_tm
* CDECL
MSVCRT__localtime64(const MSVCRT___time64_t
* secs
)
133 time_t seconds
= *secs
;
135 if (seconds
< 0) return NULL
;
138 if (!(tm
= localtime( &seconds
))) {
139 _munlock(_TIME_LOCK
);
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.@)
163 struct MSVCRT_tm
* CDECL
MSVCRT_localtime(const MSVCRT___time64_t
* secs
)
165 return MSVCRT__localtime64( secs
);
168 struct MSVCRT_tm
* CDECL
MSVCRT_localtime(const MSVCRT___time32_t
* secs
)
170 return MSVCRT__localtime32( secs
);
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();
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 /*********************************************************************
223 struct MSVCRT_tm
* CDECL
MSVCRT_gmtime(const MSVCRT___time64_t
* secs
)
225 return MSVCRT__gmtime64( secs
);
228 struct MSVCRT_tm
* CDECL
MSVCRT_gmtime(const MSVCRT___time32_t
* secs
)
230 return MSVCRT__gmtime32( secs
);
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);
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);
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);
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);
282 /*********************************************************************
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
);
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.@)
322 double CDECL
MSVCRT_difftime(MSVCRT___time64_t time1
, MSVCRT___time64_t time2
)
324 return MSVCRT__difftime64( time1
, time2
);
327 double CDECL
MSVCRT_difftime(MSVCRT___time32_t time1
, MSVCRT___time32_t time2
)
329 return MSVCRT__difftime32( time1
, time2
);
333 /*********************************************************************
334 * _ftime64 (MSVCRT.@)
336 void CDECL
MSVCRT__ftime64(struct MSVCRT___timeb64
*buf
)
338 TIME_ZONE_INFORMATION tzinfo
;
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 /*********************************************************************
373 void CDECL
MSVCRT__ftime(struct MSVCRT___timeb64
*buf
)
375 return MSVCRT__ftime64( buf
);
378 void CDECL
MSVCRT__ftime(struct MSVCRT___timeb32
*buf
)
380 return MSVCRT__ftime32( buf
);
384 /*********************************************************************
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
);
395 return buf
? *buf
= curtime
: curtime
;
398 /*********************************************************************
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
);
409 return buf
? *buf
= curtime
: curtime
;
412 /*********************************************************************
416 MSVCRT___time64_t CDECL
MSVCRT_time(MSVCRT___time64_t
* buf
)
418 return MSVCRT__time64( buf
);
421 MSVCRT___time32_t CDECL
MSVCRT_time(MSVCRT___time32_t
* buf
)
423 return MSVCRT__time32( buf
);
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 /*********************************************************************
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
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 /*********************************************************************
488 void CDECL
MSVCRT__tzset(void)
491 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
492 MSVCRT___daylight
= daylight
;
493 MSVCRT___timezone
= timezone
;
496 static const time_t seconds_in_year
= (365 * 24 + 6) * 3600;
499 int zone_january
, zone_july
;
502 t
= (time(NULL
) / seconds_in_year
) * seconds_in_year
;
504 zone_january
= -tmp
->tm_gmtoff
;
505 t
+= seconds_in_year
/ 2;
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
);
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
)
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
)
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 )))
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
);
562 /*********************************************************************
565 char * CDECL
MSVCRT_asctime(const struct MSVCRT_tm
*mstm
)
567 thread_data_t
*data
= msvcrt_get_thread_data();
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
);
579 strcpy( data
->asctime_buffer
, asctime(&tm
) );
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();
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
);
600 strcpy( buffer
, asctime(&tm
) );
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
)
612 t
= MSVCRT__localtime64( time
);
614 return MSVCRT_asctime( t
);
617 /*********************************************************************
618 * _ctime32 (MSVCRT.@)
620 char * CDECL
MSVCRT__ctime32(const MSVCRT___time32_t
*time
)
623 t
= MSVCRT__localtime32( time
);
625 return MSVCRT_asctime( t
);
628 /*********************************************************************
632 char * CDECL
MSVCRT_ctime(const MSVCRT___time64_t
*time
)
634 return MSVCRT__ctime64( time
);
637 char * CDECL
MSVCRT_ctime(const MSVCRT___time32_t
*time
)
639 return MSVCRT__ctime32( time
);
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 /*********************************************************************
663 MSVCRT_wchar_t
* CDECL
MSVCRT__wctime(const MSVCRT___time64_t
*time
)
665 return MSVCRT__wctime64( time
);
668 MSVCRT_wchar_t
* CDECL
MSVCRT__wctime(const MSVCRT___time32_t
*time
)
670 return MSVCRT__wctime32( time
);