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*/
30 #define SECSPERDAY 86400
31 #define SECSPERHOUR 3600
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);
66 static void test_ctime(void)
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
;
80 win_skip("Skipping _gmtime32 tests\n");
84 gmt_tm
= p_gmtime32(NULL
);
85 ok(gmt_tm
== NULL
, "gmt_tm != NULL\n");
88 gmt_tm
= p_gmtime32(&gmt
);
89 ok(gmt_tm
== NULL
, "gmt_tm != NULL\n");
92 gmt_tm
= p_gmtime32(&gmt
);
94 ok(0, "_gmtime32() failed\n");
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
);
106 win_skip("Skipping _mkgmtime32 tests\n");
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
);
133 ok(0, "_gmtime32() failed\n");
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
);
148 win_skip("Skipping _gmtime32_s tests\n");
154 err
= p_gmtime32_s(NULL
, &gmt
);
155 ok(err
== EINVAL
, "err = %d\n", err
);
156 ok(errno
== EINVAL
, "errno = %d\n", errno
);
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
;
177 year
= get_test_year( &ref
);
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
;
196 my_tm
.tm_year
= year
;
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 */
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
);
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
);
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
);
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 */
264 local_time
= mktime(&my_tm
);
265 ok((local_time
== -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time
);
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"):""));
272 nulltime
= mktime(&my_tm
);
273 ok(nulltime
== ref
,"mktime returned 0x%08x\n",(DWORD
)nulltime
);
277 static void test_localtime(void)
279 TIME_ZONE_INFORMATION tzinfo
;
280 DWORD res
= GetTimeZoneInformation(&tzinfo
);
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"):""));
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
);
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
;
330 result
= _strdate(date
);
331 ok(result
== date
, "Wrong return value\n");
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
);
338 win_skip("Skipping _strdate_s tests\n");
343 err
= p_strdate_s(NULL
, 1);
344 ok(err
== EINVAL
, "err = %d\n", err
);
345 ok(errno
== EINVAL
, "errno = %d\n", errno
);
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
;
365 result
= _strtime(time
);
366 ok(result
== time
, "Wrong return value\n");
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
);
373 win_skip("Skipping _strtime_s tests\n");
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
);
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");
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");
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
);