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