Assorted spelling fixes.
[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 void test_GetTimeZoneInformation()
34 {
35     TIME_ZONE_INFORMATION tzinfo, tzinfo1;
36     DWORD res =  GetTimeZoneInformation(&tzinfo);
37     ok(res != 0, "GetTimeZoneInformation failed\n");
38     ok(SetEnvironmentVariableA("TZ","GMT0") != 0,
39        "SetEnvironmentVariableA failed\n");
40     res =  GetTimeZoneInformation(&tzinfo1);
41     ok(res != 0, "GetTimeZoneInformation failed\n");
42
43     ok(((tzinfo.Bias == tzinfo1.Bias) && 
44         (tzinfo.StandardBias == tzinfo1.StandardBias) &&
45         (tzinfo.DaylightBias == tzinfo1.DaylightBias)),
46        "Bias influenced by TZ variable\n"); 
47     ok(SetEnvironmentVariableA("TZ",NULL) != 0,
48        "SetEnvironmentVariableA failed\n");
49         
50 }
51
52 void test_FileTimeToSystemTime()
53 {
54     FILETIME ft;
55     SYSTEMTIME st;
56     ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
57
58     ft.dwHighDateTime = 0;
59     ft.dwLowDateTime  = 0;
60     ok(FileTimeToSystemTime(&ft, &st),
61        "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
62     ok(((st.wYear == 1601) && (st.wMonth  == 1) && (st.wDay    == 1) &&
63         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 0) &&
64         (st.wMilliseconds == 0)),
65         "Got Year %4d Month %2d Day %2d\n",  st.wYear, st.wMonth, st.wDay);
66
67     ft.dwHighDateTime = (UINT)(time >> 32);
68     ft.dwLowDateTime  = (UINT)time;
69     ok(FileTimeToSystemTime(&ft, &st),
70        "FileTimeToSystemTime() failed with Error 0x%08lx\n",GetLastError());
71     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
72         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
73         (st.wMilliseconds == 0)),
74        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
75        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
76        st.wMilliseconds);
77 }
78
79 void test_FileTimeToLocalFileTime()
80 {
81     FILETIME ft, lft;
82     SYSTEMTIME st;
83     TIME_ZONE_INFORMATION tzinfo;
84     DWORD res =  GetTimeZoneInformation(&tzinfo);
85     ULONGLONG time = (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970 
86         + (ULONGLONG)tzinfo.Bias*SECSPERMIN *TICKSPERSEC;
87
88     ok( res != 0, "GetTimeZoneInformation failed\n");
89     ft.dwHighDateTime = (UINT)(time >> 32);
90     ft.dwLowDateTime  = (UINT)time;
91     ok(FileTimeToLocalFileTime(&ft, &lft) !=0 ,
92        "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
93     FileTimeToSystemTime(&lft, &st);
94     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
95         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
96         (st.wMilliseconds == 0)),
97        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
98        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
99        st.wMilliseconds);
100
101     ok(SetEnvironmentVariableA("TZ","GMT") != 0,
102        "SetEnvironmentVariableA failed\n");
103     ok(res != 0, "GetTimeZoneInformation failed\n");
104     ok(FileTimeToLocalFileTime(&ft, &lft) !=0 ,
105        "FileTimeToLocalFileTime() failed with Error 0x%08lx\n",GetLastError());
106     FileTimeToSystemTime(&lft, &st);
107     ok(((st.wYear == 1970) && (st.wMonth == 1) && (st.wDay == 1) &&
108         (st.wHour ==    0) && (st.wMinute == 0) && (st.wSecond == 1) &&
109         (st.wMilliseconds == 0)),
110        "Got Year %4d Month %2d Day %2d Hour %2d Min %2d Sec %2d mSec %3d\n",
111        st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
112        st.wMilliseconds);
113     ok(SetEnvironmentVariableA("TZ",NULL) != 0,
114        "SetEnvironmentVariableA failed\n");
115 }
116
117
118 /* test TzSpecificLocalTimeToSystemTime and SystemTimeToTzSpecificLocalTime
119  * these are in winXP and later */
120 typedef HANDLE (WINAPI *fnTzSpecificLocalTimeToSystemTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
121 typedef HANDLE (WINAPI *fnSystemTimeToTzSpecificLocalTime)( LPTIME_ZONE_INFORMATION, LPSYSTEMTIME, LPSYSTEMTIME);
122
123 typedef struct {
124     int nr;             /* test case number for easier lookup */
125     TIME_ZONE_INFORMATION *ptz; /* ptr to timezone */
126     SYSTEMTIME slt;     /* system/local time to convert */
127     WORD ehour;        /* expected hour */
128 } TZLT2ST_case;
129
130 void test_TzSpecificLocalTimeToSystemTime()
131 {    
132     HMODULE hKernel = GetModuleHandle("kernel32");
133     fnTzSpecificLocalTimeToSystemTime pTzSpecificLocalTimeToSystemTime;
134     fnSystemTimeToTzSpecificLocalTime pSystemTimeToTzSpecificLocalTime = NULL;
135     TIME_ZONE_INFORMATION tzE, tzW, tzS;
136     SYSTEMTIME result;
137     int i;
138     pTzSpecificLocalTimeToSystemTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "TzSpecificLocalTimeToSystemTime");
139     if(pTzSpecificLocalTimeToSystemTime)
140         pSystemTimeToTzSpecificLocalTime = (fnTzSpecificLocalTimeToSystemTime) GetProcAddress( hKernel, "SystemTimeToTzSpecificLocalTime");
141     if( !pSystemTimeToTzSpecificLocalTime)
142         return;
143     ZeroMemory( &tzE, sizeof(tzE));
144     ZeroMemory( &tzW, sizeof(tzW));
145     ZeroMemory( &tzS, sizeof(tzS));
146     /* timezone Eastern hemisphere */
147     tzE.Bias=-600;
148     tzE.StandardBias=0;
149     tzE.DaylightBias=-60;
150     tzE.StandardDate.wMonth=10;
151     tzE.StandardDate.wDayOfWeek=0; /*sunday */
152     tzE.StandardDate.wDay=5;       /* last (sunday) of the month */
153     tzE.StandardDate.wHour=3;
154     tzE.DaylightDate.wMonth=3;
155     tzE.DaylightDate.wDay=5;
156     tzE.DaylightDate.wHour=2;
157     /* timezone Western hemisphere */
158     tzW.Bias=240;
159     tzW.StandardBias=0;
160     tzW.DaylightBias=-60;
161     tzW.StandardDate.wMonth=10;
162     tzW.StandardDate.wDayOfWeek=0; /*sunday */
163     tzW.StandardDate.wDay=4;       /* 4th (sunday) of the month */
164     tzW.StandardDate.wHour=2;
165     tzW.DaylightDate.wMonth=4;
166     tzW.DaylightDate.wDay=1;
167     tzW.DaylightDate.wHour=2;
168     /* timezone Eastern hemisphere */
169     tzS.Bias=240;
170     tzS.StandardBias=0;
171     tzS.DaylightBias=-60;
172     tzS.StandardDate.wMonth=4;
173     tzS.StandardDate.wDayOfWeek=0; /*sunday */
174     tzS.StandardDate.wDay=1;       /* 1st  (sunday) of the month */
175     tzS.StandardDate.wHour=2;
176     tzS.DaylightDate.wMonth=10;
177     tzS.DaylightDate.wDay=4;
178     tzS.DaylightDate.wHour=2;
179     /*tests*/
180         /* TzSpecificLocalTimeToSystemTime */
181     {   TZLT2ST_case cases[] = {
182             /* standard->daylight transition */
183             { 1, &tzE, {2004,3,-1,28,1,0,0,0}, 15 },
184             { 2, &tzE, {2004,3,-1,28,1,59,59,999}, 15},
185             { 3, &tzE, {2004,3,-1,28,2,0,0,0}, 15},
186             /* daylight->standard transition */
187             { 4, &tzE, {2004,10,-1,31,2,0,0,0} , 15 },
188             { 5, &tzE, {2004,10,-1,31,2,59,59,999}, 15 },
189             { 6, &tzE, {2004,10,-1,31,3,0,0,0}, 17 },
190             /* West and with fixed weekday of the month */
191             { 7, &tzW, {2004,4,-1,4,1,0,0,0}, 5},
192             { 8, &tzW, {2004,4,-1,4,1,59,59,999}, 5},
193             { 9, &tzW, {2004,4,-1,4,2,0,0,0}, 5},
194             { 10, &tzW, {2004,10,-1,24,1,0,0,0}, 4},
195             { 11, &tzW, {2004,10,-1,24,1,59,59,999}, 4},
196             { 12, &tzW, {2004,10,-1,24,2,0,0,0 }, 6},
197             /* and now south */
198             { 13, &tzS, {2004,4,-1,4,1,0,0,0}, 4},
199             { 14, &tzS, {2004,4,-1,4,1,59,59,999}, 4},
200             { 15, &tzS, {2004,4,-1,4,2,0,0,0}, 6},
201             { 16, &tzS, {2004,10,-1,24,1,0,0,0}, 5},
202             { 17, &tzS, {2004,10,-1,24,1,59,59,999}, 5},
203             { 18, &tzS, {2004,10,-1,24,2,0,0,0}, 5},
204             {0}
205        };
206         for (i=0; cases[i].nr; i++) {
207             pTzSpecificLocalTimeToSystemTime( cases[i].ptz, &(cases[i].slt), &result);
208             ok( result.wHour == cases[i].ehour,
209                     "Test TzSpecificLocalTimeToSystemTime #%d. wrong system time. Hour is %d expected %d\n", 
210                     cases[i].nr, result.wHour, cases[i].ehour);
211         }
212
213     }
214         /* SystemTimeToTzSpecificLocalTime */
215     {   TZLT2ST_case cases[] = {
216             /* standard->daylight transition */
217             { 1, &tzE, {2004,3,-1,27,15,0,0,0}, 1 },
218             { 2, &tzE, {2004,3,-1,27,15,59,59,999}, 1},
219             { 3, &tzE, {2004,3,-1,27,16,0,0,0}, 3},
220             /* daylight->standard transition */
221             { 4,  &tzE, {2004,10,-1,30,15,0,0,0}, 2 },
222             { 5, &tzE, {2004,10,-1,30,15,59,59,999}, 2 },
223             { 6, &tzE, {2004,10,-1,30,16,0,0,0}, 2 },
224             /* West and with fixed weekday of the month */
225             { 7, &tzW, {2004,4,-1,4,5,0,0,0}, 1},
226             { 8, &tzW, {2004,4,-1,4,5,59,59,999}, 1},
227             { 9, &tzW, {2004,4,-1,4,6,0,0,0}, 3},
228             { 10, &tzW, {2004,10,-1,24,4,0,0,0}, 1},
229             { 11, &tzW, {2004,10,-1,24,4,59,59,999}, 1},
230             { 12, &tzW, {2004,10,-1,24,5,0,0,0 }, 1},
231             /* and now south */
232             { 13, &tzS, {2004,4,-1,4,4,0,0,0}, 1},
233             { 14, &tzS, {2004,4,-1,4,4,59,59,999}, 1},
234             { 15, &tzS, {2004,4,-1,4,5,0,0,0}, 1},
235             { 16, &tzS, {2004,10,-1,24,5,0,0,0}, 1},
236             { 17, &tzS, {2004,10,-1,24,5,59,59,999}, 1},
237             { 18, &tzS, {2004,10,-1,24,6,0,0,0}, 3},
238
239             {0}
240        };
241         for (i=0; cases[i].nr; i++) {
242             pSystemTimeToTzSpecificLocalTime( cases[i].ptz, &(cases[i].slt), &result);
243             ok( result.wHour == cases[i].ehour,
244                     "Test SystemTimeToTzSpecificLocalTime #%d. wrong system time. Hour is %d expected %d\n", 
245                     cases[i].nr, result.wHour, cases[i].ehour);
246         }
247
248     }        
249 }
250
251 START_TEST(time)
252 {
253     test_GetTimeZoneInformation();
254     test_FileTimeToSystemTime();
255     test_FileTimeToLocalFileTime();
256     test_TzSpecificLocalTimeToSystemTime();
257 }