f04571a2ae9f23dcae0fd38a1283673f21df174b
[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
29 #define SECSPERDAY 86400
30 #define SECSPERHOUR 3600
31 #define SECSPERMIN 60
32 #define MINSPERHOUR 60
33 #define HOURSPERDAY 24
34
35 #define NDEBUG
36 #include <debug.h>
37
38 static int get_test_year(time_t *start)
39 {
40 time_t now = time(NULL);
41 struct tm *tm = localtime(&now);
42
43 /* compute start of year in seconds */
44 *start = SECSPERDAY * ((tm->tm_year - 70) * 365 +
45 (tm->tm_year - 69) / 4 -
46 (tm->tm_year - 1) / 100 +
47 (tm->tm_year + 299) / 400);
48 return tm->tm_year;
49 }
50
51 static void test_ctime(void)
52 {
53 time_t badtime = -1;
54 char* ret;
55 ret = ctime(&badtime);
56 ok(ret == NULL, "expected ctime to return NULL, got %s\n", ret);
57 }
58 static void test_gmtime(void)
59 {
60 time_t gmt = 0;
61 struct tm* gmt_tm = gmtime(&gmt);
62 if(gmt_tm == 0)
63 {
64 ok(0,"gmtime() error\n");
65 return;
66 }
67 ok(((gmt_tm->tm_year == 70) && (gmt_tm->tm_mon == 0) && (gmt_tm->tm_yday == 0) &&
68 (gmt_tm->tm_mday == 1) && (gmt_tm->tm_wday == 4) && (gmt_tm->tm_hour == 0) &&
69 (gmt_tm->tm_min == 0) && (gmt_tm->tm_sec == 0) && (gmt_tm->tm_isdst == 0)),
70 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour%2d min %2d sec %2d dst %2d\n",
71 gmt_tm->tm_year, gmt_tm->tm_mon, gmt_tm->tm_yday, gmt_tm->tm_mday, gmt_tm->tm_wday,
72 gmt_tm->tm_hour, gmt_tm->tm_min, gmt_tm->tm_sec, gmt_tm->tm_isdst);
73
74 }
75
76 static void test_mktime(void)
77 {
78 TIME_ZONE_INFORMATION tzinfo;
79 DWORD res = GetTimeZoneInformation(&tzinfo);
80 struct tm my_tm, sav_tm;
81 time_t nulltime, local_time;
82 char TZ_env[256];
83 char buffer[64];
84 int year;
85 time_t ref, secs;
86
87 year = get_test_year( &ref );
88 ref += SECSPERDAY;
89
90 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
91 WideCharToMultiByte( CP_ACP, 0, tzinfo.StandardName, -1, buffer, sizeof(buffer), NULL, NULL );
92 trace( "bias %d std %d dst %d zone %s\n",
93 tzinfo.Bias, tzinfo.StandardBias, tzinfo.DaylightBias, buffer );
94 DPRINT1("Finshed 1st\n");
95 /* Bias may be positive or negative, to use offset of one day */
96 my_tm = *localtime(&ref); /* retrieve current dst flag */
97 secs = SECSPERDAY - tzinfo.Bias * SECSPERMIN;
98 secs -= (my_tm.tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
99 my_tm.tm_mday = 1 + secs/SECSPERDAY;
100 secs = secs % SECSPERDAY;
101 my_tm.tm_hour = secs / SECSPERHOUR;
102 secs = secs % SECSPERHOUR;
103 my_tm.tm_min = secs / SECSPERMIN;
104 secs = secs % SECSPERMIN;
105 my_tm.tm_sec = secs;
106
107 my_tm.tm_year = year;
108 my_tm.tm_mon = 0;
109
110 sav_tm = my_tm;
111
112 local_time = mktime(&my_tm);
113 ok(local_time == ref, "mktime returned %u, expected %u\n",
114 (DWORD)local_time, (DWORD)ref);
115 DPRINT1("Finshed 2nd\n");
116 /* now test some unnormalized struct tm's */
117 my_tm = sav_tm;
118 my_tm.tm_sec += 60;
119 my_tm.tm_min -= 1;
120 local_time = mktime(&my_tm);
121 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
122 (DWORD)local_time, (DWORD)ref);
123 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
124 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
125 my_tm.tm_sec == sav_tm.tm_sec,
126 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
127 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
128 my_tm.tm_hour,my_tm.tm_sec,
129 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
130 sav_tm.tm_hour,sav_tm.tm_sec);
131 DPRINT1("Finshed 3rd\n");
132 my_tm = sav_tm;
133 my_tm.tm_min -= 60;
134 my_tm.tm_hour += 1;
135 local_time = mktime(&my_tm);
136 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
137 (DWORD)local_time, (DWORD)ref);
138 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
139 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
140 my_tm.tm_sec == sav_tm.tm_sec,
141 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
142 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
143 my_tm.tm_hour,my_tm.tm_sec,
144 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
145 sav_tm.tm_hour,sav_tm.tm_sec);
146 DPRINT1("Finshed 4th\n");
147 my_tm = sav_tm;
148 my_tm.tm_mon -= 12;
149 my_tm.tm_year += 1;
150 local_time = mktime(&my_tm);
151 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
152 (DWORD)local_time, (DWORD)ref);
153 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
154 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
155 my_tm.tm_sec == sav_tm.tm_sec,
156 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
157 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
158 my_tm.tm_hour,my_tm.tm_sec,
159 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
160 sav_tm.tm_hour,sav_tm.tm_sec);
161 DPRINT1("Finshed 5th\n");
162 my_tm = sav_tm;
163 my_tm.tm_mon += 12;
164 my_tm.tm_year -= 1;
165 local_time = mktime(&my_tm);
166 ok(local_time == ref, "Unnormalized mktime returned %u, expected %u\n",
167 (DWORD)local_time, (DWORD)ref);
168 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
169 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
170 my_tm.tm_sec == sav_tm.tm_sec,
171 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
172 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
173 my_tm.tm_hour,my_tm.tm_sec,
174 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
175 sav_tm.tm_hour,sav_tm.tm_sec);
176 DPRINT1("Finshed 6th\n");
177 /* now a bad time example */
178 my_tm = sav_tm;
179 my_tm.tm_year = 69;
180 local_time = mktime(&my_tm);
181 ok((local_time == -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time);
182 DPRINT1("Finshed 7th\n");
183 my_tm = sav_tm;
184 /* TEST that we are independent from the TZ variable */
185 /*Argh, msvcrt doesn't have setenv() */
186 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
187 putenv("TZ=GMT");
188 nulltime = mktime(&my_tm);
189 ok(nulltime == ref,"mktime returned 0x%08x\n",(DWORD)nulltime);
190 DPRINT1("Finshed 8th\n");
191 putenv(TZ_env);
192 }
193
194 static void test_localtime(void)
195 {
196 TIME_ZONE_INFORMATION tzinfo;
197 DWORD res = GetTimeZoneInformation(&tzinfo);
198 time_t gmt, ref;
199
200 char TZ_env[256];
201 struct tm* lt;
202 int year = get_test_year( &ref );
203 int is_leap = !(year % 4) && ((year % 100) || !((year + 300) % 400));
204
205 gmt = ref + SECSPERDAY + tzinfo.Bias * SECSPERMIN;
206 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
207 lt = localtime(&gmt);
208 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
209 lt = localtime(&gmt);
210 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
211 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
212 (lt->tm_min == 0) && (lt->tm_sec == 0)),
213 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
214 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
215 lt->tm_min, lt->tm_sec, lt->tm_isdst);
216
217 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
218 putenv("TZ=GMT");
219 lt = localtime(&gmt);
220 ok(((lt->tm_year == year) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
221 (lt->tm_mday == 2) && (lt->tm_hour == 0) &&
222 (lt->tm_min == 0) && (lt->tm_sec == 0)),
223 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
224 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
225 lt->tm_min, lt->tm_sec, lt->tm_isdst);
226 putenv(TZ_env);
227
228 /* June 22 */
229 gmt = ref + 202 * SECSPERDAY + tzinfo.Bias * SECSPERMIN;
230 lt = localtime(&gmt);
231 gmt += (lt->tm_isdst ? tzinfo.DaylightBias : tzinfo.StandardBias) * SECSPERMIN;
232 lt = localtime(&gmt);
233 ok(((lt->tm_year == year) && (lt->tm_mon == 6) && (lt->tm_yday == 202) &&
234 (lt->tm_mday == 22 - is_leap) && (lt->tm_hour == 0) &&
235 (lt->tm_min == 0) && (lt->tm_sec == 0)),
236 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
237 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
238 lt->tm_min, lt->tm_sec, lt->tm_isdst);
239 }
240
241 static void test_strdate(void)
242 {
243 char date[16], * result;
244 int month, day, year, count, len;
245
246 result = _strdate(date);
247 ok(result == date, "Wrong return value\n");
248 len = strlen(date);
249 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
250 count = sscanf(date, "%02d/%02d/%02d", &month, &day, &year);
251 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
252 }
253
254 static void test_strtime(void)
255 {
256 char time[16], * result;
257 int hour, minute, second, count, len;
258
259 result = _strtime(time);
260 ok(result == time, "Wrong return value\n");
261 len = strlen(time);
262 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
263 count = sscanf(time, "%02d:%02d:%02d", &hour, &minute, &second);
264 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
265 }
266
267 static void test_wstrdate(void)
268 {
269 wchar_t date[16], * result;
270 int month, day, year, count, len;
271 wchar_t format[] = { '%','0','2','d','/','%','0','2','d','/','%','0','2','d',0 };
272
273 result = _wstrdate(date);
274 ok(result == date, "Wrong return value\n");
275 len = wcslen(date);
276 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
277 count = swscanf(date, format, &month, &day, &year);
278 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
279 }
280
281 static void test_wstrtime(void)
282 {
283 wchar_t time[16], * result;
284 int hour, minute, second, count, len;
285 wchar_t format[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',0 };
286
287 result = _wstrtime(time);
288 ok(result == time, "Wrong return value\n");
289 len = wcslen(time);
290 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
291 count = swscanf(time, format, &hour, &minute, &second);
292 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
293 }
294
295 START_TEST(time)
296 {
297 test_ctime();
298 DPRINT1("test_ctime finished\n");
299 test_gmtime();
300 DPRINT1("test_gmtime finished\n");
301 test_mktime();
302 DPRINT1("test_mktime finished\n");
303 test_localtime();
304 DPRINT1("test_localtime finished\n");
305 test_strdate();
306 DPRINT1("test_strdate finished\n");
307 test_strtime();
308 DPRINT1("test_strtime finished\n");
309 test_wstrdate();
310 DPRINT1("test_wstrdate finished\n");
311 test_wstrtime();
312 DPRINT1("test_wstrtime finished\n");
313 }