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>
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 /**********************************************************************
90 MSVCRT_time_t CDECL
MSVCRT_mktime(struct MSVCRT_tm
*mstm
)
95 msvcrt_tm_to_unix( &tm
, mstm
);
97 unix_tm_to_msvcrt( mstm
, &tm
);
99 return secs
< 0 ? -1 : secs
;
102 /*********************************************************************
103 * localtime (MSVCRT.@)
105 struct MSVCRT_tm
* CDECL
MSVCRT_localtime(const MSVCRT_time_t
* secs
)
109 time_t seconds
= *secs
;
111 if (seconds
< 0) return NULL
;
113 if (!localtime_r( &seconds
, &tm
)) return NULL
;
115 data
= msvcrt_get_thread_data();
116 unix_tm_to_msvcrt( &data
->time_buffer
, &tm
);
118 return &data
->time_buffer
;
121 /*********************************************************************
124 struct MSVCRT_tm
* CDECL
MSVCRT_gmtime(const MSVCRT_time_t
* secs
)
126 thread_data_t
* const data
= msvcrt_get_thread_data();
131 ULONGLONG time
= *secs
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
133 ft
.dwHighDateTime
= (UINT
)(time
>> 32);
134 ft
.dwLowDateTime
= (UINT
)time
;
136 FileTimeToSystemTime(&ft
, &st
);
138 if (st
.wYear
< 1970) return NULL
;
140 data
->time_buffer
.tm_sec
= st
.wSecond
;
141 data
->time_buffer
.tm_min
= st
.wMinute
;
142 data
->time_buffer
.tm_hour
= st
.wHour
;
143 data
->time_buffer
.tm_mday
= st
.wDay
;
144 data
->time_buffer
.tm_year
= st
.wYear
- 1900;
145 data
->time_buffer
.tm_mon
= st
.wMonth
- 1;
146 data
->time_buffer
.tm_wday
= st
.wDayOfWeek
;
147 for (i
= data
->time_buffer
.tm_yday
= 0; i
< st
.wMonth
- 1; i
++) {
148 data
->time_buffer
.tm_yday
+= MonthLengths
[IsLeapYear(st
.wYear
)][i
];
151 data
->time_buffer
.tm_yday
+= st
.wDay
- 1;
152 data
->time_buffer
.tm_isdst
= 0;
154 return &data
->time_buffer
;
157 /**********************************************************************
158 * _strdate (MSVCRT.@)
160 char* CDECL
_strdate(char* date
)
162 static const char format
[] = "MM'/'dd'/'yy";
164 GetDateFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
169 /**********************************************************************
170 * _wstrdate (MSVCRT.@)
172 MSVCRT_wchar_t
* CDECL
_wstrdate(MSVCRT_wchar_t
* date
)
174 static const WCHAR format
[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
176 GetDateFormatW(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
181 /*********************************************************************
182 * _strtime (MSVCRT.@)
184 char* CDECL
_strtime(char* time
)
186 static const char format
[] = "HH':'mm':'ss";
188 GetTimeFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, time
, 9);
193 /*********************************************************************
194 * _wstrtime (MSVCRT.@)
196 MSVCRT_wchar_t
* CDECL
_wstrtime(MSVCRT_wchar_t
* time
)
198 static const WCHAR format
[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
200 GetTimeFormatW(LOCALE_NEUTRAL
, 0, NULL
, format
, time
, 9);
205 /*********************************************************************
208 MSVCRT_clock_t CDECL
MSVCRT_clock(void)
210 FILETIME ftc
, fte
, ftk
, ftu
;
211 ULONGLONG utime
, ktime
;
213 MSVCRT_clock_t clock
;
215 GetProcessTimes(GetCurrentProcess(), &ftc
, &fte
, &ftk
, &ftu
);
217 ktime
= ((ULONGLONG
)ftk
.dwHighDateTime
<< 32) | ftk
.dwLowDateTime
;
218 utime
= ((ULONGLONG
)ftu
.dwHighDateTime
<< 32) | ftu
.dwLowDateTime
;
220 clock
= (utime
+ ktime
) / (TICKSPERSEC
/ MSVCRT_CLOCKS_PER_SEC
);
225 /*********************************************************************
226 * difftime (MSVCRT.@)
228 double CDECL
MSVCRT_difftime(MSVCRT_time_t time1
, MSVCRT_time_t time2
)
230 return (double)(time1
- time2
);
233 /*********************************************************************
236 void CDECL
MSVCRT__ftime(struct MSVCRT__timeb
*buf
)
238 TIME_ZONE_INFORMATION tzinfo
;
242 DWORD tzid
= GetTimeZoneInformation(&tzinfo
);
243 GetSystemTimeAsFileTime(&ft
);
245 time
= ((ULONGLONG
)ft
.dwHighDateTime
<< 32) | ft
.dwLowDateTime
;
247 buf
->time
= time
/ TICKSPERSEC
- SECS_1601_TO_1970
;
248 buf
->millitm
= (time
% TICKSPERSEC
) / TICKSPERMSEC
;
249 buf
->timezone
= tzinfo
.Bias
+
250 ( tzid
== TIME_ZONE_ID_STANDARD
? tzinfo
.StandardBias
:
251 ( tzid
== TIME_ZONE_ID_DAYLIGHT
? tzinfo
.DaylightBias
: 0 ));
252 buf
->dstflag
= (tzid
== TIME_ZONE_ID_DAYLIGHT
?1:0);
255 /*********************************************************************
258 MSVCRT_time_t CDECL
MSVCRT_time(MSVCRT_time_t
* buf
)
260 MSVCRT_time_t curtime
;
261 struct MSVCRT__timeb tb
;
266 return buf
? *buf
= curtime
: curtime
;
269 /*********************************************************************
270 * _daylight (MSVCRT.@)
272 int MSVCRT___daylight
= 0;
274 /*********************************************************************
275 * __p_daylight (MSVCRT.@)
277 int * CDECL
MSVCRT___p__daylight(void)
279 return &MSVCRT___daylight
;
282 /*********************************************************************
283 * _dstbias (MSVCRT.@)
285 int MSVCRT__dstbias
= 0;
287 /*********************************************************************
288 * __p_dstbias (MSVCRT.@)
290 int * CDECL
__p__dstbias(void)
292 return &MSVCRT__dstbias
;
295 /*********************************************************************
296 * _timezone (MSVCRT.@)
298 long MSVCRT___timezone
= 0;
300 /*********************************************************************
301 * __p_timezone (MSVCRT.@)
303 long * CDECL
MSVCRT___p__timezone(void)
305 return &MSVCRT___timezone
;
308 /*********************************************************************
311 * Some apps (notably Mozilla) insist on writing to these, so the buffer
312 * must be large enough. The size is picked based on observation of
315 static char tzname_std
[64] = "";
316 static char tzname_dst
[64] = "";
317 char *MSVCRT__tzname
[2] = { tzname_std
, tzname_dst
};
319 /*********************************************************************
320 * __p_tzname (MSVCRT.@)
322 char ** CDECL
__p__tzname(void)
324 return MSVCRT__tzname
;
327 /*********************************************************************
330 void CDECL
MSVCRT__tzset(void)
333 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
334 MSVCRT___daylight
= daylight
;
335 MSVCRT___timezone
= timezone
;
338 static const time_t seconds_in_year
= (365 * 24 + 6) * 3600;
341 long zone_january
, zone_july
;
343 t
= (time(NULL
) / seconds_in_year
) * seconds_in_year
;
345 zone_january
= -tmp
->tm_gmtoff
;
346 t
+= seconds_in_year
/ 2;
348 zone_july
= -tmp
->tm_gmtoff
;
349 MSVCRT___daylight
= (zone_january
!= zone_july
);
350 MSVCRT___timezone
= max(zone_january
, zone_july
);
353 lstrcpynA(tzname_std
, tzname
[0], sizeof(tzname_std
));
354 tzname_std
[sizeof(tzname_std
) - 1] = '\0';
355 lstrcpynA(tzname_dst
, tzname
[1], sizeof(tzname_dst
));
356 tzname_dst
[sizeof(tzname_dst
) - 1] = '\0';
359 /*********************************************************************
360 * strftime (MSVCRT.@)
362 MSVCRT_size_t CDECL
MSVCRT_strftime( char *str
, MSVCRT_size_t max
, const char *format
,
363 const struct MSVCRT_tm
*mstm
)
367 msvcrt_tm_to_unix( &tm
, mstm
);
368 return strftime( str
, max
, format
, &tm
);
371 /*********************************************************************
372 * wcsftime (MSVCRT.@)
374 MSVCRT_size_t CDECL
MSVCRT_wcsftime( MSVCRT_wchar_t
*str
, MSVCRT_size_t max
,
375 const MSVCRT_wchar_t
*format
, const struct MSVCRT_tm
*mstm
)
380 TRACE("%p %ld %s %p\n", str
, max
, debugstr_w(format
), mstm
);
382 len
= WideCharToMultiByte( CP_UNIXCP
, 0, format
, -1, NULL
, 0, NULL
, NULL
);
383 if (!(fmt
= MSVCRT_malloc( len
))) return 0;
384 WideCharToMultiByte( CP_UNIXCP
, 0, format
, -1, fmt
, len
, NULL
, NULL
);
386 if ((s
= MSVCRT_malloc( max
*4 )))
389 msvcrt_tm_to_unix( &tm
, mstm
);
390 if (!strftime( s
, max
*4, fmt
, &tm
)) s
[0] = 0;
391 len
= MultiByteToWideChar( CP_UNIXCP
, 0, s
, -1, str
, max
);
401 /*********************************************************************
404 char * CDECL
MSVCRT_asctime(const struct MSVCRT_tm
*mstm
)
406 thread_data_t
*data
= msvcrt_get_thread_data();
409 msvcrt_tm_to_unix( &tm
, mstm
);
411 if (!data
->asctime_buffer
)
412 data
->asctime_buffer
= MSVCRT_malloc( 30 ); /* ought to be enough */
414 /* FIXME: may want to map from Unix codepage to CP_ACP */
415 #ifdef HAVE_ASCTIME_R
416 asctime_r( &tm
, data
->asctime_buffer
);
418 strcpy( data
->asctime_buffer
, asctime(&tm
) );
420 return data
->asctime_buffer
;
423 /*********************************************************************
424 * _wasctime (MSVCRT.@)
426 MSVCRT_wchar_t
* CDECL
MSVCRT__wasctime(const struct MSVCRT_tm
*mstm
)
428 thread_data_t
*data
= msvcrt_get_thread_data();
432 msvcrt_tm_to_unix( &tm
, mstm
);
434 if (!data
->wasctime_buffer
)
435 data
->wasctime_buffer
= MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t
) ); /* ought to be enough */
436 #ifdef HAVE_ASCTIME_R
437 asctime_r( &tm
, buffer
);
439 strcpy( buffer
, asctime(&tm
) );
441 MultiByteToWideChar( CP_UNIXCP
, 0, buffer
, -1, data
->wasctime_buffer
, 30 );
442 return data
->wasctime_buffer
;
445 /*********************************************************************
448 char * CDECL
MSVCRT_ctime(const MSVCRT_time_t
*time
)
451 t
= MSVCRT_localtime( time
);
453 return MSVCRT_asctime( t
);
456 /*********************************************************************
459 MSVCRT_wchar_t
* CDECL
MSVCRT__wctime(const MSVCRT_time_t
*time
)
461 return MSVCRT__wasctime( MSVCRT_localtime(time
) );