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>
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
40 static const int MonthLengths[2][12] =
42 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
43 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
46 static inline int IsLeapYear(int Year)
48 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
51 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
53 memset( dest, 0, sizeof(*dest) );
54 dest->tm_sec = src->tm_sec;
55 dest->tm_min = src->tm_min;
56 dest->tm_hour = src->tm_hour;
57 dest->tm_mday = src->tm_mday;
58 dest->tm_mon = src->tm_mon;
59 dest->tm_year = src->tm_year;
60 dest->tm_wday = src->tm_wday;
61 dest->tm_yday = src->tm_yday;
62 dest->tm_isdst = src->tm_isdst;
65 #define SECSPERDAY 86400
66 /* 1601 to 1970 is 369 years plus 89 leap days */
67 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
68 #define TICKSPERSEC 10000000
69 #define TICKSPERMSEC 10000
70 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
72 /**********************************************************************
75 MSVCRT_time_t CDECL MSVCRT_mktime(struct MSVCRT_tm *t)
80 struct MSVCRT_tm ts, *ptm;
84 /* to prevent arithmetic overflows put constraints on some fields */
85 /* whether the effective date falls in the 1970-2038 time period */
86 /* will be tested later */
87 /* BTW, I have no idea what limits native msvcrt has. */
88 if ( ts.tm_year < 0 || ts.tm_year > 140 ||
89 ts.tm_mon < -840 || ts.tm_mon > 840 ||
90 ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
91 ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
92 ts.tm_min < -29000000 || ts.tm_min > 29000000 )
95 /* normalize the tm month fields */
96 if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
98 int dy = (11 - ts.tm_mon) / 12;
100 ts.tm_mon += dy * 12;
102 /* now calculate a day count from the date
103 * First start counting years from March. This way the leap days
104 * are added at the end of the year, not somewhere in the middle.
105 * Formula's become so much less complicate that way.
106 * To convert: add 12 to the month numbers of Jan and Feb, and
107 * take 1 from the year */
115 cleaps = (3 * (ts.tm_year / 100) + 3) / 4; /* nr of "century leap years"*/
116 day = (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
117 (1959 * ts.tm_mon) / 64 + /* months * daypermonth */
118 ts.tm_mday - /* day of the month */
119 584817 ; /* zero that on 1601-01-01 */
122 /* convert to 100 ns ticks */
123 time = ((((ULONGLONG) day * 24 +
126 ts.tm_sec ) * TICKSPERSEC;
128 lft.dwHighDateTime = (DWORD) (time >> 32);
129 lft.dwLowDateTime = (DWORD) time;
131 LocalFileTimeToFileTime(&lft, &uft);
133 time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
135 if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
137 secs = time - SECS_1601_TO_1970;
138 /* compute tm_wday, tm_yday and renormalize the other fields of the
140 if ((ptm = MSVCRT_localtime( &secs ))) *t = *ptm;
145 /*********************************************************************
146 * localtime (MSVCRT.@)
148 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT_time_t* secs)
150 thread_data_t * const data = msvcrt_get_thread_data();
155 TIME_ZONE_INFORMATION tzinfo;
158 /* time < 0 means a date before midnight of January 1, 1970 */
159 if (*secs < 0) return NULL;
161 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
163 ft.dwHighDateTime = (UINT)(time >> 32);
164 ft.dwLowDateTime = (UINT)time;
166 FileTimeToLocalFileTime(&ft, &lft);
167 FileTimeToSystemTime(&lft, &st);
169 data->time_buffer.tm_sec = st.wSecond;
170 data->time_buffer.tm_min = st.wMinute;
171 data->time_buffer.tm_hour = st.wHour;
172 data->time_buffer.tm_mday = st.wDay;
173 data->time_buffer.tm_year = st.wYear - 1900;
174 data->time_buffer.tm_mon = st.wMonth - 1;
175 data->time_buffer.tm_wday = st.wDayOfWeek;
177 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
178 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
181 data->time_buffer.tm_yday += st.wDay - 1;
183 tzid = GetTimeZoneInformation(&tzinfo);
185 if (tzid == TIME_ZONE_ID_INVALID)
186 data->time_buffer.tm_isdst = -1;
188 data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
190 return &data->time_buffer;
193 /*********************************************************************
196 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT_time_t* secs)
198 thread_data_t * const data = msvcrt_get_thread_data();
203 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
205 ft.dwHighDateTime = (UINT)(time >> 32);
206 ft.dwLowDateTime = (UINT)time;
208 FileTimeToSystemTime(&ft, &st);
210 if (st.wYear < 1970) return NULL;
212 data->time_buffer.tm_sec = st.wSecond;
213 data->time_buffer.tm_min = st.wMinute;
214 data->time_buffer.tm_hour = st.wHour;
215 data->time_buffer.tm_mday = st.wDay;
216 data->time_buffer.tm_year = st.wYear - 1900;
217 data->time_buffer.tm_mon = st.wMonth - 1;
218 data->time_buffer.tm_wday = st.wDayOfWeek;
219 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
220 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
223 data->time_buffer.tm_yday += st.wDay - 1;
224 data->time_buffer.tm_isdst = 0;
226 return &data->time_buffer;
229 /**********************************************************************
230 * _strdate (MSVCRT.@)
232 char* CDECL _strdate(char* date)
234 LPCSTR format = "MM'/'dd'/'yy";
236 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
241 /**********************************************************************
242 * _wstrdate (MSVCRT.@)
244 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
246 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
248 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
253 /*********************************************************************
254 * _strtime (MSVCRT.@)
256 char* CDECL _strtime(char* time)
258 LPCSTR format = "HH':'mm':'ss";
260 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
265 /*********************************************************************
266 * _wstrtime (MSVCRT.@)
268 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
270 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
272 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
277 /*********************************************************************
280 MSVCRT_clock_t CDECL MSVCRT_clock(void)
282 FILETIME ftc, fte, ftk, ftu;
283 ULONGLONG utime, ktime;
285 MSVCRT_clock_t clock;
287 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
289 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
290 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
292 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
297 /*********************************************************************
298 * difftime (MSVCRT.@)
300 double CDECL MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
302 return (double)(time1 - time2);
305 /*********************************************************************
308 void CDECL _ftime(struct MSVCRT__timeb *buf)
310 TIME_ZONE_INFORMATION tzinfo;
314 DWORD tzid = GetTimeZoneInformation(&tzinfo);
315 GetSystemTimeAsFileTime(&ft);
317 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
319 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
320 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
321 buf->timezone = tzinfo.Bias +
322 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
323 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
324 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
327 /*********************************************************************
330 MSVCRT_time_t CDECL MSVCRT_time(MSVCRT_time_t* buf)
332 MSVCRT_time_t curtime;
333 struct MSVCRT__timeb tb;
338 return buf ? *buf = curtime : curtime;
341 /*********************************************************************
342 * _daylight (MSVCRT.@)
344 int MSVCRT___daylight = 0;
346 /*********************************************************************
347 * __p_daylight (MSVCRT.@)
349 int * CDECL MSVCRT___p__daylight(void)
351 return &MSVCRT___daylight;
354 /*********************************************************************
355 * _dstbias (MSVCRT.@)
357 int MSVCRT__dstbias = 0;
359 /*********************************************************************
360 * __p_dstbias (MSVCRT.@)
362 int * CDECL __p__dstbias(void)
364 return &MSVCRT__dstbias;
367 /*********************************************************************
368 * _timezone (MSVCRT.@)
370 long MSVCRT___timezone = 0;
372 /*********************************************************************
373 * __p_timezone (MSVCRT.@)
375 long * CDECL MSVCRT___p__timezone(void)
377 return &MSVCRT___timezone;
380 /*********************************************************************
383 * Some apps (notably Mozilla) insist on writing to these, so the buffer
384 * must be large enough. The size is picked based on observation of
387 static char tzname_std[64] = "";
388 static char tzname_dst[64] = "";
389 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
391 /*********************************************************************
392 * __p_tzname (MSVCRT.@)
394 char ** CDECL __p__tzname(void)
396 return MSVCRT__tzname;
399 /*********************************************************************
402 void CDECL MSVCRT__tzset(void)
405 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
406 MSVCRT___daylight = daylight;
407 MSVCRT___timezone = timezone;
410 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
413 long zone_january, zone_july;
415 t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
417 zone_january = -tmp->tm_gmtoff;
418 t += seconds_in_year / 2;
420 zone_july = -tmp->tm_gmtoff;
421 MSVCRT___daylight = (zone_january != zone_july);
422 MSVCRT___timezone = max(zone_january, zone_july);
425 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
426 tzname_std[sizeof(tzname_std) - 1] = '\0';
427 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
428 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
431 /*********************************************************************
432 * strftime (MSVCRT.@)
434 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
435 const struct MSVCRT_tm *mstm )
439 msvcrt_tm_to_unix( &tm, mstm );
440 return strftime( str, max, format, &tm );
443 /*********************************************************************
444 * wcsftime (MSVCRT.@)
446 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
447 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
452 TRACE("%p %d %s %p\n", str, max, debugstr_w(format), mstm );
454 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
455 if (!(fmt = MSVCRT_malloc( len ))) return 0;
456 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
458 if ((s = MSVCRT_malloc( max*4 )))
461 msvcrt_tm_to_unix( &tm, mstm );
462 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
463 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
473 /*********************************************************************
476 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
478 thread_data_t *data = msvcrt_get_thread_data();
481 msvcrt_tm_to_unix( &tm, mstm );
483 if (!data->asctime_buffer)
484 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
486 /* FIXME: may want to map from Unix codepage to CP_ACP */
487 #ifdef HAVE_ASCTIME_R
488 asctime_r( &tm, data->asctime_buffer );
490 strcpy( data->asctime_buffer, asctime(&tm) );
492 return data->asctime_buffer;
495 /*********************************************************************
496 * _wasctime (MSVCRT.@)
498 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
500 thread_data_t *data = msvcrt_get_thread_data();
504 msvcrt_tm_to_unix( &tm, mstm );
506 if (!data->wasctime_buffer)
507 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
508 #ifdef HAVE_ASCTIME_R
509 asctime_r( &tm, buffer );
511 strcpy( buffer, asctime(&tm) );
513 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
514 return data->wasctime_buffer;
517 /*********************************************************************
520 char * CDECL MSVCRT_ctime(const MSVCRT_time_t *time)
522 return MSVCRT_asctime( MSVCRT_localtime(time) );
525 /*********************************************************************
528 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT_time_t *time)
530 return MSVCRT__wasctime( MSVCRT_localtime(time) );