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"
26 #include <stdlib.h> /*setenv*/
27 #include <stdio.h> /*printf*/
29 #define SECSPERDAY 86400
30 #define SECSPERHOUR 3600
32 #define MINSPERHOUR 60
33 #define HOURSPERDAY 24
35 static int get_test_year(time_t *start)
37 time_t now = time(NULL);
38 struct tm *tm = localtime(&now);
40 /* compute start of year in seconds */
41 *start = SECSPERDAY * ((tm->tm_year - 70) * 365 +
42 (tm->tm_year - 69) / 4 -
43 (tm->tm_year - 1) / 100 +
44 (tm->tm_year + 299) / 400);
48 static void test_ctime(void)
52 ret = ctime(&badtime);
53 ok(ret == NULL, "expected ctime to return NULL, got %s\n", ret);
55 static void test_gmtime(void)
57 static __time32_t (__cdecl *p_mkgmtime32)(struct tm*);
58 static struct tm* (__cdecl *p_gmtime32)(__time32_t*);
60 HMODULE hmod = GetModuleHandleA("msvcrt.dll");
61 __time32_t valid, gmt;
64 p_gmtime32 = (void*)GetProcAddress(hmod, "_gmtime32");
66 win_skip("Skipping _gmtime32 tests\n");
71 gmt_tm = p_gmtime32(&gmt);
73 ok(0, "_gmtime32() failed\n");
77 ok(((gmt_tm->tm_year == 70) && (gmt_tm->tm_mon == 0) && (gmt_tm->tm_yday == 0) &&
78 (gmt_tm->tm_mday == 1) && (gmt_tm->tm_wday == 4) && (gmt_tm->tm_hour == 0) &&
79 (gmt_tm->tm_min == 0) && (gmt_tm->tm_sec == 0) && (gmt_tm->tm_isdst == 0)),
80 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour%2d min %2d sec %2d dst %2d\n",
81 gmt_tm->tm_year, gmt_tm->tm_mon, gmt_tm->tm_yday, gmt_tm->tm_mday, gmt_tm->tm_wday,
82 gmt_tm->tm_hour, gmt_tm->tm_min, gmt_tm->tm_sec, gmt_tm->tm_isdst);
84 p_mkgmtime32 = (void*)GetProcAddress(hmod, "_mkgmtime32");
86 win_skip("Skipping _mkgmtime32 tests\n");
90 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
91 gmt = p_mkgmtime32(gmt_tm);
92 ok(gmt == valid, "gmt = %u\n", gmt);
93 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
94 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
96 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
97 gmt_tm->tm_isdst = -1;
98 gmt = p_mkgmtime32(gmt_tm);
99 ok(gmt == valid, "gmt = %u\n", gmt);
100 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
101 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
103 gmt_tm->tm_wday = gmt_tm->tm_yday = 0;
104 gmt_tm->tm_isdst = 1;
105 gmt = p_mkgmtime32(gmt_tm);
106 ok(gmt == valid, "gmt = %u\n", gmt);
107 ok(gmt_tm->tm_wday == 4, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
108 ok(gmt_tm->tm_yday == 0, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
110 gmt = valid = 173921;
111 gmt_tm = p_gmtime32(&gmt);
113 ok(0, "_gmtime32() failed\n");
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 == 6, "gmt_tm->tm_wday = %d\n", gmt_tm->tm_wday);
121 ok(gmt_tm->tm_yday == 2, "gmt_tm->tm_yday = %d\n", gmt_tm->tm_yday);
123 gmt_tm->tm_isdst = 1;
124 gmt = p_mkgmtime32(gmt_tm);
125 ok(gmt == valid, "gmt = %u\n", gmt);
128 static void test_mktime(void)
130 TIME_ZONE_INFORMATION tzinfo;
131 DWORD res = GetTimeZoneInformation(&tzinfo);
132 struct tm my_tm, sav_tm;
133 time_t nulltime, local_time;
139 year = get_test_year( &ref );
142 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
143 WideCharToMultiByte( CP_ACP, 0, tzinfo.StandardName, -1, buffer, sizeof(buffer), NULL, NULL );
144 trace( "bias %d std %d dst %d zone %s\n",
145 tzinfo.Bias, tzinfo.StandardBias, tzinfo.DaylightBias, buffer );
146 /* Bias may be positive or negative, to use offset of one day */
147 my_tm = *localtime(&ref); /* retrieve current dst flag */
148 secs = SECSPERDAY - tzinfo.Bias * SECSPERMIN;
149 secs -= (my_tm.tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
150 my_tm.tm_mday = 1 + secs/SECSPERDAY;
151 secs = secs % SECSPERDAY;
152 my_tm.tm_hour = secs / SECSPERHOUR;
153 secs = secs % SECSPERHOUR;
154 my_tm.tm_min = secs / SECSPERMIN;
155 secs = secs % SECSPERMIN;
158 my_tm.tm_year = year;
163 local_time = mktime(&my_tm);
164 ok(local_time == ref, "mktime returned %u, expected %u\n",
165 (DWORD)local_time, (DWORD)ref);
166 /* now test some unnormalized struct tm's */
170 local_time = mktime(&my_tm);
171 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
172 (DWORD)local_time, (DWORD)ref);
173 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
174 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
175 my_tm.tm_sec == sav_tm.tm_sec,
176 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
177 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
178 my_tm.tm_hour,my_tm.tm_sec,
179 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
180 sav_tm.tm_hour,sav_tm.tm_sec);
184 local_time = mktime(&my_tm);
185 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
186 (DWORD)local_time, (DWORD)ref);
187 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
188 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
189 my_tm.tm_sec == sav_tm.tm_sec,
190 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
191 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
192 my_tm.tm_hour,my_tm.tm_sec,
193 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
194 sav_tm.tm_hour,sav_tm.tm_sec);
198 local_time = mktime(&my_tm);
199 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
200 (DWORD)local_time, (DWORD)ref);
201 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
202 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
203 my_tm.tm_sec == sav_tm.tm_sec,
204 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
205 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
206 my_tm.tm_hour,my_tm.tm_sec,
207 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
208 sav_tm.tm_hour,sav_tm.tm_sec);
212 local_time = mktime(&my_tm);
213 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
214 (DWORD)local_time, (DWORD)ref);
215 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
216 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
217 my_tm.tm_sec == sav_tm.tm_sec,
218 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
219 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
220 my_tm.tm_hour,my_tm.tm_sec,
221 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
222 sav_tm.tm_hour,sav_tm.tm_sec);
223 /* now a bad time example */
226 local_time = mktime(&my_tm);
227 ok((local_time == -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time);
230 /* TEST that we are independent from the TZ variable */
231 /*Argh, msvcrt doesn't have setenv() */
232 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
234 nulltime = mktime(&my_tm);
235 ok(nulltime == ref,"mktime returned 0x%08x\n",(DWORD)nulltime);
239 static void test_localtime(void)
241 TIME_ZONE_INFORMATION tzinfo;
242 DWORD res = GetTimeZoneInformation(&tzinfo);
247 int year = get_test_year( &ref );
248 int is_leap = !(year % 4) && ((year % 100) || !((year + 300) % 400));
250 gmt = ref + SECSPERDAY + tzinfo.Bias * SECSPERMIN;
251 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
252 lt = localtime(&gmt);
253 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
254 lt = localtime(&gmt);
255 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
256 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
257 (lt->tm_min == 0) && (lt->tm_sec == 0)),
258 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
259 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
260 lt->tm_min, lt->tm_sec, lt->tm_isdst);
262 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
264 lt = localtime(&gmt);
265 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
266 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
267 (lt->tm_min == 0) && (lt->tm_sec == 0)),
268 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
269 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
270 lt->tm_min, lt->tm_sec, lt->tm_isdst);
274 gmt = ref + 202 * SECSPERDAY + tzinfo.Bias * SECSPERMIN;
275 lt = localtime(&gmt);
276 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
277 lt = localtime(&gmt);
278 ok(((lt->tm_year == year) && (lt->tm_mon == 6) && (lt->tm_yday == 202) &&
279 (lt->tm_mday == 22 - is_leap) && (lt->tm_hour == 0) &&
280 (lt->tm_min == 0) && (lt->tm_sec == 0)),
281 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
282 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
283 lt->tm_min, lt->tm_sec, lt->tm_isdst);
286 static void test_strdate(void)
288 char date[16], * result;
289 int month, day, year, count, len;
291 result = _strdate(date);
292 ok(result == date, "Wrong return value\n");
294 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
295 count = sscanf(date, "%02d/%02d/%02d", &month, &day, &year);
296 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
299 static void test_strtime(void)
301 char time[16], * result;
302 int hour, minute, second, count, len;
304 result = _strtime(time);
305 ok(result == time, "Wrong return value\n");
307 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
308 count = sscanf(time, "%02d:%02d:%02d", &hour, &minute, &second);
309 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
312 static void test_wstrdate(void)
314 wchar_t date[16], * result;
315 int month, day, year, count, len;
316 wchar_t format[] = { '%','0','2','d','/','%','0','2','d','/','%','0','2','d',0 };
318 result = _wstrdate(date);
319 ok(result == date, "Wrong return value\n");
321 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
322 count = swscanf(date, format, &month, &day, &year);
323 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
326 static void test_wstrtime(void)
328 wchar_t time[16], * result;
329 int hour, minute, second, count, len;
330 wchar_t format[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',0 };
332 result = _wstrtime(time);
333 ok(result == time, "Wrong return value\n");
335 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
336 count = swscanf(time, format, &hour, &minute, &second);
337 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);