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