kernel32: Rewrote toolhelp implementation only on top of ntdll functions.
[wine] / dlls / kernel / tests / time.c
1 /*
2  * Unit test suite for time functions
3  *
4  * Copyright 2004 Uwe Bonnes
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "wine/test.h"
22 #include "winbase.h"
23
24 #define SECSPERMIN         60
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)
31
32
33 #define NEWYEAR_1980_HI 0x01a8e79f
34 #define NEWYEAR_1980_LO 0xe1d58000
35
36 #define MAYDAY_2002_HI 0x01c1f107
37 #define MAYDAY_2002_LO 0xb82b6000
38
39 #define ATIME_HI  0x1c2349b
40 #define ATIME_LOW 0x580716b0
41
42 #define LOCAL_ATIME_HI  0x01c23471
43 #define LOCAL_ATIME_LOW 0x6f310eb0
44
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) )
47
48
49 #define SETUP_1980(st) \
50     (st).wYear = 1980; \
51     (st).wMonth = 1; \
52     (st).wDay = 1; \
53     (st).wHour = 0; \
54     (st).wMinute = 0; \
55     (st).wSecond = 0; \
56     (st).wMilliseconds = 0;
57
58 #define SETUP_2002(st) \
59     (st).wYear = 2002; \
60     (st).wMonth = 5; \
61     (st).wDay = 1; \
62     (st).wHour = 12; \
63     (st).wMinute = 0; \
64     (st).wSecond = 0; \
65     (st).wMilliseconds = 0;
66
67 #define SETUP_ATIME(st) \
68     (st).wYear = 2002; \
69     (st).wMonth = 7; \
70     (st).wDay = 26; \
71     (st).wHour = 11; \
72     (st).wMinute = 55; \
73     (st).wSecond = 32; \
74     (st).wMilliseconds = 123;
75
76
77
78 static void test_conversions(void)
79 {
80     FILETIME ft;
81     SYSTEMTIME st;
82
83     memset(&ft,0,sizeof ft);
84
85     SETUP_ATIME(st)
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);
90
91
92     SETUP_2002(st)
93     ok (SystemTimeToFileTime(&st, &ft), "Conversion failed 2002\n");
94
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);
99
100
101     SETUP_1980(st)
102     ok((SystemTimeToFileTime(&st, &ft)), "Conversion failed 1980\n");
103
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  );
108
109     ok(DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft),
110         "DosDateTimeToFileTime() failed\n");
111
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);
116
117 }
118
119 static void test_invalid_arg(void)
120 {
121     FILETIME ft;
122     SYSTEMTIME st;
123
124
125     /* Invalid argument checks */
126
127     memset(&ft,0,sizeof ft);
128     ok( DosDateTimeToFileTime(DOS_DATE(1980,1,1),DOS_TIME(0,0,0),&ft), /* this is 1 Jan 1980 00:00:00 */
129         "DosDateTimeToFileTime() failed\n");
130
131     ok( (ft.dwHighDateTime==NEWYEAR_1980_HI) && (ft.dwLowDateTime==NEWYEAR_1980_LO),
132         "filetime for 1/1/80 00:00:00 was %08lx %08lx\n", ft.dwHighDateTime, ft.dwLowDateTime);
133
134     /* now check SystemTimeToFileTime */
135     memset(&ft,0,sizeof ft);
136
137
138     /* try with a bad month */
139     SETUP_1980(st)
140     st.wMonth = 0;
141
142     ok( !SystemTimeToFileTime(&st, &ft), "bad month\n");
143
144     /* with a bad hour */
145     SETUP_1980(st)
146     st.wHour = 24;
147
148     ok( !SystemTimeToFileTime(&st, &ft), "bad hour\n");
149
150     /* with a bad minute */
151     SETUP_1980(st)
152     st.wMinute = 60;
153
154     ok( !SystemTimeToFileTime(&st, &ft), "bad minute\n");
155 }
156  
157 static void test_GetTimeZoneInformation(void)
158 {
159     TIME_ZONE_INFORMATION tzinfo, tzinfo1;
160     DWORD res =  GetTimeZoneInformation(&tzinfo);
161     ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
162     ok(SetEnvironmentVariableA("TZ","GMT0") != 0,
163        "SetEnvironmentVariableA failed\n");
164     res =  GetTimeZoneInformation(&tzinfo1);
165     ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
166
167     ok(((tzinfo.Bias == tzinfo1.Bias) && 
168         (tzinfo.StandardBias == tzinfo1.StandardBias) &&
169         (tzinfo.DaylightBias == tzinfo1.DaylightBias)),
170        "Bias influenced by TZ variable\n"); 
171     ok(SetEnvironmentVariableA("TZ",NULL) != 0,
172        "SetEnvironmentVariableA failed\n");
173         
174 }
175
176 static void test_FileTimeToSystemTime(void)
177 {
178     FILETIME ft;
179     SYSTEMTIME st;
180     ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
181     BOOL ret;
182
183     ft.dwHighDateTime = 0;
184     ft.dwLowDateTime  = 0;
185     ret = FileTimeToSystemTime(&ft, &st);
186     ok( ret,
187        "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
188     ok(((st.wYear == 1601) && (st.wMonth  == 1) && (st.wDay    == 1) &&
189         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 0) &&
190         (st.wMilliseconds == 0)),
191         "Got Year %4d Month %2d Day %2d\n",  st.wYear, st.wMonth, st.wDay);
192
193     ft.dwHighDateTime = (UINT)(time >> 32);
194     ft.dwLowDateTime  = (UINT)time;
195     ret = FileTimeToSystemTime(&ft, &st);
196     ok( ret,
197        "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
198     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
199         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
200         (st.wMilliseconds == 0)),
201        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
202        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
203        st.wMilliseconds);
204 }
205
206 static void test_FileTimeToLocalFileTime(void)
207 {
208     FILETIME ft, lft;
209     SYSTEMTIME st;
210     TIME_ZONE_INFORMATION tzinfo;
211     DWORD res =  GetTimeZoneInformation(&tzinfo);
212     ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970 +
213         (LONGLONG)(tzinfo.Bias + 
214             ( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
215             ( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ))) *
216              SECSPERMIN *TICKSPERSEC;
217     BOOL ret;
218
219     ok( res != TIME_ZONE_ID_INVALID , "GetTimeZoneInformation failed\n");
220     ft.dwHighDateTime = (UINT)(time >> 32);
221     ft.dwLowDateTime  = (UINT)time;
222     ret = FileTimeToLocalFileTime(&ft, &lft);
223     ok( ret,
224        "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
225     FileTimeToSystemTime(&lft, &st);
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,
231        st.wMilliseconds);
232
233     ok(SetEnvironmentVariableA("TZ","GMT") != 0,
234        "SetEnvironmentVariableA failed\n");
235     ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
236     ret = FileTimeToLocalFileTime(&ft, &lft);
237     ok( ret,
238        "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
239     FileTimeToSystemTime(&lft, &st);
240     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
241         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
242         (st.wMilliseconds == 0)),
243        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
244        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
245        st.wMilliseconds);
246     ok(SetEnvironmentVariableA("TZ",NULL) != 0,
247        "SetEnvironmentVariableA failed\n");
248 }
249
250
251 /* test TzSpecificLocalTimeToSystemTime and SystemTimeToTzSpecificLocalTime
252  * these are in winXP and later */
253 typedef HANDLE (WINAPI *fnTzSpecificLocalTimeToSystemTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
254 typedef HANDLE (WINAPI *fnSystemTimeToTzSpecificLocalTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
255
256 typedef struct {
257     int nr;             /* test case number for easier lookup */
258     TIME_ZONE_INFORMATION *ptz; /* ptr to timezone */
259     SYSTEMTIME slt;     /* system/local time to convert */
260     WORD ehour;        /* expected hour */
261 } TZLT2ST_case;
262
263 static void test_TzSpecificLocalTimeToSystemTime(void)
264 {    
265     HMODULE hKernel = GetModuleHandle("kernel32");
266     fnTzSpecificLocalTimeToSystemTime pTzSpecificLocalTimeToSystemTime;
267     fnSystemTimeToTzSpecificLocalTime pSystemTimeToTzSpecificLocalTime = NULL;
268     TIME_ZONE_INFORMATION tzE, tzW, tzS;
269     SYSTEMTIME result;
270     int i, j, year;
271     pTzSpecificLocalTimeToSystemTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "TzSpecificLocalTimeToSystemTime");
272     if(pTzSpecificLocalTimeToSystemTime)
273         pSystemTimeToTzSpecificLocalTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
274     if( !pSystemTimeToTzSpecificLocalTime)
275         return;
276     ZeroMemory( &tzE, sizeof(tzE));
277     ZeroMemory( &tzW, sizeof(tzW));
278     ZeroMemory( &tzS, sizeof(tzS));
279     /* timezone Eastern hemisphere */
280     tzE.Bias=-600;
281     tzE.StandardBias=0;
282     tzE.DaylightBias=-60;
283     tzE.StandardDate.wMonth=10;
284     tzE.StandardDate.wDayOfWeek=0; /*sunday */
285     tzE.StandardDate.wDay=5;       /* last (sunday) of the month */
286     tzE.StandardDate.wHour=3;
287     tzE.DaylightDate.wMonth=3;
288     tzE.DaylightDate.wDay=5;
289     tzE.DaylightDate.wHour=2;
290     /* timezone Western hemisphere */
291     tzW.Bias=240;
292     tzW.StandardBias=0;
293     tzW.DaylightBias=-60;
294     tzW.StandardDate.wMonth=10;
295     tzW.StandardDate.wDayOfWeek=0; /*sunday */
296     tzW.StandardDate.wDay=4;       /* 4th (sunday) of the month */
297     tzW.StandardDate.wHour=2;
298     tzW.DaylightDate.wMonth=4;
299     tzW.DaylightDate.wDay=1;
300     tzW.DaylightDate.wHour=2;
301     /* timezone Eastern hemisphere */
302     tzS.Bias=240;
303     tzS.StandardBias=0;
304     tzS.DaylightBias=-60;
305     tzS.StandardDate.wMonth=4;
306     tzS.StandardDate.wDayOfWeek=0; /*sunday */
307     tzS.StandardDate.wDay=1;       /* 1st  (sunday) of the month */
308     tzS.StandardDate.wHour=2;
309     tzS.DaylightDate.wMonth=10;
310     tzS.DaylightDate.wDay=4;
311     tzS.DaylightDate.wHour=2;
312     /*tests*/
313         /* TzSpecificLocalTimeToSystemTime */
314     {   TZLT2ST_case cases[] = {
315             /* standard->daylight transition */
316             { 1, &tzE, {2004,3,-1,28,1,0,0,0}, 15 },
317             { 2, &tzE, {2004,3,-1,28,1,59,59,999}, 15},
318             { 3, &tzE, {2004,3,-1,28,2,0,0,0}, 15},
319             /* daylight->standard transition */
320             { 4, &tzE, {2004,10,-1,31,2,0,0,0} , 15 },
321             { 5, &tzE, {2004,10,-1,31,2,59,59,999}, 15 },
322             { 6, &tzE, {2004,10,-1,31,3,0,0,0}, 17 },
323             /* West and with fixed weekday of the month */
324             { 7, &tzW, {2004,4,-1,4,1,0,0,0}, 5},
325             { 8, &tzW, {2004,4,-1,4,1,59,59,999}, 5},
326             { 9, &tzW, {2004,4,-1,4,2,0,0,0}, 5},
327             { 10, &tzW, {2004,10,-1,24,1,0,0,0}, 4},
328             { 11, &tzW, {2004,10,-1,24,1,59,59,999}, 4},
329             { 12, &tzW, {2004,10,-1,24,2,0,0,0 }, 6},
330             /* and now south */
331             { 13, &tzS, {2004,4,-1,4,1,0,0,0}, 4},
332             { 14, &tzS, {2004,4,-1,4,1,59,59,999}, 4},
333             { 15, &tzS, {2004,4,-1,4,2,0,0,0}, 6},
334             { 16, &tzS, {2004,10,-1,24,1,0,0,0}, 5},
335             { 17, &tzS, {2004,10,-1,24,1,59,59,999}, 5},
336             { 18, &tzS, {2004,10,-1,24,2,0,0,0}, 5},
337             {0}
338         };
339     /*  days of transitions to put into the cases array */
340         int yeardays[][6]=
341         {
342               {28,31,4,24,4,24}  /* 1999 */
343             , {26,29,2,22,2,22}  /* 2000 */
344             , {25,28,1,28,1,28}  /* 2001 */
345             , {31,27,7,27,7,27}  /* 2002 */
346             , {30,26,6,26,6,26}  /* 2003 */
347             , {28,31,4,24,4,24}  /* 2004 */
348             , {27,30,3,23,3,23}  /* 2005 */
349             , {26,29,2,22,2,22}  /* 2006 */
350             , {25,28,1,28,1,28}  /* 2007 */
351             , {30,26,6,26,6,26}  /* 2008 */
352             , {29,25,5,25,5,25}  /* 2009 */
353             , {28,31,4,24,4,24}  /* 2010 */
354             , {27,30,3,23,3,23}  /* 2011 */
355             , {25,28,1,28,1,28}  /* 2012 */
356             , {31,27,7,27,7,27}  /* 2013 */
357             , {30,26,6,26,6,26}  /* 2014 */
358             , {29,25,5,25,5,25}  /* 2015 */
359             , {27,30,3,23,3,23}  /* 2016 */
360             , {26,29,2,22,2,22}  /* 2017 */
361             , {25,28,1,28,1,28}  /* 2018 */
362             , {31,27,7,27,7,27}  /* 2019 */
363             ,{0}
364         };
365         for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
366             for (i=0; cases[i].nr; i++) {
367                 if(i) cases[i].nr += 18;
368                 cases[i].slt.wYear = year;
369                 cases[i].slt.wDay = yeardays[j][i/3];
370                 pTzSpecificLocalTimeToSystemTime( cases[i].ptz, &(cases[i].slt), &result);
371                 ok( result.wHour == cases[i].ehour,
372                         "Test TzSpecificLocalTimeToSystemTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour =  %02d\n", 
373                         cases[i].nr, result.wYear, result.wMonth, result.wDay,
374                         result.wHour, result.wMinute, cases[i].ehour);
375             }
376         }
377     }
378         /* SystemTimeToTzSpecificLocalTime */
379     {   TZLT2ST_case cases[] = {
380             /* standard->daylight transition */
381             { 1, &tzE, {2004,3,-1,27,15,0,0,0}, 1 },
382             { 2, &tzE, {2004,3,-1,27,15,59,59,999}, 1},
383             { 3, &tzE, {2004,3,-1,27,16,0,0,0}, 3},
384             /* daylight->standard transition */
385             { 4,  &tzE, {2004,10,-1,30,15,0,0,0}, 2 },
386             { 5, &tzE, {2004,10,-1,30,15,59,59,999}, 2 },
387             { 6, &tzE, {2004,10,-1,30,16,0,0,0}, 2 },
388             /* West and with fixed weekday of the month */
389             { 7, &tzW, {2004,4,-1,4,5,0,0,0}, 1},
390             { 8, &tzW, {2004,4,-1,4,5,59,59,999}, 1},
391             { 9, &tzW, {2004,4,-1,4,6,0,0,0}, 3},
392             { 10, &tzW, {2004,10,-1,24,4,0,0,0}, 1},
393             { 11, &tzW, {2004,10,-1,24,4,59,59,999}, 1},
394             { 12, &tzW, {2004,10,-1,24,5,0,0,0 }, 1},
395             /* and now south */
396             { 13, &tzS, {2004,4,-1,4,4,0,0,0}, 1},
397             { 14, &tzS, {2004,4,-1,4,4,59,59,999}, 1},
398             { 15, &tzS, {2004,4,-1,4,5,0,0,0}, 1},
399             { 16, &tzS, {2004,10,-1,24,5,0,0,0}, 1},
400             { 17, &tzS, {2004,10,-1,24,5,59,59,999}, 1},
401             { 18, &tzS, {2004,10,-1,24,6,0,0,0}, 3},
402
403             {0}
404         }; 
405     /*  days of transitions to put into the cases array */
406         int yeardays[][6]=
407         {
408               {27,30,4,24,4,24}  /* 1999 */
409             , {25,28,2,22,2,22}  /* 2000 */
410             , {24,27,1,28,1,28}  /* 2001 */
411             , {30,26,7,27,7,27}  /* 2002 */
412             , {29,25,6,26,6,26}  /* 2003 */
413             , {27,30,4,24,4,24}  /* 2004 */
414             , {26,29,3,23,3,23}  /* 2005 */
415             , {25,28,2,22,2,22}  /* 2006 */
416             , {24,27,1,28,1,28}  /* 2007 */
417             , {29,25,6,26,6,26}  /* 2008 */
418             , {28,24,5,25,5,25}  /* 2009 */
419             , {27,30,4,24,4,24}  /* 2010 */
420             , {26,29,3,23,3,23}  /* 2011 */
421             , {24,27,1,28,1,28}  /* 2012 */
422             , {30,26,7,27,7,27}  /* 2013 */
423             , {29,25,6,26,6,26}  /* 2014 */
424             , {28,24,5,25,5,25}  /* 2015 */
425             , {26,29,3,23,3,23}  /* 2016 */
426             , {25,28,2,22,2,22}  /* 2017 */
427             , {24,27,1,28,1,28}  /* 2018 */
428             , {30,26,7,27,7,27}  /* 2019 */
429         };
430         for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
431             for (i=0; cases[i].nr; i++) {
432                 if(i) cases[i].nr += 18;
433                 cases[i].slt.wYear = year;
434                 cases[i].slt.wDay = yeardays[j][i/3];
435                 pSystemTimeToTzSpecificLocalTime( cases[i].ptz, &(cases[i].slt), &result);
436                 ok( result.wHour == cases[i].ehour,
437                         "Test SystemTimeToTzSpecificLocalTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n", 
438                         cases[i].nr, result.wYear, result.wMonth, result.wDay,
439                         result.wHour, result.wMinute, cases[i].ehour);
440             }
441         }
442
443     }        
444 }
445
446 START_TEST(time)
447 {
448     test_conversions();
449     test_invalid_arg();
450     test_GetTimeZoneInformation();
451     test_FileTimeToSystemTime();
452     test_FileTimeToLocalFileTime();
453     test_TzSpecificLocalTimeToSystemTime();
454 }