Added tests for environment variables, debug and suspend flags.
[wine] / dlls / kernel / time.c
1 /*
2  * Win32 kernel time functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
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 "config.h"
22
23 #include <string.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <stdlib.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 #endif
31 #ifdef HAVE_SYS_TIMES_H
32 # include <sys/times.h>
33 #endif
34
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37 #include "file.h"
38 #include "winternl.h"
39 #include "winerror.h"
40 #include "winnls.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(win32);
45
46 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
47 #define SETTIME_MAX_ADJUST 120
48 #define CALINFO_MAX_YEAR 2029
49
50
51 /***********************************************************************
52  *              SetLocalTime            (KERNEL32.@)
53  *
54  *  Sets the local time using current time zone and daylight
55  *  savings settings.
56  *
57  * RETURNS
58  *
59  *  True if the time was set, false if the time was invalid or the
60  *  necessary permissions were not held.
61  */
62 BOOL WINAPI SetLocalTime(
63     const SYSTEMTIME *systime) /* [in] The desired local time. */
64 {
65     FILETIME ft;
66     LARGE_INTEGER st, st2;
67     NTSTATUS status;
68
69     SystemTimeToFileTime( systime, &ft );
70     st.s.LowPart = ft.dwLowDateTime;
71     st.s.HighPart = ft.dwHighDateTime;
72     RtlLocalTimeToSystemTime( &st, &st2 );
73
74     if ((status = NtSetSystemTime(&st2, NULL)))
75         SetLastError( RtlNtStatusToDosError(status) );
76     return !status;
77 }
78
79
80 /***********************************************************************
81  *           GetSystemTimeAdjustment     (KERNEL32.@)
82  *
83  *  Indicates the period between clock interrupt and the amount the clock
84  *  is adjusted each interrupt so as to keep it insync with an external source.
85  *
86  * RETURNS
87  *
88  *  Always returns true.
89  *
90  * BUGS
91  *
92  *  Only the special case of disabled time adjustments is supported.
93  */
94 BOOL WINAPI GetSystemTimeAdjustment(
95     PDWORD lpTimeAdjustment,         /* [out] The clock adjustment per interupt in 100's of nanoseconds. */
96     PDWORD lpTimeIncrement,          /* [out] The time between clock interupts in 100's of nanoseconds. */
97     PBOOL  lpTimeAdjustmentDisabled) /* [out] The clock synchonisation has been disabled. */
98 {
99     *lpTimeAdjustment = 0;
100     *lpTimeIncrement = 0;
101     *lpTimeAdjustmentDisabled = TRUE;
102     return TRUE;
103 }
104
105
106 /***********************************************************************
107  *              SetSystemTime            (KERNEL32.@)
108  *
109  *  Sets the system time (utc).
110  *
111  * RETURNS
112  *
113  *  True if the time was set, false if the time was invalid or the
114  *  necessary permissions were not held.
115  */
116 BOOL WINAPI SetSystemTime(
117     const SYSTEMTIME *systime) /* [in] The desired system time. */
118 {
119     FILETIME ft;
120     LARGE_INTEGER t;
121     NTSTATUS status;
122
123     SystemTimeToFileTime( systime, &ft );
124     t.s.LowPart = ft.dwLowDateTime;
125     t.s.HighPart = ft.dwHighDateTime;
126     if ((status = NtSetSystemTime(&t, NULL)))
127         SetLastError( RtlNtStatusToDosError(status) );
128     return !status;
129 }
130
131
132 /***********************************************************************
133  *              GetTimeZoneInformation  (KERNEL32.@)
134  *
135  *  Fills in the a time zone information structure with values based on
136  *  the current local time.
137  *
138  * RETURNS
139  *
140  *  The daylight savings time standard or TIME_ZONE_ID_INVALID if the call failed.
141  */
142 DWORD WINAPI GetTimeZoneInformation(
143     LPTIME_ZONE_INFORMATION tzinfo) /* [out] The time zone structure to be filled in. */
144 {
145     NTSTATUS status;
146     if ((status = RtlQueryTimeZoneInformation(tzinfo)))
147         SetLastError( RtlNtStatusToDosError(status) );
148     return TIME_ZONE_ID_STANDARD;
149 }
150
151
152 /***********************************************************************
153  *              SetTimeZoneInformation  (KERNEL32.@)
154  *
155  *  Set the local time zone with values based on the time zone structure.
156  *
157  * RETURNS
158  *
159  *  True on successful setting of the time zone.
160  */
161 BOOL WINAPI SetTimeZoneInformation(
162     const LPTIME_ZONE_INFORMATION tzinfo) /* [in] The new time zone. */
163 {
164     NTSTATUS status;
165     if ((status = RtlSetTimeZoneInformation(tzinfo)))
166         SetLastError( RtlNtStatusToDosError(status) );
167     return !status;
168 }
169
170
171 /***********************************************************************
172  *  _DayLightCompareDate
173  *
174  *  Compares two dates without looking at the year
175  *
176  * RETURNS
177  *
178  *  -1 if date < compareDate
179  *   0 if date == compareDate
180  *   1 if date > compareDate
181  *  -2 if an error occures
182  */
183
184 static int _DayLightCompareDate(
185     const LPSYSTEMTIME date,                       /* [in] The date to compare. */
186     const LPSYSTEMTIME compareDate)                /* [in] The daylight saving begin or end date */
187 {
188     int limit_day;
189
190     if (compareDate->wYear != 0)
191     {
192         if (date->wMonth < compareDate->wMonth)
193             return -1; /* We are in a year before the date limit. */
194
195         if (date->wMonth > compareDate->wMonth)
196             return 1; /* We are in a year after the date limit. */
197     }
198
199     if (date->wMonth < compareDate->wMonth)
200         return -1; /* We are in a month before the date limit. */
201
202     if (date->wMonth > compareDate->wMonth)
203         return 1; /* We are in a month after the date limit. */
204
205     if (compareDate->wDayOfWeek <= 6)
206     {
207        SYSTEMTIME tmp;
208        FILETIME tmp_ft;
209
210        /* compareDate->wDay is interpreted as number of the week in the month. */
211        /* 5 means: the last week in the month */
212        int weekofmonth = compareDate->wDay;
213
214          /* calculate day of week for the first day in the month */
215         memcpy(&tmp, date, sizeof(SYSTEMTIME));
216         tmp.wDay = 1;
217         tmp.wDayOfWeek = -1;
218
219         if (weekofmonth == 5)
220         {
221              /* Go to the beginning of the next month. */
222             if (++tmp.wMonth > 12)
223             {
224                 tmp.wMonth = 1;
225                 ++tmp.wYear;
226             }
227         }
228
229         if (!SystemTimeToFileTime(&tmp, &tmp_ft))
230             return -2;
231
232         if (weekofmonth == 5)
233         {
234           LONGLONG t, one_day;
235
236           t = tmp_ft.dwHighDateTime;
237           t <<= 32;
238           t += (UINT)tmp_ft.dwLowDateTime;
239
240           /* substract one day */
241           one_day = 24*60*60;
242           one_day *= 10000000;
243           t -= one_day;
244
245           tmp_ft.dwLowDateTime  = (UINT)t;
246           tmp_ft.dwHighDateTime = (UINT)(t >> 32);
247         }
248
249         if (!FileTimeToSystemTime(&tmp_ft, &tmp))
250             return -2;
251
252        if (weekofmonth == 5)
253        {
254           /* calculate the last matching day of the week in this month */
255           int dif = tmp.wDayOfWeek - compareDate->wDayOfWeek;
256           if (dif < 0)
257              dif += 7;
258
259           limit_day = tmp.wDay - dif;
260        }
261        else
262        {
263           /* calulcate the matching day of the week in the given week */
264           int dif = compareDate->wDayOfWeek - tmp.wDayOfWeek;
265           if (dif < 0)
266              dif += 7;
267
268           limit_day = tmp.wDay + 7*(weekofmonth-1) + dif;
269        }
270     }
271     else
272     {
273        limit_day = compareDate->wDay;
274     }
275
276     if (date->wDay < limit_day)
277         return -1;
278
279     if (date->wDay > limit_day)
280         return 1;
281
282     return 0;   /* date is equal to the date limit. */
283 }
284
285
286 /***********************************************************************
287  *  _GetTimezoneBias
288  *
289  *  Calculates the local time bias for a given time zone
290  *
291  * RETURNS
292  *
293  *  Returns TRUE when the time zone bias was calculated.
294  */
295
296 static BOOL _GetTimezoneBias(
297     const LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The time zone data.            */
298     LPSYSTEMTIME                  lpSystemTime,          /* [in] The system time.               */
299     LONG*                         pBias)                 /* [out] The calulated bias in minutes */
300 {
301     int ret;
302     BOOL beforedaylightsaving, afterdaylightsaving;
303     BOOL daylightsaving = FALSE;
304     LONG bias = lpTimeZoneInformation->Bias;
305
306     if (lpTimeZoneInformation->DaylightDate.wMonth != 0)
307     {
308         if (lpTimeZoneInformation->StandardDate.wMonth == 0 ||
309             lpTimeZoneInformation->StandardDate.wDay<1 ||
310             lpTimeZoneInformation->StandardDate.wDay>5 ||
311             lpTimeZoneInformation->DaylightDate.wDay<1 ||
312             lpTimeZoneInformation->DaylightDate.wDay>5)
313         {
314             SetLastError(ERROR_INVALID_PARAMETER);
315             return FALSE;
316         }
317
318          /* check for daylight saving */
319        ret = _DayLightCompareDate(lpSystemTime, &lpTimeZoneInformation->StandardDate);
320        if (ret == -2)
321           return FALSE;
322
323         beforedaylightsaving = ret < 0;
324
325        _DayLightCompareDate(lpSystemTime, &lpTimeZoneInformation->DaylightDate);
326        if (ret == -2)
327           return FALSE;
328
329         afterdaylightsaving = ret >= 0;
330
331         if (!beforedaylightsaving && !afterdaylightsaving)
332             daylightsaving = TRUE;
333     }
334
335     if (daylightsaving)
336         bias += lpTimeZoneInformation->DaylightBias;
337     else if (lpTimeZoneInformation->StandardDate.wMonth != 0)
338         bias += lpTimeZoneInformation->StandardBias;
339
340     *pBias = bias;
341
342     return TRUE;
343 }
344
345
346 /***********************************************************************
347  *              SystemTimeToTzSpecificLocalTime  (KERNEL32.@)
348  *
349  *  Converts the system time (utc) to the local time in the specified time zone.
350  *
351  * RETURNS
352  *
353  *  Returns true when the local time was calculated.
354  *  Returns TRUE when the local time was calculated.
355  *
356  */
357
358 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
359     LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
360     LPSYSTEMTIME            lpUniversalTime,       /* [in] The utc time to base local time on. */
361     LPSYSTEMTIME            lpLocalTime)           /* [out] The local time in the time zone. */
362 {
363     FILETIME ft;
364     LONG lBias;
365     LONGLONG t, bias;
366     TIME_ZONE_INFORMATION tzinfo;
367
368     if (lpTimeZoneInformation != NULL)
369     {
370         memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
371     }
372     else
373     {
374         if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
375             return FALSE;
376     }
377
378     if (!SystemTimeToFileTime(lpUniversalTime, &ft))
379         return FALSE;
380
381     t = ft.dwHighDateTime;
382     t <<= 32;
383     t += (UINT)ft.dwLowDateTime;
384
385     if (!_GetTimezoneBias(&tzinfo, lpUniversalTime, &lBias))
386         return FALSE;
387
388     bias = lBias * 600000000; /* 60 seconds per minute, 100000 [100-nanoseconds-ticks] per second */
389     t += bias;
390
391     ft.dwLowDateTime  = (UINT)t;
392     ft.dwHighDateTime = (UINT)(t >> 32);
393
394     return FileTimeToSystemTime(&ft, lpLocalTime);
395 }
396
397
398 /***********************************************************************
399  *              TzSpecificLocalTimeToSystemTime  (KERNEL32.@)
400  *
401  *  Converts a local time to a time in Coordinated Universal Time (UTC).
402  *
403  * RETURNS
404  *
405  *  Returns TRUE when the utc time was calculated.
406  */
407
408 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
409     LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
410     LPSYSTEMTIME            lpLocalTime,           /* [in] The local time. */
411     LPSYSTEMTIME            lpUniversalTime)       /* [out] The calculated utc time. */
412 {
413     FILETIME ft;
414     LONG lBias;
415     LONGLONG t, bias;
416     TIME_ZONE_INFORMATION tzinfo;
417
418     if (lpTimeZoneInformation != NULL)
419     {
420         memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
421     }
422     else
423     {
424         if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
425             return FALSE;
426     }
427
428     if (!SystemTimeToFileTime(lpLocalTime, &ft))
429         return FALSE;
430
431     t = ft.dwHighDateTime;
432     t <<= 32;
433     t += (UINT)ft.dwLowDateTime;
434
435     if (!_GetTimezoneBias(&tzinfo, lpUniversalTime, &lBias))
436         return FALSE;
437
438     bias = lBias * 600000000; /* 60 seconds per minute, 100000 [100-nanoseconds-ticks] per second */
439     t -= bias;
440
441     ft.dwLowDateTime  = (UINT)t;
442     ft.dwHighDateTime = (UINT)(t >> 32);
443
444     return FileTimeToSystemTime(&ft, lpUniversalTime);
445 }
446
447
448
449
450 /***********************************************************************
451  *              GetSystemTimeAsFileTime  (KERNEL32.@)
452  *
453  *  Fills in a file time structure with the current time in UTC format.
454  */
455 VOID WINAPI GetSystemTimeAsFileTime(
456     LPFILETIME time) /* [out] The file time struct to be filled with the system time. */
457 {
458     LARGE_INTEGER t;
459     NtQuerySystemTime( &t );
460     time->dwLowDateTime = t.s.LowPart;
461     time->dwHighDateTime = t.s.HighPart;
462 }
463
464
465 /*********************************************************************
466  *      TIME_ClockTimeToFileTime    (olorin@fandra.org, 20-Sep-1998)
467  *
468  *  Used by GetProcessTimes to convert clock_t into FILETIME.
469  *
470  *      Differences to UnixTimeToFileTime:
471  *          1) Divided by CLK_TCK
472  *          2) Time is relative. There is no 'starting date', so there is
473  *             no need in offset correction, like in UnixTimeToFileTime
474  */
475 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
476 {
477     ULONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
478     secs = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
479     filetime->dwLowDateTime  = (DWORD)secs;
480     filetime->dwHighDateTime = (DWORD)(secs >> 32);
481 }
482
483 /*********************************************************************
484  *      GetProcessTimes                         (KERNEL32.@)
485  *
486  *  Returns the user and kernel execution times of a process,
487  *  along with the creation and exit times if known.
488  *
489  *  olorin@fandra.org:
490  *  Would be nice to subtract the cpu time, used by Wine at startup.
491  *  Also, there is a need to separate times used by different applications.
492  *
493  * RETURNS
494  *
495  *  Always returns true.
496  *
497  * BUGS
498  *
499  *  lpCreationTime, lpExitTime are NOT INITIALIZED.
500  */
501 BOOL WINAPI GetProcessTimes(
502     HANDLE     hprocess,       /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
503     LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
504     LPFILETIME lpExitTime,     /* [out] The exit time of the process if exited. */
505     LPFILETIME lpKernelTime,   /* [out] The time spent in kernal routines in 100's of nanoseconds. */
506     LPFILETIME lpUserTime)     /* [out] The time spent in user routines in 100's of nanoseconds. */
507 {
508     struct tms tms;
509
510     times(&tms);
511     TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
512     TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
513     return TRUE;
514 }
515
516 /*********************************************************************
517  *      GetCalendarInfoA                                (KERNEL32.@)
518  *
519  */
520 int WINAPI GetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType,
521                             LPSTR lpCalData, int cchData, LPDWORD lpValue)
522 {
523     int ret;
524     LPWSTR lpCalDataW = NULL;
525
526     FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
527           Locale, Calendar, CalType, lpCalData, cchData, lpValue);
528     /* FIXME: Should verify if Locale is allowable in ANSI, as per MSDN */
529
530     if(cchData)
531       if(!(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchData*sizeof(WCHAR)))) return 0;
532
533     ret = GetCalendarInfoW(Locale, Calendar, CalType, lpCalDataW, cchData, lpValue);
534     if(ret && lpCalDataW && lpCalData)
535       WideCharToMultiByte(CP_ACP, 0, lpCalDataW, cchData, lpCalData, cchData, NULL, NULL);
536     if(lpCalDataW)
537       HeapFree(GetProcessHeap(), 0, lpCalDataW);
538
539     return ret;
540 }
541
542 /*********************************************************************
543  *      GetCalendarInfoW                                (KERNEL32.@)
544  *
545  */
546 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
547                             LPWSTR lpCalData, int cchData, LPDWORD lpValue)
548 {
549     FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
550           Locale, Calendar, CalType, lpCalData, cchData, lpValue);
551
552     if (CalType & CAL_NOUSEROVERRIDE)
553         FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
554     if (CalType & CAL_USE_CP_ACP)
555         FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
556
557     if (CalType & CAL_RETURN_NUMBER) {
558         if (lpCalData != NULL)
559             WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
560         if (cchData != 0)
561             WARN("cchData not 0 (%d) when it should!\n", cchData);
562     } else {
563         if (lpValue != NULL)
564             WARN("lpValue not NULL (%p) when it should!\n", lpValue);
565     }
566
567     /* FIXME: No verification is made yet wrt Locale
568      * for the CALTYPES not requiring GetLocaleInfoA */
569     switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {
570         case CAL_ICALINTVALUE:
571             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
572             return E_FAIL;
573         case CAL_SCALNAME:
574             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
575             return E_FAIL;
576         case CAL_IYEAROFFSETRANGE:
577             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
578             return E_FAIL;
579         case CAL_SERASTRING:
580             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
581             return E_FAIL;
582         case CAL_SSHORTDATE:
583             return GetLocaleInfoW(Locale, LOCALE_SSHORTDATE, lpCalData, cchData);
584         case CAL_SLONGDATE:
585             return GetLocaleInfoW(Locale, LOCALE_SLONGDATE, lpCalData, cchData);
586         case CAL_SDAYNAME1:
587             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME1, lpCalData, cchData);
588         case CAL_SDAYNAME2:
589             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME2, lpCalData, cchData);
590         case CAL_SDAYNAME3:
591             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME3, lpCalData, cchData);
592         case CAL_SDAYNAME4:
593             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME4, lpCalData, cchData);
594         case CAL_SDAYNAME5:
595             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME5, lpCalData, cchData);
596         case CAL_SDAYNAME6:
597             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME6, lpCalData, cchData);
598         case CAL_SDAYNAME7:
599             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME7, lpCalData, cchData);
600         case CAL_SABBREVDAYNAME1:
601             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME1, lpCalData, cchData);
602         case CAL_SABBREVDAYNAME2:
603             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME2, lpCalData, cchData);
604         case CAL_SABBREVDAYNAME3:
605             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME3, lpCalData, cchData);
606         case CAL_SABBREVDAYNAME4:
607             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME4, lpCalData, cchData);
608         case CAL_SABBREVDAYNAME5:
609             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME5, lpCalData, cchData);
610         case CAL_SABBREVDAYNAME6:
611             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME6, lpCalData, cchData);
612         case CAL_SABBREVDAYNAME7:
613             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME7, lpCalData, cchData);
614         case CAL_SMONTHNAME1:
615             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME1, lpCalData, cchData);
616         case CAL_SMONTHNAME2:
617             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME2, lpCalData, cchData);
618         case CAL_SMONTHNAME3:
619             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME3, lpCalData, cchData);
620         case CAL_SMONTHNAME4:
621             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME4, lpCalData, cchData);
622         case CAL_SMONTHNAME5:
623             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME5, lpCalData, cchData);
624         case CAL_SMONTHNAME6:
625             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME6, lpCalData, cchData);
626         case CAL_SMONTHNAME7:
627             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME7, lpCalData, cchData);
628         case CAL_SMONTHNAME8:
629             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME8, lpCalData, cchData);
630         case CAL_SMONTHNAME9:
631             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME9, lpCalData, cchData);
632         case CAL_SMONTHNAME10:
633             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME10, lpCalData, cchData);
634         case CAL_SMONTHNAME11:
635             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME11, lpCalData, cchData);
636         case CAL_SMONTHNAME12:
637             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME12, lpCalData, cchData);
638         case CAL_SMONTHNAME13:
639             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME13, lpCalData, cchData);
640         case CAL_SABBREVMONTHNAME1:
641             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME1, lpCalData, cchData);
642         case CAL_SABBREVMONTHNAME2:
643             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME2, lpCalData, cchData);
644         case CAL_SABBREVMONTHNAME3:
645             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME3, lpCalData, cchData);
646         case CAL_SABBREVMONTHNAME4:
647             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME4, lpCalData, cchData);
648         case CAL_SABBREVMONTHNAME5:
649             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME5, lpCalData, cchData);
650         case CAL_SABBREVMONTHNAME6:
651             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME6, lpCalData, cchData);
652         case CAL_SABBREVMONTHNAME7:
653             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME7, lpCalData, cchData);
654         case CAL_SABBREVMONTHNAME8:
655             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME8, lpCalData, cchData);
656         case CAL_SABBREVMONTHNAME9:
657             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME9, lpCalData, cchData);
658         case CAL_SABBREVMONTHNAME10:
659             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME10, lpCalData, cchData);
660         case CAL_SABBREVMONTHNAME11:
661             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME11, lpCalData, cchData);
662         case CAL_SABBREVMONTHNAME12:
663             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME12, lpCalData, cchData);
664         case CAL_SABBREVMONTHNAME13:
665             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME13, lpCalData, cchData);
666         case CAL_SYEARMONTH:
667             return GetLocaleInfoW(Locale, LOCALE_SYEARMONTH, lpCalData, cchData);
668         case CAL_ITWODIGITYEARMAX:
669             if (lpValue) *lpValue = CALINFO_MAX_YEAR;
670             break;
671         default: MESSAGE("Unknown caltype %ld\n",CalType & 0xffff);
672                  return E_FAIL;
673     }
674     return 0;
675 }
676
677 /*********************************************************************
678  *      SetCalendarInfoA                                (KERNEL32.@)
679  *
680  */
681 int WINAPI      SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
682 {
683     FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
684           Locale, Calendar, CalType, debugstr_a(lpCalData));
685     return 0;
686 }
687
688 /*********************************************************************
689  *      SetCalendarInfoW                                (KERNEL32.@)
690  *
691  */
692 int WINAPI      SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
693 {
694     FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
695           Locale, Calendar, CalType, debugstr_w(lpCalData));
696     return 0;
697 }
698
699 /*********************************************************************
700  *      LocalFileTimeToFileTime                         (KERNEL32.@)
701  */
702 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
703 {
704     NTSTATUS status;
705     LARGE_INTEGER local, utc;
706
707     local.s.LowPart = localft->dwLowDateTime;
708     local.s.HighPart = localft->dwHighDateTime;
709     if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
710     {
711         utcft->dwLowDateTime = utc.s.LowPart;
712         utcft->dwHighDateTime = utc.s.HighPart;
713     }
714     else SetLastError( RtlNtStatusToDosError(status) );
715
716     return !status;
717 }
718
719 /*********************************************************************
720  *      FileTimeToLocalFileTime                         (KERNEL32.@)
721  */
722 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
723 {
724     NTSTATUS status;
725     LARGE_INTEGER local, utc;
726
727     utc.s.LowPart = utcft->dwLowDateTime;
728     utc.s.HighPart = utcft->dwHighDateTime;
729     if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
730     {
731         localft->dwLowDateTime = local.s.LowPart;
732         localft->dwHighDateTime = local.s.HighPart;
733     }
734     else SetLastError( RtlNtStatusToDosError(status) );
735
736     return !status;
737 }
738
739 /*********************************************************************
740  *      FileTimeToSystemTime                            (KERNEL32.@)
741  */
742 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
743 {
744     TIME_FIELDS tf;
745     LARGE_INTEGER t;
746
747     t.s.LowPart = ft->dwLowDateTime;
748     t.s.HighPart = ft->dwHighDateTime;
749     RtlTimeToTimeFields(&t, &tf);
750
751     syst->wYear = tf.Year;
752     syst->wMonth = tf.Month;
753     syst->wDay = tf.Day;
754     syst->wHour = tf.Hour;
755     syst->wMinute = tf.Minute;
756     syst->wSecond = tf.Second;
757     syst->wMilliseconds = tf.Milliseconds;
758     syst->wDayOfWeek = tf.Weekday;
759     return TRUE;
760 }
761
762 /*********************************************************************
763  *      SystemTimeToFileTime                            (KERNEL32.@)
764  */
765 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
766 {
767     TIME_FIELDS tf;
768     LARGE_INTEGER t;
769
770     tf.Year = syst->wYear;
771     tf.Month = syst->wMonth;
772     tf.Day = syst->wDay;
773     tf.Hour = syst->wHour;
774     tf.Minute = syst->wMinute;
775     tf.Second = syst->wSecond;
776     tf.Milliseconds = syst->wMilliseconds;
777
778     RtlTimeFieldsToTime(&tf, &t);
779     ft->dwLowDateTime = t.s.LowPart;
780     ft->dwHighDateTime = t.s.HighPart;
781     return TRUE;
782 }
783
784 /*********************************************************************
785  *      CompareFileTime                                 (KERNEL32.@)
786  */
787 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
788 {
789     if (!x || !y) return -1;
790
791     if (x->dwHighDateTime > y->dwHighDateTime)
792         return 1;
793     if (x->dwHighDateTime < y->dwHighDateTime)
794         return -1;
795     if (x->dwLowDateTime > y->dwLowDateTime)
796         return 1;
797     if (x->dwLowDateTime < y->dwLowDateTime)
798         return -1;
799     return 0;
800 }
801
802 /*********************************************************************
803  *      GetLocalTime                                    (KERNEL32.@)
804  */
805 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime)
806 {
807     FILETIME lft;
808     LARGE_INTEGER ft, ft2;
809
810     NtQuerySystemTime(&ft);
811     RtlSystemTimeToLocalTime(&ft, &ft2);
812     lft.dwLowDateTime = ft2.s.LowPart;
813     lft.dwHighDateTime = ft2.s.HighPart;
814     FileTimeToSystemTime(&lft, systime);
815 }
816
817 /*********************************************************************
818  *      GetSystemTime                                   (KERNEL32.@)
819  */
820 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
821 {
822     FILETIME ft;
823     LARGE_INTEGER t;
824
825     NtQuerySystemTime(&t);
826     ft.dwLowDateTime = t.s.LowPart;
827     ft.dwHighDateTime = t.s.HighPart;
828     FileTimeToSystemTime(&ft, systime);
829 }