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"
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;
76 #define SETUP_ZEROTIME(st) \
83 (st).wMilliseconds = 0;
85 #define SETUP_EARLY(st) \
92 (st).wMilliseconds = 999;
95 static void test_conversions(void)
100 memset(&ft,0,sizeof ft);
103 ok (!SystemTimeToFileTime(&st, &ft), "Conversion succeeded EARLY\n");
104 ok (GetLastError() == ERROR_INVALID_PARAMETER, "EARLY should be INVALID\n");
107 ok (SystemTimeToFileTime(&st, &ft), "Conversion failed ZERO_TIME\n");
108 ok( (!((ft.dwHighDateTime != 0) || (ft.dwLowDateTime != 0))),
109 "Wrong time for ATIME: %08x %08x (correct %08x %08x)\n",
110 ft.dwLowDateTime, ft.dwHighDateTime, 0, 0);
114 ok (SystemTimeToFileTime(&st,&ft), "Conversion Failed ATIME\n");
115 ok( (!((ft.dwHighDateTime != ATIME_HI) || (ft.dwLowDateTime!=ATIME_LOW))),
116 "Wrong time for ATIME: %08x %08x (correct %08x %08x)\n",
117 ft.dwLowDateTime, ft.dwHighDateTime, ATIME_LOW, ATIME_HI);
121 ok (SystemTimeToFileTime(&st, &ft), "Conversion failed 2002\n");
123 ok( (!((ft.dwHighDateTime != MAYDAY_2002_HI) ||
124 (ft.dwLowDateTime!=MAYDAY_2002_LO))),
125 "Wrong time for 2002 %08x %08x (correct %08x %08x)\n", ft.dwLowDateTime,
126 ft.dwHighDateTime, MAYDAY_2002_LO, MAYDAY_2002_HI);
130 ok((SystemTimeToFileTime(&st, &ft)), "Conversion failed 1980\n");
132 ok( (!((ft.dwHighDateTime!=NEWYEAR_1980_HI) ||
133 (ft.dwLowDateTime!=NEWYEAR_1980_LO))) ,
134 "Wrong time for 1980 %08x %08x (correct %08x %08x)\n", ft.dwLowDateTime,
135 ft.dwHighDateTime, NEWYEAR_1980_LO,NEWYEAR_1980_HI );
137 ok(DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft),
138 "DosDateTimeToFileTime() failed\n");
140 ok( (!((ft.dwHighDateTime!=NEWYEAR_1980_HI) ||
141 (ft.dwLowDateTime!=NEWYEAR_1980_LO))),
142 "Wrong time DosDateTimeToFileTime %08x %08x (correct %08x %08x)\n",
143 ft.dwHighDateTime, ft.dwLowDateTime, NEWYEAR_1980_HI, NEWYEAR_1980_LO);
147 static void test_invalid_arg(void)
153 /* Invalid argument checks */
155 memset(&ft,0,sizeof ft);
156 ok( DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft), /* this is 1 Jan 1980 00:00:00 */
157 "DosDateTimeToFileTime() failed\n");
159 ok( (ft.dwHighDateTime==NEWYEAR_1980_HI) && (ft.dwLowDateTime==NEWYEAR_1980_LO),
160 "filetime for 1/1/80 00:00:00 was %08x %08x\n", ft.dwHighDateTime, ft.dwLowDateTime);
162 /* now check SystemTimeToFileTime */
163 memset(&ft,0,sizeof ft);
166 /* try with a bad month */
170 ok( !SystemTimeToFileTime(&st, &ft), "bad month\n");
172 /* with a bad hour */
176 ok( !SystemTimeToFileTime(&st, &ft), "bad hour\n");
178 /* with a bad minute */
182 ok( !SystemTimeToFileTime(&st, &ft), "bad minute\n");
185 static void test_GetTimeZoneInformation(void)
187 TIME_ZONE_INFORMATION tzinfo, tzinfo1;
188 DWORD res = GetTimeZoneInformation(&tzinfo);
189 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
190 ok(SetEnvironmentVariableA("TZ","GMT0") != 0,
191 "SetEnvironmentVariableA failed\n");
192 res = GetTimeZoneInformation(&tzinfo1);
193 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
195 ok(((tzinfo.Bias == tzinfo1.Bias) &&
196 (tzinfo.StandardBias == tzinfo1.StandardBias) &&
197 (tzinfo.DaylightBias == tzinfo1.DaylightBias)),
198 "Bias influenced by TZ variable\n");
199 ok(SetEnvironmentVariableA("TZ",NULL) != 0,
200 "SetEnvironmentVariableA failed\n");
204 static void test_FileTimeToSystemTime(void)
208 ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
211 ft.dwHighDateTime = 0;
212 ft.dwLowDateTime = 0;
213 ret = FileTimeToSystemTime(&ft, &st);
215 "FileTimeToSystemTime() failed with Error %d\n",GetLastError());
216 ok(((st.wYear == 1601) && (st.wMonth == 1) && (st.wDay == 1) &&
217 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 0) &&
218 (st.wMilliseconds == 0)),
219 "Got Year %4d Month %2d Day %2d\n", st.wYear, st.wMonth, st.wDay);
221 ft.dwHighDateTime = (UINT)(time >> 32);
222 ft.dwLowDateTime = (UINT)time;
223 ret = FileTimeToSystemTime(&ft, &st);
225 "FileTimeToSystemTime() failed with Error %d\n",GetLastError());
226 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
227 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
228 (st.wMilliseconds == 0)),
229 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
230 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
234 static void test_FileTimeToLocalFileTime(void)
238 TIME_ZONE_INFORMATION tzinfo;
239 DWORD res = GetTimeZoneInformation(&tzinfo);
240 ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970 +
241 (LONGLONG)(tzinfo.Bias +
242 ( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
243 ( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ))) *
244 SECSPERMIN *TICKSPERSEC;
247 ok( res != TIME_ZONE_ID_INVALID , "GetTimeZoneInformation failed\n");
248 ft.dwHighDateTime = (UINT)(time >> 32);
249 ft.dwLowDateTime = (UINT)time;
250 ret = FileTimeToLocalFileTime(&ft, &lft);
252 "FileTimeToLocalFileTime() failed with Error %d\n",GetLastError());
253 FileTimeToSystemTime(&lft, &st);
254 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
255 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
256 (st.wMilliseconds == 0)),
257 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
258 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
261 ok(SetEnvironmentVariableA("TZ","GMT") != 0,
262 "SetEnvironmentVariableA failed\n");
263 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
264 ret = FileTimeToLocalFileTime(&ft, &lft);
266 "FileTimeToLocalFileTime() failed with Error %d\n",GetLastError());
267 FileTimeToSystemTime(&lft, &st);
268 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
269 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
270 (st.wMilliseconds == 0)),
271 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
272 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
274 ok(SetEnvironmentVariableA("TZ",NULL) != 0,
275 "SetEnvironmentVariableA failed\n");
279 /* test TzSpecificLocalTimeToSystemTime and SystemTimeToTzSpecificLocalTime
280 * these are in winXP and later */
281 typedef HANDLE (WINAPI *fnTzSpecificLocalTimeToSystemTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
282 typedef HANDLE (WINAPI *fnSystemTimeToTzSpecificLocalTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
285 int nr; /* test case number for easier lookup */
286 TIME_ZONE_INFORMATION *ptz; /* ptr to timezone */
287 SYSTEMTIME slt; /* system/local time to convert */
288 WORD ehour; /* expected hour */
291 static void test_TzSpecificLocalTimeToSystemTime(void)
293 HMODULE hKernel = GetModuleHandle("kernel32");
294 fnTzSpecificLocalTimeToSystemTime pTzSpecificLocalTimeToSystemTime;
295 fnSystemTimeToTzSpecificLocalTime pSystemTimeToTzSpecificLocalTime = NULL;
296 TIME_ZONE_INFORMATION tzE, tzW, tzS;
299 pTzSpecificLocalTimeToSystemTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "TzSpecificLocalTimeToSystemTime");
300 if(pTzSpecificLocalTimeToSystemTime)
301 pSystemTimeToTzSpecificLocalTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
302 if( !pSystemTimeToTzSpecificLocalTime)
304 ZeroMemory( &tzE, sizeof(tzE));
305 ZeroMemory( &tzW, sizeof(tzW));
306 ZeroMemory( &tzS, sizeof(tzS));
307 /* timezone Eastern hemisphere */
310 tzE.DaylightBias=-60;
311 tzE.StandardDate.wMonth=10;
312 tzE.StandardDate.wDayOfWeek=0; /*sunday */
313 tzE.StandardDate.wDay=5; /* last (sunday) of the month */
314 tzE.StandardDate.wHour=3;
315 tzE.DaylightDate.wMonth=3;
316 tzE.DaylightDate.wDay=5;
317 tzE.DaylightDate.wHour=2;
318 /* timezone Western hemisphere */
321 tzW.DaylightBias=-60;
322 tzW.StandardDate.wMonth=10;
323 tzW.StandardDate.wDayOfWeek=0; /*sunday */
324 tzW.StandardDate.wDay=4; /* 4th (sunday) of the month */
325 tzW.StandardDate.wHour=2;
326 tzW.DaylightDate.wMonth=4;
327 tzW.DaylightDate.wDay=1;
328 tzW.DaylightDate.wHour=2;
329 /* timezone Eastern hemisphere */
332 tzS.DaylightBias=-60;
333 tzS.StandardDate.wMonth=4;
334 tzS.StandardDate.wDayOfWeek=0; /*sunday */
335 tzS.StandardDate.wDay=1; /* 1st (sunday) of the month */
336 tzS.StandardDate.wHour=2;
337 tzS.DaylightDate.wMonth=10;
338 tzS.DaylightDate.wDay=4;
339 tzS.DaylightDate.wHour=2;
341 /* TzSpecificLocalTimeToSystemTime */
342 { TZLT2ST_case cases[] = {
343 /* standard->daylight transition */
344 { 1, &tzE, {2004,3,-1,28,1,0,0,0}, 15 },
345 { 2, &tzE, {2004,3,-1,28,1,59,59,999}, 15},
346 { 3, &tzE, {2004,3,-1,28,2,0,0,0}, 15},
347 /* daylight->standard transition */
348 { 4, &tzE, {2004,10,-1,31,2,0,0,0} , 15 },
349 { 5, &tzE, {2004,10,-1,31,2,59,59,999}, 15 },
350 { 6, &tzE, {2004,10,-1,31,3,0,0,0}, 17 },
351 /* West and with fixed weekday of the month */
352 { 7, &tzW, {2004,4,-1,4,1,0,0,0}, 5},
353 { 8, &tzW, {2004,4,-1,4,1,59,59,999}, 5},
354 { 9, &tzW, {2004,4,-1,4,2,0,0,0}, 5},
355 { 10, &tzW, {2004,10,-1,24,1,0,0,0}, 4},
356 { 11, &tzW, {2004,10,-1,24,1,59,59,999}, 4},
357 { 12, &tzW, {2004,10,-1,24,2,0,0,0 }, 6},
359 { 13, &tzS, {2004,4,-1,4,1,0,0,0}, 4},
360 { 14, &tzS, {2004,4,-1,4,1,59,59,999}, 4},
361 { 15, &tzS, {2004,4,-1,4,2,0,0,0}, 6},
362 { 16, &tzS, {2004,10,-1,24,1,0,0,0}, 5},
363 { 17, &tzS, {2004,10,-1,24,1,59,59,999}, 5},
364 { 18, &tzS, {2004,10,-1,24,2,0,0,0}, 5},
367 /* days of transitions to put into the cases array */
370 {28,31,4,24,4,24} /* 1999 */
371 , {26,29,2,22,2,22} /* 2000 */
372 , {25,28,1,28,1,28} /* 2001 */
373 , {31,27,7,27,7,27} /* 2002 */
374 , {30,26,6,26,6,26} /* 2003 */
375 , {28,31,4,24,4,24} /* 2004 */
376 , {27,30,3,23,3,23} /* 2005 */
377 , {26,29,2,22,2,22} /* 2006 */
378 , {25,28,1,28,1,28} /* 2007 */
379 , {30,26,6,26,6,26} /* 2008 */
380 , {29,25,5,25,5,25} /* 2009 */
381 , {28,31,4,24,4,24} /* 2010 */
382 , {27,30,3,23,3,23} /* 2011 */
383 , {25,28,1,28,1,28} /* 2012 */
384 , {31,27,7,27,7,27} /* 2013 */
385 , {30,26,6,26,6,26} /* 2014 */
386 , {29,25,5,25,5,25} /* 2015 */
387 , {27,30,3,23,3,23} /* 2016 */
388 , {26,29,2,22,2,22} /* 2017 */
389 , {25,28,1,28,1,28} /* 2018 */
390 , {31,27,7,27,7,27} /* 2019 */
393 for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
394 for (i=0; cases[i].nr; i++) {
395 if(i) cases[i].nr += 18;
396 cases[i].slt.wYear = year;
397 cases[i].slt.wDay = yeardays[j][i/3];
398 pTzSpecificLocalTimeToSystemTime( cases[i].ptz, &(cases[i].slt), &result);
399 ok( result.wHour == cases[i].ehour,
400 "Test TzSpecificLocalTimeToSystemTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n",
401 cases[i].nr, result.wYear, result.wMonth, result.wDay,
402 result.wHour, result.wMinute, cases[i].ehour);
406 /* SystemTimeToTzSpecificLocalTime */
407 { TZLT2ST_case cases[] = {
408 /* standard->daylight transition */
409 { 1, &tzE, {2004,3,-1,27,15,0,0,0}, 1 },
410 { 2, &tzE, {2004,3,-1,27,15,59,59,999}, 1},
411 { 3, &tzE, {2004,3,-1,27,16,0,0,0}, 3},
412 /* daylight->standard transition */
413 { 4, &tzE, {2004,10,-1,30,15,0,0,0}, 2 },
414 { 5, &tzE, {2004,10,-1,30,15,59,59,999}, 2 },
415 { 6, &tzE, {2004,10,-1,30,16,0,0,0}, 2 },
416 /* West and with fixed weekday of the month */
417 { 7, &tzW, {2004,4,-1,4,5,0,0,0}, 1},
418 { 8, &tzW, {2004,4,-1,4,5,59,59,999}, 1},
419 { 9, &tzW, {2004,4,-1,4,6,0,0,0}, 3},
420 { 10, &tzW, {2004,10,-1,24,4,0,0,0}, 1},
421 { 11, &tzW, {2004,10,-1,24,4,59,59,999}, 1},
422 { 12, &tzW, {2004,10,-1,24,5,0,0,0 }, 1},
424 { 13, &tzS, {2004,4,-1,4,4,0,0,0}, 1},
425 { 14, &tzS, {2004,4,-1,4,4,59,59,999}, 1},
426 { 15, &tzS, {2004,4,-1,4,5,0,0,0}, 1},
427 { 16, &tzS, {2004,10,-1,24,5,0,0,0}, 1},
428 { 17, &tzS, {2004,10,-1,24,5,59,59,999}, 1},
429 { 18, &tzS, {2004,10,-1,24,6,0,0,0}, 3},
433 /* days of transitions to put into the cases array */
436 {27,30,4,24,4,24} /* 1999 */
437 , {25,28,2,22,2,22} /* 2000 */
438 , {24,27,1,28,1,28} /* 2001 */
439 , {30,26,7,27,7,27} /* 2002 */
440 , {29,25,6,26,6,26} /* 2003 */
441 , {27,30,4,24,4,24} /* 2004 */
442 , {26,29,3,23,3,23} /* 2005 */
443 , {25,28,2,22,2,22} /* 2006 */
444 , {24,27,1,28,1,28} /* 2007 */
445 , {29,25,6,26,6,26} /* 2008 */
446 , {28,24,5,25,5,25} /* 2009 */
447 , {27,30,4,24,4,24} /* 2010 */
448 , {26,29,3,23,3,23} /* 2011 */
449 , {24,27,1,28,1,28} /* 2012 */
450 , {30,26,7,27,7,27} /* 2013 */
451 , {29,25,6,26,6,26} /* 2014 */
452 , {28,24,5,25,5,25} /* 2015 */
453 , {26,29,3,23,3,23} /* 2016 */
454 , {25,28,2,22,2,22} /* 2017 */
455 , {24,27,1,28,1,28} /* 2018 */
456 , {30,26,7,27,7,27} /* 2019 */
458 for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
459 for (i=0; cases[i].nr; i++) {
460 if(i) cases[i].nr += 18;
461 cases[i].slt.wYear = year;
462 cases[i].slt.wDay = yeardays[j][i/3];
463 pSystemTimeToTzSpecificLocalTime( cases[i].ptz, &(cases[i].slt), &result);
464 ok( result.wHour == cases[i].ehour,
465 "Test SystemTimeToTzSpecificLocalTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n",
466 cases[i].nr, result.wYear, result.wMonth, result.wDay,
467 result.wHour, result.wMinute, cases[i].ehour);
478 test_GetTimeZoneInformation();
479 test_FileTimeToSystemTime();
480 test_FileTimeToLocalFileTime();
481 test_TzSpecificLocalTimeToSystemTime();