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