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