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