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