Added a test to make sure that CreateDirectoryA/W does not create
[wine] / dlls / kernel / time.c
1 /*
2  * Win32 kernel time functions
3  *
4  * Copyright 1995 Martin von Loewis and Cameron Heide
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <time.h>
30 #ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
32 #endif
33 #ifdef HAVE_SYS_TIMES_H
34 # include <sys/times.h>
35 #endif
36
37 #define NONAMELESSUNION
38 #define NONAMELESSSTRUCT
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winreg.h"
42 #include "ntstatus.h"
43 #include "winternl.h"
44 #include "winerror.h"
45 #include "winnls.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 occures
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 calulated 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 interupt in 100's of nanoseconds. */
306     PDWORD lpTimeIncrement,          /* [out] The time between clock interupts in 100's of nanoseconds. */
307     PBOOL  lpTimeAdjustmentDisabled) /* [out] The clock synchonisation 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     if ((status = RtlQueryTimeZoneInformation(tzinfo))) {
376         SetLastError( RtlNtStatusToDosError(status) );
377         return TIME_ZONE_ID_INVALID;
378     }
379     return TIME_ZoneID( tzinfo );
380 }
381
382 /***********************************************************************
383  *              SetTimeZoneInformation  (KERNEL32.@)
384  *
385  *  Change the settings of the current local time zone.
386  *
387  * RETURNS
388  *  Success: TRUE. The time zone was updated with the settings from tzinfo
389  *  Failure: FALSE.
390  */
391 BOOL WINAPI SetTimeZoneInformation(
392     const TIME_ZONE_INFORMATION *tzinfo) /* [in] The new time zone. */
393 {
394     NTSTATUS status;
395     if ((status = RtlSetTimeZoneInformation(tzinfo)))
396         SetLastError( RtlNtStatusToDosError(status) );
397     return !status;
398 }
399
400 /***********************************************************************
401  *              SystemTimeToTzSpecificLocalTime  (KERNEL32.@)
402  *
403  *  Convert a utc system time to a local time in a given time zone.
404  *
405  * RETURNS
406  *  Success: TRUE. lpLocalTime contains the converted time
407  *  Failure: FALSE.
408  */
409
410 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
411     LPTIME_ZONE_INFORMATION
412            lpTimeZoneInformation, /* [in] The desired time zone. */
413     LPSYSTEMTIME lpUniversalTime, /* [in] The utc time to base local time on. */
414     LPSYSTEMTIME lpLocalTime)     /* [out] The local time in the time zone. */
415 {
416     FILETIME ft;
417     LONG lBias;
418     LONGLONG llTime;
419     TIME_ZONE_INFORMATION tzinfo;
420
421     if (lpTimeZoneInformation != NULL)
422     {
423         memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
424     }
425     else
426     {
427         if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
428             return FALSE;
429     }
430
431     if (!SystemTimeToFileTime(lpUniversalTime, &ft))
432         return FALSE;
433     FILETIME2LL( &ft, llTime)
434     if (!TIME_GetTimezoneBias(&tzinfo, &ft, FALSE, &lBias))
435         return FALSE;
436     /* convert minutes to 100-nanoseconds-ticks */
437     llTime -= (LONGLONG)lBias * 600000000;
438     LL2FILETIME( llTime, &ft)
439
440     return FileTimeToSystemTime(&ft, lpLocalTime);
441 }
442
443
444 /***********************************************************************
445  *              TzSpecificLocalTimeToSystemTime  (KERNEL32.@)
446  *
447  *  Converts a local time to a time in utc.
448  *
449  * RETURNS
450  *  Success: TRUE. lpUniversalTime contains the converted time
451  *  Failure: FALSE.
452  */
453 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
454     LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
455     LPSYSTEMTIME            lpLocalTime,           /* [in] The local time. */
456     LPSYSTEMTIME            lpUniversalTime)       /* [out] The calculated utc time. */
457 {
458     FILETIME ft;
459     LONG lBias;
460     LONGLONG t;
461     TIME_ZONE_INFORMATION tzinfo;
462
463     if (lpTimeZoneInformation != NULL)
464     {
465         memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
466     }
467     else
468     {
469         if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
470             return FALSE;
471     }
472
473     if (!SystemTimeToFileTime(lpLocalTime, &ft))
474         return FALSE;
475     FILETIME2LL( &ft, t)
476     if (!TIME_GetTimezoneBias(&tzinfo, &ft, TRUE, &lBias))
477         return FALSE;
478     /* convert minutes to 100-nanoseconds-ticks */
479     t += (LONGLONG)lBias * 600000000;
480     LL2FILETIME( t, &ft)
481     return FileTimeToSystemTime(&ft, lpUniversalTime);
482 }
483
484
485 /***********************************************************************
486  *              GetSystemTimeAsFileTime  (KERNEL32.@)
487  *
488  *  Get the current time in utc format.
489  *
490  *  RETURNS
491  *   Nothing.
492  */
493 VOID WINAPI GetSystemTimeAsFileTime(
494     LPFILETIME time) /* [out] Destination for the current utc time */
495 {
496     LARGE_INTEGER t;
497     NtQuerySystemTime( &t );
498     time->dwLowDateTime = t.u.LowPart;
499     time->dwHighDateTime = t.u.HighPart;
500 }
501
502
503 /*********************************************************************
504  *      TIME_ClockTimeToFileTime    (olorin@fandra.org, 20-Sep-1998)
505  *
506  *  Used by GetProcessTimes to convert clock_t into FILETIME.
507  *
508  *      Differences to UnixTimeToFileTime:
509  *          1) Divided by CLK_TCK
510  *          2) Time is relative. There is no 'starting date', so there is
511  *             no need for offset correction, like in UnixTimeToFileTime
512  */
513 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
514 {
515     ULONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
516     secs = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
517     filetime->dwLowDateTime  = (DWORD)secs;
518     filetime->dwHighDateTime = (DWORD)(secs >> 32);
519 }
520
521 /*********************************************************************
522  *      GetProcessTimes                         (KERNEL32.@)
523  *
524  *  Get the user and kernel execution times of a process,
525  *  along with the creation and exit times if known.
526  *
527  * RETURNS
528  *  TRUE.
529  *
530  * NOTES
531  *  olorin@fandra.org:
532  *  Would be nice to subtract the cpu time used by Wine at startup.
533  *  Also, there is a need to separate times used by different applications.
534  *
535  * BUGS
536  *  lpCreationTime and lpExitTime are not initialised in the Wine implementation.
537  */
538 BOOL WINAPI GetProcessTimes(
539     HANDLE     hprocess,       /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
540     LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
541     LPFILETIME lpExitTime,     /* [out] The exit time of the process if exited. */
542     LPFILETIME lpKernelTime,   /* [out] The time spent in kernal routines in 100's of nanoseconds. */
543     LPFILETIME lpUserTime)     /* [out] The time spent in user routines in 100's of nanoseconds. */
544 {
545     struct tms tms;
546
547     times(&tms);
548     TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
549     TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
550     return TRUE;
551 }
552
553 /*********************************************************************
554  *      GetCalendarInfoA                                (KERNEL32.@)
555  *
556  */
557 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
558                             LPSTR lpCalData, int cchData, LPDWORD lpValue)
559 {
560     int ret;
561     LPWSTR lpCalDataW = NULL;
562
563     FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
564           lcid, Calendar, CalType, lpCalData, cchData, lpValue);
565
566     lcid = ConvertDefaultLocale(lcid);
567
568     if (NLS_IsUnicodeOnlyLcid(lcid))
569     {
570       SetLastError(ERROR_INVALID_PARAMETER);
571       return 0;
572     }
573
574     if (cchData &&
575         !(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchData*sizeof(WCHAR))))
576       return 0;
577
578     ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchData, lpValue);
579     if(ret && lpCalDataW && lpCalData)
580       WideCharToMultiByte(CP_ACP, 0, lpCalDataW, cchData, lpCalData, cchData, NULL, NULL);
581     if(lpCalDataW)
582       HeapFree(GetProcessHeap(), 0, lpCalDataW);
583
584     return ret;
585 }
586
587 /*********************************************************************
588  *      GetCalendarInfoW                                (KERNEL32.@)
589  *
590  * See GetCalendarInfoA.
591  */
592 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
593                             LPWSTR lpCalData, int cchData, LPDWORD lpValue)
594 {
595     FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
596           Locale, Calendar, CalType, lpCalData, cchData, lpValue);
597
598     if (CalType & CAL_NOUSEROVERRIDE)
599         FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
600     if (CalType & CAL_USE_CP_ACP)
601         FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
602
603     if (CalType & CAL_RETURN_NUMBER) {
604         if (lpCalData != NULL)
605             WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
606         if (cchData != 0)
607             WARN("cchData not 0 (%d) when it should!\n", cchData);
608     } else {
609         if (lpValue != NULL)
610             WARN("lpValue not NULL (%p) when it should!\n", lpValue);
611     }
612
613     /* FIXME: No verification is made yet wrt Locale
614      * for the CALTYPES not requiring GetLocaleInfoA */
615     switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {
616         case CAL_ICALINTVALUE:
617             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
618             return E_FAIL;
619         case CAL_SCALNAME:
620             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
621             return E_FAIL;
622         case CAL_IYEAROFFSETRANGE:
623             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
624             return E_FAIL;
625         case CAL_SERASTRING:
626             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
627             return E_FAIL;
628         case CAL_SSHORTDATE:
629             return GetLocaleInfoW(Locale, LOCALE_SSHORTDATE, lpCalData, cchData);
630         case CAL_SLONGDATE:
631             return GetLocaleInfoW(Locale, LOCALE_SLONGDATE, lpCalData, cchData);
632         case CAL_SDAYNAME1:
633             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME1, lpCalData, cchData);
634         case CAL_SDAYNAME2:
635             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME2, lpCalData, cchData);
636         case CAL_SDAYNAME3:
637             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME3, lpCalData, cchData);
638         case CAL_SDAYNAME4:
639             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME4, lpCalData, cchData);
640         case CAL_SDAYNAME5:
641             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME5, lpCalData, cchData);
642         case CAL_SDAYNAME6:
643             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME6, lpCalData, cchData);
644         case CAL_SDAYNAME7:
645             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME7, lpCalData, cchData);
646         case CAL_SABBREVDAYNAME1:
647             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME1, lpCalData, cchData);
648         case CAL_SABBREVDAYNAME2:
649             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME2, lpCalData, cchData);
650         case CAL_SABBREVDAYNAME3:
651             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME3, lpCalData, cchData);
652         case CAL_SABBREVDAYNAME4:
653             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME4, lpCalData, cchData);
654         case CAL_SABBREVDAYNAME5:
655             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME5, lpCalData, cchData);
656         case CAL_SABBREVDAYNAME6:
657             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME6, lpCalData, cchData);
658         case CAL_SABBREVDAYNAME7:
659             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME7, lpCalData, cchData);
660         case CAL_SMONTHNAME1:
661             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME1, lpCalData, cchData);
662         case CAL_SMONTHNAME2:
663             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME2, lpCalData, cchData);
664         case CAL_SMONTHNAME3:
665             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME3, lpCalData, cchData);
666         case CAL_SMONTHNAME4:
667             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME4, lpCalData, cchData);
668         case CAL_SMONTHNAME5:
669             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME5, lpCalData, cchData);
670         case CAL_SMONTHNAME6:
671             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME6, lpCalData, cchData);
672         case CAL_SMONTHNAME7:
673             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME7, lpCalData, cchData);
674         case CAL_SMONTHNAME8:
675             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME8, lpCalData, cchData);
676         case CAL_SMONTHNAME9:
677             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME9, lpCalData, cchData);
678         case CAL_SMONTHNAME10:
679             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME10, lpCalData, cchData);
680         case CAL_SMONTHNAME11:
681             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME11, lpCalData, cchData);
682         case CAL_SMONTHNAME12:
683             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME12, lpCalData, cchData);
684         case CAL_SMONTHNAME13:
685             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME13, lpCalData, cchData);
686         case CAL_SABBREVMONTHNAME1:
687             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME1, lpCalData, cchData);
688         case CAL_SABBREVMONTHNAME2:
689             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME2, lpCalData, cchData);
690         case CAL_SABBREVMONTHNAME3:
691             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME3, lpCalData, cchData);
692         case CAL_SABBREVMONTHNAME4:
693             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME4, lpCalData, cchData);
694         case CAL_SABBREVMONTHNAME5:
695             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME5, lpCalData, cchData);
696         case CAL_SABBREVMONTHNAME6:
697             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME6, lpCalData, cchData);
698         case CAL_SABBREVMONTHNAME7:
699             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME7, lpCalData, cchData);
700         case CAL_SABBREVMONTHNAME8:
701             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME8, lpCalData, cchData);
702         case CAL_SABBREVMONTHNAME9:
703             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME9, lpCalData, cchData);
704         case CAL_SABBREVMONTHNAME10:
705             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME10, lpCalData, cchData);
706         case CAL_SABBREVMONTHNAME11:
707             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME11, lpCalData, cchData);
708         case CAL_SABBREVMONTHNAME12:
709             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME12, lpCalData, cchData);
710         case CAL_SABBREVMONTHNAME13:
711             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME13, lpCalData, cchData);
712         case CAL_SYEARMONTH:
713             return GetLocaleInfoW(Locale, LOCALE_SYEARMONTH, lpCalData, cchData);
714         case CAL_ITWODIGITYEARMAX:
715             if (lpValue) *lpValue = CALINFO_MAX_YEAR;
716             break;
717         default: MESSAGE("Unknown caltype %ld\n",CalType & 0xffff);
718                  return E_FAIL;
719     }
720     return 0;
721 }
722
723 /*********************************************************************
724  *      SetCalendarInfoA                                (KERNEL32.@)
725  *
726  */
727 int WINAPI      SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
728 {
729     FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
730           Locale, Calendar, CalType, debugstr_a(lpCalData));
731     return 0;
732 }
733
734 /*********************************************************************
735  *      SetCalendarInfoW                                (KERNEL32.@)
736  *
737  * See SetCalendarInfoA.
738  */
739 int WINAPI      SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
740 {
741     FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
742           Locale, Calendar, CalType, debugstr_w(lpCalData));
743     return 0;
744 }
745
746 /*********************************************************************
747  *      LocalFileTimeToFileTime                         (KERNEL32.@)
748  */
749 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
750 {
751     NTSTATUS status;
752     LARGE_INTEGER local, utc;
753
754     local.u.LowPart = localft->dwLowDateTime;
755     local.u.HighPart = localft->dwHighDateTime;
756     if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
757     {
758         utcft->dwLowDateTime = utc.u.LowPart;
759         utcft->dwHighDateTime = utc.u.HighPart;
760     }
761     else SetLastError( RtlNtStatusToDosError(status) );
762
763     return !status;
764 }
765
766 /*********************************************************************
767  *      FileTimeToLocalFileTime                         (KERNEL32.@)
768  */
769 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
770 {
771     NTSTATUS status;
772     LARGE_INTEGER local, utc;
773
774     utc.u.LowPart = utcft->dwLowDateTime;
775     utc.u.HighPart = utcft->dwHighDateTime;
776     if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
777     {
778         localft->dwLowDateTime = local.u.LowPart;
779         localft->dwHighDateTime = local.u.HighPart;
780     }
781     else SetLastError( RtlNtStatusToDosError(status) );
782
783     return !status;
784 }
785
786 /*********************************************************************
787  *      FileTimeToSystemTime                            (KERNEL32.@)
788  */
789 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
790 {
791     TIME_FIELDS tf;
792     LARGE_INTEGER t;
793
794     t.u.LowPart = ft->dwLowDateTime;
795     t.u.HighPart = ft->dwHighDateTime;
796     RtlTimeToTimeFields(&t, &tf);
797
798     syst->wYear = tf.Year;
799     syst->wMonth = tf.Month;
800     syst->wDay = tf.Day;
801     syst->wHour = tf.Hour;
802     syst->wMinute = tf.Minute;
803     syst->wSecond = tf.Second;
804     syst->wMilliseconds = tf.Milliseconds;
805     syst->wDayOfWeek = tf.Weekday;
806     return TRUE;
807 }
808
809 /*********************************************************************
810  *      SystemTimeToFileTime                            (KERNEL32.@)
811  */
812 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
813 {
814     TIME_FIELDS tf;
815     LARGE_INTEGER t;
816
817     tf.Year = syst->wYear;
818     tf.Month = syst->wMonth;
819     tf.Day = syst->wDay;
820     tf.Hour = syst->wHour;
821     tf.Minute = syst->wMinute;
822     tf.Second = syst->wSecond;
823     tf.Milliseconds = syst->wMilliseconds;
824
825     if( !RtlTimeFieldsToTime(&tf, &t)) {
826         SetLastError( ERROR_INVALID_PARAMETER);
827         return FALSE;
828     }
829     ft->dwLowDateTime = t.u.LowPart;
830     ft->dwHighDateTime = t.u.HighPart;
831     return TRUE;
832 }
833
834 /*********************************************************************
835  *      CompareFileTime                                 (KERNEL32.@)
836  *
837  * Compare two FILETIME's to each other.
838  *
839  * PARAMS
840  *  x [I] First time
841  *  y [I] time to compare to x
842  *
843  * RETURNS
844  *  -1, 0, or 1 indicating that x is less than, equal to, or greater
845  *  than y respectively.
846  */
847 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
848 {
849     if (!x || !y) return -1;
850
851     if (x->dwHighDateTime > y->dwHighDateTime)
852         return 1;
853     if (x->dwHighDateTime < y->dwHighDateTime)
854         return -1;
855     if (x->dwLowDateTime > y->dwLowDateTime)
856         return 1;
857     if (x->dwLowDateTime < y->dwLowDateTime)
858         return -1;
859     return 0;
860 }
861
862 /*********************************************************************
863  *      GetLocalTime                                    (KERNEL32.@)
864  *
865  * Get the current local time.
866  *
867  * RETURNS
868  *  Nothing.
869  */
870 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
871 {
872     FILETIME lft;
873     LARGE_INTEGER ft, ft2;
874
875     NtQuerySystemTime(&ft);
876     RtlSystemTimeToLocalTime(&ft, &ft2);
877     lft.dwLowDateTime = ft2.u.LowPart;
878     lft.dwHighDateTime = ft2.u.HighPart;
879     FileTimeToSystemTime(&lft, systime);
880 }
881
882 /*********************************************************************
883  *      GetSystemTime                                   (KERNEL32.@)
884  *
885  * Get the current system time.
886  *
887  * RETURNS
888  *  Nothing.
889  */
890 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
891 {
892     FILETIME ft;
893     LARGE_INTEGER t;
894
895     NtQuerySystemTime(&t);
896     ft.dwLowDateTime = t.u.LowPart;
897     ft.dwHighDateTime = t.u.HighPart;
898     FileTimeToSystemTime(&ft, systime);
899 }
900
901 /*********************************************************************
902  *      GetDaylightFlag                                   (KERNEL32.@)
903  *
904  *      returns TRUE if daylight saving time is in operation
905  *
906  *      Note: this function is called from the Win98's control applet
907  *      timedate.cpl
908  */
909 BOOL WINAPI GetDaylightFlag(void)
910 {
911     TIME_ZONE_INFORMATION tzinfo;
912     return GetTimeZoneInformation( &tzinfo) == TIME_ZONE_ID_DAYLIGHT;
913 }
914
915 /***********************************************************************
916  *           DosDateTimeToFileTime   (KERNEL32.@)
917  */
918 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
919 {
920     struct tm newtm;
921 #ifndef HAVE_TIMEGM
922     struct tm *gtm;
923     time_t time1, time2;
924 #endif
925
926     newtm.tm_sec  = (fattime & 0x1f) * 2;
927     newtm.tm_min  = (fattime >> 5) & 0x3f;
928     newtm.tm_hour = (fattime >> 11);
929     newtm.tm_mday = (fatdate & 0x1f);
930     newtm.tm_mon  = ((fatdate >> 5) & 0x0f) - 1;
931     newtm.tm_year = (fatdate >> 9) + 80;
932 #ifdef HAVE_TIMEGM
933     RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
934 #else
935     time1 = mktime(&newtm);
936     gtm = gmtime(&time1);
937     time2 = mktime(gtm);
938     RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
939 #endif
940     return TRUE;
941 }
942
943
944 /***********************************************************************
945  *           FileTimeToDosDateTime   (KERNEL32.@)
946  */
947 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
948                                      LPWORD fattime )
949 {
950     LARGE_INTEGER       li;
951     ULONG               t;
952     time_t              unixtime;
953     struct tm*          tm;
954
955     li.u.LowPart = ft->dwLowDateTime;
956     li.u.HighPart = ft->dwHighDateTime;
957     RtlTimeToSecondsSince1970( &li, &t );
958     unixtime = t;
959     tm = gmtime( &unixtime );
960     if (fattime)
961         *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
962     if (fatdate)
963         *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
964                    + tm->tm_mday;
965     return TRUE;
966 }