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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 static const int MonthLengths[2][12] =
41 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
42 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
45 static inline int IsLeapYear(int Year)
47 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
50 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
52 memset( dest, 0, sizeof(*dest) );
53 dest->tm_sec = src->tm_sec;
54 dest->tm_min = src->tm_min;
55 dest->tm_hour = src->tm_hour;
56 dest->tm_mday = src->tm_mday;
57 dest->tm_mon = src->tm_mon;
58 dest->tm_year = src->tm_year;
59 dest->tm_wday = src->tm_wday;
60 dest->tm_yday = src->tm_yday;
61 dest->tm_isdst = src->tm_isdst;
64 #define SECSPERDAY 86400
65 /* 1601 to 1970 is 369 years plus 89 leap days */
66 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
67 #define TICKSPERSEC 10000000
68 #define TICKSPERMSEC 10000
69 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
71 /**********************************************************************
74 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
79 struct MSVCRT_tm ts, *ptm;
83 /* to prevent arithmetic overflows put constraints on some fields */
84 /* whether the effective date falls in the 1970-2038 time period */
85 /* will be tested later */
86 /* BTW, I have no idea what limits native msvcrt has. */
87 if ( ts.tm_year < 0 || ts.tm_year > 140 ||
88 ts.tm_mon < -840 || ts.tm_mon > 840 ||
89 ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
90 ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
91 ts.tm_min < -29000000 || ts.tm_min > 29000000 )
94 /* normalize the tm month fields */
95 if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
97 int dy = (11 - ts.tm_mon) / 12;
101 /* now calculate a day count from the date
102 * First start counting years from March. This way the leap days
103 * are added at the end of the year, not somewhere in the middle.
104 * Formula's become so much less complicate that way.
105 * To convert: add 12 to the month numbers of Jan and Feb, and
106 * take 1 from the year */
114 cleaps = (3 * (ts.tm_year / 100) + 3) / 4; /* nr of "century leap years"*/
115 day = (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
116 (1959 * ts.tm_mon) / 64 + /* months * daypermonth */
117 ts.tm_mday - /* day of the month */
118 584817 ; /* zero that on 1601-01-01 */
121 /* convert to 100 ns ticks */
122 time = ((((ULONGLONG) day * 24 +
125 ts.tm_sec ) * TICKSPERSEC;
127 lft.dwHighDateTime = (DWORD) (time >> 32);
128 lft.dwLowDateTime = (DWORD) time;
130 LocalFileTimeToFileTime(&lft, &uft);
132 time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
134 if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
136 secs = time - SECS_1601_TO_1970;
137 /* compute tm_wday, tm_yday and renormalize the other fields of the
139 if ((ptm = MSVCRT_localtime( &secs ))) *t = *ptm;
144 /*********************************************************************
145 * localtime (MSVCRT.@)
147 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
149 thread_data_t * const data = msvcrt_get_thread_data();
154 TIME_ZONE_INFORMATION tzinfo;
157 /* time < 0 means a date before midnight of January 1, 1970 */
158 if (*secs < 0) return NULL;
160 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
162 ft.dwHighDateTime = (UINT)(time >> 32);
163 ft.dwLowDateTime = (UINT)time;
165 FileTimeToLocalFileTime(&ft, &lft);
166 FileTimeToSystemTime(&lft, &st);
168 data->time_buffer.tm_sec = st.wSecond;
169 data->time_buffer.tm_min = st.wMinute;
170 data->time_buffer.tm_hour = st.wHour;
171 data->time_buffer.tm_mday = st.wDay;
172 data->time_buffer.tm_year = st.wYear - 1900;
173 data->time_buffer.tm_mon = st.wMonth - 1;
174 data->time_buffer.tm_wday = st.wDayOfWeek;
176 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
177 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
180 data->time_buffer.tm_yday += st.wDay - 1;
182 tzid = GetTimeZoneInformation(&tzinfo);
184 if (tzid == TIME_ZONE_ID_INVALID)
185 data->time_buffer.tm_isdst = -1;
187 data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
189 return &data->time_buffer;
192 /*********************************************************************
195 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
197 thread_data_t * const data = msvcrt_get_thread_data();
202 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
204 ft.dwHighDateTime = (UINT)(time >> 32);
205 ft.dwLowDateTime = (UINT)time;
207 FileTimeToSystemTime(&ft, &st);
209 if (st.wYear < 1970) return NULL;
211 data->time_buffer.tm_sec = st.wSecond;
212 data->time_buffer.tm_min = st.wMinute;
213 data->time_buffer.tm_hour = st.wHour;
214 data->time_buffer.tm_mday = st.wDay;
215 data->time_buffer.tm_year = st.wYear - 1900;
216 data->time_buffer.tm_mon = st.wMonth - 1;
217 data->time_buffer.tm_wday = st.wDayOfWeek;
218 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
219 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
222 data->time_buffer.tm_yday += st.wDay - 1;
223 data->time_buffer.tm_isdst = 0;
225 return &data->time_buffer;
228 /**********************************************************************
229 * _strdate (MSVCRT.@)
231 char* _strdate(char* date)
233 LPCSTR format = "MM'/'dd'/'yy";
235 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
240 /**********************************************************************
241 * _wstrdate (MSVCRT.@)
243 MSVCRT_wchar_t* _wstrdate(MSVCRT_wchar_t* date)
245 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
247 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
252 /*********************************************************************
253 * _strtime (MSVCRT.@)
255 char* _strtime(char* time)
257 LPCSTR format = "HH':'mm':'ss";
259 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
264 /*********************************************************************
265 * _wstrtime (MSVCRT.@)
267 MSVCRT_wchar_t* _wstrtime(MSVCRT_wchar_t* time)
269 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
271 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
276 /*********************************************************************
279 MSVCRT_clock_t 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);
296 /*********************************************************************
297 * difftime (MSVCRT.@)
299 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
301 return (double)(time1 - time2);
304 /*********************************************************************
307 void _ftime(struct MSVCRT__timeb *buf)
309 TIME_ZONE_INFORMATION tzinfo;
313 DWORD tzid = GetTimeZoneInformation(&tzinfo);
314 GetSystemTimeAsFileTime(&ft);
316 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
318 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
319 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
320 buf->timezone = tzinfo.Bias +
321 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
322 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
323 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
326 /*********************************************************************
329 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
331 MSVCRT_time_t curtime;
332 struct MSVCRT__timeb tb;
337 return buf ? *buf = curtime : curtime;
340 /*********************************************************************
341 * _daylight (MSVCRT.@)
343 int MSVCRT___daylight = 0;
345 /*********************************************************************
346 * __p_daylight (MSVCRT.@)
348 int *MSVCRT___p__daylight(void)
350 return &MSVCRT___daylight;
353 /*********************************************************************
354 * _dstbias (MSVCRT.@)
356 int MSVCRT__dstbias = 0;
358 /*********************************************************************
359 * __p_dstbias (MSVCRT.@)
361 int *__p__dstbias(void)
363 return &MSVCRT__dstbias;
366 /*********************************************************************
367 * _timezone (MSVCRT.@)
369 long MSVCRT___timezone = 0;
371 /*********************************************************************
372 * __p_timezone (MSVCRT.@)
374 long *MSVCRT___p__timezone(void)
376 return &MSVCRT___timezone;
379 /*********************************************************************
382 * Some apps (notably Mozilla) insist on writing to these, so the buffer
383 * must be large enough. The size is picked based on observation of
386 static char tzname_std[64] = "";
387 static char tzname_dst[64] = "";
388 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
390 /*********************************************************************
391 * __p_tzname (MSVCRT.@)
393 char **__p__tzname(void)
395 return MSVCRT__tzname;
398 /*********************************************************************
401 void MSVCRT__tzset(void)
404 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
405 MSVCRT___daylight = daylight;
406 MSVCRT___timezone = timezone;
409 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
412 long zone_january, zone_july;
414 t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
416 zone_january = -tmp->tm_gmtoff;
417 t += seconds_in_year / 2;
419 zone_july = -tmp->tm_gmtoff;
420 MSVCRT___daylight = (zone_january != zone_july);
421 MSVCRT___timezone = max(zone_january, zone_july);
424 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
425 tzname_std[sizeof(tzname_std) - 1] = '\0';
426 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
427 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
430 /*********************************************************************
431 * strftime (MSVCRT.@)
433 MSVCRT_size_t MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
434 const struct MSVCRT_tm *mstm )
438 msvcrt_tm_to_unix( &tm, mstm );
439 return strftime( str, max, format, &tm );
442 /*********************************************************************
443 * wcsftime (MSVCRT.@)
445 MSVCRT_size_t MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
446 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
451 TRACE("%p %d %s %p\n", str, max, debugstr_w(format), mstm );
453 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
454 if (!(fmt = MSVCRT_malloc( len ))) return 0;
455 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
457 if ((s = MSVCRT_malloc( max*4 )))
460 msvcrt_tm_to_unix( &tm, mstm );
461 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
462 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
472 /*********************************************************************
475 char *MSVCRT_asctime(const struct MSVCRT_tm *mstm)
477 thread_data_t *data = msvcrt_get_thread_data();
480 msvcrt_tm_to_unix( &tm, mstm );
482 if (!data->asctime_buffer)
483 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
485 /* FIXME: may want to map from Unix codepage to CP_ACP */
486 #ifdef HAVE_ASCTIME_R
487 asctime_r( &tm, data->asctime_buffer );
489 strcpy( data->asctime_buffer, asctime(&tm) );
491 return data->asctime_buffer;
494 /*********************************************************************
495 * _wasctime (MSVCRT.@)
497 MSVCRT_wchar_t *MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
499 thread_data_t *data = msvcrt_get_thread_data();
503 msvcrt_tm_to_unix( &tm, mstm );
505 if (!data->wasctime_buffer)
506 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
507 #ifdef HAVE_ASCTIME_R
508 asctime_r( &tm, buffer );
510 strcpy( buffer, asctime(&tm) );
512 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
513 return data->wasctime_buffer;
516 /*********************************************************************
519 char *MSVCRT_ctime(const MSVCRT_time_t *time)
521 return MSVCRT_asctime( MSVCRT_localtime(time) );
524 /*********************************************************************
527 MSVCRT_wchar_t *MSVCRT__wctime(const MSVCRT_time_t *time)
529 return MSVCRT__wasctime( MSVCRT_localtime(time) );