kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / msvcrt / tests / time.c
blob7f0a80b7542d53d5f15cd5ac6234786539303f08
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*/
29 #define SECSPERDAY 86400
30 #define SECSPERHOUR 3600
31 #define SECSPERMIN 60
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);
45 return tm->tm_year;
48 static void test_ctime(void)
50 time_t badtime = -1;
51 char* ret;
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;
62 struct tm* gmt_tm;
64 p_gmtime32 = (void*)GetProcAddress(hmod, "_gmtime32");
65 if(!p_gmtime32) {
66 win_skip("Skipping _gmtime32 tests\n");
67 return;
70 gmt = valid = 0;
71 gmt_tm = p_gmtime32(&gmt);
72 if(!gmt_tm) {
73 ok(0, "_gmtime32() failed\n");
74 return;
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");
85 if(!p_mkgmtime32) {
86 win_skip("Skipping _mkgmtime32 tests\n");
87 return;
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);
112 if(!gmt_tm) {
113 ok(0, "_gmtime32() failed\n");
114 return;
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;
134 char TZ_env[256];
135 char buffer[64];
136 int year;
137 time_t ref, secs;
139 year = get_test_year( &ref );
140 ref += SECSPERDAY;
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;
156 my_tm.tm_sec = secs;
158 my_tm.tm_year = year;
159 my_tm.tm_mon = 0;
161 sav_tm = my_tm;
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 */
167 my_tm = sav_tm;
168 my_tm.tm_sec += 60;
169 my_tm.tm_min -= 1;
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);
181 my_tm = sav_tm;
182 my_tm.tm_min -= 60;
183 my_tm.tm_hour += 1;
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);
195 my_tm = sav_tm;
196 my_tm.tm_mon -= 12;
197 my_tm.tm_year += 1;
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);
209 my_tm = sav_tm;
210 my_tm.tm_mon += 12;
211 my_tm.tm_year -= 1;
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 */
224 my_tm = sav_tm;
225 my_tm.tm_year = 69;
226 local_time = mktime(&my_tm);
227 ok((local_time == -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time);
229 my_tm = sav_tm;
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"):""));
233 putenv("TZ=GMT");
234 nulltime = mktime(&my_tm);
235 ok(nulltime == ref,"mktime returned 0x%08x\n",(DWORD)nulltime);
236 putenv(TZ_env);
239 static void test_localtime(void)
241 TIME_ZONE_INFORMATION tzinfo;
242 DWORD res = GetTimeZoneInformation(&tzinfo);
243 time_t gmt, ref;
245 char TZ_env[256];
246 struct tm* lt;
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"):""));
263 putenv("TZ=GMT");
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);
271 putenv(TZ_env);
273 /* June 22 */
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");
293 len = strlen(date);
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");
306 len = strlen(time);
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");
320 len = wcslen(date);
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");
334 len = wcslen(time);
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);
340 START_TEST(time)
342 test_ctime();
343 test_gmtime();
344 test_mktime();
345 test_localtime();
346 test_strdate();
347 test_strtime();
348 test_wstrdate();
349 test_wstrtime();