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