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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "wine/test.h"
25 #define SECSPERDAY 86400
26 /* 1601 to 1970 is 369 years plus 89 leap days */
27 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
28 #define TICKSPERSEC 10000000
29 #define TICKSPERMSEC 10000
30 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
33 #define NEWYEAR_1980_HI 0x01a8e79f
34 #define NEWYEAR_1980_LO 0xe1d58000
36 #define MAYDAY_2002_HI 0x01c1f107
37 #define MAYDAY_2002_LO 0xb82b6000
39 #define ATIME_HI 0x1c2349b
40 #define ATIME_LOW 0x580716b0
42 #define LOCAL_ATIME_HI 0x01c23471
43 #define LOCAL_ATIME_LOW 0x6f310eb0
45 #define DOS_DATE(y,m,d) ( (((y)-1980)<<9) | ((m)<<5) | (d) )
46 #define DOS_TIME(h,m,s) ( ((h)<<11) | ((m)<<5) | ((s)>>1) )
49 #define SETUP_1980(st) \
56 (st).wMilliseconds = 0;
58 #define SETUP_2002(st) \
65 (st).wMilliseconds = 0;
67 #define SETUP_ATIME(st) \
74 (st).wMilliseconds = 123;
78 static void test_conversions()
83 memset(&ft,0,sizeof ft);
86 ok (SystemTimeToFileTime(&st,&ft), "Conversion Failed ATIME\n");
87 ok( (!((ft.dwHighDateTime != ATIME_HI) || (ft.dwLowDateTime!=ATIME_LOW))),
88 "Wrong time for ATIME: %08lx %08lx (correct %08x %08x)\n",
89 ft.dwLowDateTime, ft.dwHighDateTime, ATIME_LOW, ATIME_HI);
93 ok (SystemTimeToFileTime(&st, &ft), "Conversion failed 2002\n");
95 ok( (!((ft.dwHighDateTime != MAYDAY_2002_HI) ||
96 (ft.dwLowDateTime!=MAYDAY_2002_LO))),
97 "Wrong time for 2002 %08lx %08lx (correct %08x %08x)\n", ft.dwLowDateTime,
98 ft.dwHighDateTime, MAYDAY_2002_LO, MAYDAY_2002_HI);
102 ok((SystemTimeToFileTime(&st, &ft)), "Conversion failed 1980\n");
104 ok( (!((ft.dwHighDateTime!=NEWYEAR_1980_HI) ||
105 (ft.dwLowDateTime!=NEWYEAR_1980_LO))) ,
106 "Wrong time for 1980 %08lx %08lx (correct %08x %08x)\n", ft.dwLowDateTime,
107 ft.dwHighDateTime, NEWYEAR_1980_LO,NEWYEAR_1980_HI );
109 ok(DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft),
110 "DosDateTimeToFileTime() failed\n");
112 ok( (!((ft.dwHighDateTime!=NEWYEAR_1980_HI) ||
113 (ft.dwLowDateTime!=NEWYEAR_1980_LO))),
114 "Wrong time DosDateTimeToFileTime %08lx %08lx (correct %08x %08x)\n",
115 ft.dwHighDateTime, ft.dwLowDateTime, NEWYEAR_1980_HI, NEWYEAR_1980_LO);
119 static void test_invalid_arg()
124 memset(&ft,0,sizeof ft);
126 /* Invalid argument checks */
129 ok( !DosDateTimeToFileTime(0,0,&ft), /* invalid dos date/time */
130 "DosDateTimeToFileTime() didn't fail\n");
133 ok( DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft), /* this is 1 Jan 1980 00:00:00 */
134 "DosDateTimeToFileTime() failed\n");
136 ok( (ft.dwHighDateTime==NEWYEAR_1980_HI) && (ft.dwLowDateTime==NEWYEAR_1980_LO),
137 "filetime for 1/1/80 00:00:00 was %08lx %08lx\n", ft.dwHighDateTime, ft.dwLowDateTime);
139 /* now check SystemTimeToFileTime */
140 memset(&ft,0,sizeof ft);
143 /* try with a bad month */
147 ok( !SystemTimeToFileTime(&st, &ft), "bad month\n");
153 ok( !SystemTimeToFileTime(&st, &ft), "bad day\n");
155 /* with a bad hour */
159 ok( !SystemTimeToFileTime(&st, &ft), "bad hour\n");
161 /* with a bad minute */
165 ok( !SystemTimeToFileTime(&st, &ft), "bad minute\n");
168 void test_GetTimeZoneInformation()
170 TIME_ZONE_INFORMATION tzinfo, tzinfo1;
171 DWORD res = GetTimeZoneInformation(&tzinfo);
172 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
173 ok(SetEnvironmentVariableA("TZ","GMT0") != 0,
174 "SetEnvironmentVariableA failed\n");
175 res = GetTimeZoneInformation(&tzinfo1);
176 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
178 ok(((tzinfo.Bias == tzinfo1.Bias) &&
179 (tzinfo.StandardBias == tzinfo1.StandardBias) &&
180 (tzinfo.DaylightBias == tzinfo1.DaylightBias)),
181 "Bias influenced by TZ variable\n");
182 ok(SetEnvironmentVariableA("TZ",NULL) != 0,
183 "SetEnvironmentVariableA failed\n");
187 void test_FileTimeToSystemTime()
191 ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
193 ft.dwHighDateTime = 0;
194 ft.dwLowDateTime = 0;
195 ok(FileTimeToSystemTime(&ft, &st),
196 "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
197 ok(((st.wYear == 1601) && (st.wMonth == 1) && (st.wDay == 1) &&
198 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 0) &&
199 (st.wMilliseconds == 0)),
200 "Got Year %4d Month %2d Day %2d\n", st.wYear, st.wMonth, st.wDay);
202 ft.dwHighDateTime = (UINT)(time >> 32);
203 ft.dwLowDateTime = (UINT)time;
204 ok(FileTimeToSystemTime(&ft, &st),
205 "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
206 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
207 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
208 (st.wMilliseconds == 0)),
209 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
210 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
214 void test_FileTimeToLocalFileTime()
218 TIME_ZONE_INFORMATION tzinfo;
219 DWORD res = GetTimeZoneInformation(&tzinfo);
220 ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970 +
221 (LONGLONG)(tzinfo.Bias +
222 ( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
223 ( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ))) *
224 SECSPERMIN *TICKSPERSEC;
225 ok( res != TIME_ZONE_ID_INVALID , "GetTimeZoneInformation failed\n");
226 ft.dwHighDateTime = (UINT)(time >> 32);
227 ft.dwLowDateTime = (UINT)time;
228 ok(FileTimeToLocalFileTime(&ft, &lft) !=0 ,
229 "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
230 FileTimeToSystemTime(&lft, &st);
231 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
232 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
233 (st.wMilliseconds == 0)),
234 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
235 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
238 ok(SetEnvironmentVariableA("TZ","GMT") != 0,
239 "SetEnvironmentVariableA failed\n");
240 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
241 ok(FileTimeToLocalFileTime(&ft, &lft) !=0 ,
242 "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
243 FileTimeToSystemTime(&lft, &st);
244 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
245 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
246 (st.wMilliseconds == 0)),
247 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
248 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
250 ok(SetEnvironmentVariableA("TZ",NULL) != 0,
251 "SetEnvironmentVariableA failed\n");
255 /* test TzSpecificLocalTimeToSystemTime and SystemTimeToTzSpecificLocalTime
256 * these are in winXP and later */
257 typedef HANDLE (WINAPI *fnTzSpecificLocalTimeToSystemTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
258 typedef HANDLE (WINAPI *fnSystemTimeToTzSpecificLocalTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
261 int nr; /* test case number for easier lookup */
262 TIME_ZONE_INFORMATION *ptz; /* ptr to timezone */
263 SYSTEMTIME slt; /* system/local time to convert */
264 WORD ehour; /* expected hour */
267 void test_TzSpecificLocalTimeToSystemTime()
269 HMODULE hKernel = GetModuleHandle("kernel32");
270 fnTzSpecificLocalTimeToSystemTime pTzSpecificLocalTimeToSystemTime;
271 fnSystemTimeToTzSpecificLocalTime pSystemTimeToTzSpecificLocalTime = NULL;
272 TIME_ZONE_INFORMATION tzE, tzW, tzS;
275 pTzSpecificLocalTimeToSystemTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "TzSpecificLocalTimeToSystemTime");
276 if(pTzSpecificLocalTimeToSystemTime)
277 pSystemTimeToTzSpecificLocalTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
278 if( !pSystemTimeToTzSpecificLocalTime)
280 ZeroMemory( &tzE, sizeof(tzE));
281 ZeroMemory( &tzW, sizeof(tzW));
282 ZeroMemory( &tzS, sizeof(tzS));
283 /* timezone Eastern hemisphere */
286 tzE.DaylightBias=-60;
287 tzE.StandardDate.wMonth=10;
288 tzE.StandardDate.wDayOfWeek=0; /*sunday */
289 tzE.StandardDate.wDay=5; /* last (sunday) of the month */
290 tzE.StandardDate.wHour=3;
291 tzE.DaylightDate.wMonth=3;
292 tzE.DaylightDate.wDay=5;
293 tzE.DaylightDate.wHour=2;
294 /* timezone Western hemisphere */
297 tzW.DaylightBias=-60;
298 tzW.StandardDate.wMonth=10;
299 tzW.StandardDate.wDayOfWeek=0; /*sunday */
300 tzW.StandardDate.wDay=4; /* 4th (sunday) of the month */
301 tzW.StandardDate.wHour=2;
302 tzW.DaylightDate.wMonth=4;
303 tzW.DaylightDate.wDay=1;
304 tzW.DaylightDate.wHour=2;
305 /* timezone Eastern hemisphere */
308 tzS.DaylightBias=-60;
309 tzS.StandardDate.wMonth=4;
310 tzS.StandardDate.wDayOfWeek=0; /*sunday */
311 tzS.StandardDate.wDay=1; /* 1st (sunday) of the month */
312 tzS.StandardDate.wHour=2;
313 tzS.DaylightDate.wMonth=10;
314 tzS.DaylightDate.wDay=4;
315 tzS.DaylightDate.wHour=2;
317 /* TzSpecificLocalTimeToSystemTime */
318 { TZLT2ST_case cases[] = {
319 /* standard->daylight transition */
320 { 1, &tzE, {2004,3,-1,28,1,0,0,0}, 15 },
321 { 2, &tzE, {2004,3,-1,28,1,59,59,999}, 15},
322 { 3, &tzE, {2004,3,-1,28,2,0,0,0}, 15},
323 /* daylight->standard transition */
324 { 4, &tzE, {2004,10,-1,31,2,0,0,0} , 15 },
325 { 5, &tzE, {2004,10,-1,31,2,59,59,999}, 15 },
326 { 6, &tzE, {2004,10,-1,31,3,0,0,0}, 17 },
327 /* West and with fixed weekday of the month */
328 { 7, &tzW, {2004,4,-1,4,1,0,0,0}, 5},
329 { 8, &tzW, {2004,4,-1,4,1,59,59,999}, 5},
330 { 9, &tzW, {2004,4,-1,4,2,0,0,0}, 5},
331 { 10, &tzW, {2004,10,-1,24,1,0,0,0}, 4},
332 { 11, &tzW, {2004,10,-1,24,1,59,59,999}, 4},
333 { 12, &tzW, {2004,10,-1,24,2,0,0,0 }, 6},
335 { 13, &tzS, {2004,4,-1,4,1,0,0,0}, 4},
336 { 14, &tzS, {2004,4,-1,4,1,59,59,999}, 4},
337 { 15, &tzS, {2004,4,-1,4,2,0,0,0}, 6},
338 { 16, &tzS, {2004,10,-1,24,1,0,0,0}, 5},
339 { 17, &tzS, {2004,10,-1,24,1,59,59,999}, 5},
340 { 18, &tzS, {2004,10,-1,24,2,0,0,0}, 5},
343 /* days of transitions to put into the cases array */
346 {28,31,4,24,4,24} /* 1999 */
347 , {26,29,2,22,2,22} /* 2000 */
348 , {25,28,1,28,1,28} /* 2001 */
349 , {31,27,7,27,7,27} /* 2002 */
350 , {30,26,6,26,6,26} /* 2003 */
351 , {28,31,4,24,4,24} /* 2004 */
352 , {27,30,3,23,3,23} /* 2005 */
353 , {26,29,2,22,2,22} /* 2006 */
354 , {25,28,1,28,1,28} /* 2007 */
355 , {30,26,6,26,6,26} /* 2008 */
356 , {29,25,5,25,5,25} /* 2009 */
357 , {28,31,4,24,4,24} /* 2010 */
358 , {27,30,3,23,3,23} /* 2011 */
359 , {25,28,1,28,1,28} /* 2012 */
360 , {31,27,7,27,7,27} /* 2013 */
361 , {30,26,6,26,6,26} /* 2014 */
362 , {29,25,5,25,5,25} /* 2015 */
363 , {27,30,3,23,3,23} /* 2016 */
364 , {26,29,2,22,2,22} /* 2017 */
365 , {25,28,1,28,1,28} /* 2018 */
366 , {31,27,7,27,7,27} /* 2019 */
369 for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
370 for (i=0; cases[i].nr; i++) {
371 if(i) cases[i].nr += 18;
372 cases[i].slt.wYear = year;
373 cases[i].slt.wDay = yeardays[j][i/3];
374 pTzSpecificLocalTimeToSystemTime( cases[i].ptz, &(cases[i].slt), &result);
375 ok( result.wHour == cases[i].ehour,
376 "Test TzSpecificLocalTimeToSystemTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n",
377 cases[i].nr, result.wYear, result.wMonth, result.wDay,
378 result.wHour, result.wMinute, cases[i].ehour);
382 /* SystemTimeToTzSpecificLocalTime */
383 { TZLT2ST_case cases[] = {
384 /* standard->daylight transition */
385 { 1, &tzE, {2004,3,-1,27,15,0,0,0}, 1 },
386 { 2, &tzE, {2004,3,-1,27,15,59,59,999}, 1},
387 { 3, &tzE, {2004,3,-1,27,16,0,0,0}, 3},
388 /* daylight->standard transition */
389 { 4, &tzE, {2004,10,-1,30,15,0,0,0}, 2 },
390 { 5, &tzE, {2004,10,-1,30,15,59,59,999}, 2 },
391 { 6, &tzE, {2004,10,-1,30,16,0,0,0}, 2 },
392 /* West and with fixed weekday of the month */
393 { 7, &tzW, {2004,4,-1,4,5,0,0,0}, 1},
394 { 8, &tzW, {2004,4,-1,4,5,59,59,999}, 1},
395 { 9, &tzW, {2004,4,-1,4,6,0,0,0}, 3},
396 { 10, &tzW, {2004,10,-1,24,4,0,0,0}, 1},
397 { 11, &tzW, {2004,10,-1,24,4,59,59,999}, 1},
398 { 12, &tzW, {2004,10,-1,24,5,0,0,0 }, 1},
400 { 13, &tzS, {2004,4,-1,4,4,0,0,0}, 1},
401 { 14, &tzS, {2004,4,-1,4,4,59,59,999}, 1},
402 { 15, &tzS, {2004,4,-1,4,5,0,0,0}, 1},
403 { 16, &tzS, {2004,10,-1,24,5,0,0,0}, 1},
404 { 17, &tzS, {2004,10,-1,24,5,59,59,999}, 1},
405 { 18, &tzS, {2004,10,-1,24,6,0,0,0}, 3},
409 /* days of transitions to put into the cases array */
412 {27,30,4,24,4,24} /* 1999 */
413 , {25,28,2,22,2,22} /* 2000 */
414 , {24,27,1,28,1,28} /* 2001 */
415 , {30,26,7,27,7,27} /* 2002 */
416 , {29,25,6,26,6,26} /* 2003 */
417 , {27,30,4,24,4,24} /* 2004 */
418 , {26,29,3,23,3,23} /* 2005 */
419 , {25,28,2,22,2,22} /* 2006 */
420 , {24,27,1,28,1,28} /* 2007 */
421 , {29,25,6,26,6,26} /* 2008 */
422 , {28,24,5,25,5,25} /* 2009 */
423 , {27,30,4,24,4,24} /* 2010 */
424 , {26,29,3,23,3,23} /* 2011 */
425 , {24,27,1,28,1,28} /* 2012 */
426 , {30,26,7,27,7,27} /* 2013 */
427 , {29,25,6,26,6,26} /* 2014 */
428 , {28,24,5,25,5,25} /* 2015 */
429 , {26,29,3,23,3,23} /* 2016 */
430 , {25,28,2,22,2,22} /* 2017 */
431 , {24,27,1,28,1,28} /* 2018 */
432 , {30,26,7,27,7,27} /* 2019 */
434 for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
435 for (i=0; cases[i].nr; i++) {
436 if(i) cases[i].nr += 18;
437 cases[i].slt.wYear = year;
438 cases[i].slt.wDay = yeardays[j][i/3];
439 pSystemTimeToTzSpecificLocalTime( cases[i].ptz, &(cases[i].slt), &result);
440 ok( result.wHour == cases[i].ehour,
441 "Test SystemTimeToTzSpecificLocalTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n",
442 cases[i].nr, result.wYear, result.wMonth, result.wDay,
443 result.wHour, result.wMinute, cases[i].ehour);
454 test_GetTimeZoneInformation();
455 test_FileTimeToSystemTime();
456 test_FileTimeToLocalFileTime();
457 test_TzSpecificLocalTimeToSystemTime();