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