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