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