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