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