Added stubs for SendIMEMessageEx[A,W].
[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()
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()
120 {
121     FILETIME ft;
122     SYSTEMTIME st;
123
124     memset(&ft,0,sizeof ft);
125
126     /* Invalid argument checks */
127
128     todo_wine {
129     ok( !DosDateTimeToFileTime(0,0,&ft), /* invalid dos date/time */
130         "DosDateTimeToFileTime() didn't fail\n");
131     }
132
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");
135
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);
138
139     /* now check SystemTimeToFileTime */
140     memset(&ft,0,sizeof ft);
141
142
143     /* try with a bad month */
144     SETUP_1980(st)
145     st.wMonth = 0;
146
147     ok( !SystemTimeToFileTime(&st, &ft), "bad month\n");
148
149     /* with a bad day */
150     SETUP_1980(st)
151     st.wDay = 0;
152
153     ok( !SystemTimeToFileTime(&st, &ft), "bad day\n");
154
155     /* with a bad hour */
156     SETUP_1980(st)
157     st.wHour = 24;
158
159     ok( !SystemTimeToFileTime(&st, &ft), "bad hour\n");
160
161     /* with a bad minute */
162     SETUP_1980(st)
163     st.wMinute = 60;
164
165     ok( !SystemTimeToFileTime(&st, &ft), "bad minute\n");
166 }
167  
168 void test_GetTimeZoneInformation()
169 {
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");
177
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");
184         
185 }
186
187 void test_FileTimeToSystemTime()
188 {
189     FILETIME ft;
190     SYSTEMTIME st;
191     ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
192     BOOL ret;
193
194     ft.dwHighDateTime = 0;
195     ft.dwLowDateTime  = 0;
196     ret = FileTimeToSystemTime(&ft, &st);
197     ok( ret,
198        "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
199     ok(((st.wYear == 1601) && (st.wMonth  == 1) && (st.wDay    == 1) &&
200         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 0) &&
201         (st.wMilliseconds == 0)),
202         "Got Year %4d Month %2d Day %2d\n",  st.wYear, st.wMonth, st.wDay);
203
204     ft.dwHighDateTime = (UINT)(time >> 32);
205     ft.dwLowDateTime  = (UINT)time;
206     ret = FileTimeToSystemTime(&ft, &st);
207     ok( ret,
208        "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
209     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
210         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
211         (st.wMilliseconds == 0)),
212        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
213        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
214        st.wMilliseconds);
215 }
216
217 void test_FileTimeToLocalFileTime()
218 {
219     FILETIME ft, lft;
220     SYSTEMTIME st;
221     TIME_ZONE_INFORMATION tzinfo;
222     DWORD res =  GetTimeZoneInformation(&tzinfo);
223     ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970 +
224         (LONGLONG)(tzinfo.Bias + 
225             ( res == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
226             ( res == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ))) *
227              SECSPERMIN *TICKSPERSEC;
228     BOOL ret;
229
230     ok( res != TIME_ZONE_ID_INVALID , "GetTimeZoneInformation failed\n");
231     ft.dwHighDateTime = (UINT)(time >> 32);
232     ft.dwLowDateTime  = (UINT)time;
233     ret = FileTimeToLocalFileTime(&ft, &lft);
234     ok( ret,
235        "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
236     FileTimeToSystemTime(&lft, &st);
237     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
238         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
239         (st.wMilliseconds == 0)),
240        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
241        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
242        st.wMilliseconds);
243
244     ok(SetEnvironmentVariableA("TZ","GMT") != 0,
245        "SetEnvironmentVariableA failed\n");
246     ok(res != TIME_ZONE_ID_INVALID, "GetTimeZoneInformation failed\n");
247     ret = FileTimeToLocalFileTime(&ft, &lft);
248     ok( ret,
249        "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
250     FileTimeToSystemTime(&lft, &st);
251     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
252         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
253         (st.wMilliseconds == 0)),
254        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
255        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
256        st.wMilliseconds);
257     ok(SetEnvironmentVariableA("TZ",NULL) != 0,
258        "SetEnvironmentVariableA failed\n");
259 }
260
261
262 /* test TzSpecificLocalTimeToSystemTime and SystemTimeToTzSpecificLocalTime
263  * these are in winXP and later */
264 typedef HANDLE (WINAPI *fnTzSpecificLocalTimeToSystemTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
265 typedef HANDLE (WINAPI *fnSystemTimeToTzSpecificLocalTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
266
267 typedef struct {
268     int nr;             /* test case number for easier lookup */
269     TIME_ZONE_INFORMATION *ptz; /* ptr to timezone */
270     SYSTEMTIME slt;     /* system/local time to convert */
271     WORD ehour;        /* expected hour */
272 } TZLT2ST_case;
273
274 void test_TzSpecificLocalTimeToSystemTime()
275 {    
276     HMODULE hKernel = GetModuleHandle("kernel32");
277     fnTzSpecificLocalTimeToSystemTime pTzSpecificLocalTimeToSystemTime;
278     fnSystemTimeToTzSpecificLocalTime pSystemTimeToTzSpecificLocalTime = NULL;
279     TIME_ZONE_INFORMATION tzE, tzW, tzS;
280     SYSTEMTIME result;
281     int i, j, year;
282     pTzSpecificLocalTimeToSystemTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "TzSpecificLocalTimeToSystemTime");
283     if(pTzSpecificLocalTimeToSystemTime)
284         pSystemTimeToTzSpecificLocalTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
285     if( !pSystemTimeToTzSpecificLocalTime)
286         return;
287     ZeroMemory( &tzE, sizeof(tzE));
288     ZeroMemory( &tzW, sizeof(tzW));
289     ZeroMemory( &tzS, sizeof(tzS));
290     /* timezone Eastern hemisphere */
291     tzE.Bias=-600;
292     tzE.StandardBias=0;
293     tzE.DaylightBias=-60;
294     tzE.StandardDate.wMonth=10;
295     tzE.StandardDate.wDayOfWeek=0; /*sunday */
296     tzE.StandardDate.wDay=5;       /* last (sunday) of the month */
297     tzE.StandardDate.wHour=3;
298     tzE.DaylightDate.wMonth=3;
299     tzE.DaylightDate.wDay=5;
300     tzE.DaylightDate.wHour=2;
301     /* timezone Western hemisphere */
302     tzW.Bias=240;
303     tzW.StandardBias=0;
304     tzW.DaylightBias=-60;
305     tzW.StandardDate.wMonth=10;
306     tzW.StandardDate.wDayOfWeek=0; /*sunday */
307     tzW.StandardDate.wDay=4;       /* 4th (sunday) of the month */
308     tzW.StandardDate.wHour=2;
309     tzW.DaylightDate.wMonth=4;
310     tzW.DaylightDate.wDay=1;
311     tzW.DaylightDate.wHour=2;
312     /* timezone Eastern hemisphere */
313     tzS.Bias=240;
314     tzS.StandardBias=0;
315     tzS.DaylightBias=-60;
316     tzS.StandardDate.wMonth=4;
317     tzS.StandardDate.wDayOfWeek=0; /*sunday */
318     tzS.StandardDate.wDay=1;       /* 1st  (sunday) of the month */
319     tzS.StandardDate.wHour=2;
320     tzS.DaylightDate.wMonth=10;
321     tzS.DaylightDate.wDay=4;
322     tzS.DaylightDate.wHour=2;
323     /*tests*/
324         /* TzSpecificLocalTimeToSystemTime */
325     {   TZLT2ST_case cases[] = {
326             /* standard->daylight transition */
327             { 1, &tzE, {2004,3,-1,28,1,0,0,0}, 15 },
328             { 2, &tzE, {2004,3,-1,28,1,59,59,999}, 15},
329             { 3, &tzE, {2004,3,-1,28,2,0,0,0}, 15},
330             /* daylight->standard transition */
331             { 4, &tzE, {2004,10,-1,31,2,0,0,0} , 15 },
332             { 5, &tzE, {2004,10,-1,31,2,59,59,999}, 15 },
333             { 6, &tzE, {2004,10,-1,31,3,0,0,0}, 17 },
334             /* West and with fixed weekday of the month */
335             { 7, &tzW, {2004,4,-1,4,1,0,0,0}, 5},
336             { 8, &tzW, {2004,4,-1,4,1,59,59,999}, 5},
337             { 9, &tzW, {2004,4,-1,4,2,0,0,0}, 5},
338             { 10, &tzW, {2004,10,-1,24,1,0,0,0}, 4},
339             { 11, &tzW, {2004,10,-1,24,1,59,59,999}, 4},
340             { 12, &tzW, {2004,10,-1,24,2,0,0,0 }, 6},
341             /* and now south */
342             { 13, &tzS, {2004,4,-1,4,1,0,0,0}, 4},
343             { 14, &tzS, {2004,4,-1,4,1,59,59,999}, 4},
344             { 15, &tzS, {2004,4,-1,4,2,0,0,0}, 6},
345             { 16, &tzS, {2004,10,-1,24,1,0,0,0}, 5},
346             { 17, &tzS, {2004,10,-1,24,1,59,59,999}, 5},
347             { 18, &tzS, {2004,10,-1,24,2,0,0,0}, 5},
348             {0}
349         };
350     /*  days of transitions to put into the cases array */
351         int yeardays[][6]=
352         {
353               {28,31,4,24,4,24}  /* 1999 */
354             , {26,29,2,22,2,22}  /* 2000 */
355             , {25,28,1,28,1,28}  /* 2001 */
356             , {31,27,7,27,7,27}  /* 2002 */
357             , {30,26,6,26,6,26}  /* 2003 */
358             , {28,31,4,24,4,24}  /* 2004 */
359             , {27,30,3,23,3,23}  /* 2005 */
360             , {26,29,2,22,2,22}  /* 2006 */
361             , {25,28,1,28,1,28}  /* 2007 */
362             , {30,26,6,26,6,26}  /* 2008 */
363             , {29,25,5,25,5,25}  /* 2009 */
364             , {28,31,4,24,4,24}  /* 2010 */
365             , {27,30,3,23,3,23}  /* 2011 */
366             , {25,28,1,28,1,28}  /* 2012 */
367             , {31,27,7,27,7,27}  /* 2013 */
368             , {30,26,6,26,6,26}  /* 2014 */
369             , {29,25,5,25,5,25}  /* 2015 */
370             , {27,30,3,23,3,23}  /* 2016 */
371             , {26,29,2,22,2,22}  /* 2017 */
372             , {25,28,1,28,1,28}  /* 2018 */
373             , {31,27,7,27,7,27}  /* 2019 */
374             ,{0}
375         };
376         for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
377             for (i=0; cases[i].nr; i++) {
378                 if(i) cases[i].nr += 18;
379                 cases[i].slt.wYear = year;
380                 cases[i].slt.wDay = yeardays[j][i/3];
381                 pTzSpecificLocalTimeToSystemTime( cases[i].ptz, &(cases[i].slt), &result);
382                 ok( result.wHour == cases[i].ehour,
383                         "Test TzSpecificLocalTimeToSystemTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour =  %02d\n", 
384                         cases[i].nr, result.wYear, result.wMonth, result.wDay,
385                         result.wHour, result.wMinute, cases[i].ehour);
386             }
387         }
388     }
389         /* SystemTimeToTzSpecificLocalTime */
390     {   TZLT2ST_case cases[] = {
391             /* standard->daylight transition */
392             { 1, &tzE, {2004,3,-1,27,15,0,0,0}, 1 },
393             { 2, &tzE, {2004,3,-1,27,15,59,59,999}, 1},
394             { 3, &tzE, {2004,3,-1,27,16,0,0,0}, 3},
395             /* daylight->standard transition */
396             { 4,  &tzE, {2004,10,-1,30,15,0,0,0}, 2 },
397             { 5, &tzE, {2004,10,-1,30,15,59,59,999}, 2 },
398             { 6, &tzE, {2004,10,-1,30,16,0,0,0}, 2 },
399             /* West and with fixed weekday of the month */
400             { 7, &tzW, {2004,4,-1,4,5,0,0,0}, 1},
401             { 8, &tzW, {2004,4,-1,4,5,59,59,999}, 1},
402             { 9, &tzW, {2004,4,-1,4,6,0,0,0}, 3},
403             { 10, &tzW, {2004,10,-1,24,4,0,0,0}, 1},
404             { 11, &tzW, {2004,10,-1,24,4,59,59,999}, 1},
405             { 12, &tzW, {2004,10,-1,24,5,0,0,0 }, 1},
406             /* and now south */
407             { 13, &tzS, {2004,4,-1,4,4,0,0,0}, 1},
408             { 14, &tzS, {2004,4,-1,4,4,59,59,999}, 1},
409             { 15, &tzS, {2004,4,-1,4,5,0,0,0}, 1},
410             { 16, &tzS, {2004,10,-1,24,5,0,0,0}, 1},
411             { 17, &tzS, {2004,10,-1,24,5,59,59,999}, 1},
412             { 18, &tzS, {2004,10,-1,24,6,0,0,0}, 3},
413
414             {0}
415         }; 
416     /*  days of transitions to put into the cases array */
417         int yeardays[][6]=
418         {
419               {27,30,4,24,4,24}  /* 1999 */
420             , {25,28,2,22,2,22}  /* 2000 */
421             , {24,27,1,28,1,28}  /* 2001 */
422             , {30,26,7,27,7,27}  /* 2002 */
423             , {29,25,6,26,6,26}  /* 2003 */
424             , {27,30,4,24,4,24}  /* 2004 */
425             , {26,29,3,23,3,23}  /* 2005 */
426             , {25,28,2,22,2,22}  /* 2006 */
427             , {24,27,1,28,1,28}  /* 2007 */
428             , {29,25,6,26,6,26}  /* 2008 */
429             , {28,24,5,25,5,25}  /* 2009 */
430             , {27,30,4,24,4,24}  /* 2010 */
431             , {26,29,3,23,3,23}  /* 2011 */
432             , {24,27,1,28,1,28}  /* 2012 */
433             , {30,26,7,27,7,27}  /* 2013 */
434             , {29,25,6,26,6,26}  /* 2014 */
435             , {28,24,5,25,5,25}  /* 2015 */
436             , {26,29,3,23,3,23}  /* 2016 */
437             , {25,28,2,22,2,22}  /* 2017 */
438             , {24,27,1,28,1,28}  /* 2018 */
439             , {30,26,7,27,7,27}  /* 2019 */
440         };
441         for( j=0 , year = 1999; yeardays[j][0] ; j++, year++) {
442             for (i=0; cases[i].nr; i++) {
443                 if(i) cases[i].nr += 18;
444                 cases[i].slt.wYear = year;
445                 cases[i].slt.wDay = yeardays[j][i/3];
446                 pSystemTimeToTzSpecificLocalTime( cases[i].ptz, &(cases[i].slt), &result);
447                 ok( result.wHour == cases[i].ehour,
448                         "Test SystemTimeToTzSpecificLocalTime #%d. Got %4d-%02d-%02d %02d:%02d. Expect hour = %02d\n", 
449                         cases[i].nr, result.wYear, result.wMonth, result.wDay,
450                         result.wHour, result.wMinute, cases[i].ehour);
451             }
452         }
453
454     }        
455 }
456
457 START_TEST(time)
458 {
459     test_conversions();
460     test_invalid_arg();
461     test_GetTimeZoneInformation();
462     test_FileTimeToSystemTime();
463     test_FileTimeToLocalFileTime();
464     test_TzSpecificLocalTimeToSystemTime();
465 }