Use wine_get_dos_file_name rather than relying on GetFullPathNameW
[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 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
520 {
521     ULONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
522     secs = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
523     filetime->dwLowDateTime  = (DWORD)secs;
524     filetime->dwHighDateTime = (DWORD)(secs >> 32);
525 }
526
527 /*********************************************************************
528  *      GetProcessTimes                         (KERNEL32.@)
529  *
530  *  Get the user and kernel execution times of a process,
531  *  along with the creation and exit times if known.
532  *
533  * RETURNS
534  *  TRUE.
535  *
536  * NOTES
537  *  olorin@fandra.org:
538  *  Would be nice to subtract the cpu time used by Wine at startup.
539  *  Also, there is a need to separate times used by different applications.
540  *
541  * BUGS
542  *  lpCreationTime and lpExitTime are not initialised in the Wine implementation.
543  */
544 BOOL WINAPI GetProcessTimes(
545     HANDLE     hprocess,       /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
546     LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
547     LPFILETIME lpExitTime,     /* [out] The exit time of the process if exited. */
548     LPFILETIME lpKernelTime,   /* [out] The time spent in kernel routines in 100's of nanoseconds. */
549     LPFILETIME lpUserTime)     /* [out] The time spent in user routines in 100's of nanoseconds. */
550 {
551     struct tms tms;
552
553     times(&tms);
554     TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
555     TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
556     return TRUE;
557 }
558
559 /*********************************************************************
560  *      GetCalendarInfoA                                (KERNEL32.@)
561  *
562  */
563 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
564                             LPSTR lpCalData, int cchData, LPDWORD lpValue)
565 {
566     int ret;
567     LPWSTR lpCalDataW = NULL;
568
569     FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
570           lcid, Calendar, CalType, lpCalData, cchData, lpValue);
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  * See GetCalendarInfoA.
596  */
597 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
598                             LPWSTR lpCalData, int cchData, LPDWORD lpValue)
599 {
600     FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
601           Locale, Calendar, CalType, lpCalData, cchData, lpValue);
602
603     if (CalType & CAL_NOUSEROVERRIDE)
604         FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
605     if (CalType & CAL_USE_CP_ACP)
606         FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
607
608     if (CalType & CAL_RETURN_NUMBER) {
609         if (lpCalData != NULL)
610             WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
611         if (cchData != 0)
612             WARN("cchData not 0 (%d) when it should!\n", cchData);
613     } else {
614         if (lpValue != NULL)
615             WARN("lpValue not NULL (%p) when it should!\n", lpValue);
616     }
617
618     /* FIXME: No verification is made yet wrt Locale
619      * for the CALTYPES not requiring GetLocaleInfoA */
620     switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {
621         case CAL_ICALINTVALUE:
622             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
623             return E_FAIL;
624         case CAL_SCALNAME:
625             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
626             return E_FAIL;
627         case CAL_IYEAROFFSETRANGE:
628             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
629             return E_FAIL;
630         case CAL_SERASTRING:
631             FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
632             return E_FAIL;
633         case CAL_SSHORTDATE:
634             return GetLocaleInfoW(Locale, LOCALE_SSHORTDATE, lpCalData, cchData);
635         case CAL_SLONGDATE:
636             return GetLocaleInfoW(Locale, LOCALE_SLONGDATE, lpCalData, cchData);
637         case CAL_SDAYNAME1:
638             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME1, lpCalData, cchData);
639         case CAL_SDAYNAME2:
640             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME2, lpCalData, cchData);
641         case CAL_SDAYNAME3:
642             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME3, lpCalData, cchData);
643         case CAL_SDAYNAME4:
644             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME4, lpCalData, cchData);
645         case CAL_SDAYNAME5:
646             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME5, lpCalData, cchData);
647         case CAL_SDAYNAME6:
648             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME6, lpCalData, cchData);
649         case CAL_SDAYNAME7:
650             return GetLocaleInfoW(Locale, LOCALE_SDAYNAME7, lpCalData, cchData);
651         case CAL_SABBREVDAYNAME1:
652             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME1, lpCalData, cchData);
653         case CAL_SABBREVDAYNAME2:
654             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME2, lpCalData, cchData);
655         case CAL_SABBREVDAYNAME3:
656             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME3, lpCalData, cchData);
657         case CAL_SABBREVDAYNAME4:
658             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME4, lpCalData, cchData);
659         case CAL_SABBREVDAYNAME5:
660             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME5, lpCalData, cchData);
661         case CAL_SABBREVDAYNAME6:
662             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME6, lpCalData, cchData);
663         case CAL_SABBREVDAYNAME7:
664             return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME7, lpCalData, cchData);
665         case CAL_SMONTHNAME1:
666             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME1, lpCalData, cchData);
667         case CAL_SMONTHNAME2:
668             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME2, lpCalData, cchData);
669         case CAL_SMONTHNAME3:
670             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME3, lpCalData, cchData);
671         case CAL_SMONTHNAME4:
672             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME4, lpCalData, cchData);
673         case CAL_SMONTHNAME5:
674             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME5, lpCalData, cchData);
675         case CAL_SMONTHNAME6:
676             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME6, lpCalData, cchData);
677         case CAL_SMONTHNAME7:
678             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME7, lpCalData, cchData);
679         case CAL_SMONTHNAME8:
680             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME8, lpCalData, cchData);
681         case CAL_SMONTHNAME9:
682             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME9, lpCalData, cchData);
683         case CAL_SMONTHNAME10:
684             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME10, lpCalData, cchData);
685         case CAL_SMONTHNAME11:
686             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME11, lpCalData, cchData);
687         case CAL_SMONTHNAME12:
688             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME12, lpCalData, cchData);
689         case CAL_SMONTHNAME13:
690             return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME13, lpCalData, cchData);
691         case CAL_SABBREVMONTHNAME1:
692             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME1, lpCalData, cchData);
693         case CAL_SABBREVMONTHNAME2:
694             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME2, lpCalData, cchData);
695         case CAL_SABBREVMONTHNAME3:
696             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME3, lpCalData, cchData);
697         case CAL_SABBREVMONTHNAME4:
698             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME4, lpCalData, cchData);
699         case CAL_SABBREVMONTHNAME5:
700             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME5, lpCalData, cchData);
701         case CAL_SABBREVMONTHNAME6:
702             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME6, lpCalData, cchData);
703         case CAL_SABBREVMONTHNAME7:
704             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME7, lpCalData, cchData);
705         case CAL_SABBREVMONTHNAME8:
706             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME8, lpCalData, cchData);
707         case CAL_SABBREVMONTHNAME9:
708             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME9, lpCalData, cchData);
709         case CAL_SABBREVMONTHNAME10:
710             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME10, lpCalData, cchData);
711         case CAL_SABBREVMONTHNAME11:
712             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME11, lpCalData, cchData);
713         case CAL_SABBREVMONTHNAME12:
714             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME12, lpCalData, cchData);
715         case CAL_SABBREVMONTHNAME13:
716             return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME13, lpCalData, cchData);
717         case CAL_SYEARMONTH:
718             return GetLocaleInfoW(Locale, LOCALE_SYEARMONTH, lpCalData, cchData);
719         case CAL_ITWODIGITYEARMAX:
720             if (lpValue) *lpValue = CALINFO_MAX_YEAR;
721             break;
722         default: MESSAGE("Unknown caltype %ld\n",CalType & 0xffff);
723                  return E_FAIL;
724     }
725     return 0;
726 }
727
728 /*********************************************************************
729  *      SetCalendarInfoA                                (KERNEL32.@)
730  *
731  */
732 int WINAPI      SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
733 {
734     FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
735           Locale, Calendar, CalType, debugstr_a(lpCalData));
736     return 0;
737 }
738
739 /*********************************************************************
740  *      SetCalendarInfoW                                (KERNEL32.@)
741  *
742  * See SetCalendarInfoA.
743  */
744 int WINAPI      SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
745 {
746     FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
747           Locale, Calendar, CalType, debugstr_w(lpCalData));
748     return 0;
749 }
750
751 /*********************************************************************
752  *      LocalFileTimeToFileTime                         (KERNEL32.@)
753  */
754 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
755 {
756     NTSTATUS status;
757     LARGE_INTEGER local, utc;
758
759     local.u.LowPart = localft->dwLowDateTime;
760     local.u.HighPart = localft->dwHighDateTime;
761     if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
762     {
763         utcft->dwLowDateTime = utc.u.LowPart;
764         utcft->dwHighDateTime = utc.u.HighPart;
765     }
766     else SetLastError( RtlNtStatusToDosError(status) );
767
768     return !status;
769 }
770
771 /*********************************************************************
772  *      FileTimeToLocalFileTime                         (KERNEL32.@)
773  */
774 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
775 {
776     NTSTATUS status;
777     LARGE_INTEGER local, utc;
778
779     utc.u.LowPart = utcft->dwLowDateTime;
780     utc.u.HighPart = utcft->dwHighDateTime;
781     if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
782     {
783         localft->dwLowDateTime = local.u.LowPart;
784         localft->dwHighDateTime = local.u.HighPart;
785     }
786     else SetLastError( RtlNtStatusToDosError(status) );
787
788     return !status;
789 }
790
791 /*********************************************************************
792  *      FileTimeToSystemTime                            (KERNEL32.@)
793  */
794 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
795 {
796     TIME_FIELDS tf;
797     LARGE_INTEGER t;
798
799     t.u.LowPart = ft->dwLowDateTime;
800     t.u.HighPart = ft->dwHighDateTime;
801     RtlTimeToTimeFields(&t, &tf);
802
803     syst->wYear = tf.Year;
804     syst->wMonth = tf.Month;
805     syst->wDay = tf.Day;
806     syst->wHour = tf.Hour;
807     syst->wMinute = tf.Minute;
808     syst->wSecond = tf.Second;
809     syst->wMilliseconds = tf.Milliseconds;
810     syst->wDayOfWeek = tf.Weekday;
811     return TRUE;
812 }
813
814 /*********************************************************************
815  *      SystemTimeToFileTime                            (KERNEL32.@)
816  */
817 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
818 {
819     TIME_FIELDS tf;
820     LARGE_INTEGER t;
821
822     tf.Year = syst->wYear;
823     tf.Month = syst->wMonth;
824     tf.Day = syst->wDay;
825     tf.Hour = syst->wHour;
826     tf.Minute = syst->wMinute;
827     tf.Second = syst->wSecond;
828     tf.Milliseconds = syst->wMilliseconds;
829
830     if( !RtlTimeFieldsToTime(&tf, &t)) {
831         SetLastError( ERROR_INVALID_PARAMETER);
832         return FALSE;
833     }
834     ft->dwLowDateTime = t.u.LowPart;
835     ft->dwHighDateTime = t.u.HighPart;
836     return TRUE;
837 }
838
839 /*********************************************************************
840  *      CompareFileTime                                 (KERNEL32.@)
841  *
842  * Compare two FILETIME's to each other.
843  *
844  * PARAMS
845  *  x [I] First time
846  *  y [I] time to compare to x
847  *
848  * RETURNS
849  *  -1, 0, or 1 indicating that x is less than, equal to, or greater
850  *  than y respectively.
851  */
852 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
853 {
854     if (!x || !y) return -1;
855
856     if (x->dwHighDateTime > y->dwHighDateTime)
857         return 1;
858     if (x->dwHighDateTime < y->dwHighDateTime)
859         return -1;
860     if (x->dwLowDateTime > y->dwLowDateTime)
861         return 1;
862     if (x->dwLowDateTime < y->dwLowDateTime)
863         return -1;
864     return 0;
865 }
866
867 /*********************************************************************
868  *      GetLocalTime                                    (KERNEL32.@)
869  *
870  * Get the current local time.
871  *
872  * RETURNS
873  *  Nothing.
874  */
875 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
876 {
877     FILETIME lft;
878     LARGE_INTEGER ft, ft2;
879
880     NtQuerySystemTime(&ft);
881     RtlSystemTimeToLocalTime(&ft, &ft2);
882     lft.dwLowDateTime = ft2.u.LowPart;
883     lft.dwHighDateTime = ft2.u.HighPart;
884     FileTimeToSystemTime(&lft, systime);
885 }
886
887 /*********************************************************************
888  *      GetSystemTime                                   (KERNEL32.@)
889  *
890  * Get the current system time.
891  *
892  * RETURNS
893  *  Nothing.
894  */
895 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
896 {
897     FILETIME ft;
898     LARGE_INTEGER t;
899
900     NtQuerySystemTime(&t);
901     ft.dwLowDateTime = t.u.LowPart;
902     ft.dwHighDateTime = t.u.HighPart;
903     FileTimeToSystemTime(&ft, systime);
904 }
905
906 /*********************************************************************
907  *      GetDaylightFlag                                   (KERNEL32.@)
908  *
909  *      returns TRUE if daylight saving time is in operation
910  *
911  *      Note: this function is called from the Win98's control applet
912  *      timedate.cpl
913  */
914 BOOL WINAPI GetDaylightFlag(void)
915 {
916     TIME_ZONE_INFORMATION tzinfo;
917     return GetTimeZoneInformation( &tzinfo) == TIME_ZONE_ID_DAYLIGHT;
918 }
919
920 /***********************************************************************
921  *           DosDateTimeToFileTime   (KERNEL32.@)
922  */
923 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
924 {
925     struct tm newtm;
926 #ifndef HAVE_TIMEGM
927     struct tm *gtm;
928     time_t time1, time2;
929 #endif
930
931     newtm.tm_sec  = (fattime & 0x1f) * 2;
932     newtm.tm_min  = (fattime >> 5) & 0x3f;
933     newtm.tm_hour = (fattime >> 11);
934     newtm.tm_mday = (fatdate & 0x1f);
935     newtm.tm_mon  = ((fatdate >> 5) & 0x0f) - 1;
936     newtm.tm_year = (fatdate >> 9) + 80;
937 #ifdef HAVE_TIMEGM
938     RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
939 #else
940     time1 = mktime(&newtm);
941     gtm = gmtime(&time1);
942     time2 = mktime(gtm);
943     RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
944 #endif
945     return TRUE;
946 }
947
948
949 /***********************************************************************
950  *           FileTimeToDosDateTime   (KERNEL32.@)
951  */
952 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
953                                      LPWORD fattime )
954 {
955     LARGE_INTEGER       li;
956     ULONG               t;
957     time_t              unixtime;
958     struct tm*          tm;
959
960     li.u.LowPart = ft->dwLowDateTime;
961     li.u.HighPart = ft->dwHighDateTime;
962     RtlTimeToSecondsSince1970( &li, &t );
963     unixtime = t;
964     tm = gmtime( &unixtime );
965     if (fattime)
966         *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
967     if (fatdate)
968         *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
969                    + tm->tm_mday;
970     return TRUE;
971 }