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()
125 /* Invalid argument checks */
127 ft.dwHighDateTime = 12345;
128 ft.dwLowDateTime = 12345;
130 ok( 0 == DosDateTimeToFileTime(0,0,&ft),
131 "Invalid DOS date/time, DosDateTimeToFileTime() didn't fail!\n"
132 "ft.dwHighDateTime=%d, ft.dwLowDateTime=%d.\n\n", (int)ft.dwHighDateTime, (int)ft.dwLowDateTime);
135 memset(&ft,0,sizeof ft);
136 ok( DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft), /* this is 1 Jan 1980 00:00:00 */
137 "DosDateTimeToFileTime() failed\n");
139 ok( (ft.dwHighDateTime==NEWYEAR_1980_HI) && (ft.dwLowDateTime==NEWYEAR_1980_LO),
140 "filetime for 1/1/80 00:00:00 was %08lx %08lx\n", ft.dwHighDateTime, ft.dwLowDateTime);
142 /* now check SystemTimeToFileTime */
143 memset(&ft,0,sizeof ft);
146 /* try with a bad month */
150 ok( !SystemTimeToFileTime(&st, &ft), "bad month\n");
156 ft.dwHighDateTime = 11111;
157 ft.dwLowDateTime = 22222;
158 ok( 0 == SystemTimeToFileTime(&st, &ft), "SystemTimeToFileTime() succeded unexpectedly!\n"
159 "ft.dwHighDateTime=%d, ft.dwLowDateTime=%d.\n\n", (int)ft.dwHighDateTime, (int)ft.dwLowDateTime);
161 /* with a bad hour */
165 ok( !SystemTimeToFileTime(&st, &ft), "bad hour\n");
167 /* with a bad minute */
171 ok( !SystemTimeToFileTime(&st, &ft), "bad minute\n");
174 void test_GetTimeZoneInformation()
176 TIME_ZONE_INFORMATION tzinfo, tzinfo1;
177 DWORD res = GetTimeZoneInformation(&tzinfo);
178 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
179 ok(SetEnvironmentVariableA("TZ","GMT0") != 0,
180 "SetEnvironmentVariableA failed\n");
181 res = GetTimeZoneInformation(&tzinfo1);
182 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
184 ok(((tzinfo.Bias == tzinfo1.Bias) &&
185 (tzinfo.StandardBias == tzinfo1.StandardBias) &&
186 (tzinfo.DaylightBias == tzinfo1.DaylightBias)),
187 "Bias influenced by TZ variable\n");
188 ok(SetEnvironmentVariableA("TZ",NULL) != 0,
189 "SetEnvironmentVariableA failed\n");
193 void test_FileTimeToSystemTime()
197 ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
200 ft.dwHighDateTime = 0;
201 ft.dwLowDateTime = 0;
202 ret = FileTimeToSystemTime(&ft, &st);
204 "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
205 ok(((st.wYear == 1601) && (st.wMonth == 1) && (st.wDay == 1) &&
206 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 0) &&
207 (st.wMilliseconds == 0)),
208 "Got Year %4d Month %2d Day %2d\n", st.wYear, st.wMonth, st.wDay);
210 ft.dwHighDateTime = (UINT)(time >> 32);
211 ft.dwLowDateTime = (UINT)time;
212 ret = FileTimeToSystemTime(&ft, &st);
214 "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
215 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
216 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
217 (st.wMilliseconds == 0)),
218 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
219 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
223 void test_FileTimeToLocalFileTime()
227 TIME_ZONE_INFORMATION tzinfo;
228 DWORD res = GetTimeZoneInformation(&tzinfo);
229 ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970 +
230 (LONGLONG)(tzinfo.Bias +
231 ( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
232 ( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ))) *
233 SECSPERMIN *TICKSPERSEC;
236 ok( res != TIME_ZONE_ID_INVALID , "GetTimeZoneInformation failed\n");
237 ft.dwHighDateTime = (UINT)(time >> 32);
238 ft.dwLowDateTime = (UINT)time;
239 ret = FileTimeToLocalFileTime(&ft, &lft);
241 "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
242 FileTimeToSystemTime(&lft, &st);
243 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
244 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
245 (st.wMilliseconds == 0)),
246 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
247 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
250 ok(SetEnvironmentVariableA("TZ","GMT") != 0,
251 "SetEnvironmentVariableA failed\n");
252 ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
253 ret = FileTimeToLocalFileTime(&ft, &lft);
255 "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
256 FileTimeToSystemTime(&lft, &st);
257 ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
258 (st.wHour == 0) && (st.wMinute == 0) && (st.wSecond == 1) &&
259 (st.wMilliseconds == 0)),
260 "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
261 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
263 ok(SetEnvironmentVariableA("TZ",NULL) != 0,
264 "SetEnvironmentVariableA failed\n");
268 /* test TzSpecificLocalTimeToSystemTime and SystemTimeToTzSpecificLocalTime
269 * these are in winXP and later */
270 typedef HANDLE (WINAPI *fnTzSpecificLocalTimeToSystemTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
271 typedef HANDLE (WINAPI *fnSystemTimeToTzSpecificLocalTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
274 int nr; /* test case number for easier lookup */
275 TIME_ZONE_INFORMATION *ptz; /* ptr to timezone */
276 SYSTEMTIME slt; /* system/local time to convert */
277 WORD ehour; /* expected hour */
280 void test_TzSpecificLocalTimeToSystemTime()
282 HMODULE hKernel = GetModuleHandle("kernel32");
283 fnTzSpecificLocalTimeToSystemTime pTzSpecificLocalTimeToSystemTime;
284 fnSystemTimeToTzSpecificLocalTime pSystemTimeToTzSpecificLocalTime = NULL;
285 TIME_ZONE_INFORMATION tzE, tzW, tzS;
288 pTzSpecificLocalTimeToSystemTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "TzSpecificLocalTimeToSystemTime");
289 if(pTzSpecificLocalTimeToSystemTime)
290 pSystemTimeToTzSpecificLocalTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
291 if( !pSystemTimeToTzSpecificLocalTime)
293 ZeroMemory( &tzE, sizeof(tzE));
294 ZeroMemory( &tzW, sizeof(tzW));
295 ZeroMemory( &tzS, sizeof(tzS));
296 /* timezone Eastern hemisphere */
299 tzE.DaylightBias=-60;
300 tzE.StandardDate.wMonth=10;
301 tzE.StandardDate.wDayOfWeek=0; /*sunday */
302 tzE.StandardDate.wDay=5; /* last (sunday) of the month */
303 tzE.StandardDate.wHour=3;
304 tzE.DaylightDate.wMonth=3;
305 tzE.DaylightDate.wDay=5;
306 tzE.DaylightDate.wHour=2;
307 /* timezone Western hemisphere */
310 tzW.DaylightBias=-60;
311 tzW.StandardDate.wMonth=10;
312 tzW.StandardDate.wDayOfWeek=0; /*sunday */
313 tzW.StandardDate.wDay=4; /* 4th (sunday) of the month */
314 tzW.StandardDate.wHour=2;
315 tzW.DaylightDate.wMonth=4;
316 tzW.DaylightDate.wDay=1;
317 tzW.DaylightDate.wHour=2;
318 /* timezone Eastern hemisphere */
321 tzS.DaylightBias=-60;
322 tzS.StandardDate.wMonth=4;
323 tzS.StandardDate.wDayOfWeek=0; /*sunday */
324 tzS.StandardDate.wDay=1; /* 1st (sunday) of the month */
325 tzS.StandardDate.wHour=2;
326 tzS.DaylightDate.wMonth=10;
327 tzS.DaylightDate.wDay=4;
328 tzS.DaylightDate.wHour=2;
330 /* TzSpecificLocalTimeToSystemTime */
331 { TZLT2ST_case cases[] = {
332 /* standard->daylight transition */
333 { 1, &tzE, {2004,3,-1,28,1,0,0,0}, 15 },
334 { 2, &tzE, {2004,3,-1,28,1,59,59,999}, 15},
335 { 3, &tzE, {2004,3,-1,28,2,0,0,0}, 15},
336 /* daylight->standard transition */
337 { 4, &tzE, {2004,10,-1,31,2,0,0,0} , 15 },
338 { 5, &tzE, {2004,10,-1,31,2,59,59,999}, 15 },
339 { 6, &tzE, {2004,10,-1,31,3,0,0,0}, 17 },
340 /* West and with fixed weekday of the month */
341 { 7, &tzW, {2004,4,-1,4,1,0,0,0}, 5},
342 { 8, &tzW, {2004,4,-1,4,1,59,59,999}, 5},
343 { 9, &tzW, {2004,4,-1,4,2,0,0,0}, 5},
344 { 10, &tzW, {2004,10,-1,24,1,0,0,0}, 4},
345 { 11, &tzW, {2004,10,-1,24,1,59,59,999}, 4},
346 { 12, &tzW, {2004,10,-1,24,2,0,0,0 }, 6},
348 { 13, &tzS, {2004,4,-1,4,1,0,0,0}, 4},
349 { 14, &tzS, {2004,4,-1,4,1,59,59,999}, 4},
350 { 15, &tzS, {2004,4,-1,4,2,0,0,0}, 6},
351 { 16, &tzS, {2004,10,-1,24,1,0,0,0}, 5},
352 { 17, &tzS, {2004,10,-1,24,1,59,59,999}, 5},
353 { 18, &tzS, {2004,10,-1,24,2,0,0,0}, 5},
356 /* days of transitions to put into the cases array */
359 {28,31,4,24,4,24} /* 1999 */
360 , {26,29,2,22,2,22} /* 2000 */
361 , {25,28,1,28,1,28} /* 2001 */
362 , {31,27,7,27,7,27} /* 2002 */
363 , {30,26,6,26,6,26} /* 2003 */
364 , {28,31,4,24,4,24} /* 2004 */
365 , {27,30,3,23,3,23} /* 2005 */
366 , {26,29,2,22,2,22} /* 2006 */
367 , {25,28,1,28,1,28} /* 2007 */
368 , {30,26,6,26,6,26} /* 2008 */
369 , {29,25,5,25,5,25} /* 2009 */
370 , {28,31,4,24,4,24} /* 2010 */
371 , {27,30,3,23,3,23} /* 2011 */
372 , {25,28,1,28,1,28} /* 2012 */
373 , {31,27,7,27,7,27} /* 2013 */
374 , {30,26,6,26,6,26} /* 2014 */
375 , {29,25,5,25,5,25} /* 2015 */
376 , {27,30,3,23,3,23} /* 2016 */
377 , {26,29,2,22,2,22} /* 2017 */
378 , {25,28,1,28,1,28} /* 2018 */
379 , {31,27,7,27,7,27} /* 2019 */
382 for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
383 for (i=0; cases[i].nr; i++) {
384 if(i) cases[i].nr += 18;
385 cases[i].slt.wYear = year;
386 cases[i].slt.wDay = yeardays[j][i/3];
387 pTzSpecificLocalTimeToSystemTime( cases[i].ptz, &(cases[i].slt), &result);
388 ok( result.wHour == cases[i].ehour,
389 "Test TzSpecificLocalTimeToSystemTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n",
390 cases[i].nr, result.wYear, result.wMonth, result.wDay,
391 result.wHour, result.wMinute, cases[i].ehour);
395 /* SystemTimeToTzSpecificLocalTime */
396 { TZLT2ST_case cases[] = {
397 /* standard->daylight transition */
398 { 1, &tzE, {2004,3,-1,27,15,0,0,0}, 1 },
399 { 2, &tzE, {2004,3,-1,27,15,59,59,999}, 1},
400 { 3, &tzE, {2004,3,-1,27,16,0,0,0}, 3},
401 /* daylight->standard transition */
402 { 4, &tzE, {2004,10,-1,30,15,0,0,0}, 2 },
403 { 5, &tzE, {2004,10,-1,30,15,59,59,999}, 2 },
404 { 6, &tzE, {2004,10,-1,30,16,0,0,0}, 2 },
405 /* West and with fixed weekday of the month */
406 { 7, &tzW, {2004,4,-1,4,5,0,0,0}, 1},
407 { 8, &tzW, {2004,4,-1,4,5,59,59,999}, 1},
408 { 9, &tzW, {2004,4,-1,4,6,0,0,0}, 3},
409 { 10, &tzW, {2004,10,-1,24,4,0,0,0}, 1},
410 { 11, &tzW, {2004,10,-1,24,4,59,59,999}, 1},
411 { 12, &tzW, {2004,10,-1,24,5,0,0,0 }, 1},
413 { 13, &tzS, {2004,4,-1,4,4,0,0,0}, 1},
414 { 14, &tzS, {2004,4,-1,4,4,59,59,999}, 1},
415 { 15, &tzS, {2004,4,-1,4,5,0,0,0}, 1},
416 { 16, &tzS, {2004,10,-1,24,5,0,0,0}, 1},
417 { 17, &tzS, {2004,10,-1,24,5,59,59,999}, 1},
418 { 18, &tzS, {2004,10,-1,24,6,0,0,0}, 3},
422 /* days of transitions to put into the cases array */
425 {27,30,4,24,4,24} /* 1999 */
426 , {25,28,2,22,2,22} /* 2000 */
427 , {24,27,1,28,1,28} /* 2001 */
428 , {30,26,7,27,7,27} /* 2002 */
429 , {29,25,6,26,6,26} /* 2003 */
430 , {27,30,4,24,4,24} /* 2004 */
431 , {26,29,3,23,3,23} /* 2005 */
432 , {25,28,2,22,2,22} /* 2006 */
433 , {24,27,1,28,1,28} /* 2007 */
434 , {29,25,6,26,6,26} /* 2008 */
435 , {28,24,5,25,5,25} /* 2009 */
436 , {27,30,4,24,4,24} /* 2010 */
437 , {26,29,3,23,3,23} /* 2011 */
438 , {24,27,1,28,1,28} /* 2012 */
439 , {30,26,7,27,7,27} /* 2013 */
440 , {29,25,6,26,6,26} /* 2014 */
441 , {28,24,5,25,5,25} /* 2015 */
442 , {26,29,3,23,3,23} /* 2016 */
443 , {25,28,2,22,2,22} /* 2017 */
444 , {24,27,1,28,1,28} /* 2018 */
445 , {30,26,7,27,7,27} /* 2019 */
447 for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
448 for (i=0; cases[i].nr; i++) {
449 if(i) cases[i].nr += 18;
450 cases[i].slt.wYear = year;
451 cases[i].slt.wDay = yeardays[j][i/3];
452 pSystemTimeToTzSpecificLocalTime( cases[i].ptz, &(cases[i].slt), &result);
453 ok( result.wHour == cases[i].ehour,
454 "Test SystemTimeToTzSpecificLocalTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n",
455 cases[i].nr, result.wYear, result.wMonth, result.wDay,
456 result.wHour, result.wMinute, cases[i].ehour);
467 test_GetTimeZoneInformation();
468 test_FileTimeToSystemTime();
469 test_FileTimeToLocalFileTime();
470 test_TzSpecificLocalTimeToSystemTime();