We should always allocate in NdrConformantStringUnmarshal if the
[wine.git] / dlls / msvcrt / time.c
blobf7daab5dce28740b8c7b86d3efac357b3f9fc1ec
1 /*
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
25 #include "config.h"
27 #include <time.h>
28 #ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
30 #endif
31 #include <limits.h>
33 #include "msvcrt.h"
34 #include "winbase.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 /**********************************************************************
61 * mktime (MSVCRT.@)
63 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
65 MSVCRT_time_t secs;
66 FILETIME lft, uft;
67 ULONGLONG time;
68 struct MSVCRT_tm ts;
69 int cleaps, day;
71 ts=*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 )
81 return -1;
83 /* normalize the tm month fields */
84 if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
85 if( ts.tm_mon < 0) {
86 int dy = (11 - ts.tm_mon) / 12;
87 ts.tm_year -= dy;
88 ts.tm_mon += dy * 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 */
96 if(ts.tm_mon < 2) {
97 ts.tm_mon += 14;
98 ts.tm_year += 1899;
99 } else {
100 ts.tm_mon += 2;
101 ts.tm_year += 1900;
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 */
108 /* done */
110 /* convert to 100 ns ticks */
111 time = ((((ULONGLONG) day * 24 +
112 ts.tm_hour) * 60 +
113 ts.tm_min) * 60 +
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;
122 time /= TICKSPERSEC;
123 if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
124 return -1;
125 secs = time - SECS_1601_TO_1970;
126 /* compute tm_wday, tm_yday and renormalize the other fields of the
127 * tm structure */
128 if( MSVCRT_localtime( &secs)) *t = tm;
130 return secs;
133 /*********************************************************************
134 * localtime (MSVCRT.@)
136 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
138 int i;
140 FILETIME ft, lft;
141 SYSTEMTIME st;
142 DWORD tzid;
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)
172 tm.tm_isdst = -1;
173 else
174 tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
176 return &tm;
179 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
181 int i;
183 FILETIME ft;
184 SYSTEMTIME st;
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;
207 tm.tm_isdst = 0;
209 return &tm;
212 /**********************************************************************
213 * _strdate (MSVCRT.@)
215 char* _strdate(char* date)
217 LPCSTR format = "MM'/'dd'/'yy";
219 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
221 return date;
224 /**********************************************************************
225 * _wstrdate (MSVCRT.@)
227 MSVCRT_wchar_t* _wstrdate(MSVCRT_wchar_t* date)
229 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
231 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
233 return date;
236 /*********************************************************************
237 * _strtime (MSVCRT.@)
239 char* _strtime(char* time)
241 LPCSTR format = "HH':'mm':'ss";
243 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
245 return time;
248 /*********************************************************************
249 * _wstrtime (MSVCRT.@)
251 MSVCRT_wchar_t* _wstrtime(MSVCRT_wchar_t* time)
253 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
255 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
257 return time;
260 /*********************************************************************
261 * clock (MSVCRT.@)
263 MSVCRT_clock_t MSVCRT_clock(void)
265 FILETIME ftc, fte, ftk, ftu;
266 ULONGLONG utime, ktime;
268 MSVCRT_clock_t clock;
270 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
272 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
273 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
275 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
277 return clock;
280 /*********************************************************************
281 * difftime (MSVCRT.@)
283 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
285 return (double)(time1 - time2);
288 /*********************************************************************
289 * _ftime (MSVCRT.@)
291 void _ftime(struct MSVCRT__timeb *buf)
293 TIME_ZONE_INFORMATION tzinfo;
294 FILETIME ft;
295 ULONGLONG time;
297 DWORD tzid = GetTimeZoneInformation(&tzinfo);
298 GetSystemTimeAsFileTime(&ft);
300 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
302 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
303 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
304 buf->timezone = tzinfo.Bias +
305 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
306 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
307 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
310 /*********************************************************************
311 * time (MSVCRT.@)
313 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
315 MSVCRT_time_t curtime;
316 struct MSVCRT__timeb tb;
318 _ftime(&tb);
320 curtime = tb.time;
321 return buf ? *buf = curtime : curtime;
324 /*********************************************************************
325 * _daylight (MSVCRT.@)
327 int MSVCRT___daylight = 0;
329 /*********************************************************************
330 * __p_daylight (MSVCRT.@)
332 int *MSVCRT___p__daylight(void)
334 return &MSVCRT___daylight;
337 /*********************************************************************
338 * _dstbias (MSVCRT.@)
340 int MSVCRT__dstbias = 0;
342 /*********************************************************************
343 * __p_dstbias (MSVCRT.@)
345 int *__p__dstbias(void)
347 return &MSVCRT__dstbias;
350 /*********************************************************************
351 * _timezone (MSVCRT.@)
353 long MSVCRT___timezone = 0;
355 /*********************************************************************
356 * __p_timezone (MSVCRT.@)
358 long *MSVCRT___p__timezone(void)
360 return &MSVCRT___timezone;
363 /*********************************************************************
364 * _tzname (MSVCRT.@)
365 * NOTES
366 * Some apps (notably Mozilla) insist on writing to these, so the buffer
367 * must be large enough. The size is picked based on observation of
368 * Windows XP.
370 static char tzname_std[64] = "";
371 static char tzname_dst[64] = "";
372 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
374 /*********************************************************************
375 * __p_tzname (MSVCRT.@)
377 char **__p__tzname(void)
379 return MSVCRT__tzname;
382 /*********************************************************************
383 * _tzset (MSVCRT.@)
385 void MSVCRT__tzset(void)
387 tzset();
388 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
389 MSVCRT___daylight = daylight;
390 MSVCRT___timezone = timezone;
391 #else
393 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
394 time_t t;
395 struct tm *tmp;
396 long zone_january, zone_july;
398 t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
399 tmp = localtime(&t);
400 zone_january = -tmp->tm_gmtoff;
401 t += seconds_in_year / 2;
402 tmp = localtime(&t);
403 zone_july = -tmp->tm_gmtoff;
404 MSVCRT___daylight = (zone_january != zone_july);
405 MSVCRT___timezone = max(zone_january, zone_july);
407 #endif
408 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
409 tzname_std[sizeof(tzname_std) - 1] = '\0';
410 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
411 tzname_dst[sizeof(tzname_dst) - 1] = '\0';