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 #define SECSPERDAY 86400
51 /* 1601 to 1970 is 369 years plus 89 leap days */
52 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
53 #define TICKSPERSEC 10000000
54 #define TICKSPERMSEC 10000
55 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
57 /* native uses a single static buffer for localtime/gmtime/mktime */
58 static struct MSVCRT_tm tm;
60 /**********************************************************************
63 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
72 /* to prevent arithmetic overflows put constraints on some fields */
73 /* whether the effective date falls in the 1970-2038 time period */
74 /* will be tested later */
75 /* BTW, I have no idea what limits native msvcrt has. */
76 if ( ts.tm_year < 0 || ts.tm_year > 140 ||
77 ts.tm_mon < -840 || ts.tm_mon > 840 ||
78 ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
79 ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
80 ts.tm_min < -29000000 || ts.tm_min > 29000000 )
83 /* normalize the tm month fields */
84 if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
86 int dy = (11 - ts.tm_mon) / 12;
90 /* now calculate a day count from the date
91 * First start counting years from March. This way the leap days
92 * are added at the end of the year, not somewhere in the middle.
93 * Formula's become so much less complicate that way.
94 * To convert: add 12 to the month numbers of Jan and Feb, and
95 * take 1 from the year */
103 cleaps = (3 * (ts.tm_year / 100) + 3) / 4; /* nr of "century leap years"*/
104 day = (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
105 (1959 * ts.tm_mon) / 64 + /* months * daypermonth */
106 ts.tm_mday - /* day of the month */
107 584817 ; /* zero that on 1601-01-01 */
110 /* convert to 100 ns ticks */
111 time = ((((ULONGLONG) day * 24 +
114 ts.tm_sec ) * TICKSPERSEC;
116 lft.dwHighDateTime = (DWORD) (time >> 32);
117 lft.dwLowDateTime = (DWORD) time;
119 LocalFileTimeToFileTime(&lft, &uft);
121 time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
123 if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
125 secs = time - SECS_1601_TO_1970;
126 /* compute tm_wday, tm_yday and renormalize the other fields of the
128 if( MSVCRT_localtime( &secs)) *t = tm;
133 /*********************************************************************
134 * localtime (MSVCRT.@)
136 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
143 TIME_ZONE_INFORMATION tzinfo;
145 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
147 ft.dwHighDateTime = (UINT)(time >> 32);
148 ft.dwLowDateTime = (UINT)time;
150 FileTimeToLocalFileTime(&ft, &lft);
151 FileTimeToSystemTime(&lft, &st);
153 if (st.wYear < 1970) return NULL;
155 tm.tm_sec = st.wSecond;
156 tm.tm_min = st.wMinute;
157 tm.tm_hour = st.wHour;
158 tm.tm_mday = st.wDay;
159 tm.tm_year = st.wYear - 1900;
160 tm.tm_mon = st.wMonth - 1;
161 tm.tm_wday = st.wDayOfWeek;
163 for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
164 tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
167 tm.tm_yday += st.wDay - 1;
169 tzid = GetTimeZoneInformation(&tzinfo);
171 if (tzid == TIME_ZONE_ID_INVALID)
174 tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
179 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
186 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
188 ft.dwHighDateTime = (UINT)(time >> 32);
189 ft.dwLowDateTime = (UINT)time;
191 FileTimeToSystemTime(&ft, &st);
193 if (st.wYear < 1970) return NULL;
195 tm.tm_sec = st.wSecond;
196 tm.tm_min = st.wMinute;
197 tm.tm_hour = st.wHour;
198 tm.tm_mday = st.wDay;
199 tm.tm_year = st.wYear - 1900;
200 tm.tm_mon = st.wMonth - 1;
201 tm.tm_wday = st.wDayOfWeek;
202 for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
203 tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
206 tm.tm_yday += st.wDay - 1;
212 /**********************************************************************
213 * _strdate (MSVCRT.@)
215 char* _strdate(char* date)
217 LPCSTR format = "MM'/'dd'/'yy";
219 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
224 /*********************************************************************
225 * _strtime (MSVCRT.@)
227 char* _strtime(char* date)
229 LPCSTR format = "HH':'mm':'ss";
231 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
236 /*********************************************************************
239 MSVCRT_clock_t MSVCRT_clock(void)
241 FILETIME ftc, fte, ftk, ftu;
242 ULONGLONG utime, ktime;
244 MSVCRT_clock_t clock;
246 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
248 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
249 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
251 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
256 /*********************************************************************
257 * difftime (MSVCRT.@)
259 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
261 return (double)(time1 - time2);
264 /*********************************************************************
267 void _ftime(struct MSVCRT__timeb *buf)
269 TIME_ZONE_INFORMATION tzinfo;
273 DWORD tzid = GetTimeZoneInformation(&tzinfo);
274 GetSystemTimeAsFileTime(&ft);
276 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
278 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
279 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
280 buf->timezone = tzinfo.Bias +
281 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
282 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
283 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
286 /*********************************************************************
289 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
291 MSVCRT_time_t curtime;
292 struct MSVCRT__timeb tb;
297 return buf ? *buf = curtime : curtime;
300 /*********************************************************************
301 * _daylight (MSVCRT.@)
303 int MSVCRT___daylight = 0;
305 /*********************************************************************
306 * __p_daylight (MSVCRT.@)
308 int *MSVCRT___p__daylight(void)
310 return &MSVCRT___daylight;
313 /*********************************************************************
314 * _dstbias (MSVCRT.@)
316 int MSVCRT__dstbias = 0;
318 /*********************************************************************
319 * __p_dstbias (MSVCRT.@)
321 int *__p__dstbias(void)
323 return &MSVCRT__dstbias;
326 /*********************************************************************
327 * _timezone (MSVCRT.@)
329 long MSVCRT___timezone = 0;
331 /*********************************************************************
332 * __p_timezone (MSVCRT.@)
334 long *MSVCRT___p__timezone(void)
336 return &MSVCRT___timezone;
339 /*********************************************************************
342 * Some apps (notably Mozilla) insist on writing to these, so the buffer
343 * must be large enough. The size is picked based on observation of
346 static char tzname_std[64] = "";
347 static char tzname_dst[64] = "";
348 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
350 /*********************************************************************
351 * __p_tzname (MSVCRT.@)
353 char **__p__tzname(void)
355 return MSVCRT__tzname;
358 /*********************************************************************
361 void MSVCRT__tzset(void)
364 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
365 MSVCRT___daylight = daylight;
366 MSVCRT___timezone = timezone;
369 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
372 long zone_january, zone_july;
374 t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
376 zone_january = -tmp->tm_gmtoff;
377 t += seconds_in_year / 2;
379 zone_july = -tmp->tm_gmtoff;
380 MSVCRT___daylight = (zone_january != zone_july);
381 MSVCRT___timezone = max(zone_january, zone_july);
384 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
385 tzname_std[sizeof(tzname_std) - 1] = '\0';
386 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
387 tzname_dst[sizeof(tzname_dst) - 1] = '\0';