6865e3d269180fcc8e1a2ab43fd518b67929fd25
[reactos.git] / modules / rostests / winetests / msvcrt / time.c
1 /*
2 * Unit test suite for time functions.
3 *
4 * Copyright 2004 Uwe Bonnes
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include "time.h"
25
26 #include <stdlib.h> /*setenv*/
27 #include <stdio.h> /*printf*/
28 #include <locale.h>
29 #include <errno.h>
30
31 #define _MAX__TIME64_T (((__time64_t)0x00000007 << 32) | 0x93406FFF)
32
33 #define SECSPERDAY 86400
34 #define SECSPERHOUR 3600
35 #define SECSPERMIN 60
36 #define MINSPERHOUR 60
37 #define HOURSPERDAY 24
38
39 static __time32_t (__cdecl *p_mkgmtime32)(struct tm*);
40 static struct tm* (__cdecl *p_gmtime32)(__time32_t*);
41 static struct tm* (__cdecl *p_gmtime)(time_t*);
42 static errno_t (__cdecl *p_gmtime32_s)(struct tm*, __time32_t*);
43 static errno_t (__cdecl *p_strtime_s)(char*,size_t);
44 static errno_t (__cdecl *p_strdate_s)(char*,size_t);
45 static errno_t (__cdecl *p_localtime32_s)(struct tm*, __time32_t*);
46 static errno_t (__cdecl *p_localtime64_s)(struct tm*, __time64_t*);
47 static int* (__cdecl *p__daylight)(void);
48 static int* (__cdecl *p___p__daylight)(void);
49 static long* (__cdecl *p___p__dstbias)(void);
50 static long* (__cdecl *p__dstbias)(void);
51 static long* (__cdecl *p___p__timezone)(void);
52 static size_t (__cdecl *p_strftime)(char *, size_t, const char *, const struct tm *);
53 static size_t (__cdecl *p_wcsftime)(wchar_t *, size_t, const wchar_t *, const struct tm *);
54 static char* (__cdecl *p_asctime)(const struct tm *);
55
56 static void init(void)
57 {
58 HMODULE hmod = LoadLibraryA("msvcrt.dll");
59
60 p_gmtime32 = (void*)GetProcAddress(hmod, "_gmtime32");
61 p_gmtime = (void*)GetProcAddress(hmod, "gmtime");
62 p_gmtime32_s = (void*)GetProcAddress(hmod, "_gmtime32_s");
63 p_mkgmtime32 = (void*)GetProcAddress(hmod, "_mkgmtime32");
64 p_strtime_s = (void*)GetProcAddress(hmod, "_strtime_s");
65 p_strdate_s = (void*)GetProcAddress(hmod, "_strdate_s");
66 p_localtime32_s = (void*)GetProcAddress(hmod, "_localtime32_s");
67 p_localtime64_s = (void*)GetProcAddress(hmod, "_localtime64_s");
68 p__daylight = (void*)GetProcAddress(hmod, "__daylight");
69 p___p__daylight = (void*)GetProcAddress(hmod, "__p__daylight");
70 p___p__dstbias = (void*)GetProcAddress(hmod, "__p__dstbias");
71 p__dstbias = (void*)GetProcAddress(hmod, "__dstbias");
72 p___p__timezone = (void*)GetProcAddress(hmod, "__p__timezone");
73 p_strftime = (void*)GetProcAddress(hmod, "strftime");
74 p_wcsftime = (void*)GetProcAddress(hmod, "wcsftime");
75 p_asctime = (void*)GetProcAddress(hmod, "asctime");
76 }
77
78 static int get_test_year(time_t *start)
79 {
80 time_t now = time(NULL);
81 struct tm *tm = localtime(&now);
82
83 /* compute start of year in seconds */
84 *start = SECSPERDAY * ((tm->tm_year - 70) * 365 +
85 (tm->tm_year - 69) / 4 -
86 (tm->tm_year - 1) / 100 +
87 (tm->tm_year + 299) / 400);
88 return tm->tm_year;
89 }
90
91 static void test_ctime(void)
92 {
93 time_t badtime = -1;
94 char* ret;
95 ret = ctime(&badtime);
96 ok(ret == NULL, "expected ctime to return NULL, got %s\n", ret);
97 }
98 static void test_gmtime(void)
99 {
100 __time32_t valid, gmt;
101 struct tm* gmt_tm, gmt_tm_s;
102 errno_t err;
103
104 if(!p_gmtime32) {
105 win_skip("Skipping _gmtime32 tests\n");
106 return;
107 }
108
109 gmt_tm = p_gmtime32(NULL);
110 ok(gmt_tm == NULL, "gmt_tm != NULL\n");
111
112 gmt = -1;
113 gmt_tm = p_gmtime32(&gmt);
114 ok(gmt_tm==NULL || broken(gmt_tm->tm_year==70 && gmt_tm->tm_sec<0), "gmt_tm != NULL\n");
115
116 gmt = valid = 0;
117 gmt_tm = p_gmtime32(&gmt);
118 if(!gmt_tm) {
119 ok(0, "_gmtime32() failed\n");
120 return;
121 }
122
123 ok(((gmt_tm->tm_year == 70) && (gmt_tm->tm_mon == 0) && (gmt_tm->tm_yday == 0) &&
124 (gmt_tm->tm_mday == 1) && (gmt_tm->tm_wday == 4) && (gmt_tm->tm_hour == 0) &&
125 (gmt_tm->tm_min == 0) && (gmt_tm->tm_sec == 0) && (gmt_tm->tm_isdst == 0)),
126 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour%2d min %2d sec %2d dst %2d\n",
127 gmt_tm->tm_year, gmt_tm->tm_mon, gmt_tm->tm_yday, gmt_tm->tm_mday, gmt_tm->tm_wday,
128 gmt_tm->tm_hour, gmt_tm->tm_min, gmt_tm->tm_sec, gmt_tm->tm_isdst);
129
130 if(!p_mkgmtime32) {
131 win_skip("Skipping _mkgmtime32 tests\n");
132 return;
133 }
134
135 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
136 gmt = p_mkgmtime32(gmt_tm);
137 ok(gmt == valid, "gmt = %u\n", gmt);
138 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
139 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
140
141 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
142 gmt_tm->tm_isdst = -1;
143 gmt = p_mkgmtime32(gmt_tm);
144 ok(gmt == valid, "gmt = %u\n", gmt);
145 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
146 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
147
148 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
149 gmt_tm->tm_isdst = 1;
150 gmt = p_mkgmtime32(gmt_tm);
151 ok(gmt == valid, "gmt = %u\n", gmt);
152 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
153 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
154
155 gmt = valid = 173921;
156 gmt_tm = p_gmtime32(&gmt);
157 if(!gmt_tm) {
158 ok(0, "_gmtime32() failed\n");
159 return;
160 }
161
162 gmt_tm->tm_isdst = -1;
163 gmt = p_mkgmtime32(gmt_tm);
164 ok(gmt == valid, "gmt = %u\n", gmt);
165 ok(gmt_tm->tm_wday == 6, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
166 ok(gmt_tm->tm_yday == 2, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
167
168 gmt_tm->tm_isdst = 1;
169 gmt = p_mkgmtime32(gmt_tm);
170 ok(gmt == valid, "gmt = %u\n", gmt);
171
172 if(!p_gmtime32_s) {
173 win_skip("Skipping _gmtime32_s tests\n");
174 return;
175 }
176
177 errno = 0;
178 gmt = 0;
179 err = p_gmtime32_s(NULL, &gmt);
180 ok(err == EINVAL, "err = %d\n", err);
181 ok(errno == EINVAL, "errno = %d\n", errno);
182
183 errno = 0;
184 gmt = -1;
185 err = p_gmtime32_s(&gmt_tm_s, &gmt);
186 ok(gmt_tm_s.tm_year == -1 || broken(gmt_tm_s.tm_year == 70 && gmt_tm_s.tm_sec < 0),
187 "tm_year = %d, tm_sec = %d\n", gmt_tm_s.tm_year, gmt_tm_s.tm_sec);
188 if(gmt_tm_s.tm_year == -1) {
189 ok(err==EINVAL, "err = %d\n", err);
190 ok(errno==EINVAL, "errno = %d\n", errno);
191 }
192 }
193
194 static void test_mktime(void)
195 {
196 TIME_ZONE_INFORMATION tzinfo;
197 DWORD res = GetTimeZoneInformation(&tzinfo);
198 struct tm my_tm, sav_tm;
199 time_t nulltime, local_time;
200 char TZ_env[256];
201 char buffer[64];
202 int year;
203 time_t ref, secs;
204
205 year = get_test_year( &ref );
206 ref += SECSPERDAY;
207
208 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
209 WideCharToMultiByte( CP_ACP, 0, tzinfo.StandardName, -1, buffer, sizeof(buffer), NULL, NULL );
210 trace( "bias %d std %d dst %d zone %s\n",
211 tzinfo.Bias, tzinfo.StandardBias, tzinfo.DaylightBias, buffer );
212 /* Bias may be positive or negative, to use offset of one day */
213 my_tm = *localtime(&ref); /* retrieve current dst flag */
214 secs = SECSPERDAY - tzinfo.Bias * SECSPERMIN;
215 secs -= (my_tm.tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
216 my_tm.tm_mday = 1 + secs/SECSPERDAY;
217 secs = secs % SECSPERDAY;
218 my_tm.tm_hour = secs / SECSPERHOUR;
219 secs = secs % SECSPERHOUR;
220 my_tm.tm_min = secs / SECSPERMIN;
221 secs = secs % SECSPERMIN;
222 my_tm.tm_sec = secs;
223
224 my_tm.tm_year = year;
225 my_tm.tm_mon = 0;
226
227 sav_tm = my_tm;
228
229 local_time = mktime(&my_tm);
230 ok(local_time == ref, "mktime returned %u, expected %u\n",
231 (DWORD)local_time, (DWORD)ref);
232 /* now test some unnormalized struct tm's */
233 my_tm = sav_tm;
234 my_tm.tm_sec += 60;
235 my_tm.tm_min -= 1;
236 local_time = mktime(&my_tm);
237 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
238 (DWORD)local_time, (DWORD)ref);
239 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
240 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
241 my_tm.tm_sec == sav_tm.tm_sec,
242 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
243 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
244 my_tm.tm_hour,my_tm.tm_sec,
245 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
246 sav_tm.tm_hour,sav_tm.tm_sec);
247 my_tm = sav_tm;
248 my_tm.tm_min -= 60;
249 my_tm.tm_hour += 1;
250 local_time = mktime(&my_tm);
251 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
252 (DWORD)local_time, (DWORD)ref);
253 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
254 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
255 my_tm.tm_sec == sav_tm.tm_sec,
256 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
257 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
258 my_tm.tm_hour,my_tm.tm_sec,
259 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
260 sav_tm.tm_hour,sav_tm.tm_sec);
261 my_tm = sav_tm;
262 my_tm.tm_mon -= 12;
263 my_tm.tm_year += 1;
264 local_time = mktime(&my_tm);
265 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
266 (DWORD)local_time, (DWORD)ref);
267 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
268 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
269 my_tm.tm_sec == sav_tm.tm_sec,
270 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
271 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
272 my_tm.tm_hour,my_tm.tm_sec,
273 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
274 sav_tm.tm_hour,sav_tm.tm_sec);
275 my_tm = sav_tm;
276 my_tm.tm_mon += 12;
277 my_tm.tm_year -= 1;
278 local_time = mktime(&my_tm);
279 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
280 (DWORD)local_time, (DWORD)ref);
281 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
282 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
283 my_tm.tm_sec == sav_tm.tm_sec,
284 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
285 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
286 my_tm.tm_hour,my_tm.tm_sec,
287 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
288 sav_tm.tm_hour,sav_tm.tm_sec);
289 /* now a bad time example */
290 my_tm = sav_tm;
291 my_tm.tm_year = 69;
292 local_time = mktime(&my_tm);
293 ok((local_time == -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time);
294
295 my_tm = sav_tm;
296 /* TEST that we are independent from the TZ variable */
297 /*Argh, msvcrt doesn't have setenv() */
298 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
299 putenv("TZ=GMT");
300 nulltime = mktime(&my_tm);
301 ok(nulltime == ref,"mktime returned 0x%08x\n",(DWORD)nulltime);
302 putenv(TZ_env);
303 }
304
305 static void test_localtime(void)
306 {
307 TIME_ZONE_INFORMATION tzinfo;
308 DWORD res = GetTimeZoneInformation(&tzinfo);
309 time_t gmt, ref;
310
311 char TZ_env[256];
312 struct tm* lt;
313 int year = get_test_year( &ref );
314 int is_leap = !(year % 4) && ((year % 100) || !((year + 300) % 400));
315
316 gmt = ref + SECSPERDAY + tzinfo.Bias * SECSPERMIN;
317 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
318 lt = localtime(&gmt);
319 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
320 lt = localtime(&gmt);
321 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
322 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
323 (lt->tm_min == 0) && (lt->tm_sec == 0)),
324 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
325 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
326 lt->tm_min, lt->tm_sec, lt->tm_isdst);
327
328 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
329 putenv("TZ=GMT");
330 lt = localtime(&gmt);
331 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
332 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
333 (lt->tm_min == 0) && (lt->tm_sec == 0)),
334 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
335 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
336 lt->tm_min, lt->tm_sec, lt->tm_isdst);
337 putenv(TZ_env);
338
339 /* June 22 */
340 gmt = ref + 202 * SECSPERDAY + tzinfo.Bias * SECSPERMIN;
341 lt = localtime(&gmt);
342 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
343 lt = localtime(&gmt);
344 ok(((lt->tm_year == year) && (lt->tm_mon == 6) && (lt->tm_yday == 202) &&
345 (lt->tm_mday == 22 - is_leap) && (lt->tm_hour == 0) &&
346 (lt->tm_min == 0) && (lt->tm_sec == 0)),
347 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
348 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
349 lt->tm_min, lt->tm_sec, lt->tm_isdst);
350 }
351
352 static void test_strdate(void)
353 {
354 char date[16], * result;
355 int month, day, year, count, len;
356 errno_t err;
357
358 result = _strdate(date);
359 ok(result == date, "Wrong return value\n");
360 len = strlen(date);
361 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
362 count = sscanf(date, "%02d/%02d/%02d", &month, &day, &year);
363 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
364
365 if(!p_strdate_s) {
366 win_skip("Skipping _strdate_s tests\n");
367 return;
368 }
369
370 errno = 0;
371 err = p_strdate_s(NULL, 1);
372 ok(err == EINVAL, "err = %d\n", err);
373 ok(errno == EINVAL, "errno = %d\n", errno);
374
375 date[0] = 'x';
376 date[1] = 'x';
377 err = p_strdate_s(date, 8);
378 ok(err == ERANGE, "err = %d\n", err);
379 ok(errno == ERANGE, "errno = %d\n", errno);
380 ok(date[0] == '\0', "date[0] != '\\0'\n");
381 ok(date[1] == 'x', "date[1] != 'x'\n");
382
383 err = p_strdate_s(date, 9);
384 ok(err == 0, "err = %x\n", err);
385 }
386
387 static void test_strtime(void)
388 {
389 char time[16], * result;
390 int hour, minute, second, count, len;
391 errno_t err;
392
393 result = _strtime(time);
394 ok(result == time, "Wrong return value\n");
395 len = strlen(time);
396 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
397 count = sscanf(time, "%02d:%02d:%02d", &hour, &minute, &second);
398 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
399
400 if(!p_strtime_s) {
401 win_skip("Skipping _strtime_s tests\n");
402 return;
403 }
404
405 errno = 0;
406 err = p_strtime_s(NULL, 0);
407 ok(err == EINVAL, "err = %d\n", err);
408 ok(errno == EINVAL, "errno = %d\n", errno);
409
410 err = p_strtime_s(NULL, 1);
411 ok(err == EINVAL, "err = %d\n", err);
412 ok(errno == EINVAL, "errno = %d\n", errno);
413
414 time[0] = 'x';
415 err = p_strtime_s(time, 8);
416 ok(err == ERANGE, "err = %d\n", err);
417 ok(errno == ERANGE, "errno = %d\n", errno);
418 ok(time[0] == '\0', "time[0] != '\\0'\n");
419
420 err = p_strtime_s(time, 9);
421 ok(err == 0, "err = %x\n", err);
422 }
423
424 static void test_wstrdate(void)
425 {
426 wchar_t date[16], * result;
427 int month, day, year, count, len;
428 wchar_t format[] = { '%','0','2','d','/','%','0','2','d','/','%','0','2','d',0 };
429
430 result = _wstrdate(date);
431 ok(result == date, "Wrong return value\n");
432 len = wcslen(date);
433 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
434 count = swscanf(date, format, &month, &day, &year);
435 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
436 }
437
438 static void test_wstrtime(void)
439 {
440 wchar_t time[16], * result;
441 int hour, minute, second, count, len;
442 wchar_t format[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',0 };
443
444 result = _wstrtime(time);
445 ok(result == time, "Wrong return value\n");
446 len = wcslen(time);
447 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
448 count = swscanf(time, format, &hour, &minute, &second);
449 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
450 }
451
452 static void test_localtime32_s(void)
453 {
454 struct tm tm;
455 __time32_t time;
456 errno_t err;
457
458 if (!p_localtime32_s)
459 {
460 win_skip("Skipping _localtime32_s tests\n");
461 return;
462 }
463
464 errno = EBADF;
465 err = p_localtime32_s(NULL, NULL);
466 ok(err == EINVAL, "Expected _localtime32_s to return EINVAL, got %d\n", err);
467 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
468
469 errno = EBADF;
470 time = 0x12345678;
471 err = p_localtime32_s(NULL, &time);
472 ok(err == EINVAL, "Expected _localtime32_s to return EINVAL, got %d\n", err);
473 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
474
475 memset(&tm, 0, sizeof(tm));
476 errno = EBADF;
477 err = p_localtime32_s(&tm, NULL);
478 ok(err == EINVAL, "Expected _localtime32_s to return EINVAL, got %d\n", err);
479 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
480 ok(tm.tm_sec == -1 && tm.tm_min == -1 && tm.tm_hour == -1 &&
481 tm.tm_mday == -1 && tm.tm_mon == -1 && tm.tm_year == -1 &&
482 tm.tm_wday == -1 && tm.tm_yday == -1 && tm.tm_isdst == -1,
483 "Expected tm structure members to be initialized to -1, got "
484 "(%d, %d, %d, %d, %d, %d, %d, %d, %d)\n", tm.tm_sec, tm.tm_min,
485 tm.tm_hour, tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_wday, tm.tm_yday,
486 tm.tm_isdst);
487
488 memset(&tm, 0, sizeof(tm));
489 time = -1;
490 errno = EBADF;
491 err = p_localtime32_s(&tm, &time);
492 ok(err == EINVAL, "Expected _localtime32_s to return EINVAL, got %d\n", err);
493 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
494 ok(tm.tm_sec == -1 && tm.tm_min == -1 && tm.tm_hour == -1 &&
495 tm.tm_mday == -1 && tm.tm_mon == -1 && tm.tm_year == -1 &&
496 tm.tm_wday == -1 && tm.tm_yday == -1 && tm.tm_isdst == -1,
497 "Expected tm structure members to be initialized to -1, got "
498 "(%d, %d, %d, %d, %d, %d, %d, %d, %d)\n", tm.tm_sec, tm.tm_min,
499 tm.tm_hour, tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_wday, tm.tm_yday,
500 tm.tm_isdst);
501 }
502
503 static void test_localtime64_s(void)
504 {
505 struct tm tm;
506 __time64_t time;
507 errno_t err;
508
509 if (!p_localtime64_s)
510 {
511 win_skip("Skipping _localtime64_s tests\n");
512 return;
513 }
514
515 errno = EBADF;
516 err = p_localtime64_s(NULL, NULL);
517 ok(err == EINVAL, "Expected _localtime64_s to return EINVAL, got %d\n", err);
518 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
519
520 errno = EBADF;
521 time = 0xdeadbeef;
522 err = p_localtime64_s(NULL, &time);
523 ok(err == EINVAL, "Expected _localtime64_s to return EINVAL, got %d\n", err);
524 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
525
526 memset(&tm, 0, sizeof(tm));
527 errno = EBADF;
528 err = p_localtime64_s(&tm, NULL);
529 ok(err == EINVAL, "Expected _localtime64_s to return EINVAL, got %d\n", err);
530 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
531 ok(tm.tm_sec == -1 && tm.tm_min == -1 && tm.tm_hour == -1 &&
532 tm.tm_mday == -1 && tm.tm_mon == -1 && tm.tm_year == -1 &&
533 tm.tm_wday == -1 && tm.tm_yday == -1 && tm.tm_isdst == -1,
534 "Expected tm structure members to be initialized to -1, got "
535 "(%d, %d, %d, %d, %d, %d, %d, %d, %d)\n", tm.tm_sec, tm.tm_min,
536 tm.tm_hour, tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_wday, tm.tm_yday,
537 tm.tm_isdst);
538
539 memset(&tm, 0, sizeof(tm));
540 time = -1;
541 errno = EBADF;
542 err = p_localtime64_s(&tm, &time);
543 ok(err == EINVAL, "Expected _localtime64_s to return EINVAL, got %d\n", err);
544 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
545 ok(tm.tm_sec == -1 && tm.tm_min == -1 && tm.tm_hour == -1 &&
546 tm.tm_mday == -1 && tm.tm_mon == -1 && tm.tm_year == -1 &&
547 tm.tm_wday == -1 && tm.tm_yday == -1 && tm.tm_isdst == -1,
548 "Expected tm structure members to be initialized to -1, got "
549 "(%d, %d, %d, %d, %d, %d, %d, %d, %d)\n", tm.tm_sec, tm.tm_min,
550 tm.tm_hour, tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_wday, tm.tm_yday,
551 tm.tm_isdst);
552
553 memset(&tm, 0, sizeof(tm));
554 time = _MAX__TIME64_T + 1;
555 errno = EBADF;
556 err = p_localtime64_s(&tm, &time);
557 ok(err == EINVAL, "Expected _localtime64_s to return EINVAL, got %d\n", err);
558 ok(errno == EINVAL, "Expected errno to be EINVAL, got %d\n", errno);
559 ok(tm.tm_sec == -1 && tm.tm_min == -1 && tm.tm_hour == -1 &&
560 tm.tm_mday == -1 && tm.tm_mon == -1 && tm.tm_year == -1 &&
561 tm.tm_wday == -1 && tm.tm_yday == -1 && tm.tm_isdst == -1,
562 "Expected tm structure members to be initialized to -1, got "
563 "(%d, %d, %d, %d, %d, %d, %d, %d, %d)\n", tm.tm_sec, tm.tm_min,
564 tm.tm_hour, tm.tm_mday, tm.tm_mon, tm.tm_year, tm.tm_wday, tm.tm_yday,
565 tm.tm_isdst);
566 }
567
568 static void test_daylight(void)
569 {
570 int *ret1, *ret2;
571
572 if (!p__daylight)
573 {
574 win_skip("__daylight() not available\n");
575 return;
576 }
577
578 if (!p___p__daylight)
579 {
580 win_skip("__p__daylight not available\n");
581 return;
582 }
583
584 ret1 = p__daylight();
585 ret2 = p___p__daylight();
586 ok(ret1 && ret1 == ret2, "got %p\n", ret1);
587 }
588
589 static void test_strftime(void)
590 {
591 static const wchar_t cW[] = { '%','c',0 };
592 static const char expected[] = "01/01/70 00:00:00";
593 time_t gmt;
594 struct tm* gmt_tm;
595 char buf[256], bufA[256];
596 WCHAR bufW[256];
597 long retA, retW;
598
599 if (!p_strftime || !p_wcsftime || !p_gmtime)
600 {
601 win_skip("strftime, wcsftime or gmtime is not available\n");
602 return;
603 }
604
605 setlocale(LC_TIME, "C");
606
607 gmt = 0;
608 gmt_tm = p_gmtime(&gmt);
609 ok(gmt_tm != NULL, "gmtime failed\n");
610
611 errno = 0xdeadbeef;
612 retA = strftime(NULL, 0, "copy", gmt_tm);
613 ok(retA == 0, "expected 0, got %ld\n", retA);
614 ok(errno==EINVAL || broken(errno==0xdeadbeef), "errno = %d\n", errno);
615
616 retA = strftime(bufA, 256, "copy", NULL);
617 ok(retA == 4, "expected 4, got %ld\n", retA);
618 ok(!strcmp(bufA, "copy"), "got %s\n", bufA);
619
620 retA = strftime(bufA, 256, "copy it", gmt_tm);
621 ok(retA == 7, "expected 7, got %ld\n", retA);
622 ok(!strcmp(bufA, "copy it"), "got %s\n", bufA);
623
624 errno = 0xdeadbeef;
625 retA = strftime(bufA, 2, "copy", gmt_tm);
626 ok(retA == 0, "expected 0, got %ld\n", retA);
627 ok(!strcmp(bufA, "") || broken(!strcmp(bufA, "copy it")), "got %s\n", bufA);
628 ok(errno==ERANGE || errno==0xdeadbeef, "errno = %d\n", errno);
629
630 errno = 0xdeadbeef;
631 retA = strftime(bufA, 256, "a%e", gmt_tm);
632 ok(retA==0 || broken(retA==1), "expected 0, got %ld\n", retA);
633 ok(!strcmp(bufA, "") || broken(!strcmp(bufA, "a")), "got %s\n", bufA);
634 ok(errno==EINVAL || broken(errno==0xdeadbeef), "errno = %d\n", errno);
635
636 if(0) { /* crashes on Win2k */
637 errno = 0xdeadbeef;
638 retA = strftime(bufA, 256, "%c", NULL);
639 ok(retA == 0, "expected 0, got %ld\n", retA);
640 ok(!strcmp(bufA, ""), "got %s\n", bufA);
641 ok(errno == EINVAL, "errno = %d\n", errno);
642 }
643
644 retA = strftime(bufA, 256, "e%#%e", gmt_tm);
645 ok(retA == 3, "expected 3, got %ld\n", retA);
646 ok(!strcmp(bufA, "e%e"), "got %s\n", bufA);
647
648 retA = strftime(bufA, 256, "%c", gmt_tm);
649 ok(retA == 17, "expected 17, got %ld\n", retA);
650 ok(strcmp(bufA, expected) == 0, "expected %s, got %s\n", expected, bufA);
651
652 retW = wcsftime(bufW, 256, cW, gmt_tm);
653 ok(retW == 17, "expected 17, got %ld\n", retW);
654 ok(retA == retW, "expected %ld, got %ld\n", retA, retW);
655 buf[0] = 0;
656 retA = WideCharToMultiByte(CP_ACP, 0, bufW, retW, buf, 256, NULL, NULL);
657 buf[retA] = 0;
658 ok(strcmp(bufA, buf) == 0, "expected %s, got %s\n", bufA, buf);
659
660 retA = strftime(bufA, 256, "%x", gmt_tm);
661 ok(retA == 8, "expected 8, got %ld\n", retA);
662 ok(!strcmp(bufA, "01/01/70"), "got %s\n", bufA);
663
664 retA = strftime(bufA, 256, "%X", gmt_tm);
665 ok(retA == 8, "expected 8, got %ld\n", retA);
666 ok(!strcmp(bufA, "00:00:00"), "got %s\n", bufA);
667
668 retA = strftime(bufA, 256, "%a", gmt_tm);
669 ok(retA == 3, "expected 3, got %ld\n", retA);
670 ok(!strcmp(bufA, "Thu"), "got %s\n", bufA);
671
672 retA = strftime(bufA, 256, "%A", gmt_tm);
673 ok(retA == 8, "expected 8, got %ld\n", retA);
674 ok(!strcmp(bufA, "Thursday"), "got %s\n", bufA);
675
676 retA = strftime(bufA, 256, "%b", gmt_tm);
677 ok(retA == 3, "expected 3, got %ld\n", retA);
678 ok(!strcmp(bufA, "Jan"), "got %s\n", bufA);
679
680 retA = strftime(bufA, 256, "%B", gmt_tm);
681 ok(retA == 7, "expected 7, got %ld\n", retA);
682 ok(!strcmp(bufA, "January"), "got %s\n", bufA);
683
684 retA = strftime(bufA, 256, "%d", gmt_tm);
685 ok(retA == 2, "expected 2, got %ld\n", retA);
686 ok(!strcmp(bufA, "01"), "got %s\n", bufA);
687
688 retA = strftime(bufA, 256, "%#d", gmt_tm);
689 ok(retA == 1, "expected 1, got %ld\n", retA);
690 ok(!strcmp(bufA, "1"), "got %s\n", bufA);
691
692 retA = strftime(bufA, 256, "%H", gmt_tm);
693 ok(retA == 2, "expected 2, got %ld\n", retA);
694 ok(!strcmp(bufA, "00"), "got %s\n", bufA);
695
696 retA = strftime(bufA, 256, "%I", gmt_tm);
697 ok(retA == 2, "expected 2, got %ld\n", retA);
698 ok(!strcmp(bufA, "12"), "got %s\n", bufA);
699
700 retA = strftime(bufA, 256, "%j", gmt_tm);
701 ok(retA == 3, "expected 3, got %ld\n", retA);
702 ok(!strcmp(bufA, "001"), "got %s\n", bufA);
703
704 retA = strftime(bufA, 256, "%m", gmt_tm);
705 ok(retA == 2, "expected 2, got %ld\n", retA);
706 ok(!strcmp(bufA, "01"), "got %s\n", bufA);
707
708 retA = strftime(bufA, 256, "%#M", gmt_tm);
709 ok(retA == 1, "expected 1, got %ld\n", retA);
710 ok(!strcmp(bufA, "0"), "got %s\n", bufA);
711
712 retA = strftime(bufA, 256, "%p", gmt_tm);
713 ok(retA == 2, "expected 2, got %ld\n", retA);
714 ok(!strcmp(bufA, "AM"), "got %s\n", bufA);
715
716 retA = strftime(bufA, 256, "%U", gmt_tm);
717 ok(retA == 2, "expected 2, got %ld\n", retA);
718 ok(!strcmp(bufA, "00"), "got %s\n", bufA);
719
720 retA = strftime(bufA, 256, "%W", gmt_tm);
721 ok(retA == 2, "expected 2, got %ld\n", retA);
722 ok(!strcmp(bufA, "00"), "got %s\n", bufA);
723
724 gmt_tm->tm_wday = 0;
725 retA = strftime(bufA, 256, "%U", gmt_tm);
726 ok(retA == 2, "expected 2, got %ld\n", retA);
727 ok(!strcmp(bufA, "01"), "got %s\n", bufA);
728
729 retA = strftime(bufA, 256, "%W", gmt_tm);
730 ok(retA == 2, "expected 2, got %ld\n", retA);
731 ok(!strcmp(bufA, "00"), "got %s\n", bufA);
732
733 gmt_tm->tm_yday = 365;
734 retA = strftime(bufA, 256, "%U", gmt_tm);
735 ok(retA == 2, "expected 2, got %ld\n", retA);
736 ok(!strcmp(bufA, "53"), "got %s\n", bufA);
737
738 retA = strftime(bufA, 256, "%W", gmt_tm);
739 ok(retA == 2, "expected 2, got %ld\n", retA);
740 ok(!strcmp(bufA, "52"), "got %s\n", bufA);
741
742 gmt_tm->tm_mon = 1;
743 gmt_tm->tm_mday = 30;
744 retA = strftime(bufA, 256, "%c", gmt_tm);
745 todo_wine {
746 ok(retA == 17, "expected 17, got %ld\n", retA);
747 ok(!strcmp(bufA, "02/30/70 00:00:00"), "got %s\n", bufA);
748 }
749
750 if(!setlocale(LC_ALL, "Japanese_Japan.932")) {
751 win_skip("Japanese_Japan.932 locale not available\n");
752 return;
753 }
754
755 /* test with multibyte character */
756 retA = strftime(bufA, 256, "\x82%c", gmt_tm);
757 ok(retA == 3, "expected 3, got %ld\n", retA);
758 ok(!strcmp(bufA, "\x82%c"), "got %s\n", bufA);
759 }
760
761 static void test_asctime(void)
762 {
763 struct tm* gmt_tm;
764 time_t gmt;
765 char *ret;
766
767 if(!p_asctime || !p_gmtime)
768 {
769 win_skip("asctime or gmtime is not available\n");
770 return;
771 }
772
773 gmt = 0;
774 gmt_tm = p_gmtime(&gmt);
775 ret = p_asctime(gmt_tm);
776 ok(!strcmp(ret, "Thu Jan 01 00:00:00 1970\n"), "asctime returned %s\n", ret);
777
778 gmt = 312433121;
779 gmt_tm = p_gmtime(&gmt);
780 ret = p_asctime(gmt_tm);
781 ok(!strcmp(ret, "Mon Nov 26 02:58:41 1979\n"), "asctime returned %s\n", ret);
782
783 /* Week day is only checked if it's in 0..6 range */
784 gmt_tm->tm_wday = 3;
785 ret = p_asctime(gmt_tm);
786 ok(!strcmp(ret, "Wed Nov 26 02:58:41 1979\n"), "asctime returned %s\n", ret);
787
788 errno = 0xdeadbeef;
789 gmt_tm->tm_wday = 7;
790 ret = p_asctime(gmt_tm);
791 ok(!ret || broken(!ret[0]), "asctime returned %s\n", ret);
792 ok(errno==EINVAL || broken(errno==0xdeadbeef), "errno = %d\n", errno);
793
794 /* Year day is ignored */
795 gmt_tm->tm_wday = 3;
796 gmt_tm->tm_yday = 1300;
797 ret = p_asctime(gmt_tm);
798 ok(!strcmp(ret, "Wed Nov 26 02:58:41 1979\n"), "asctime returned %s\n", ret);
799
800 /* Dates that can't be displayed using 26 characters are broken */
801 gmt_tm->tm_mday = 28;
802 gmt_tm->tm_year = 8100;
803 ret = p_asctime(gmt_tm);
804 ok(!strcmp(ret, "Wed Nov 28 02:58:41 :000\n"), "asctime returned %s\n", ret);
805
806 gmt_tm->tm_year = 264100;
807 ret = p_asctime(gmt_tm);
808 ok(!strcmp(ret, "Wed Nov 28 02:58:41 :000\n"), "asctime returned %s\n", ret);
809
810 /* asctime works from year 1900 */
811 errno = 0xdeadbeef;
812 gmt_tm->tm_year = -1;
813 ret = p_asctime(gmt_tm);
814 ok(!ret || broken(!strcmp(ret, "Wed Nov 28 02:58:41 190/\n")), "asctime returned %s\n", ret);
815 ok(errno==EINVAL || broken(errno == 0xdeadbeef), "errno = %d\n", errno);
816
817 errno = 0xdeadbeef;
818 gmt_tm->tm_mon = 1;
819 gmt_tm->tm_mday = 30;
820 gmt_tm->tm_year = 79;
821 ret = p_asctime(gmt_tm);
822 ok(!ret || broken(!strcmp(ret, "Wed Feb 30 02:58:41 1979\n")), "asctime returned %s\n", ret);
823 ok(errno==EINVAL || broken(errno==0xdeadbeef), "errno = %d\n", errno);
824 }
825
826 static void test__tzset(void)
827 {
828 char TZ_env[256];
829 int ret;
830
831 if(!p___p__daylight || !p___p__timezone || !p___p__dstbias) {
832 win_skip("__p__daylight, __p__timezone or __p__dstbias is not available\n");
833 return;
834 }
835
836 if (p__dstbias) {
837 ret = *p__dstbias();
838 ok(ret == -3600, "*__dstbias() = %d\n", ret);
839 ret = *p___p__dstbias();
840 ok(ret == -3600, "*__p__dstbias() = %d\n", ret);
841 }
842 else
843 win_skip("__dstbias() is not available.\n");
844
845 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
846
847 ret = *p___p__daylight();
848 ok(ret == 1, "*__p__daylight() = %d\n", ret);
849 ret = *p___p__timezone();
850 ok(ret == 28800, "*__p__timezone() = %d\n", ret);
851 ret = *p___p__dstbias();
852 ok(ret == -3600, "*__p__dstbias() = %d\n", ret);
853
854 _putenv("TZ=xxx+1yyy");
855 _tzset();
856 ret = *p___p__daylight();
857 ok(ret == 121, "*__p__daylight() = %d\n", ret);
858 ret = *p___p__timezone();
859 ok(ret == 3600, "*__p__timezone() = %d\n", ret);
860 ret = *p___p__dstbias();
861 ok(ret == -3600, "*__p__dstbias() = %d\n", ret);
862
863 *p___p__dstbias() = 0;
864 _putenv("TZ=xxx+1:3:5zzz");
865 _tzset();
866 ret = *p___p__daylight();
867 ok(ret == 122, "*__p__daylight() = %d\n", ret);
868 ret = *p___p__timezone();
869 ok(ret == 3785, "*__p__timezone() = %d\n", ret);
870 ret = *p___p__dstbias();
871 ok(ret == 0, "*__p__dstbias() = %d\n", ret);
872
873 _putenv(TZ_env);
874 }
875
876 static void test_clock(void)
877 {
878 static const int THRESH = 50;
879 clock_t s, e;
880 int i;
881
882 for (i = 0; i < 10; i++)
883 {
884 s = clock();
885 Sleep(1000);
886 e = clock();
887
888 ok(abs((e-s) - 1000) < THRESH, "clock off on loop %i: %i\n", i, e-s);
889 }
890 }
891
892 START_TEST(time)
893 {
894 init();
895
896 test__tzset();
897 test_strftime();
898 test_ctime();
899 test_gmtime();
900 test_mktime();
901 test_localtime();
902 test_strdate();
903 test_strtime();
904 test_wstrdate();
905 test_wstrtime();
906 test_localtime32_s();
907 test_localtime64_s();
908 test_daylight();
909 test_asctime();
910 test_clock();
911 }