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