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