Update the MSI version to be the same as on win2k.
[wine] / dlls / msvcrt / time.c
1 /*
2  * msvcrt.dll date/time functions
3  *
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
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26
27 #include <time.h>
28 #ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
30 #endif
31
32 #include "msvcrt.h"
33 #include "winbase.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37
38 static const int MonthLengths[2][12] =
39 {
40     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
41     { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
42 };
43
44 static inline int IsLeapYear(int Year)
45 {
46     return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
47 }
48
49 #define SECSPERDAY        86400
50 /* 1601 to 1970 is 369 years plus 89 leap days */
51 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
52 #define TICKSPERSEC       10000000
53 #define TICKSPERMSEC      10000
54 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
55
56 /* native uses a single static buffer for localtime/gmtime/mktime */
57 static struct MSVCRT_tm tm;
58
59 /**********************************************************************
60  *              mktime (MSVCRT.@)
61  */
62 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
63 {
64   MSVCRT_time_t secs;
65
66   SYSTEMTIME st;
67   FILETIME lft, uft;
68   ULONGLONG time;
69
70   st.wMilliseconds = 0;
71   st.wSecond = t->tm_sec;
72   st.wMinute = t->tm_min;
73   st.wHour   = t->tm_hour;
74   st.wDay    = t->tm_mday;
75   st.wMonth  = t->tm_mon + 1;
76   st.wYear   = t->tm_year + 1900;
77
78   SystemTimeToFileTime(&st, &lft);
79   LocalFileTimeToFileTime(&lft, &uft);
80
81   time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
82   secs = time / TICKSPERSEC - SECS_1601_TO_1970;
83
84   return secs; 
85 }
86
87 /*********************************************************************
88  *      localtime (MSVCRT.@)
89  */
90 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
91 {
92   int i;
93
94   FILETIME ft, lft;
95   SYSTEMTIME st;
96   DWORD tzid;
97   TIME_ZONE_INFORMATION tzinfo;
98
99   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
100
101   ft.dwHighDateTime = (UINT)(time >> 32);
102   ft.dwLowDateTime  = (UINT)time;
103
104   FileTimeToLocalFileTime(&ft, &lft);
105   FileTimeToSystemTime(&lft, &st);
106
107   if (st.wYear < 1970) return NULL;
108
109   tm.tm_sec  = st.wSecond;
110   tm.tm_min  = st.wMinute;
111   tm.tm_hour = st.wHour;
112   tm.tm_mday = st.wDay;
113   tm.tm_year = st.wYear - 1900;
114   tm.tm_mon  = st.wMonth  - 1;
115   tm.tm_wday = st.wDayOfWeek;
116
117   for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
118     tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
119   }
120
121   tm.tm_yday += st.wDay - 1;
122  
123   tzid = GetTimeZoneInformation(&tzinfo);
124
125   if (tzid == TIME_ZONE_ID_UNKNOWN) {
126     tm.tm_isdst = -1;
127   } else {
128     tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
129   }
130
131   return &tm;
132 }
133
134 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
135 {
136   int i;
137
138   FILETIME ft;
139   SYSTEMTIME st;
140
141   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
142
143   ft.dwHighDateTime = (UINT)(time >> 32);
144   ft.dwLowDateTime  = (UINT)time;
145
146   FileTimeToSystemTime(&ft, &st);
147
148   if (st.wYear < 1970) return NULL;
149
150   tm.tm_sec  = st.wSecond;
151   tm.tm_min  = st.wMinute;
152   tm.tm_hour = st.wHour;
153   tm.tm_mday = st.wDay;
154   tm.tm_year = st.wYear - 1900;
155   tm.tm_mon  = st.wMonth - 1;
156   tm.tm_wday = st.wDayOfWeek;
157   for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
158     tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
159   }
160
161   tm.tm_yday += st.wDay - 1;
162   tm.tm_isdst = 0;
163
164   return &tm;
165 }
166
167 /**********************************************************************
168  *              _strdate (MSVCRT.@)
169  */
170 char* _strdate(char* date)
171 {
172   LPCSTR format = "MM'/'dd'/'yy";
173
174   GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
175
176   return date;
177 }
178
179 /*********************************************************************
180  *              _strtime (MSVCRT.@)
181  */
182 char* _strtime(char* date)
183 {
184   LPCSTR format = "HH':'mm':'ss";
185
186   GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9); 
187
188   return date;
189 }
190
191 /*********************************************************************
192  *              clock (MSVCRT.@)
193  */
194 MSVCRT_clock_t MSVCRT_clock(void)
195 {
196   FILETIME ftc, fte, ftk, ftu;
197   ULONGLONG utime, ktime;
198  
199   MSVCRT_clock_t clock;
200
201   GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
202
203   ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
204   utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
205
206   clock = ((utime + ktime) / TICKSPERSEC) * CLOCKS_PER_SEC;
207
208   return clock;
209 }
210
211 /*********************************************************************
212  *              difftime (MSVCRT.@)
213  */
214 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
215 {
216   return (double)(time1 - time2);
217 }
218
219 /*********************************************************************
220  *              _ftime (MSVCRT.@)
221  */
222 void _ftime(struct MSVCRT__timeb *buf)
223 {
224   TIME_ZONE_INFORMATION tzinfo;
225   FILETIME ft;
226   ULONGLONG time;
227
228   DWORD tzid = GetTimeZoneInformation(&tzinfo);
229   GetSystemTimeAsFileTime(&ft);
230
231   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
232
233   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
234   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
235   buf->timezone = tzinfo.Bias;
236   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
237 }
238
239 /*********************************************************************
240  *              time (MSVCRT.@)
241  */
242 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
243 {
244   MSVCRT_time_t curtime;
245   struct MSVCRT__timeb tb;
246
247   _ftime(&tb);
248
249   curtime = tb.time;
250   return buf ? *buf = curtime : curtime;
251 }
252
253 /*********************************************************************
254  *              _daylight (MSVCRT.@)
255  */
256 int MSVCRT___daylight = 1; /* FIXME: assume daylight */
257
258 /*********************************************************************
259  *              __p_daylight (MSVCRT.@)
260  */
261 void *MSVCRT___p__daylight(void)
262 {
263         return &MSVCRT___daylight;
264 }