Fix a regression in IE where the Favourites menu didn't appear
[wine/testsucceed.git] / dlls / kernel / time.c
blob5f3a275956ae60847728d10a73568b303f956d81
1 /*
2 * Win32 kernel time functions
4 * Copyright 1995 Martin von Loewis and Cameron Heide
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
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_MACHINE_LIMITS_H
37 #include <machine/limits.h>
38 #endif
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
42 #include "windef.h"
43 #include "winbase.h"
44 #include "winternl.h"
45 #include "ntstatus.h"
46 #include "kernel_private.h"
47 #include "wine/unicode.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(time);
52 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
53 #define SETTIME_MAX_ADJUST 120
54 #define CALINFO_MAX_YEAR 2029
56 #define LL2FILETIME( ll, pft )\
57 (pft)->dwLowDateTime = (UINT)(ll); \
58 (pft)->dwHighDateTime = (UINT)((ll) >> 32);
59 #define FILETIME2LL( pft, ll) \
60 ll = (((LONGLONG)((pft)->dwHighDateTime))<<32) + (pft)-> dwLowDateTime ;
63 static const int MonthLengths[2][12] =
65 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
66 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
69 static inline int IsLeapYear(int Year)
71 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
74 /***********************************************************************
75 * TIME_DayLightCompareDate
77 * Compares two dates without looking at the year
79 * RETURNS
81 * -1 if date < compareDate
82 * 0 if date == compareDate
83 * 1 if date > compareDate
84 * -2 if an error occurs
86 static int TIME_DayLightCompareDate(
87 const SYSTEMTIME *date, /* [in] The local time to compare. */
88 const SYSTEMTIME *compareDate) /* [in] The daylight saving begin
89 or end date */
91 int limit_day, dayinsecs;
93 if (date->wMonth < compareDate->wMonth)
94 return -1; /* We are in a month before the date limit. */
96 if (date->wMonth > compareDate->wMonth)
97 return 1; /* We are in a month after the date limit. */
99 if (compareDate->wDayOfWeek <= 6)
101 WORD First;
102 /* compareDate->wDay is interpreted as number of the week in the month
103 * 5 means: the last week in the month */
104 int weekofmonth = compareDate->wDay;
105 /* calculate the day of the first DayOfWeek in the month */
106 First = ( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay
107 ) % 7 + 1;
108 limit_day = First + 7 * (weekofmonth - 1);
109 /* check needed for the 5th weekday of the month */
110 if(limit_day > MonthLengths[date->wMonth==2 && IsLeapYear(date->wYear)]
111 [date->wMonth - 1])
112 limit_day -= 7;
114 else
116 limit_day = compareDate->wDay;
119 /* convert to seconds */
120 limit_day = ((limit_day * 24 + compareDate->wHour) * 60 +
121 compareDate->wMinute ) * 60;
122 dayinsecs = ((date->wDay * 24 + date->wHour) * 60 +
123 date->wMinute ) * 60 + date->wSecond;
124 /* and compare */
125 return dayinsecs < limit_day ? -1 :
126 dayinsecs > limit_day ? 1 :
127 0; /* date is equal to the date limit. */
130 /***********************************************************************
131 * TIME_CompTimeZoneID
133 * Computes the local time bias for a given time and time zone
135 * Returns:
136 * TIME_ZONE_ID_INVALID An error occurred
137 * TIME_ZONE_ID_UNKNOWN There are no transition time known
138 * TIME_ZONE_ID_STANDARD Current time is standard time
139 * TIME_ZONE_ID_DAYLIGHT Current time is dayligh saving time
141 static BOOL TIME_CompTimeZoneID (
142 const TIME_ZONE_INFORMATION *pTZinfo, /* [in] The time zone data. */
143 FILETIME *lpFileTime, /* [in] The system or local time. */
144 BOOL islocal /* [in] it is local time */
147 int ret;
148 BOOL beforeStandardDate, afterDaylightDate;
149 DWORD retval = TIME_ZONE_ID_INVALID;
150 LONGLONG llTime = 0; /* initialized to prevent gcc complaining */
151 SYSTEMTIME SysTime;
152 FILETIME ftTemp;
154 if (pTZinfo->DaylightDate.wMonth != 0)
156 if (pTZinfo->StandardDate.wMonth == 0 ||
157 pTZinfo->StandardDate.wDay<1 ||
158 pTZinfo->StandardDate.wDay>5 ||
159 pTZinfo->DaylightDate.wDay<1 ||
160 pTZinfo->DaylightDate.wDay>5)
162 SetLastError(ERROR_INVALID_PARAMETER);
163 return TIME_ZONE_ID_INVALID;
166 if (!islocal) {
167 FILETIME2LL( lpFileTime, llTime );
168 llTime -= ( pTZinfo->Bias + pTZinfo->DaylightBias )
169 * (LONGLONG)600000000;
170 LL2FILETIME( llTime, &ftTemp)
171 lpFileTime = &ftTemp;
174 FileTimeToSystemTime(lpFileTime, &SysTime);
176 /* check for daylight saving */
177 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->StandardDate);
178 if (ret == -2)
179 return TIME_ZONE_ID_INVALID;
181 beforeStandardDate = ret < 0;
183 if (!islocal) {
184 llTime -= ( pTZinfo->StandardBias - pTZinfo->DaylightBias )
185 * (LONGLONG)600000000;
186 LL2FILETIME( llTime, &ftTemp)
187 FileTimeToSystemTime(lpFileTime, &SysTime);
190 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->DaylightDate);
191 if (ret == -2)
192 return TIME_ZONE_ID_INVALID;
194 afterDaylightDate = ret >= 0;
196 retval = TIME_ZONE_ID_STANDARD;
197 if( pTZinfo->DaylightDate.wMonth < pTZinfo->StandardDate.wMonth ) {
198 /* Northern hemisphere */
199 if( beforeStandardDate && afterDaylightDate )
200 retval = TIME_ZONE_ID_DAYLIGHT;
201 } else /* Down south */
202 if( beforeStandardDate || afterDaylightDate )
203 retval = TIME_ZONE_ID_DAYLIGHT;
204 } else
205 /* No transition date */
206 retval = TIME_ZONE_ID_UNKNOWN;
208 return retval;
211 /***********************************************************************
212 * TIME_TimeZoneID
214 * Calculates whether daylight saving is on now.
216 * Returns:
217 * TIME_ZONE_ID_INVALID An error occurred
218 * TIME_ZONE_ID_UNKNOWN There are no transition time known
219 * TIME_ZONE_ID_STANDARD Current time is standard time
220 * TIME_ZONE_ID_DAYLIGHT Current time is dayligh saving time
222 static DWORD TIME_ZoneID(
223 const TIME_ZONE_INFORMATION *pTzi /* Timezone info */
226 FILETIME ftTime;
227 GetSystemTimeAsFileTime( &ftTime);
228 return TIME_CompTimeZoneID( pTzi, &ftTime, FALSE);
231 /***********************************************************************
232 * TIME_GetTimezoneBias
234 * Calculates the local time bias for a given time zone
236 * RETURNS
238 * Returns TRUE when the time zone bias was calculated.
240 static BOOL TIME_GetTimezoneBias(
241 const TIME_ZONE_INFORMATION
242 *pTZinfo, /* [in] The time zone data. */
243 FILETIME *lpFileTime, /* [in] The system or local time. */
244 BOOL islocal, /* [in] it is local time */
245 LONG *pBias /* [out] The calculated bias in minutes */
248 LONG bias = pTZinfo->Bias;
249 DWORD tzid = TIME_CompTimeZoneID( pTZinfo, lpFileTime, islocal);
251 if( tzid == TIME_ZONE_ID_INVALID)
252 return FALSE;
253 if (tzid == TIME_ZONE_ID_DAYLIGHT)
254 bias += pTZinfo->DaylightBias;
255 else if (tzid == TIME_ZONE_ID_STANDARD)
256 bias += pTZinfo->StandardBias;
257 *pBias = bias;
258 return TRUE;
262 /***********************************************************************
263 * SetLocalTime (KERNEL32.@)
265 * Set the local time using current time zone and daylight
266 * savings settings.
268 * RETURNS
269 * Success: TRUE. The time was set
270 * Failure: FALSE, if the time was invalid or caller does not have
271 * permission to change the time.
273 BOOL WINAPI SetLocalTime(
274 const SYSTEMTIME *systime) /* [in] The desired local time. */
276 FILETIME ft;
277 LARGE_INTEGER st, st2;
278 NTSTATUS status;
280 if( !SystemTimeToFileTime( systime, &ft ))
281 return FALSE;
282 st.u.LowPart = ft.dwLowDateTime;
283 st.u.HighPart = ft.dwHighDateTime;
284 RtlLocalTimeToSystemTime( &st, &st2 );
286 if ((status = NtSetSystemTime(&st2, NULL)))
287 SetLastError( RtlNtStatusToDosError(status) );
288 return !status;
292 /***********************************************************************
293 * GetSystemTimeAdjustment (KERNEL32.@)
295 * Get the period between clock interrupts and the amount the clock
296 * is adjusted each interrupt so as to keep it in sync with an external source.
298 * RETURNS
299 * TRUE.
301 * BUGS
302 * Only the special case of disabled time adjustments is supported.
304 BOOL WINAPI GetSystemTimeAdjustment(
305 PDWORD lpTimeAdjustment, /* [out] The clock adjustment per interrupt in 100's of nanoseconds. */
306 PDWORD lpTimeIncrement, /* [out] The time between clock interrupts in 100's of nanoseconds. */
307 PBOOL lpTimeAdjustmentDisabled) /* [out] The clock synchronisation has been disabled. */
309 *lpTimeAdjustment = 0;
310 *lpTimeIncrement = 0;
311 *lpTimeAdjustmentDisabled = TRUE;
312 return TRUE;
316 /***********************************************************************
317 * SetSystemTime (KERNEL32.@)
319 * Set the system time in utc.
321 * RETURNS
322 * Success: TRUE. The time was set
323 * Failure: FALSE, if the time was invalid or caller does not have
324 * permission to change the time.
326 BOOL WINAPI SetSystemTime(
327 const SYSTEMTIME *systime) /* [in] The desired system time. */
329 FILETIME ft;
330 LARGE_INTEGER t;
331 NTSTATUS status;
333 if( !SystemTimeToFileTime( systime, &ft ))
334 return FALSE;
335 t.u.LowPart = ft.dwLowDateTime;
336 t.u.HighPart = ft.dwHighDateTime;
337 if ((status = NtSetSystemTime(&t, NULL)))
338 SetLastError( RtlNtStatusToDosError(status) );
339 return !status;
342 /***********************************************************************
343 * SetSystemTimeAdjustment (KERNEL32.@)
345 * Enables or disables the timing adjustments to the system's clock.
347 * RETURNS
348 * Success: TRUE.
349 * Failure: FALSE.
351 BOOL WINAPI SetSystemTimeAdjustment(
352 DWORD dwTimeAdjustment,
353 BOOL bTimeAdjustmentDisabled)
355 /* Fake function for now... */
356 FIXME("(%08lx,%d): stub !\n", dwTimeAdjustment, bTimeAdjustmentDisabled);
357 return TRUE;
360 /***********************************************************************
361 * GetTimeZoneInformation (KERNEL32.@)
363 * Get information about the current local time zone.
365 * RETURNS
366 * TIME_ZONE_ID_INVALID An error occurred
367 * TIME_ZONE_ID_UNKNOWN There are no transition time known
368 * TIME_ZONE_ID_STANDARD Current time is standard time
369 * TIME_ZONE_ID_DAYLIGHT Current time is dayligh saving time
371 DWORD WINAPI GetTimeZoneInformation(
372 LPTIME_ZONE_INFORMATION tzinfo) /* [out] Destination for time zone information */
374 NTSTATUS status;
376 status = RtlQueryTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION*)tzinfo );
377 if ( status != STATUS_SUCCESS )
379 SetLastError( RtlNtStatusToDosError(status) );
380 return TIME_ZONE_ID_INVALID;
382 return TIME_ZoneID( tzinfo );
385 /***********************************************************************
386 * SetTimeZoneInformation (KERNEL32.@)
388 * Change the settings of the current local time zone.
390 * RETURNS
391 * Success: TRUE. The time zone was updated with the settings from tzinfo
392 * Failure: FALSE.
394 BOOL WINAPI SetTimeZoneInformation(
395 const TIME_ZONE_INFORMATION *tzinfo) /* [in] The new time zone. */
397 NTSTATUS status;
398 status = RtlSetTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION*) tzinfo );
399 if ( status != STATUS_SUCCESS )
400 SetLastError( RtlNtStatusToDosError(status) );
401 return !status;
404 /***********************************************************************
405 * SystemTimeToTzSpecificLocalTime (KERNEL32.@)
407 * Convert a utc system time to a local time in a given time zone.
409 * RETURNS
410 * Success: TRUE. lpLocalTime contains the converted time
411 * Failure: FALSE.
414 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
415 LPTIME_ZONE_INFORMATION
416 lpTimeZoneInformation, /* [in] The desired time zone. */
417 LPSYSTEMTIME lpUniversalTime, /* [in] The utc time to base local time on. */
418 LPSYSTEMTIME lpLocalTime) /* [out] The local time in the time zone. */
420 FILETIME ft;
421 LONG lBias;
422 LONGLONG llTime;
423 TIME_ZONE_INFORMATION tzinfo;
425 if (lpTimeZoneInformation != NULL)
427 memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
429 else
431 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
432 return FALSE;
435 if (!SystemTimeToFileTime(lpUniversalTime, &ft))
436 return FALSE;
437 FILETIME2LL( &ft, llTime)
438 if (!TIME_GetTimezoneBias(&tzinfo, &ft, FALSE, &lBias))
439 return FALSE;
440 /* convert minutes to 100-nanoseconds-ticks */
441 llTime -= (LONGLONG)lBias * 600000000;
442 LL2FILETIME( llTime, &ft)
444 return FileTimeToSystemTime(&ft, lpLocalTime);
448 /***********************************************************************
449 * TzSpecificLocalTimeToSystemTime (KERNEL32.@)
451 * Converts a local time to a time in utc.
453 * RETURNS
454 * Success: TRUE. lpUniversalTime contains the converted time
455 * Failure: FALSE.
457 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
458 LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
459 LPSYSTEMTIME lpLocalTime, /* [in] The local time. */
460 LPSYSTEMTIME lpUniversalTime) /* [out] The calculated utc time. */
462 FILETIME ft;
463 LONG lBias;
464 LONGLONG t;
465 TIME_ZONE_INFORMATION tzinfo;
467 if (lpTimeZoneInformation != NULL)
469 memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
471 else
473 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
474 return FALSE;
477 if (!SystemTimeToFileTime(lpLocalTime, &ft))
478 return FALSE;
479 FILETIME2LL( &ft, t)
480 if (!TIME_GetTimezoneBias(&tzinfo, &ft, TRUE, &lBias))
481 return FALSE;
482 /* convert minutes to 100-nanoseconds-ticks */
483 t += (LONGLONG)lBias * 600000000;
484 LL2FILETIME( t, &ft)
485 return FileTimeToSystemTime(&ft, lpUniversalTime);
489 /***********************************************************************
490 * GetSystemTimeAsFileTime (KERNEL32.@)
492 * Get the current time in utc format.
494 * RETURNS
495 * Nothing.
497 VOID WINAPI GetSystemTimeAsFileTime(
498 LPFILETIME time) /* [out] Destination for the current utc time */
500 LARGE_INTEGER t;
501 NtQuerySystemTime( &t );
502 time->dwLowDateTime = t.u.LowPart;
503 time->dwHighDateTime = t.u.HighPart;
507 /*********************************************************************
508 * TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
510 * Used by GetProcessTimes to convert clock_t into FILETIME.
512 * Differences to UnixTimeToFileTime:
513 * 1) Divided by CLK_TCK
514 * 2) Time is relative. There is no 'starting date', so there is
515 * no need for offset correction, like in UnixTimeToFileTime
517 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
519 ULONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
520 secs = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
521 filetime->dwLowDateTime = (DWORD)secs;
522 filetime->dwHighDateTime = (DWORD)(secs >> 32);
525 /*********************************************************************
526 * GetProcessTimes (KERNEL32.@)
528 * Get the user and kernel execution times of a process,
529 * along with the creation and exit times if known.
531 * RETURNS
532 * TRUE.
534 * NOTES
535 * olorin@fandra.org:
536 * Would be nice to subtract the cpu time used by Wine at startup.
537 * Also, there is a need to separate times used by different applications.
539 * BUGS
540 * lpCreationTime and lpExitTime are not initialised in the Wine implementation.
542 BOOL WINAPI GetProcessTimes(
543 HANDLE hprocess, /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
544 LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
545 LPFILETIME lpExitTime, /* [out] The exit time of the process if exited. */
546 LPFILETIME lpKernelTime, /* [out] The time spent in kernel routines in 100's of nanoseconds. */
547 LPFILETIME lpUserTime) /* [out] The time spent in user routines in 100's of nanoseconds. */
549 struct tms tms;
551 times(&tms);
552 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
553 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
554 return TRUE;
557 /*********************************************************************
558 * GetCalendarInfoA (KERNEL32.@)
561 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
562 LPSTR lpCalData, int cchData, LPDWORD lpValue)
564 int ret;
565 LPWSTR lpCalDataW = NULL;
567 FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
568 lcid, Calendar, CalType, lpCalData, cchData, lpValue);
570 lcid = ConvertDefaultLocale(lcid);
572 if (NLS_IsUnicodeOnlyLcid(lcid))
574 SetLastError(ERROR_INVALID_PARAMETER);
575 return 0;
578 if (cchData &&
579 !(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchData*sizeof(WCHAR))))
580 return 0;
582 ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchData, lpValue);
583 if(ret && lpCalDataW && lpCalData)
584 WideCharToMultiByte(CP_ACP, 0, lpCalDataW, cchData, lpCalData, cchData, NULL, NULL);
585 HeapFree(GetProcessHeap(), 0, lpCalDataW);
587 return ret;
590 /*********************************************************************
591 * GetCalendarInfoW (KERNEL32.@)
593 * See GetCalendarInfoA.
595 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
596 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
598 FIXME("(%08lx,%08lx,%08lx,%p,%d,%p): quarter-stub\n",
599 Locale, Calendar, CalType, lpCalData, cchData, lpValue);
601 if (CalType & CAL_NOUSEROVERRIDE)
602 FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
603 if (CalType & CAL_USE_CP_ACP)
604 FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
606 if (CalType & CAL_RETURN_NUMBER) {
607 if (lpCalData != NULL)
608 WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
609 if (cchData != 0)
610 WARN("cchData not 0 (%d) when it should!\n", cchData);
611 } else {
612 if (lpValue != NULL)
613 WARN("lpValue not NULL (%p) when it should!\n", lpValue);
616 /* FIXME: No verification is made yet wrt Locale
617 * for the CALTYPES not requiring GetLocaleInfoA */
618 switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {
619 case CAL_ICALINTVALUE:
620 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
621 return E_FAIL;
622 case CAL_SCALNAME:
623 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
624 return E_FAIL;
625 case CAL_IYEAROFFSETRANGE:
626 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
627 return E_FAIL;
628 case CAL_SERASTRING:
629 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
630 return E_FAIL;
631 case CAL_SSHORTDATE:
632 return GetLocaleInfoW(Locale, LOCALE_SSHORTDATE, lpCalData, cchData);
633 case CAL_SLONGDATE:
634 return GetLocaleInfoW(Locale, LOCALE_SLONGDATE, lpCalData, cchData);
635 case CAL_SDAYNAME1:
636 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME1, lpCalData, cchData);
637 case CAL_SDAYNAME2:
638 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME2, lpCalData, cchData);
639 case CAL_SDAYNAME3:
640 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME3, lpCalData, cchData);
641 case CAL_SDAYNAME4:
642 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME4, lpCalData, cchData);
643 case CAL_SDAYNAME5:
644 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME5, lpCalData, cchData);
645 case CAL_SDAYNAME6:
646 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME6, lpCalData, cchData);
647 case CAL_SDAYNAME7:
648 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME7, lpCalData, cchData);
649 case CAL_SABBREVDAYNAME1:
650 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME1, lpCalData, cchData);
651 case CAL_SABBREVDAYNAME2:
652 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME2, lpCalData, cchData);
653 case CAL_SABBREVDAYNAME3:
654 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME3, lpCalData, cchData);
655 case CAL_SABBREVDAYNAME4:
656 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME4, lpCalData, cchData);
657 case CAL_SABBREVDAYNAME5:
658 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME5, lpCalData, cchData);
659 case CAL_SABBREVDAYNAME6:
660 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME6, lpCalData, cchData);
661 case CAL_SABBREVDAYNAME7:
662 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME7, lpCalData, cchData);
663 case CAL_SMONTHNAME1:
664 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME1, lpCalData, cchData);
665 case CAL_SMONTHNAME2:
666 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME2, lpCalData, cchData);
667 case CAL_SMONTHNAME3:
668 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME3, lpCalData, cchData);
669 case CAL_SMONTHNAME4:
670 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME4, lpCalData, cchData);
671 case CAL_SMONTHNAME5:
672 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME5, lpCalData, cchData);
673 case CAL_SMONTHNAME6:
674 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME6, lpCalData, cchData);
675 case CAL_SMONTHNAME7:
676 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME7, lpCalData, cchData);
677 case CAL_SMONTHNAME8:
678 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME8, lpCalData, cchData);
679 case CAL_SMONTHNAME9:
680 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME9, lpCalData, cchData);
681 case CAL_SMONTHNAME10:
682 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME10, lpCalData, cchData);
683 case CAL_SMONTHNAME11:
684 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME11, lpCalData, cchData);
685 case CAL_SMONTHNAME12:
686 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME12, lpCalData, cchData);
687 case CAL_SMONTHNAME13:
688 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME13, lpCalData, cchData);
689 case CAL_SABBREVMONTHNAME1:
690 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME1, lpCalData, cchData);
691 case CAL_SABBREVMONTHNAME2:
692 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME2, lpCalData, cchData);
693 case CAL_SABBREVMONTHNAME3:
694 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME3, lpCalData, cchData);
695 case CAL_SABBREVMONTHNAME4:
696 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME4, lpCalData, cchData);
697 case CAL_SABBREVMONTHNAME5:
698 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME5, lpCalData, cchData);
699 case CAL_SABBREVMONTHNAME6:
700 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME6, lpCalData, cchData);
701 case CAL_SABBREVMONTHNAME7:
702 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME7, lpCalData, cchData);
703 case CAL_SABBREVMONTHNAME8:
704 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME8, lpCalData, cchData);
705 case CAL_SABBREVMONTHNAME9:
706 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME9, lpCalData, cchData);
707 case CAL_SABBREVMONTHNAME10:
708 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME10, lpCalData, cchData);
709 case CAL_SABBREVMONTHNAME11:
710 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME11, lpCalData, cchData);
711 case CAL_SABBREVMONTHNAME12:
712 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME12, lpCalData, cchData);
713 case CAL_SABBREVMONTHNAME13:
714 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME13, lpCalData, cchData);
715 case CAL_SYEARMONTH:
716 return GetLocaleInfoW(Locale, LOCALE_SYEARMONTH, lpCalData, cchData);
717 case CAL_ITWODIGITYEARMAX:
718 if (lpValue) *lpValue = CALINFO_MAX_YEAR;
719 break;
720 default: MESSAGE("Unknown caltype %ld\n",CalType & 0xffff);
721 return E_FAIL;
723 return 0;
726 /*********************************************************************
727 * SetCalendarInfoA (KERNEL32.@)
730 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
732 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
733 Locale, Calendar, CalType, debugstr_a(lpCalData));
734 return 0;
737 /*********************************************************************
738 * SetCalendarInfoW (KERNEL32.@)
740 * See SetCalendarInfoA.
742 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
744 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
745 Locale, Calendar, CalType, debugstr_w(lpCalData));
746 return 0;
749 /*********************************************************************
750 * LocalFileTimeToFileTime (KERNEL32.@)
752 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
754 NTSTATUS status;
755 LARGE_INTEGER local, utc;
757 local.u.LowPart = localft->dwLowDateTime;
758 local.u.HighPart = localft->dwHighDateTime;
759 if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
761 utcft->dwLowDateTime = utc.u.LowPart;
762 utcft->dwHighDateTime = utc.u.HighPart;
764 else SetLastError( RtlNtStatusToDosError(status) );
766 return !status;
769 /*********************************************************************
770 * FileTimeToLocalFileTime (KERNEL32.@)
772 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
774 NTSTATUS status;
775 LARGE_INTEGER local, utc;
777 utc.u.LowPart = utcft->dwLowDateTime;
778 utc.u.HighPart = utcft->dwHighDateTime;
779 if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
781 localft->dwLowDateTime = local.u.LowPart;
782 localft->dwHighDateTime = local.u.HighPart;
784 else SetLastError( RtlNtStatusToDosError(status) );
786 return !status;
789 /*********************************************************************
790 * FileTimeToSystemTime (KERNEL32.@)
792 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
794 TIME_FIELDS tf;
795 LARGE_INTEGER t;
797 t.u.LowPart = ft->dwLowDateTime;
798 t.u.HighPart = ft->dwHighDateTime;
799 RtlTimeToTimeFields(&t, &tf);
801 syst->wYear = tf.Year;
802 syst->wMonth = tf.Month;
803 syst->wDay = tf.Day;
804 syst->wHour = tf.Hour;
805 syst->wMinute = tf.Minute;
806 syst->wSecond = tf.Second;
807 syst->wMilliseconds = tf.Milliseconds;
808 syst->wDayOfWeek = tf.Weekday;
809 return TRUE;
812 /*********************************************************************
813 * SystemTimeToFileTime (KERNEL32.@)
815 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
817 TIME_FIELDS tf;
818 LARGE_INTEGER t;
820 tf.Year = syst->wYear;
821 tf.Month = syst->wMonth;
822 tf.Day = syst->wDay;
823 tf.Hour = syst->wHour;
824 tf.Minute = syst->wMinute;
825 tf.Second = syst->wSecond;
826 tf.Milliseconds = syst->wMilliseconds;
828 if( !RtlTimeFieldsToTime(&tf, &t)) {
829 SetLastError( ERROR_INVALID_PARAMETER);
830 return FALSE;
832 ft->dwLowDateTime = t.u.LowPart;
833 ft->dwHighDateTime = t.u.HighPart;
834 return TRUE;
837 /*********************************************************************
838 * CompareFileTime (KERNEL32.@)
840 * Compare two FILETIME's to each other.
842 * PARAMS
843 * x [I] First time
844 * y [I] time to compare to x
846 * RETURNS
847 * -1, 0, or 1 indicating that x is less than, equal to, or greater
848 * than y respectively.
850 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
852 if (!x || !y) return -1;
854 if (x->dwHighDateTime > y->dwHighDateTime)
855 return 1;
856 if (x->dwHighDateTime < y->dwHighDateTime)
857 return -1;
858 if (x->dwLowDateTime > y->dwLowDateTime)
859 return 1;
860 if (x->dwLowDateTime < y->dwLowDateTime)
861 return -1;
862 return 0;
865 /*********************************************************************
866 * GetLocalTime (KERNEL32.@)
868 * Get the current local time.
870 * RETURNS
871 * Nothing.
873 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
875 FILETIME lft;
876 LARGE_INTEGER ft, ft2;
878 NtQuerySystemTime(&ft);
879 RtlSystemTimeToLocalTime(&ft, &ft2);
880 lft.dwLowDateTime = ft2.u.LowPart;
881 lft.dwHighDateTime = ft2.u.HighPart;
882 FileTimeToSystemTime(&lft, systime);
885 /*********************************************************************
886 * GetSystemTime (KERNEL32.@)
888 * Get the current system time.
890 * RETURNS
891 * Nothing.
893 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
895 FILETIME ft;
896 LARGE_INTEGER t;
898 NtQuerySystemTime(&t);
899 ft.dwLowDateTime = t.u.LowPart;
900 ft.dwHighDateTime = t.u.HighPart;
901 FileTimeToSystemTime(&ft, systime);
904 /*********************************************************************
905 * GetDaylightFlag (KERNEL32.@)
907 * returns TRUE if daylight saving time is in operation
909 * Note: this function is called from the Win98's control applet
910 * timedate.cpl
912 BOOL WINAPI GetDaylightFlag(void)
914 TIME_ZONE_INFORMATION tzinfo;
915 return GetTimeZoneInformation( &tzinfo) == TIME_ZONE_ID_DAYLIGHT;
918 /***********************************************************************
919 * DosDateTimeToFileTime (KERNEL32.@)
921 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
923 struct tm newtm;
924 #ifndef HAVE_TIMEGM
925 struct tm *gtm;
926 time_t time1, time2;
927 #endif
929 newtm.tm_sec = (fattime & 0x1f) * 2;
930 newtm.tm_min = (fattime >> 5) & 0x3f;
931 newtm.tm_hour = (fattime >> 11);
932 newtm.tm_mday = (fatdate & 0x1f);
933 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
934 newtm.tm_year = (fatdate >> 9) + 80;
935 #ifdef HAVE_TIMEGM
936 RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
937 #else
938 time1 = mktime(&newtm);
939 gtm = gmtime(&time1);
940 time2 = mktime(gtm);
941 RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
942 #endif
943 return TRUE;
947 /***********************************************************************
948 * FileTimeToDosDateTime (KERNEL32.@)
950 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
951 LPWORD fattime )
953 LARGE_INTEGER li;
954 ULONG t;
955 time_t unixtime;
956 struct tm* tm;
958 li.u.LowPart = ft->dwLowDateTime;
959 li.u.HighPart = ft->dwHighDateTime;
960 RtlTimeToSecondsSince1970( &li, &t );
961 unixtime = t;
962 tm = gmtime( &unixtime );
963 if (fattime)
964 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
965 if (fatdate)
966 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
967 + tm->tm_mday;
968 return TRUE;