Reimplement time functions with Win32 APIs.
[wine.git] / dlls / msvcrt / time.c
blob35e08646e9267d485e6072fe60c05a6aae6c9229
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
32 #include "msvcrt.h"
33 #include "msvcrt/sys/timeb.h"
34 #include "msvcrt/time.h"
36 #include "winbase.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
42 static const int MonthLengths[2][12] =
44 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
45 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
48 static inline int IsLeapYear(int Year)
50 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
53 #define SECSPERDAY 86400
54 /* 1601 to 1970 is 369 years plus 89 leap days */
55 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
56 #define TICKSPERSEC 10000000
57 #define TICKSPERMSEC 10000
58 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
60 /* native uses a single static buffer for localtime/gmtime/mktime */
61 static struct MSVCRT_tm tm;
63 /**********************************************************************
64 * mktime (MSVCRT.@)
66 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
68 MSVCRT_time_t secs;
70 SYSTEMTIME st;
71 FILETIME lft, uft;
72 ULONGLONG time;
74 st.wSecond = t->tm_sec;
75 st.wMinute = t->tm_min;
76 st.wHour = t->tm_hour;
77 st.wDay = t->tm_mday - 1;
78 st.wMonth = t->tm_mon;
79 st.wYear = t->tm_year + 1900;
81 SystemTimeToFileTime(&st, &lft);
82 LocalFileTimeToFileTime(&lft, &uft);
84 time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
85 secs = time / TICKSPERSEC - SECS_1601_TO_1970;
87 return secs;
90 /*********************************************************************
91 * localtime (MSVCRT.@)
93 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
95 int i;
97 FILETIME ft, lft;
98 SYSTEMTIME st;
99 DWORD tzid;
100 TIME_ZONE_INFORMATION tzinfo;
102 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
104 ft.dwHighDateTime = (UINT)(time >> 32);
105 ft.dwLowDateTime = (UINT)time;
107 FileTimeToLocalFileTime(&ft, &lft);
108 FileTimeToSystemTime(&lft, &st);
110 if (st.wYear < 1970) return NULL;
112 tm.tm_sec = st.wSecond;
113 tm.tm_min = st.wMinute;
114 tm.tm_hour = st.wHour;
115 tm.tm_mday = st.wDay;
116 tm.tm_year = st.wYear - 1900;
117 tm.tm_mon = st.wMonth + 1;
118 tm.tm_wday = st.wDayOfWeek;
120 for (i = 0; i < st.wMonth; i++) {
121 tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
124 tm.tm_yday += st.wDay;
126 tzid = GetTimeZoneInformation(&tzinfo);
128 if (tzid == TIME_ZONE_ID_UNKNOWN) {
129 tm.tm_isdst = -1;
130 } else {
131 tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
134 return &tm;
137 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
139 int i;
141 FILETIME ft, lft;
142 SYSTEMTIME st;
144 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
146 ft.dwHighDateTime = (UINT)(time >> 32);
147 ft.dwLowDateTime = (UINT)time;
149 FileTimeToSystemTime(&lft, &st);
151 if (st.wYear < 1970) return NULL;
153 tm.tm_sec = st.wSecond;
154 tm.tm_min = st.wMinute;
155 tm.tm_hour = st.wHour;
156 tm.tm_mday = st.wDay;
157 tm.tm_year = st.wYear - 1900;
158 tm.tm_mon = st.wMonth + 1;
159 tm.tm_wday = st.wDayOfWeek;
161 for (i = 0; i < st.wMonth; i++) {
162 tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
165 tm.tm_yday += st.wDay;
166 tm.tm_isdst = 0;
168 return &tm;
171 /**********************************************************************
172 * _strdate (MSVCRT.@)
174 char* _strdate(char* date)
176 LPCSTR format = "MM'/'dd'/'yy";
178 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
180 return date;
183 /*********************************************************************
184 * _strtime (MSVCRT.@)
186 char* _strtime(char* date)
188 LPCSTR format = "HH':'mm':'ss";
190 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
192 return date;
195 /*********************************************************************
196 * clock (MSVCRT.@)
198 MSVCRT_clock_t MSVCRT_clock(void)
200 FILETIME ftc, fte, ftk, ftu;
201 ULONGLONG utime, ktime;
203 MSVCRT_clock_t clock;
205 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
207 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
208 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
210 clock = ((utime + ktime) / TICKSPERSEC) * CLOCKS_PER_SEC;
212 return clock;
215 /*********************************************************************
216 * difftime (MSVCRT.@)
218 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
220 return (double)(time1 - time2);
223 /*********************************************************************
224 * time (MSVCRT.@)
226 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
228 MSVCRT_time_t curtime;
229 struct _timeb tb;
231 _ftime(&tb);
233 curtime = tb.time;
234 return buf ? *buf = curtime : curtime;
237 /*********************************************************************
238 * _ftime (MSVCRT.@)
240 void _ftime(struct _timeb *buf)
242 TIME_ZONE_INFORMATION tzinfo;
243 FILETIME ft;
244 ULONGLONG time;
246 DWORD tzid = GetTimeZoneInformation(&tzinfo);
247 GetSystemTimeAsFileTime(&ft);
249 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
251 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
252 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
253 buf->timezone = tzinfo.Bias;
254 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
257 /*********************************************************************
258 * _daylight (MSVCRT.@)
260 int MSVCRT___daylight = 1; /* FIXME: assume daylight */
262 /*********************************************************************
263 * __p_daylight (MSVCRT.@)
265 void *MSVCRT___p__daylight(void)
267 return &MSVCRT___daylight;