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)
58 struct tm
* gmt_tm
= gmtime(&gmt
);
61 ok(0,"gmtime() error\n");
64 ok(((gmt_tm
->tm_year
== 70) && (gmt_tm
->tm_mon
== 0) && (gmt_tm
->tm_yday
== 0) &&
65 (gmt_tm
->tm_mday
== 1) && (gmt_tm
->tm_wday
== 4) && (gmt_tm
->tm_hour
== 0) &&
66 (gmt_tm
->tm_min
== 0) && (gmt_tm
->tm_sec
== 0) && (gmt_tm
->tm_isdst
== 0)),
67 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour%2d min %2d sec %2d dst %2d\n",
68 gmt_tm
->tm_year
, gmt_tm
->tm_mon
, gmt_tm
->tm_yday
, gmt_tm
->tm_mday
, gmt_tm
->tm_wday
,
69 gmt_tm
->tm_hour
, gmt_tm
->tm_min
, gmt_tm
->tm_sec
, gmt_tm
->tm_isdst
);
73 static void test_mktime(void)
75 TIME_ZONE_INFORMATION tzinfo
;
76 DWORD res
= GetTimeZoneInformation(&tzinfo
);
77 struct tm my_tm
, sav_tm
;
78 time_t nulltime
, local_time
;
84 year
= get_test_year( &ref
);
87 ok (res
!= TIME_ZONE_ID_INVALID
, "GetTimeZoneInformation failed\n");
88 WideCharToMultiByte( CP_ACP
, 0, tzinfo
.StandardName
, -1, buffer
, sizeof(buffer
), NULL
, NULL
);
89 trace( "bias %d std %d dst %d zone %s\n",
90 tzinfo
.Bias
, tzinfo
.StandardBias
, tzinfo
.DaylightBias
, buffer
);
91 /* Bias may be positive or negative, to use offset of one day */
92 my_tm
= *localtime(&ref
); /* retrieve current dst flag */
93 secs
= SECSPERDAY
- tzinfo
.Bias
* SECSPERMIN
;
94 secs
-= (my_tm
.tm_isdst
? tzinfo
.DaylightBias
: tzinfo
.StandardBias
) * SECSPERMIN
;
95 my_tm
.tm_mday
= 1 + secs
/SECSPERDAY
;
96 secs
= secs
% SECSPERDAY
;
97 my_tm
.tm_hour
= secs
/ SECSPERHOUR
;
98 secs
= secs
% SECSPERHOUR
;
99 my_tm
.tm_min
= secs
/ SECSPERMIN
;
100 secs
= secs
% SECSPERMIN
;
103 my_tm
.tm_year
= year
;
108 local_time
= mktime(&my_tm
);
109 ok(local_time
== ref
, "mktime returned %u, expected %u\n",
110 (DWORD
)local_time
, (DWORD
)ref
);
111 /* now test some unnormalized struct tm's */
115 local_time
= mktime(&my_tm
);
116 ok(local_time
== ref
, "Unnormalized mktime returned %u, expected %u\n",
117 (DWORD
)local_time
, (DWORD
)ref
);
118 ok( my_tm
.tm_year
== sav_tm
.tm_year
&& my_tm
.tm_mon
== sav_tm
.tm_mon
&&
119 my_tm
.tm_mday
== sav_tm
.tm_mday
&& my_tm
.tm_hour
== sav_tm
.tm_hour
&&
120 my_tm
.tm_sec
== sav_tm
.tm_sec
,
121 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
122 my_tm
.tm_year
,my_tm
.tm_mon
,my_tm
.tm_mday
,
123 my_tm
.tm_hour
,my_tm
.tm_sec
,
124 sav_tm
.tm_year
,sav_tm
.tm_mon
,sav_tm
.tm_mday
,
125 sav_tm
.tm_hour
,sav_tm
.tm_sec
);
129 local_time
= mktime(&my_tm
);
130 ok(local_time
== ref
, "Unnormalized mktime returned %u, expected %u\n",
131 (DWORD
)local_time
, (DWORD
)ref
);
132 ok( my_tm
.tm_year
== sav_tm
.tm_year
&& my_tm
.tm_mon
== sav_tm
.tm_mon
&&
133 my_tm
.tm_mday
== sav_tm
.tm_mday
&& my_tm
.tm_hour
== sav_tm
.tm_hour
&&
134 my_tm
.tm_sec
== sav_tm
.tm_sec
,
135 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
136 my_tm
.tm_year
,my_tm
.tm_mon
,my_tm
.tm_mday
,
137 my_tm
.tm_hour
,my_tm
.tm_sec
,
138 sav_tm
.tm_year
,sav_tm
.tm_mon
,sav_tm
.tm_mday
,
139 sav_tm
.tm_hour
,sav_tm
.tm_sec
);
143 local_time
= mktime(&my_tm
);
144 ok(local_time
== ref
, "Unnormalized mktime returned %u, expected %u\n",
145 (DWORD
)local_time
, (DWORD
)ref
);
146 ok( my_tm
.tm_year
== sav_tm
.tm_year
&& my_tm
.tm_mon
== sav_tm
.tm_mon
&&
147 my_tm
.tm_mday
== sav_tm
.tm_mday
&& my_tm
.tm_hour
== sav_tm
.tm_hour
&&
148 my_tm
.tm_sec
== sav_tm
.tm_sec
,
149 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
150 my_tm
.tm_year
,my_tm
.tm_mon
,my_tm
.tm_mday
,
151 my_tm
.tm_hour
,my_tm
.tm_sec
,
152 sav_tm
.tm_year
,sav_tm
.tm_mon
,sav_tm
.tm_mday
,
153 sav_tm
.tm_hour
,sav_tm
.tm_sec
);
157 local_time
= mktime(&my_tm
);
158 ok(local_time
== ref
, "Unnormalized mktime returned %u, expected %u\n",
159 (DWORD
)local_time
, (DWORD
)ref
);
160 ok( my_tm
.tm_year
== sav_tm
.tm_year
&& my_tm
.tm_mon
== sav_tm
.tm_mon
&&
161 my_tm
.tm_mday
== sav_tm
.tm_mday
&& my_tm
.tm_hour
== sav_tm
.tm_hour
&&
162 my_tm
.tm_sec
== sav_tm
.tm_sec
,
163 "mktime returned %2d-%02d-%02d %02d:%02d expected %2d-%02d-%02d %02d:%02d\n",
164 my_tm
.tm_year
,my_tm
.tm_mon
,my_tm
.tm_mday
,
165 my_tm
.tm_hour
,my_tm
.tm_sec
,
166 sav_tm
.tm_year
,sav_tm
.tm_mon
,sav_tm
.tm_mday
,
167 sav_tm
.tm_hour
,sav_tm
.tm_sec
);
168 /* now a bad time example */
171 local_time
= mktime(&my_tm
);
172 ok((local_time
== -1), "(bad time) mktime returned %d, expected -1\n", (int)local_time
);
175 /* TEST that we are independent from the TZ variable */
176 /*Argh, msvcrt doesn't have setenv() */
177 _snprintf(TZ_env
,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
179 nulltime
= mktime(&my_tm
);
180 ok(nulltime
== ref
,"mktime returned 0x%08x\n",(DWORD
)nulltime
);
184 static void test_localtime(void)
186 TIME_ZONE_INFORMATION tzinfo
;
187 DWORD res
= GetTimeZoneInformation(&tzinfo
);
192 int year
= get_test_year( &ref
);
193 int is_leap
= !(year
% 4) && ((year
% 100) || !((year
+ 300) % 400));
195 gmt
= ref
+ SECSPERDAY
+ tzinfo
.Bias
* SECSPERMIN
;
196 ok (res
!= TIME_ZONE_ID_INVALID
, "GetTimeZoneInformation failed\n");
197 lt
= localtime(&gmt
);
198 gmt
+= (lt
->tm_isdst
? tzinfo
.DaylightBias
: tzinfo
.StandardBias
) * SECSPERMIN
;
199 lt
= localtime(&gmt
);
200 ok(((lt
->tm_year
== year
) && (lt
->tm_mon
== 0) && (lt
->tm_yday
== 1) &&
201 (lt
->tm_mday
== 2) && (lt
->tm_hour
== 0) &&
202 (lt
->tm_min
== 0) && (lt
->tm_sec
== 0)),
203 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
204 lt
->tm_year
, lt
->tm_mon
, lt
->tm_yday
, lt
->tm_mday
, lt
->tm_wday
, lt
->tm_hour
,
205 lt
->tm_min
, lt
->tm_sec
, lt
->tm_isdst
);
207 _snprintf(TZ_env
,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
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
);
219 gmt
= ref
+ 202 * SECSPERDAY
+ tzinfo
.Bias
* SECSPERMIN
;
220 lt
= localtime(&gmt
);
221 gmt
+= (lt
->tm_isdst
? tzinfo
.DaylightBias
: tzinfo
.StandardBias
) * SECSPERMIN
;
222 lt
= localtime(&gmt
);
223 ok(((lt
->tm_year
== year
) && (lt
->tm_mon
== 6) && (lt
->tm_yday
== 202) &&
224 (lt
->tm_mday
== 22 - is_leap
) && (lt
->tm_hour
== 0) &&
225 (lt
->tm_min
== 0) && (lt
->tm_sec
== 0)),
226 "Wrong date:Year %d mon %d yday %d mday %d wday %d hour %d min %d sec %d dst %d\n",
227 lt
->tm_year
, lt
->tm_mon
, lt
->tm_yday
, lt
->tm_mday
, lt
->tm_wday
, lt
->tm_hour
,
228 lt
->tm_min
, lt
->tm_sec
, lt
->tm_isdst
);
231 static void test_strdate(void)
233 char date
[16], * result
;
234 int month
, day
, year
, count
, len
;
236 result
= _strdate(date
);
237 ok(result
== date
, "Wrong return value\n");
239 ok(len
== 8, "Wrong length: returned %d, should be 8\n", len
);
240 count
= sscanf(date
, "%02d/%02d/%02d", &month
, &day
, &year
);
241 ok(count
== 3, "Wrong format: count = %d, should be 3\n", count
);
244 static void test_strtime(void)
246 char time
[16], * result
;
247 int hour
, minute
, second
, count
, len
;
249 result
= _strtime(time
);
250 ok(result
== time
, "Wrong return value\n");
252 ok(len
== 8, "Wrong length: returned %d, should be 8\n", len
);
253 count
= sscanf(time
, "%02d:%02d:%02d", &hour
, &minute
, &second
);
254 ok(count
== 3, "Wrong format: count = %d, should be 3\n", count
);
257 static void test_wstrdate(void)
259 wchar_t date
[16], * result
;
260 int month
, day
, year
, count
, len
;
261 wchar_t format
[] = { '%','0','2','d','/','%','0','2','d','/','%','0','2','d',0 };
263 result
= _wstrdate(date
);
264 ok(result
== date
, "Wrong return value\n");
266 ok(len
== 8, "Wrong length: returned %d, should be 8\n", len
);
267 count
= swscanf(date
, format
, &month
, &day
, &year
);
268 ok(count
== 3, "Wrong format: count = %d, should be 3\n", count
);
271 static void test_wstrtime(void)
273 wchar_t time
[16], * result
;
274 int hour
, minute
, second
, count
, len
;
275 wchar_t format
[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',0 };
277 result
= _wstrtime(time
);
278 ok(result
== time
, "Wrong return value\n");
280 ok(len
== 8, "Wrong length: returned %d, should be 8\n", len
);
281 count
= swscanf(time
, format
, &hour
, &minute
, &second
);
282 ok(count
== 3, "Wrong format: count = %d, should be 3\n", count
);