Define obsoleted CLK_TCK to CLOCKS_PER_SEC if not there.
[wine/wine64.git] / dlls / kernel / time.c
blob1154adcb5af33fea3c73d690a7064224f812f95f
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_SYS_LIMITS_H
37 #include <sys/limits.h>
38 #elif defined(HAVE_MACHINE_LIMITS_H)
39 #include <machine/limits.h>
40 #endif
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winternl.h"
47 #include "ntstatus.h"
48 #include "kernel_private.h"
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(time);
54 /* maximum time adjustment in seconds for SetLocalTime and SetSystemTime */
55 #define SETTIME_MAX_ADJUST 120
56 #define CALINFO_MAX_YEAR 2029
58 #define LL2FILETIME( ll, pft )\
59 (pft)->dwLowDateTime = (UINT)(ll); \
60 (pft)->dwHighDateTime = (UINT)((ll) >> 32);
61 #define FILETIME2LL( pft, ll) \
62 ll = (((LONGLONG)((pft)->dwHighDateTime))<<32) + (pft)-> dwLowDateTime ;
65 static const int MonthLengths[2][12] =
67 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
68 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
71 static inline int IsLeapYear(int Year)
73 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
76 /***********************************************************************
77 * TIME_DayLightCompareDate
79 * Compares two dates without looking at the year
81 * RETURNS
83 * -1 if date < compareDate
84 * 0 if date == compareDate
85 * 1 if date > compareDate
86 * -2 if an error occurs
88 static int TIME_DayLightCompareDate(
89 const SYSTEMTIME *date, /* [in] The local time to compare. */
90 const SYSTEMTIME *compareDate) /* [in] The daylight saving begin
91 or end date */
93 int limit_day, dayinsecs;
95 if (date->wMonth < compareDate->wMonth)
96 return -1; /* We are in a month before the date limit. */
98 if (date->wMonth > compareDate->wMonth)
99 return 1; /* We are in a month after the date limit. */
101 if (compareDate->wDayOfWeek <= 6)
103 WORD First;
104 /* compareDate->wDay is interpreted as number of the week in the month
105 * 5 means: the last week in the month */
106 int weekofmonth = compareDate->wDay;
107 /* calculate the day of the first DayOfWeek in the month */
108 First = ( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay
109 ) % 7 + 1;
110 limit_day = First + 7 * (weekofmonth - 1);
111 /* check needed for the 5th weekday of the month */
112 if(limit_day > MonthLengths[date->wMonth==2 && IsLeapYear(date->wYear)]
113 [date->wMonth - 1])
114 limit_day -= 7;
116 else
118 limit_day = compareDate->wDay;
121 /* convert to seconds */
122 limit_day = ((limit_day * 24 + compareDate->wHour) * 60 +
123 compareDate->wMinute ) * 60;
124 dayinsecs = ((date->wDay * 24 + date->wHour) * 60 +
125 date->wMinute ) * 60 + date->wSecond;
126 /* and compare */
127 return dayinsecs < limit_day ? -1 :
128 dayinsecs > limit_day ? 1 :
129 0; /* date is equal to the date limit. */
132 /***********************************************************************
133 * TIME_CompTimeZoneID
135 * Computes the local time bias for a given time and time zone
137 * Returns:
138 * TIME_ZONE_ID_INVALID An error occurred
139 * TIME_ZONE_ID_UNKNOWN There are no transition time known
140 * TIME_ZONE_ID_STANDARD Current time is standard time
141 * TIME_ZONE_ID_DAYLIGHT Current time is dayligh saving time
143 static BOOL TIME_CompTimeZoneID (
144 const TIME_ZONE_INFORMATION *pTZinfo, /* [in] The time zone data. */
145 FILETIME *lpFileTime, /* [in] The system or local time. */
146 BOOL islocal /* [in] it is local time */
149 int ret;
150 BOOL beforeStandardDate, afterDaylightDate;
151 DWORD retval = TIME_ZONE_ID_INVALID;
152 LONGLONG llTime = 0; /* initialized to prevent gcc complaining */
153 SYSTEMTIME SysTime;
154 FILETIME ftTemp;
156 if (pTZinfo->DaylightDate.wMonth != 0)
158 if (pTZinfo->StandardDate.wMonth == 0 ||
159 pTZinfo->StandardDate.wDay<1 ||
160 pTZinfo->StandardDate.wDay>5 ||
161 pTZinfo->DaylightDate.wDay<1 ||
162 pTZinfo->DaylightDate.wDay>5)
164 SetLastError(ERROR_INVALID_PARAMETER);
165 return TIME_ZONE_ID_INVALID;
168 if (!islocal) {
169 FILETIME2LL( lpFileTime, llTime );
170 llTime -= ( pTZinfo->Bias + pTZinfo->DaylightBias )
171 * (LONGLONG)600000000;
172 LL2FILETIME( llTime, &ftTemp)
173 lpFileTime = &ftTemp;
176 FileTimeToSystemTime(lpFileTime, &SysTime);
178 /* check for daylight saving */
179 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->StandardDate);
180 if (ret == -2)
181 return TIME_ZONE_ID_INVALID;
183 beforeStandardDate = ret < 0;
185 if (!islocal) {
186 llTime -= ( pTZinfo->StandardBias - pTZinfo->DaylightBias )
187 * (LONGLONG)600000000;
188 LL2FILETIME( llTime, &ftTemp)
189 FileTimeToSystemTime(lpFileTime, &SysTime);
192 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->DaylightDate);
193 if (ret == -2)
194 return TIME_ZONE_ID_INVALID;
196 afterDaylightDate = ret >= 0;
198 retval = TIME_ZONE_ID_STANDARD;
199 if( pTZinfo->DaylightDate.wMonth < pTZinfo->StandardDate.wMonth ) {
200 /* Northern hemisphere */
201 if( beforeStandardDate && afterDaylightDate )
202 retval = TIME_ZONE_ID_DAYLIGHT;
203 } else /* Down south */
204 if( beforeStandardDate || afterDaylightDate )
205 retval = TIME_ZONE_ID_DAYLIGHT;
206 } else
207 /* No transition date */
208 retval = TIME_ZONE_ID_UNKNOWN;
210 return retval;
213 /***********************************************************************
214 * TIME_TimeZoneID
216 * Calculates whether daylight saving is on now.
218 * Returns:
219 * TIME_ZONE_ID_INVALID An error occurred
220 * TIME_ZONE_ID_UNKNOWN There are no transition time known
221 * TIME_ZONE_ID_STANDARD Current time is standard time
222 * TIME_ZONE_ID_DAYLIGHT Current time is dayligh saving time
224 static DWORD TIME_ZoneID(
225 const TIME_ZONE_INFORMATION *pTzi /* Timezone info */
228 FILETIME ftTime;
229 GetSystemTimeAsFileTime( &ftTime);
230 return TIME_CompTimeZoneID( pTzi, &ftTime, FALSE);
233 /***********************************************************************
234 * TIME_GetTimezoneBias
236 * Calculates the local time bias for a given time zone
238 * RETURNS
240 * Returns TRUE when the time zone bias was calculated.
242 static BOOL TIME_GetTimezoneBias(
243 const TIME_ZONE_INFORMATION
244 *pTZinfo, /* [in] The time zone data. */
245 FILETIME *lpFileTime, /* [in] The system or local time. */
246 BOOL islocal, /* [in] it is local time */
247 LONG *pBias /* [out] The calculated bias in minutes */
250 LONG bias = pTZinfo->Bias;
251 DWORD tzid = TIME_CompTimeZoneID( pTZinfo, lpFileTime, islocal);
253 if( tzid == TIME_ZONE_ID_INVALID)
254 return FALSE;
255 if (tzid == TIME_ZONE_ID_DAYLIGHT)
256 bias += pTZinfo->DaylightBias;
257 else if (tzid == TIME_ZONE_ID_STANDARD)
258 bias += pTZinfo->StandardBias;
259 *pBias = bias;
260 return TRUE;
264 /***********************************************************************
265 * SetLocalTime (KERNEL32.@)
267 * Set the local time using current time zone and daylight
268 * savings settings.
270 * RETURNS
271 * Success: TRUE. The time was set
272 * Failure: FALSE, if the time was invalid or caller does not have
273 * permission to change the time.
275 BOOL WINAPI SetLocalTime(
276 const SYSTEMTIME *systime) /* [in] The desired local time. */
278 FILETIME ft;
279 LARGE_INTEGER st, st2;
280 NTSTATUS status;
282 if( !SystemTimeToFileTime( systime, &ft ))
283 return FALSE;
284 st.u.LowPart = ft.dwLowDateTime;
285 st.u.HighPart = ft.dwHighDateTime;
286 RtlLocalTimeToSystemTime( &st, &st2 );
288 if ((status = NtSetSystemTime(&st2, NULL)))
289 SetLastError( RtlNtStatusToDosError(status) );
290 return !status;
294 /***********************************************************************
295 * GetSystemTimeAdjustment (KERNEL32.@)
297 * Get the period between clock interrupts and the amount the clock
298 * is adjusted each interrupt so as to keep it in sync with an external source.
300 * RETURNS
301 * TRUE.
303 * BUGS
304 * Only the special case of disabled time adjustments is supported.
306 BOOL WINAPI GetSystemTimeAdjustment(
307 PDWORD lpTimeAdjustment, /* [out] The clock adjustment per interrupt in 100's of nanoseconds. */
308 PDWORD lpTimeIncrement, /* [out] The time between clock interrupts in 100's of nanoseconds. */
309 PBOOL lpTimeAdjustmentDisabled) /* [out] The clock synchronisation has been disabled. */
311 *lpTimeAdjustment = 0;
312 *lpTimeIncrement = 0;
313 *lpTimeAdjustmentDisabled = TRUE;
314 return TRUE;
318 /***********************************************************************
319 * SetSystemTime (KERNEL32.@)
321 * Set the system time in utc.
323 * RETURNS
324 * Success: TRUE. The time was set
325 * Failure: FALSE, if the time was invalid or caller does not have
326 * permission to change the time.
328 BOOL WINAPI SetSystemTime(
329 const SYSTEMTIME *systime) /* [in] The desired system time. */
331 FILETIME ft;
332 LARGE_INTEGER t;
333 NTSTATUS status;
335 if( !SystemTimeToFileTime( systime, &ft ))
336 return FALSE;
337 t.u.LowPart = ft.dwLowDateTime;
338 t.u.HighPart = ft.dwHighDateTime;
339 if ((status = NtSetSystemTime(&t, NULL)))
340 SetLastError( RtlNtStatusToDosError(status) );
341 return !status;
344 /***********************************************************************
345 * SetSystemTimeAdjustment (KERNEL32.@)
347 * Enables or disables the timing adjustments to the system's clock.
349 * RETURNS
350 * Success: TRUE.
351 * Failure: FALSE.
353 BOOL WINAPI SetSystemTimeAdjustment(
354 DWORD dwTimeAdjustment,
355 BOOL bTimeAdjustmentDisabled)
357 /* Fake function for now... */
358 FIXME("(%08lx,%d): stub !\n", dwTimeAdjustment, bTimeAdjustmentDisabled);
359 return TRUE;
362 /***********************************************************************
363 * GetTimeZoneInformation (KERNEL32.@)
365 * Get information about the current local time zone.
367 * RETURNS
368 * TIME_ZONE_ID_INVALID An error occurred
369 * TIME_ZONE_ID_UNKNOWN There are no transition time known
370 * TIME_ZONE_ID_STANDARD Current time is standard time
371 * TIME_ZONE_ID_DAYLIGHT Current time is dayligh saving time
373 DWORD WINAPI GetTimeZoneInformation(
374 LPTIME_ZONE_INFORMATION tzinfo) /* [out] Destination for time zone information */
376 NTSTATUS status;
378 status = RtlQueryTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION*)tzinfo );
379 if ( status != STATUS_SUCCESS )
381 SetLastError( RtlNtStatusToDosError(status) );
382 return TIME_ZONE_ID_INVALID;
384 return TIME_ZoneID( tzinfo );
387 /***********************************************************************
388 * SetTimeZoneInformation (KERNEL32.@)
390 * Change the settings of the current local time zone.
392 * RETURNS
393 * Success: TRUE. The time zone was updated with the settings from tzinfo
394 * Failure: FALSE.
396 BOOL WINAPI SetTimeZoneInformation(
397 const TIME_ZONE_INFORMATION *tzinfo) /* [in] The new time zone. */
399 NTSTATUS status;
400 status = RtlSetTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION*) tzinfo );
401 if ( status != STATUS_SUCCESS )
402 SetLastError( RtlNtStatusToDosError(status) );
403 return !status;
406 /***********************************************************************
407 * SystemTimeToTzSpecificLocalTime (KERNEL32.@)
409 * Convert a utc system time to a local time in a given time zone.
411 * RETURNS
412 * Success: TRUE. lpLocalTime contains the converted time
413 * Failure: FALSE.
416 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
417 LPTIME_ZONE_INFORMATION
418 lpTimeZoneInformation, /* [in] The desired time zone. */
419 LPSYSTEMTIME lpUniversalTime, /* [in] The utc time to base local time on. */
420 LPSYSTEMTIME lpLocalTime) /* [out] The local time in the time zone. */
422 FILETIME ft;
423 LONG lBias;
424 LONGLONG llTime;
425 TIME_ZONE_INFORMATION tzinfo;
427 if (lpTimeZoneInformation != NULL)
429 memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
431 else
433 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
434 return FALSE;
437 if (!SystemTimeToFileTime(lpUniversalTime, &ft))
438 return FALSE;
439 FILETIME2LL( &ft, llTime)
440 if (!TIME_GetTimezoneBias(&tzinfo, &ft, FALSE, &lBias))
441 return FALSE;
442 /* convert minutes to 100-nanoseconds-ticks */
443 llTime -= (LONGLONG)lBias * 600000000;
444 LL2FILETIME( llTime, &ft)
446 return FileTimeToSystemTime(&ft, lpLocalTime);
450 /***********************************************************************
451 * TzSpecificLocalTimeToSystemTime (KERNEL32.@)
453 * Converts a local time to a time in utc.
455 * RETURNS
456 * Success: TRUE. lpUniversalTime contains the converted time
457 * Failure: FALSE.
459 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
460 LPTIME_ZONE_INFORMATION lpTimeZoneInformation, /* [in] The desired time zone. */
461 LPSYSTEMTIME lpLocalTime, /* [in] The local time. */
462 LPSYSTEMTIME lpUniversalTime) /* [out] The calculated utc time. */
464 FILETIME ft;
465 LONG lBias;
466 LONGLONG t;
467 TIME_ZONE_INFORMATION tzinfo;
469 if (lpTimeZoneInformation != NULL)
471 memcpy(&tzinfo, lpTimeZoneInformation, sizeof(TIME_ZONE_INFORMATION));
473 else
475 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
476 return FALSE;
479 if (!SystemTimeToFileTime(lpLocalTime, &ft))
480 return FALSE;
481 FILETIME2LL( &ft, t)
482 if (!TIME_GetTimezoneBias(&tzinfo, &ft, TRUE, &lBias))
483 return FALSE;
484 /* convert minutes to 100-nanoseconds-ticks */
485 t += (LONGLONG)lBias * 600000000;
486 LL2FILETIME( t, &ft)
487 return FileTimeToSystemTime(&ft, lpUniversalTime);
491 /***********************************************************************
492 * GetSystemTimeAsFileTime (KERNEL32.@)
494 * Get the current time in utc format.
496 * RETURNS
497 * Nothing.
499 VOID WINAPI GetSystemTimeAsFileTime(
500 LPFILETIME time) /* [out] Destination for the current utc time */
502 LARGE_INTEGER t;
503 NtQuerySystemTime( &t );
504 time->dwLowDateTime = t.u.LowPart;
505 time->dwHighDateTime = t.u.HighPart;
509 /*********************************************************************
510 * TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
512 * Used by GetProcessTimes to convert clock_t into FILETIME.
514 * Differences to UnixTimeToFileTime:
515 * 1) Divided by CLK_TCK
516 * 2) Time is relative. There is no 'starting date', so there is
517 * no need for offset correction, like in UnixTimeToFileTime
519 #ifndef CLK_TCK
520 # define CLK_TCK CLOCKS_PER_SEC
521 #endif
522 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
524 ULONGLONG secs = RtlEnlargedUnsignedMultiply( unix_time, 10000000 );
525 secs = RtlExtendedLargeIntegerDivide( secs, CLK_TCK, NULL );
526 filetime->dwLowDateTime = (DWORD)secs;
527 filetime->dwHighDateTime = (DWORD)(secs >> 32);
530 /*********************************************************************
531 * GetProcessTimes (KERNEL32.@)
533 * Get the user and kernel execution times of a process,
534 * along with the creation and exit times if known.
536 * RETURNS
537 * TRUE.
539 * NOTES
540 * olorin@fandra.org:
541 * Would be nice to subtract the cpu time used by Wine at startup.
542 * Also, there is a need to separate times used by different applications.
544 * BUGS
545 * lpCreationTime and lpExitTime are not initialised in the Wine implementation.
547 BOOL WINAPI GetProcessTimes(
548 HANDLE hprocess, /* [in] The process to be queried (obtained from PROCESS_QUERY_INFORMATION). */
549 LPFILETIME lpCreationTime, /* [out] The creation time of the process. */
550 LPFILETIME lpExitTime, /* [out] The exit time of the process if exited. */
551 LPFILETIME lpKernelTime, /* [out] The time spent in kernel routines in 100's of nanoseconds. */
552 LPFILETIME lpUserTime) /* [out] The time spent in user routines in 100's of nanoseconds. */
554 struct tms tms;
556 times(&tms);
557 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
558 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
559 return TRUE;
562 /*********************************************************************
563 * GetCalendarInfoA (KERNEL32.@)
566 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
567 LPSTR lpCalData, int cchData, LPDWORD lpValue)
569 int ret;
570 LPWSTR lpCalDataW = NULL;
572 lcid = ConvertDefaultLocale(lcid);
574 if (NLS_IsUnicodeOnlyLcid(lcid))
576 SetLastError(ERROR_INVALID_PARAMETER);
577 return 0;
580 if (cchData &&
581 !(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchData*sizeof(WCHAR))))
582 return 0;
584 ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchData, lpValue);
585 if(ret && lpCalDataW && lpCalData)
586 WideCharToMultiByte(CP_ACP, 0, lpCalDataW, cchData, lpCalData, cchData, NULL, NULL);
587 HeapFree(GetProcessHeap(), 0, lpCalDataW);
589 return ret;
592 /*********************************************************************
593 * GetCalendarInfoW (KERNEL32.@)
595 * See GetCalendarInfoA.
597 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
598 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
600 if (CalType & CAL_NOUSEROVERRIDE)
601 FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
602 if (CalType & CAL_USE_CP_ACP)
603 FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
605 if (CalType & CAL_RETURN_NUMBER) {
606 if (lpCalData != NULL)
607 WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
608 if (cchData != 0)
609 WARN("cchData not 0 (%d) when it should!\n", cchData);
610 } else {
611 if (lpValue != NULL)
612 WARN("lpValue not NULL (%p) when it should!\n", lpValue);
615 /* FIXME: No verification is made yet wrt Locale
616 * for the CALTYPES not requiring GetLocaleInfoA */
617 switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {
618 case CAL_ICALINTVALUE:
619 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
620 return E_FAIL;
621 case CAL_SCALNAME:
622 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
623 return E_FAIL;
624 case CAL_IYEAROFFSETRANGE:
625 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
626 return E_FAIL;
627 case CAL_SERASTRING:
628 FIXME("Unimplemented caltype %ld\n", CalType & 0xffff);
629 return E_FAIL;
630 case CAL_SSHORTDATE:
631 return GetLocaleInfoW(Locale, LOCALE_SSHORTDATE, lpCalData, cchData);
632 case CAL_SLONGDATE:
633 return GetLocaleInfoW(Locale, LOCALE_SLONGDATE, lpCalData, cchData);
634 case CAL_SDAYNAME1:
635 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME1, lpCalData, cchData);
636 case CAL_SDAYNAME2:
637 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME2, lpCalData, cchData);
638 case CAL_SDAYNAME3:
639 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME3, lpCalData, cchData);
640 case CAL_SDAYNAME4:
641 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME4, lpCalData, cchData);
642 case CAL_SDAYNAME5:
643 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME5, lpCalData, cchData);
644 case CAL_SDAYNAME6:
645 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME6, lpCalData, cchData);
646 case CAL_SDAYNAME7:
647 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME7, lpCalData, cchData);
648 case CAL_SABBREVDAYNAME1:
649 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME1, lpCalData, cchData);
650 case CAL_SABBREVDAYNAME2:
651 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME2, lpCalData, cchData);
652 case CAL_SABBREVDAYNAME3:
653 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME3, lpCalData, cchData);
654 case CAL_SABBREVDAYNAME4:
655 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME4, lpCalData, cchData);
656 case CAL_SABBREVDAYNAME5:
657 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME5, lpCalData, cchData);
658 case CAL_SABBREVDAYNAME6:
659 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME6, lpCalData, cchData);
660 case CAL_SABBREVDAYNAME7:
661 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME7, lpCalData, cchData);
662 case CAL_SMONTHNAME1:
663 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME1, lpCalData, cchData);
664 case CAL_SMONTHNAME2:
665 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME2, lpCalData, cchData);
666 case CAL_SMONTHNAME3:
667 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME3, lpCalData, cchData);
668 case CAL_SMONTHNAME4:
669 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME4, lpCalData, cchData);
670 case CAL_SMONTHNAME5:
671 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME5, lpCalData, cchData);
672 case CAL_SMONTHNAME6:
673 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME6, lpCalData, cchData);
674 case CAL_SMONTHNAME7:
675 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME7, lpCalData, cchData);
676 case CAL_SMONTHNAME8:
677 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME8, lpCalData, cchData);
678 case CAL_SMONTHNAME9:
679 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME9, lpCalData, cchData);
680 case CAL_SMONTHNAME10:
681 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME10, lpCalData, cchData);
682 case CAL_SMONTHNAME11:
683 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME11, lpCalData, cchData);
684 case CAL_SMONTHNAME12:
685 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME12, lpCalData, cchData);
686 case CAL_SMONTHNAME13:
687 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME13, lpCalData, cchData);
688 case CAL_SABBREVMONTHNAME1:
689 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME1, lpCalData, cchData);
690 case CAL_SABBREVMONTHNAME2:
691 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME2, lpCalData, cchData);
692 case CAL_SABBREVMONTHNAME3:
693 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME3, lpCalData, cchData);
694 case CAL_SABBREVMONTHNAME4:
695 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME4, lpCalData, cchData);
696 case CAL_SABBREVMONTHNAME5:
697 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME5, lpCalData, cchData);
698 case CAL_SABBREVMONTHNAME6:
699 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME6, lpCalData, cchData);
700 case CAL_SABBREVMONTHNAME7:
701 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME7, lpCalData, cchData);
702 case CAL_SABBREVMONTHNAME8:
703 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME8, lpCalData, cchData);
704 case CAL_SABBREVMONTHNAME9:
705 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME9, lpCalData, cchData);
706 case CAL_SABBREVMONTHNAME10:
707 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME10, lpCalData, cchData);
708 case CAL_SABBREVMONTHNAME11:
709 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME11, lpCalData, cchData);
710 case CAL_SABBREVMONTHNAME12:
711 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME12, lpCalData, cchData);
712 case CAL_SABBREVMONTHNAME13:
713 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME13, lpCalData, cchData);
714 case CAL_SYEARMONTH:
715 return GetLocaleInfoW(Locale, LOCALE_SYEARMONTH, lpCalData, cchData);
716 case CAL_ITWODIGITYEARMAX:
717 if (lpValue) *lpValue = CALINFO_MAX_YEAR;
718 break;
719 default: MESSAGE("Unknown caltype %ld\n",CalType & 0xffff);
720 return E_FAIL;
722 return 0;
725 /*********************************************************************
726 * SetCalendarInfoA (KERNEL32.@)
729 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
731 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
732 Locale, Calendar, CalType, debugstr_a(lpCalData));
733 return 0;
736 /*********************************************************************
737 * SetCalendarInfoW (KERNEL32.@)
739 * See SetCalendarInfoA.
741 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
743 FIXME("(%08lx,%08lx,%08lx,%s): stub\n",
744 Locale, Calendar, CalType, debugstr_w(lpCalData));
745 return 0;
748 /*********************************************************************
749 * LocalFileTimeToFileTime (KERNEL32.@)
751 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
753 NTSTATUS status;
754 LARGE_INTEGER local, utc;
756 local.u.LowPart = localft->dwLowDateTime;
757 local.u.HighPart = localft->dwHighDateTime;
758 if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
760 utcft->dwLowDateTime = utc.u.LowPart;
761 utcft->dwHighDateTime = utc.u.HighPart;
763 else SetLastError( RtlNtStatusToDosError(status) );
765 return !status;
768 /*********************************************************************
769 * FileTimeToLocalFileTime (KERNEL32.@)
771 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
773 NTSTATUS status;
774 LARGE_INTEGER local, utc;
776 utc.u.LowPart = utcft->dwLowDateTime;
777 utc.u.HighPart = utcft->dwHighDateTime;
778 if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
780 localft->dwLowDateTime = local.u.LowPart;
781 localft->dwHighDateTime = local.u.HighPart;
783 else SetLastError( RtlNtStatusToDosError(status) );
785 return !status;
788 /*********************************************************************
789 * FileTimeToSystemTime (KERNEL32.@)
791 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
793 TIME_FIELDS tf;
794 LARGE_INTEGER t;
796 t.u.LowPart = ft->dwLowDateTime;
797 t.u.HighPart = ft->dwHighDateTime;
798 RtlTimeToTimeFields(&t, &tf);
800 syst->wYear = tf.Year;
801 syst->wMonth = tf.Month;
802 syst->wDay = tf.Day;
803 syst->wHour = tf.Hour;
804 syst->wMinute = tf.Minute;
805 syst->wSecond = tf.Second;
806 syst->wMilliseconds = tf.Milliseconds;
807 syst->wDayOfWeek = tf.Weekday;
808 return TRUE;
811 /*********************************************************************
812 * SystemTimeToFileTime (KERNEL32.@)
814 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
816 TIME_FIELDS tf;
817 LARGE_INTEGER t;
819 tf.Year = syst->wYear;
820 tf.Month = syst->wMonth;
821 tf.Day = syst->wDay;
822 tf.Hour = syst->wHour;
823 tf.Minute = syst->wMinute;
824 tf.Second = syst->wSecond;
825 tf.Milliseconds = syst->wMilliseconds;
827 if( !RtlTimeFieldsToTime(&tf, &t)) {
828 SetLastError( ERROR_INVALID_PARAMETER);
829 return FALSE;
831 ft->dwLowDateTime = t.u.LowPart;
832 ft->dwHighDateTime = t.u.HighPart;
833 return TRUE;
836 /*********************************************************************
837 * CompareFileTime (KERNEL32.@)
839 * Compare two FILETIME's to each other.
841 * PARAMS
842 * x [I] First time
843 * y [I] time to compare to x
845 * RETURNS
846 * -1, 0, or 1 indicating that x is less than, equal to, or greater
847 * than y respectively.
849 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
851 if (!x || !y) return -1;
853 if (x->dwHighDateTime > y->dwHighDateTime)
854 return 1;
855 if (x->dwHighDateTime < y->dwHighDateTime)
856 return -1;
857 if (x->dwLowDateTime > y->dwLowDateTime)
858 return 1;
859 if (x->dwLowDateTime < y->dwLowDateTime)
860 return -1;
861 return 0;
864 /*********************************************************************
865 * GetLocalTime (KERNEL32.@)
867 * Get the current local time.
869 * RETURNS
870 * Nothing.
872 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
874 FILETIME lft;
875 LARGE_INTEGER ft, ft2;
877 NtQuerySystemTime(&ft);
878 RtlSystemTimeToLocalTime(&ft, &ft2);
879 lft.dwLowDateTime = ft2.u.LowPart;
880 lft.dwHighDateTime = ft2.u.HighPart;
881 FileTimeToSystemTime(&lft, systime);
884 /*********************************************************************
885 * GetSystemTime (KERNEL32.@)
887 * Get the current system time.
889 * RETURNS
890 * Nothing.
892 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime) /* [O] Destination for current time */
894 FILETIME ft;
895 LARGE_INTEGER t;
897 NtQuerySystemTime(&t);
898 ft.dwLowDateTime = t.u.LowPart;
899 ft.dwHighDateTime = t.u.HighPart;
900 FileTimeToSystemTime(&ft, systime);
903 /*********************************************************************
904 * GetDaylightFlag (KERNEL32.@)
906 * returns TRUE if daylight saving time is in operation
908 * Note: this function is called from the Win98's control applet
909 * timedate.cpl
911 BOOL WINAPI GetDaylightFlag(void)
913 TIME_ZONE_INFORMATION tzinfo;
914 return GetTimeZoneInformation( &tzinfo) == TIME_ZONE_ID_DAYLIGHT;
917 /***********************************************************************
918 * DosDateTimeToFileTime (KERNEL32.@)
920 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
922 struct tm newtm;
923 #ifndef HAVE_TIMEGM
924 struct tm *gtm;
925 time_t time1, time2;
926 #endif
928 newtm.tm_sec = (fattime & 0x1f) * 2;
929 newtm.tm_min = (fattime >> 5) & 0x3f;
930 newtm.tm_hour = (fattime >> 11);
931 newtm.tm_mday = (fatdate & 0x1f);
932 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
933 newtm.tm_year = (fatdate >> 9) + 80;
934 #ifdef HAVE_TIMEGM
935 RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
936 #else
937 time1 = mktime(&newtm);
938 gtm = gmtime(&time1);
939 time2 = mktime(gtm);
940 RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
941 #endif
942 return TRUE;
946 /***********************************************************************
947 * FileTimeToDosDateTime (KERNEL32.@)
949 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
950 LPWORD fattime )
952 LARGE_INTEGER li;
953 ULONG t;
954 time_t unixtime;
955 struct tm* tm;
957 li.u.LowPart = ft->dwLowDateTime;
958 li.u.HighPart = ft->dwHighDateTime;
959 RtlTimeToSecondsSince1970( &li, &t );
960 unixtime = t;
961 tm = gmtime( &unixtime );
962 if (fattime)
963 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
964 if (fatdate)
965 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
966 + tm->tm_mday;
967 return TRUE;