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;
156 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
158 ft.dwHighDateTime = (UINT)(time >> 32);
159 ft.dwLowDateTime = (UINT)time;
161 FileTimeToLocalFileTime(&ft, &lft);
162 FileTimeToSystemTime(&lft, &st);
164 if (st.wYear < 1970) return NULL;
166 data->time_buffer.tm_sec = st.wSecond;
167 data->time_buffer.tm_min = st.wMinute;
168 data->time_buffer.tm_hour = st.wHour;
169 data->time_buffer.tm_mday = st.wDay;
170 data->time_buffer.tm_year = st.wYear - 1900;
171 data->time_buffer.tm_mon = st.wMonth - 1;
172 data->time_buffer.tm_wday = st.wDayOfWeek;
174 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
175 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
178 data->time_buffer.tm_yday += st.wDay - 1;
180 tzid = GetTimeZoneInformation(&tzinfo);
182 if (tzid == TIME_ZONE_ID_INVALID)
183 data->time_buffer.tm_isdst = -1;
185 data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
187 return &data->time_buffer;
190 /*********************************************************************
193 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
195 thread_data_t * const data = msvcrt_get_thread_data();
200 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
202 ft.dwHighDateTime = (UINT)(time >> 32);
203 ft.dwLowDateTime = (UINT)time;
205 FileTimeToSystemTime(&ft, &st);
207 if (st.wYear < 1970) return NULL;
209 data->time_buffer.tm_sec = st.wSecond;
210 data->time_buffer.tm_min = st.wMinute;
211 data->time_buffer.tm_hour = st.wHour;
212 data->time_buffer.tm_mday = st.wDay;
213 data->time_buffer.tm_year = st.wYear - 1900;
214 data->time_buffer.tm_mon = st.wMonth - 1;
215 data->time_buffer.tm_wday = st.wDayOfWeek;
216 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
217 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
220 data->time_buffer.tm_yday += st.wDay - 1;
221 data->time_buffer.tm_isdst = 0;
223 return &data->time_buffer;
226 /**********************************************************************
227 * _strdate (MSVCRT.@)
229 char* _strdate(char* date)
231 LPCSTR format = "MM'/'dd'/'yy";
233 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
238 /**********************************************************************
239 * _wstrdate (MSVCRT.@)
241 MSVCRT_wchar_t* _wstrdate(MSVCRT_wchar_t* date)
243 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
245 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
250 /*********************************************************************
251 * _strtime (MSVCRT.@)
253 char* _strtime(char* time)
255 LPCSTR format = "HH':'mm':'ss";
257 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
262 /*********************************************************************
263 * _wstrtime (MSVCRT.@)
265 MSVCRT_wchar_t* _wstrtime(MSVCRT_wchar_t* time)
267 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
269 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
274 /*********************************************************************
277 MSVCRT_clock_t MSVCRT_clock(void)
279 FILETIME ftc, fte, ftk, ftu;
280 ULONGLONG utime, ktime;
282 MSVCRT_clock_t clock;
284 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
286 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
287 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
289 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
294 /*********************************************************************
295 * difftime (MSVCRT.@)
297 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
299 return (double)(time1 - time2);
302 /*********************************************************************
305 void _ftime(struct MSVCRT__timeb *buf)
307 TIME_ZONE_INFORMATION tzinfo;
311 DWORD tzid = GetTimeZoneInformation(&tzinfo);
312 GetSystemTimeAsFileTime(&ft);
314 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
316 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
317 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
318 buf->timezone = tzinfo.Bias +
319 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
320 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
321 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
324 /*********************************************************************
327 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
329 MSVCRT_time_t curtime;
330 struct MSVCRT__timeb tb;
335 return buf ? *buf = curtime : curtime;
338 /*********************************************************************
339 * _daylight (MSVCRT.@)
341 int MSVCRT___daylight = 0;
343 /*********************************************************************
344 * __p_daylight (MSVCRT.@)
346 int *MSVCRT___p__daylight(void)
348 return &MSVCRT___daylight;
351 /*********************************************************************
352 * _dstbias (MSVCRT.@)
354 int MSVCRT__dstbias = 0;
356 /*********************************************************************
357 * __p_dstbias (MSVCRT.@)
359 int *__p__dstbias(void)
361 return &MSVCRT__dstbias;
364 /*********************************************************************
365 * _timezone (MSVCRT.@)
367 long MSVCRT___timezone = 0;
369 /*********************************************************************
370 * __p_timezone (MSVCRT.@)
372 long *MSVCRT___p__timezone(void)
374 return &MSVCRT___timezone;
377 /*********************************************************************
380 * Some apps (notably Mozilla) insist on writing to these, so the buffer
381 * must be large enough. The size is picked based on observation of
384 static char tzname_std[64] = "";
385 static char tzname_dst[64] = "";
386 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
388 /*********************************************************************
389 * __p_tzname (MSVCRT.@)
391 char **__p__tzname(void)
393 return MSVCRT__tzname;
396 /*********************************************************************
399 void MSVCRT__tzset(void)
402 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
403 MSVCRT___daylight = daylight;
404 MSVCRT___timezone = timezone;
407 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
410 long zone_january, zone_july;
412 t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
414 zone_january = -tmp->tm_gmtoff;
415 t += seconds_in_year / 2;
417 zone_july = -tmp->tm_gmtoff;
418 MSVCRT___daylight = (zone_january != zone_july);
419 MSVCRT___timezone = max(zone_january, zone_july);
422 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
423 tzname_std[sizeof(tzname_std) - 1] = '\0';
424 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
425 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
428 /*********************************************************************
429 * strftime (MSVCRT.@)
431 MSVCRT_size_t MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
432 const struct MSVCRT_tm *mstm )
436 msvcrt_tm_to_unix( &tm, mstm );
437 return strftime( str, max, format, &tm );
440 /*********************************************************************
443 char *MSVCRT_asctime(const struct MSVCRT_tm *mstm)
445 thread_data_t *data = msvcrt_get_thread_data();
448 msvcrt_tm_to_unix( &tm, mstm );
450 if (!data->asctime_buffer)
451 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
453 /* FIXME: may want to map from Unix codepage to CP_ACP */
454 #ifdef HAVE_ASCTIME_R
455 asctime_r( &tm, data->asctime_buffer );
457 strcpy( data->asctime_buffer, asctime(&tm) );
459 return data->asctime_buffer;
462 /*********************************************************************
463 * _wasctime (MSVCRT.@)
465 MSVCRT_wchar_t *MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
467 thread_data_t *data = msvcrt_get_thread_data();
471 msvcrt_tm_to_unix( &tm, mstm );
473 if (!data->wasctime_buffer)
474 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
475 #ifdef HAVE_ASCTIME_R
476 asctime_r( &tm, buffer );
478 strcpy( buffer, asctime(&tm) );
480 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
481 return data->wasctime_buffer;
484 /*********************************************************************
487 char *MSVCRT_ctime(const MSVCRT_time_t *time)
489 return MSVCRT_asctime( MSVCRT_localtime(time) );
492 /*********************************************************************
495 MSVCRT_wchar_t *MSVCRT__wctime(const MSVCRT_time_t *time)
497 return MSVCRT__wasctime( MSVCRT_localtime(time) );