Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / msvcrt / tests / time.c
blob441bf014e6546541a61e126965728adde375267e
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 "time.h"
25 #include <stdlib.h> /*setenv*/
26 #include <stdio.h> /*printf*/
28 #define SECSPERDAY 86400
29 #define SECSPERHOUR 3600
30 #define SECSPERMIN 60
31 #define MINSPERHOUR 60
32 #define HOURSPERDAY 24
34 static void test_gmtime(void)
36 time_t gmt = (time_t)NULL;
37 struct tm* gmt_tm = gmtime(&gmt);
38 if(gmt_tm == 0)
40 ok(0,"gmtime() error\n");
41 return;
43 ok(((gmt_tm->tm_year == 70) && (gmt_tm->tm_mon == 0) && (gmt_tm->tm_yday == 0) &&
44 (gmt_tm->tm_mday == 1) && (gmt_tm->tm_wday == 4) && (gmt_tm->tm_hour == 0) &&
45 (gmt_tm->tm_min == 0) && (gmt_tm->tm_sec == 0) && (gmt_tm->tm_isdst == 0)),
46 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour%2d min %2d sec %2d dst %2d\n",
47 gmt_tm->tm_year, gmt_tm->tm_mon, gmt_tm->tm_yday, gmt_tm->tm_mday, gmt_tm->tm_wday,
48 gmt_tm->tm_hour, gmt_tm->tm_min, gmt_tm->tm_sec, gmt_tm->tm_isdst);
51 static void test_mktime(void)
53 TIME_ZONE_INFORMATION tzinfo;
54 DWORD res = GetTimeZoneInformation(&tzinfo);
55 struct tm my_tm, sav_tm;
56 time_t nulltime, local_time;
57 char TZ_env[256];
58 int secs;
60 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
61 /* Bias may be positive or negative, to use offset of one day */
62 secs= SECSPERDAY - tzinfo.Bias * SECSPERMIN;
63 my_tm.tm_mday = 1 + secs/SECSPERDAY;
64 secs = secs % SECSPERDAY;
65 my_tm.tm_hour = secs / SECSPERHOUR;
66 secs = secs % SECSPERHOUR;
67 my_tm.tm_min = secs / SECSPERMIN;
68 secs = secs % SECSPERMIN;
69 my_tm.tm_sec = secs;
71 my_tm.tm_year = 70;
72 my_tm.tm_mon = 0;
73 my_tm.tm_isdst= 0;
75 sav_tm = my_tm;
77 local_time = mktime(&my_tm);
78 ok(((DWORD)local_time == SECSPERDAY), "mktime returned 0x%08x\n",(DWORD)local_time);
79 /* now test some unnormalized struct tm's */
80 my_tm = sav_tm;
81 my_tm.tm_sec += 60;
82 my_tm.tm_min -= 1;
83 local_time = mktime(&my_tm);
84 ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
85 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
86 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
87 my_tm.tm_sec == sav_tm.tm_sec
88 , "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
89 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
90 my_tm.tm_hour,my_tm.tm_sec,
91 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
92 sav_tm.tm_hour,sav_tm.tm_sec);
93 my_tm = sav_tm;
94 my_tm.tm_min -= 60;
95 my_tm.tm_hour += 1;
96 local_time = mktime(&my_tm);
97 ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
98 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
99 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
100 my_tm.tm_sec == sav_tm.tm_sec
101 , "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
102 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
103 my_tm.tm_hour,my_tm.tm_sec,
104 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
105 sav_tm.tm_hour,sav_tm.tm_sec);
106 my_tm = sav_tm;
107 my_tm.tm_mon -= 12;
108 my_tm.tm_year += 1;
109 local_time = mktime(&my_tm);
110 ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
111 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
112 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
113 my_tm.tm_sec == sav_tm.tm_sec
114 , "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
115 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
116 my_tm.tm_hour,my_tm.tm_sec,
117 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
118 sav_tm.tm_hour,sav_tm.tm_sec);
119 my_tm = sav_tm;
120 my_tm.tm_mon += 12;
121 my_tm.tm_year -= 1;
122 local_time = mktime(&my_tm);
123 ok(((DWORD)local_time == SECSPERDAY), "Unnormalized mktime returned 0x%08x\n",(DWORD)local_time);
124 ok( my_tm.tm_year == sav_tm.tm_year && my_tm.tm_mon == sav_tm.tm_mon &&
125 my_tm.tm_mday == sav_tm.tm_mday && my_tm.tm_hour == sav_tm.tm_hour &&
126 my_tm.tm_sec == sav_tm.tm_sec
127 , "mktime returned %3d-%02d-%02d %02d:%02d expected %3d-%02d-%02d %02d:%02d.\n",
128 my_tm.tm_year,my_tm.tm_mon,my_tm.tm_mday,
129 my_tm.tm_hour,my_tm.tm_sec,
130 sav_tm.tm_year,sav_tm.tm_mon,sav_tm.tm_mday,
131 sav_tm.tm_hour,sav_tm.tm_sec);
132 /* now a bad time example */
133 my_tm = sav_tm;
134 my_tm.tm_year -= 1;
135 local_time = mktime(&my_tm);
136 ok((local_time == -1), "(bad time) mktime returned 0x%08x\n",(DWORD)local_time);
138 my_tm = sav_tm;
139 /* TEST that we are independent from the TZ variable */
140 /*Argh, msvcrt doesn't have setenv() */
141 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
142 putenv("TZ=GMT");
143 nulltime = mktime(&my_tm);
144 ok(((DWORD)nulltime == SECSPERDAY),"mktime returned 0x%08x\n",(DWORD)nulltime);
145 putenv(TZ_env);
147 static void test_localtime(void)
149 TIME_ZONE_INFORMATION tzinfo;
150 DWORD res = GetTimeZoneInformation(&tzinfo);
151 time_t gmt = (time_t)(SECSPERDAY + tzinfo.Bias * SECSPERMIN);
153 char TZ_env[256];
154 struct tm* lt;
156 ok (res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
157 lt = localtime(&gmt);
158 ok(((lt->tm_year == 70) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
159 (lt->tm_mday == 2) && (lt->tm_wday == 5) && (lt->tm_hour == 0) &&
160 (lt->tm_min == 0) && (lt->tm_sec == 0) && (lt->tm_isdst == 0)),
161 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour %2d min %2d sec %2d dst %2d\n",
162 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
163 lt->tm_min, lt->tm_sec, lt->tm_isdst);
165 _snprintf(TZ_env,255,"TZ=%s",(getenv("TZ")?getenv("TZ"):""));
166 putenv("TZ=GMT");
167 lt = localtime(&gmt);
168 ok(((lt->tm_year == 70) && (lt->tm_mon == 0) && (lt->tm_yday == 1) &&
169 (lt->tm_mday == 2) && (lt->tm_wday == 5) && (lt->tm_hour == 0) &&
170 (lt->tm_min == 0) && (lt->tm_sec == 0) && (lt->tm_isdst == 0)),
171 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour %2d min %2d sec %2d dst %2d\n",
172 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
173 lt->tm_min, lt->tm_sec, lt->tm_isdst);
174 putenv(TZ_env);
176 /* June 22 */
177 gmt += 201 * SECSPERDAY +
178 ( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
179 ( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 )) * SECSPERMIN;
180 lt = localtime(&gmt);
181 ok(((lt->tm_year == 70) && (lt->tm_mon == 6) && (lt->tm_yday == 202) &&
182 (lt->tm_mday == 22) && (lt->tm_wday == 3) && (lt->tm_hour == 0) &&
183 (lt->tm_min == 0) && (lt->tm_sec == 0) && (lt->tm_isdst ==
184 (res == TIME_ZONE_ID_DAYLIGHT))),
185 "Wrong date:Year %4d mon %2d yday %3d mday %2d wday %1d hour %2d min %2d sec %2d dst %2d\n",
186 lt->tm_year, lt->tm_mon, lt->tm_yday, lt->tm_mday, lt->tm_wday, lt->tm_hour,
187 lt->tm_min, lt->tm_sec, lt->tm_isdst);
189 static void test_strdate(void)
191 char date[16], * result;
192 int month, day, year, count, len;
194 result = _strdate(date);
195 ok(result == date, "Wrong return value\n");
196 len = strlen(date);
197 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
198 count = sscanf(date, "%02d/%02d/%02d", &month, &day, &year);
199 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
201 static void test_strtime(void)
203 char time[16], * result;
204 int hour, minute, second, count, len;
206 result = _strtime(time);
207 ok(result == time, "Wrong return value\n");
208 len = strlen(time);
209 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
210 count = sscanf(time, "%02d:%02d:%02d", &hour, &minute, &second);
211 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
213 static void test_wstrdate(void)
215 wchar_t date[16], * result;
216 int month, day, year, count, len;
217 wchar_t format[] = { '%','0','2','d','/','%','0','2','d','/','%','0','2','d',0 };
219 result = _wstrdate(date);
220 ok(result == date, "Wrong return value\n");
221 len = wcslen(date);
222 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
223 count = swscanf(date, format, &month, &day, &year);
224 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
226 static void test_wstrtime(void)
228 wchar_t time[16], * result;
229 int hour, minute, second, count, len;
230 wchar_t format[] = { '%','0','2','d',':','%','0','2','d',':','%','0','2','d',0 };
232 result = _wstrtime(time);
233 ok(result == time, "Wrong return value\n");
234 len = wcslen(time);
235 ok(len == 8, "Wrong length: returned %d, should be 8\n", len);
236 count = swscanf(time, format, &hour, &minute, &second);
237 ok(count == 3, "Wrong format: count = %d, should be 3\n", count);
240 START_TEST(time)
242 test_gmtime();
243 test_mktime();
244 test_localtime();
245 test_strdate();
246 test_strtime();
247 test_wstrdate();
248 test_wstrtime();