1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* msvcrt.dll date/time functions
*
* Copyright 1996,1998 Marcus Meissner
* Copyright 1996 Jukka Iivonen
* Copyright 1997,2000 Uwe Bonnes
* Copyright 2000 Jon Griffiths
* Copyright 2004 Hans Leidekker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <time.h>
#ifdef HAVE_SYS_TIMES_H
# include <sys/times.h>
#endif
#include <limits.h>
#include "msvcrt.h"
#include "winbase.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
static const int MonthLengths[2][12] =
{
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
static inline int IsLeapYear(int Year)
{
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
}
#define SECSPERDAY 86400
/* 1601 to 1970 is 369 years plus 89 leap days */
#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
#define TICKSPERSEC 10000000
#define TICKSPERMSEC 10000
#define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
/* native uses a single static buffer for localtime/gmtime/mktime */
static struct MSVCRT_tm tm;
/**********************************************************************
* mktime (MSVCRT.@)
*/
MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
{
MSVCRT_time_t secs;
FILETIME lft, uft;
ULONGLONG time;
struct MSVCRT_tm ts;
int cleaps, day;
ts=*t;
/* to prevent arithmetic overflows put constraints on some fields */
/* whether the effective date falls in the 1970-2038 time period */
/* will be tested later */
/* BTW, I have no idea what limits native msvcrt has. */
if ( ts.tm_year < 0 || ts.tm_year > 140 ||
ts.tm_mon < -840 || ts.tm_mon > 840 ||
ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
ts.tm_min < -29000000 || ts.tm_min > 29000000 )
return -1;
/* normalize the tm month fields */
if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
if( ts.tm_mon < 0) {
int dy = (11 - ts.tm_mon) / 12;
ts.tm_year -= dy;
ts.tm_mon += dy * 12;
}
/* now calculate a day count from the date
* First start counting years from March. This way the leap days
* are added at the end of the year, not somewhere in the middle.
* Formula's become so much less complicate that way.
* To convert: add 12 to the month numbers of Jan and Feb, and
* take 1 from the year */
if(ts.tm_mon < 2) {
ts.tm_mon += 14;
ts.tm_year += 1899;
} else {
ts.tm_mon += 2;
ts.tm_year += 1900;
}
cleaps = (3 * (ts.tm_year / 100) + 3) / 4; /* nr of "century leap years"*/
day = (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
(1959 * ts.tm_mon) / 64 + /* months * daypermonth */
ts.tm_mday - /* day of the month */
584817 ; /* zero that on 1601-01-01 */
/* done */
/* convert to 100 ns ticks */
time = ((((ULONGLONG) day * 24 +
ts.tm_hour) * 60 +
ts.tm_min) * 60 +
ts.tm_sec ) * TICKSPERSEC;
lft.dwHighDateTime = (DWORD) (time >> 32);
lft.dwLowDateTime = (DWORD) time;
LocalFileTimeToFileTime(&lft, &uft);
time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
time /= TICKSPERSEC;
if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
return -1;
secs = time - SECS_1601_TO_1970;
/* compute tm_wday, tm_yday and renormalize the other fields of the
* tm structure */
if( MSVCRT_localtime( &secs)) *t = tm;
return secs;
}
/*********************************************************************
* localtime (MSVCRT.@)
*/
struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
{
int i;
FILETIME ft, lft;
SYSTEMTIME st;
DWORD tzid;
TIME_ZONE_INFORMATION tzinfo;
ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
ft.dwHighDateTime = (UINT)(time >> 32);
ft.dwLowDateTime = (UINT)time;
FileTimeToLocalFileTime(&ft, &lft);
FileTimeToSystemTime(&lft, &st);
if (st.wYear < 1970) return NULL;
tm.tm_sec = st.wSecond;
tm.tm_min = st.wMinute;
tm.tm_hour = st.wHour;
tm.tm_mday = st.wDay;
tm.tm_year = st.wYear - 1900;
tm.tm_mon = st.wMonth - 1;
tm.tm_wday = st.wDayOfWeek;
for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
}
tm.tm_yday += st.wDay - 1;
tzid = GetTimeZoneInformation(&tzinfo);
if (tzid == TIME_ZONE_ID_INVALID)
tm.tm_isdst = -1;
else
tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
return &tm;
}
struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
{
int i;
FILETIME ft;
SYSTEMTIME st;
ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
ft.dwHighDateTime = (UINT)(time >> 32);
ft.dwLowDateTime = (UINT)time;
FileTimeToSystemTime(&ft, &st);
if (st.wYear < 1970) return NULL;
tm.tm_sec = st.wSecond;
tm.tm_min = st.wMinute;
tm.tm_hour = st.wHour;
tm.tm_mday = st.wDay;
tm.tm_year = st.wYear - 1900;
tm.tm_mon = st.wMonth - 1;
tm.tm_wday = st.wDayOfWeek;
for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
}
tm.tm_yday += st.wDay - 1;
tm.tm_isdst = 0;
return &tm;
}
/**********************************************************************
* _strdate (MSVCRT.@)
*/
char* _strdate(char* date)
{
LPCSTR format = "MM'/'dd'/'yy";
GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
return date;
}
/*********************************************************************
* _strtime (MSVCRT.@)
*/
char* _strtime(char* date)
{
LPCSTR format = "HH':'mm':'ss";
GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
return date;
}
/*********************************************************************
* clock (MSVCRT.@)
*/
MSVCRT_clock_t MSVCRT_clock(void)
{
FILETIME ftc, fte, ftk, ftu;
ULONGLONG utime, ktime;
MSVCRT_clock_t clock;
GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
return clock;
}
/*********************************************************************
* difftime (MSVCRT.@)
*/
double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
{
return (double)(time1 - time2);
}
/*********************************************************************
* _ftime (MSVCRT.@)
*/
void _ftime(struct MSVCRT__timeb *buf)
{
TIME_ZONE_INFORMATION tzinfo;
FILETIME ft;
ULONGLONG time;
DWORD tzid = GetTimeZoneInformation(&tzinfo);
GetSystemTimeAsFileTime(&ft);
time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
buf->timezone = tzinfo.Bias +
( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
}
/*********************************************************************
* time (MSVCRT.@)
*/
MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
{
MSVCRT_time_t curtime;
struct MSVCRT__timeb tb;
_ftime(&tb);
curtime = tb.time;
return buf ? *buf = curtime : curtime;
}
/*********************************************************************
* _daylight (MSVCRT.@)
*/
int MSVCRT___daylight = 1; /* FIXME: assume daylight */
/*********************************************************************
* __p_daylight (MSVCRT.@)
*/
void *MSVCRT___p__daylight(void)
{
return &MSVCRT___daylight;
}