shdoclc: Remove a space before an ellipsis in the Italian translation.
[wine/hramrach.git] / dlls / msvcrt / tests / time.c
blobb11ecf7862b5f7ee5a54f4947645b00567ce4d51
1 /*
2 * Unit test suite for time functions.
4 * Copyright 2004 Uwe Bonnes
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.
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.
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
21 #include "wine/test.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include "time.h"
26 #include <stdlib.h> /*setenv*/
27 #include <stdio.h> /*printf*/
28 #include <errno.h>
30 #define SECSPERDAY 86400
31 #define SECSPERHOUR 3600
32 #define SECSPERMIN 60
33 #define MINSPERHOUR 60
34 #define HOURSPERDAY 24
36 static __time32_t (__cdecl *p_mkgmtime32)(struct tm*);
37 static struct tm* (__cdecl *p_gmtime32)(__time32_t*);
38 static errno_t (__cdecl *p_gmtime32_s)(struct tm*, __time32_t*);
39 static errno_t (__cdecl *p_strtime_s)(char*,size_t);
40 static errno_t (__cdecl *p_strdate_s)(char*,size_t);
42 static void init(void)
44 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
46 p_gmtime32 = (void*)GetProcAddress(hmod, "_gmtime32");
47 p_gmtime32_s = (void*)GetProcAddress(hmod, "_gmtime32_s");
48 p_mkgmtime32 = (void*)GetProcAddress(hmod, "_mkgmtime32");
49 p_strtime_s = (void*)GetProcAddress(hmod, "_strtime_s");
50 p_strdate_s = (void*)GetProcAddress(hmod, "_strdate_s");
53 static int get_test_year(time_t *start)
55 time_t now = time(NULL);
56 struct tm *tm = localtime(&now);
58 /* compute start of year in seconds */
59 *start = SECSPERDAY * ((tm->tm_year - 70) * 365 +
60 (tm->tm_year - 69) / 4 -
61 (tm->tm_year - 1) / 100 +
62 (tm->tm_year + 299) / 400);
63 return tm->tm_year;
66 static void test_ctime(void)
68 time_t badtime = -1;
69 char* ret;
70 ret = ctime(&badtime);
71 ok(ret == NULL, "expected ctime to return NULL, got %s\n", ret);
73 static void test_gmtime(void)
75 __time32_t valid, gmt;
76 struct tm* gmt_tm, gmt_tm_s;
77 errno_t err;
79 if(!p_gmtime32) {
80 win_skip("Skipping _gmtime32 tests\n");
81 return;
84 gmt_tm = p_gmtime32(NULL);
85 ok(gmt_tm == NULL, "gmt_tm != NULL\n");
87 gmt = -1;
88 gmt_tm = p_gmtime32(&gmt);
89 ok(gmt_tm == NULL, "gmt_tm != NULL\n");
91 gmt = valid = 0;
92 gmt_tm = p_gmtime32(&gmt);
93 if(!gmt_tm) {
94 ok(0, "_gmtime32() failed\n");
95 return;
98 ok(((gmt_tm->tm_year == 70) && (gmt_tm->tm_mon == 0) && (gmt_tm->tm_yday == 0) &&
99 (gmt_tm->tm_mday == 1) && (gmt_tm->tm_wday == 4) && (gmt_tm->tm_hour == 0) &&
100 (gmt_tm->tm_min == 0) && (gmt_tm->tm_sec == 0) && (gmt_tm->tm_isdst == 0)),
101 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour%2d min %2d sec %2d dst %2d\n",
102 gmt_tm->tm_year, gmt_tm->tm_mon, gmt_tm->tm_yday, gmt_tm->tm_mday, gmt_tm->tm_wday,
103 gmt_tm->tm_hour, gmt_tm->tm_min, gmt_tm->tm_sec, gmt_tm->tm_isdst);
105 if(!p_mkgmtime32) {
106 win_skip("Skipping _mkgmtime32 tests\n");
107 return;
110 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
111 gmt = p_mkgmtime32(gmt_tm);
112 ok(gmt == valid, "gmt = %u\n", gmt);
113 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
114 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
116 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
117 gmt_tm->tm_isdst = -1;
118 gmt = p_mkgmtime32(gmt_tm);
119 ok(gmt == valid, "gmt = %u\n", gmt);
120 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
121 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
123 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
124 gmt_tm->tm_isdst = 1;
125 gmt = p_mkgmtime32(gmt_tm);
126 ok(gmt == valid, "gmt = %u\n", gmt);
127 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
128 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
130 gmt = valid = 173921;
131 gmt_tm = p_gmtime32(&gmt);
132 if(!gmt_tm) {
133 ok(0, "_gmtime32() failed\n");
134 return;
137 gmt_tm->tm_isdst = -1;
138 gmt = p_mkgmtime32(gmt_tm);
139 ok(gmt == valid, "gmt = %u\n", gmt);
140 ok(gmt_tm->tm_wday == 6, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
141 ok(gmt_tm->tm_yday == 2, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
143 gmt_tm->tm_isdst = 1;
144 gmt = p_mkgmtime32(gmt_tm);
145 ok(gmt == valid, "gmt = %u\n", gmt);
147 if(!p_gmtime32_s) {
148 win_skip("Skipping _gmtime32_s tests\n");
149 return;
152 errno = 0;
153 gmt = 0;
154 err = p_gmtime32_s(NULL, &gmt);
155 ok(err == EINVAL, "err = %d\n", err);
156 ok(errno == EINVAL, "errno = %d\n", errno);
158 errno = 0;
159 gmt = -1;
160 err = p_gmtime32_s(&gmt_tm_s, &gmt);
161 ok(err == EINVAL, "err = %d\n", err);
162 ok(errno == EINVAL, "errno = %d\n", errno);
163 ok(gmt_tm_s.tm_year == -1, "tm_year = %d\n", gmt_tm_s.tm_year);
166 static void test_mktime(void)
168 TIME_ZONE_INFORMATION tzinfo;
169 DWORD res = GetTimeZoneInformation(&tzinfo);
170 struct tm my_tm, sav_tm;
171 time_t nulltime, local_time;
172 char TZ_env[256];
173 char buffer[64];
174 int year;
175 time_t ref, secs;
177 year = get_test_year( &ref );
178 ref += SECSPERDAY;
180 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
181 WideCharToMultiByte( CP_ACP, 0, tzinfo.StandardName, -1, buffer, sizeof(buffer), NULL, NULL );
182 trace( "bias %d std %d dst %d zone %s\n",
183 tzinfo.Bias, tzinfo.StandardBias, tzinfo.DaylightBias, buffer );
184 /* Bias may be positive or negative, to use offset of one day */
185 my_tm = *localtime(&ref); /* retrieve current dst flag */
186 secs = SECSPERDAY - tzinfo.Bias * SECSPERMIN;
187 secs -= (my_tm.tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
188 my_tm.tm_mday = 1 + secs/SECSPERDAY;
189 secs = secs % SECSPERDAY;
190 my_tm.tm_hour = secs / SECSPERHOUR;
191 secs = secs % SECSPERHOUR;
192 my_tm.tm_min = secs / SECSPERMIN;
193 secs = secs % SECSPERMIN;
194 my_tm.tm_sec = secs;
196 my_tm.tm_year = year;
197 my_tm.tm_mon = 0;
199 sav_tm = my_tm;
201 local_time = mktime(&my_tm);
202 ok(local_time == ref, "mktime returned %u, expected %u\n",
203 (DWORD)local_time, (DWORD)ref);
204 /* now test some unnormalized struct tm's */
205 my_tm = sav_tm;
206 my_tm.tm_sec += 60;
207 my_tm.tm_min -= 1;
208 local_time = mktime(&my_tm);
209 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
210 (DWORD)local_time, (DWORD)ref);
211 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
212 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
213 my_tm.tm_sec == sav_tm.tm_sec,
214 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
215 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
216 my_tm.tm_hour,my_tm.tm_sec,
217 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
218 sav_tm.tm_hour,sav_tm.tm_sec);
219 my_tm = sav_tm;
220 my_tm.tm_min -= 60;
221 my_tm.tm_hour += 1;
222 local_time = mktime(&my_tm);
223 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
224 (DWORD)local_time, (DWORD)ref);
225 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
226 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
227 my_tm.tm_sec == sav_tm.tm_sec,
228 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
229 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
230 my_tm.tm_hour,my_tm.tm_sec,
231 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
232 sav_tm.tm_hour,sav_tm.tm_sec);
233 my_tm = sav_tm;
234 my_tm.tm_mon -= 12;
235 my_tm.tm_year += 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_mon += 12;
249 my_tm.tm_year -= 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 /* now a bad time example */
262 my_tm = sav_tm;
263 my_tm.tm_year = 69;
264 local_time = mktime(&my_tm);
265 ok((local_time == -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time);
267 my_tm = sav_tm;
268 /* TEST that we are independent from the TZ variable */
269 /*Argh, msvcrt doesn't have setenv() */
270 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
271 putenv("TZ=GMT");
272 nulltime = mktime(&my_tm);
273 ok(nulltime == ref,"mktime returned 0x%08x\n",(DWORD)nulltime);
274 putenv(TZ_env);
277 static void test_localtime(void)
279 TIME_ZONE_INFORMATION tzinfo;
280 DWORD res = GetTimeZoneInformation(&tzinfo);
281 time_t gmt, ref;
283 char TZ_env[256];
284 struct tm* lt;
285 int year = get_test_year( &ref );
286 int is_leap = !(year % 4) && ((year % 100) || !((year + 300) % 400));
288 gmt = ref + SECSPERDAY + tzinfo.Bias * SECSPERMIN;
289 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
290 lt = localtime(&gmt);
291 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
292 lt = localtime(&gmt);
293 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
294 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
295 (lt->tm_min == 0) && (lt->tm_sec == 0)),
296 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
297 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
298 lt->tm_min, lt->tm_sec, lt->tm_isdst);
300 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
301 putenv("TZ=GMT");
302 lt = localtime(&gmt);
303 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
304 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
305 (lt->tm_min == 0) && (lt->tm_sec == 0)),
306 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
307 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
308 lt->tm_min, lt->tm_sec, lt->tm_isdst);
309 putenv(TZ_env);
311 /* June 22 */
312 gmt = ref + 202 * SECSPERDAY + tzinfo.Bias * SECSPERMIN;
313 lt = localtime(&gmt);
314 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
315 lt = localtime(&gmt);
316 ok(((lt->tm_year == year) && (lt->tm_mon == 6) && (lt->tm_yday == 202) &&
317 (lt->tm_mday == 22 - is_leap) && (lt->tm_hour == 0) &&
318 (lt->tm_min == 0) && (lt->tm_sec == 0)),
319 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
320 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
321 lt->tm_min, lt->tm_sec, lt->tm_isdst);
324 static void test_strdate(void)
326 char date[16], * result;
327 int month, day, year, count, len;
328 errno_t err;
330 result = _strdate(date);
331 ok(result == date, "Wrong return value\n");
332 len = strlen(date);
333 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
334 count = sscanf(date, "%02d/%02d/%02d", &month, &day, &year);
335 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
337 if(!p_strdate_s) {
338 win_skip("Skipping _strdate_s tests\n");
339 return;
342 errno = 0;
343 err = p_strdate_s(NULL, 1);
344 ok(err == EINVAL, "err = %d\n", err);
345 ok(errno == EINVAL, "errno = %d\n", errno);
347 date[0] = 'x';
348 date[1] = 'x';
349 err = p_strdate_s(date, 8);
350 ok(err == ERANGE, "err = %d\n", err);
351 ok(errno == ERANGE, "errno = %d\n", errno);
352 ok(date[0] == '\0', "date[0] != '\\0'\n");
353 ok(date[1] == 'x', "date[1] != 'x'\n");
355 err = p_strdate_s(date, 9);
356 ok(err == 0, "err = %x\n", err);
359 static void test_strtime(void)
361 char time[16], * result;
362 int hour, minute, second, count, len;
363 errno_t err;
365 result = _strtime(time);
366 ok(result == time, "Wrong return value\n");
367 len = strlen(time);
368 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
369 count = sscanf(time, "%02d:%02d:%02d", &hour, &minute, &second);
370 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
372 if(!p_strtime_s) {
373 win_skip("Skipping _strtime_s tests\n");
374 return;
377 errno = 0;
378 err = p_strtime_s(NULL, 0);
379 ok(err == EINVAL, "err = %d\n", err);
380 ok(errno == EINVAL, "errno = %d\n", errno);
382 err = p_strtime_s(NULL, 1);
383 ok(err == EINVAL, "err = %d\n", err);
384 ok(errno == EINVAL, "errno = %d\n", errno);
386 time[0] = 'x';
387 err = p_strtime_s(time, 8);
388 ok(err == ERANGE, "err = %d\n", err);
389 ok(errno == ERANGE, "errno = %d\n", errno);
390 ok(time[0] == '\0', "time[0] != '\\0'\n");
392 err = p_strtime_s(time, 9);
393 ok(err == 0, "err = %x\n", err);
396 static void test_wstrdate(void)
398 wchar_t date[16], * result;
399 int month, day, year, count, len;
400 wchar_t format[] = { '%','0','2','d','/','%','0','2','d','/','%','0','2','d',0 };
402 result = _wstrdate(date);
403 ok(result == date, "Wrong return value\n");
404 len = wcslen(date);
405 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
406 count = swscanf(date, format, &month, &day, &year);
407 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
410 static void test_wstrtime(void)
412 wchar_t time[16], * result;
413 int hour, minute, second, count, len;
414 wchar_t format[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',0 };
416 result = _wstrtime(time);
417 ok(result == time, "Wrong return value\n");
418 len = wcslen(time);
419 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
420 count = swscanf(time, format, &hour, &minute, &second);
421 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
424 START_TEST(time)
426 init();
428 test_ctime();
429 test_gmtime();
430 test_mktime();
431 test_localtime();
432 test_strdate();
433 test_strtime();
434 test_wstrdate();
435 test_wstrtime();