oleview: Fix compilation with PSDK.
[wine/multimedia.git] / dlls / kernel32 / time.c
blobac39fdec121e687363032b710d5b3f7302ed9eb9
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include "ntstatus.h"
43 #define WIN32_NO_STATUS
44 #define NONAMELESSUNION
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winternl.h"
48 #include "kernel_private.h"
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(time);
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 BOOL IsLeapYear(int Year)
71 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
74 /***********************************************************************
75 * TIME_DayLightCompareDate
77 * Compares two dates without looking at the year.
79 * PARAMS
80 * date [in] The local time to compare.
81 * compareDate [in] The daylight savings begin or end date.
83 * RETURNS
85 * -1 if date < compareDate
86 * 0 if date == compareDate
87 * 1 if date > compareDate
88 * -2 if an error occurs
90 static int TIME_DayLightCompareDate( const SYSTEMTIME *date,
91 const SYSTEMTIME *compareDate )
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 year is 0 then date is in day-of-week format, otherwise
102 * it's absolute date.
104 if (compareDate->wYear == 0)
106 WORD First;
107 /* compareDate->wDay is interpreted as number of the week in the month
108 * 5 means: the last week in the month */
109 int weekofmonth = compareDate->wDay;
110 /* calculate the day of the first DayOfWeek in the month */
111 First = ( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay
112 ) % 7 + 1;
113 limit_day = First + 7 * (weekofmonth - 1);
114 /* check needed for the 5th weekday of the month */
115 if(limit_day > MonthLengths[date->wMonth==2 && IsLeapYear(date->wYear)]
116 [date->wMonth - 1])
117 limit_day -= 7;
119 else
121 limit_day = compareDate->wDay;
124 /* convert to seconds */
125 limit_day = ((limit_day * 24 + compareDate->wHour) * 60 +
126 compareDate->wMinute ) * 60;
127 dayinsecs = ((date->wDay * 24 + date->wHour) * 60 +
128 date->wMinute ) * 60 + date->wSecond;
129 /* and compare */
130 return dayinsecs < limit_day ? -1 :
131 dayinsecs > limit_day ? 1 :
132 0; /* date is equal to the date limit. */
135 /***********************************************************************
136 * TIME_CompTimeZoneID
138 * Computes the local time bias for a given time and time zone.
140 * PARAMS
141 * pTZinfo [in] The time zone data.
142 * lpFileTime [in] The system or local time.
143 * islocal [in] it is local time.
145 * RETURNS
146 * TIME_ZONE_ID_INVALID An error occurred
147 * TIME_ZONE_ID_UNKNOWN There are no transition time known
148 * TIME_ZONE_ID_STANDARD Current time is standard time
149 * TIME_ZONE_ID_DAYLIGHT Current time is daylight savings time
151 static DWORD TIME_CompTimeZoneID ( const TIME_ZONE_INFORMATION *pTZinfo,
152 FILETIME *lpFileTime, BOOL islocal )
154 int ret, year;
155 BOOL beforeStandardDate, afterDaylightDate;
156 DWORD retval = TIME_ZONE_ID_INVALID;
157 LONGLONG llTime = 0; /* initialized to prevent gcc complaining */
158 SYSTEMTIME SysTime;
159 FILETIME ftTemp;
161 if (pTZinfo->DaylightDate.wMonth != 0)
163 /* if year is 0 then date is in day-of-week format, otherwise
164 * it's absolute date.
166 if (pTZinfo->StandardDate.wMonth == 0 ||
167 (pTZinfo->StandardDate.wYear == 0 &&
168 (pTZinfo->StandardDate.wDay<1 ||
169 pTZinfo->StandardDate.wDay>5 ||
170 pTZinfo->DaylightDate.wDay<1 ||
171 pTZinfo->DaylightDate.wDay>5)))
173 SetLastError(ERROR_INVALID_PARAMETER);
174 return TIME_ZONE_ID_INVALID;
177 if (!islocal) {
178 FILETIME2LL( lpFileTime, llTime );
179 llTime -= pTZinfo->Bias * (LONGLONG)600000000;
180 LL2FILETIME( llTime, &ftTemp)
181 lpFileTime = &ftTemp;
184 FileTimeToSystemTime(lpFileTime, &SysTime);
185 year = SysTime.wYear;
187 if (!islocal) {
188 llTime -= pTZinfo->DaylightBias * (LONGLONG)600000000;
189 LL2FILETIME( llTime, &ftTemp)
190 FileTimeToSystemTime(lpFileTime, &SysTime);
193 /* check for daylight savings */
194 if(year == SysTime.wYear) {
195 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->StandardDate);
196 if (ret == -2)
197 return TIME_ZONE_ID_INVALID;
199 beforeStandardDate = ret < 0;
200 } else
201 beforeStandardDate = SysTime.wYear < year;
203 if (!islocal) {
204 llTime -= ( pTZinfo->StandardBias - pTZinfo->DaylightBias )
205 * (LONGLONG)600000000;
206 LL2FILETIME( llTime, &ftTemp)
207 FileTimeToSystemTime(lpFileTime, &SysTime);
210 if(year == SysTime.wYear) {
211 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->DaylightDate);
212 if (ret == -2)
213 return TIME_ZONE_ID_INVALID;
215 afterDaylightDate = ret >= 0;
216 } else
217 afterDaylightDate = SysTime.wYear > year;
219 retval = TIME_ZONE_ID_STANDARD;
220 if( pTZinfo->DaylightDate.wMonth < pTZinfo->StandardDate.wMonth ) {
221 /* Northern hemisphere */
222 if( beforeStandardDate && afterDaylightDate )
223 retval = TIME_ZONE_ID_DAYLIGHT;
224 } else /* Down south */
225 if( beforeStandardDate || afterDaylightDate )
226 retval = TIME_ZONE_ID_DAYLIGHT;
227 } else
228 /* No transition date */
229 retval = TIME_ZONE_ID_UNKNOWN;
231 return retval;
234 /***********************************************************************
235 * TIME_TimeZoneID
237 * Calculates whether daylight savings is on now.
239 * PARAMS
240 * pTzi [in] Timezone info.
242 * RETURNS
243 * TIME_ZONE_ID_INVALID An error occurred
244 * TIME_ZONE_ID_UNKNOWN There are no transition time known
245 * TIME_ZONE_ID_STANDARD Current time is standard time
246 * TIME_ZONE_ID_DAYLIGHT Current time is daylight savings time
248 static DWORD TIME_ZoneID( const TIME_ZONE_INFORMATION *pTzi )
250 FILETIME ftTime;
251 GetSystemTimeAsFileTime( &ftTime);
252 return TIME_CompTimeZoneID( pTzi, &ftTime, FALSE);
255 /***********************************************************************
256 * TIME_GetTimezoneBias
258 * Calculates the local time bias for a given time zone.
260 * PARAMS
261 * pTZinfo [in] The time zone data.
262 * lpFileTime [in] The system or local time.
263 * islocal [in] It is local time.
264 * pBias [out] The calculated bias in minutes.
266 * RETURNS
267 * TRUE when the time zone bias was calculated.
269 static BOOL TIME_GetTimezoneBias( const TIME_ZONE_INFORMATION *pTZinfo,
270 FILETIME *lpFileTime, BOOL islocal, LONG *pBias )
272 LONG bias = pTZinfo->Bias;
273 DWORD tzid = TIME_CompTimeZoneID( pTZinfo, lpFileTime, islocal);
275 if( tzid == TIME_ZONE_ID_INVALID)
276 return FALSE;
277 if (tzid == TIME_ZONE_ID_DAYLIGHT)
278 bias += pTZinfo->DaylightBias;
279 else if (tzid == TIME_ZONE_ID_STANDARD)
280 bias += pTZinfo->StandardBias;
281 *pBias = bias;
282 return TRUE;
286 /***********************************************************************
287 * SetLocalTime (KERNEL32.@)
289 * Set the local time using current time zone and daylight
290 * savings settings.
292 * PARAMS
293 * systime [in] The desired local time.
295 * RETURNS
296 * Success: TRUE. The time was set.
297 * Failure: FALSE, if the time was invalid or caller does not have
298 * permission to change the time.
300 BOOL WINAPI SetLocalTime( const SYSTEMTIME *systime )
302 FILETIME ft;
303 LARGE_INTEGER st, st2;
304 NTSTATUS status;
306 if( !SystemTimeToFileTime( systime, &ft ))
307 return FALSE;
308 st.u.LowPart = ft.dwLowDateTime;
309 st.u.HighPart = ft.dwHighDateTime;
310 RtlLocalTimeToSystemTime( &st, &st2 );
312 if ((status = NtSetSystemTime(&st2, NULL)))
313 SetLastError( RtlNtStatusToDosError(status) );
314 return !status;
318 /***********************************************************************
319 * GetSystemTimeAdjustment (KERNEL32.@)
321 * Get the period between clock interrupts and the amount the clock
322 * is adjusted each interrupt so as to keep it in sync with an external source.
324 * PARAMS
325 * lpTimeAdjustment [out] The clock adjustment per interrupt in 100's of nanoseconds.
326 * lpTimeIncrement [out] The time between clock interrupts in 100's of nanoseconds.
327 * lpTimeAdjustmentDisabled [out] The clock synchronisation has been disabled.
329 * RETURNS
330 * TRUE.
332 * BUGS
333 * Only the special case of disabled time adjustments is supported.
335 BOOL WINAPI GetSystemTimeAdjustment( PDWORD lpTimeAdjustment, PDWORD lpTimeIncrement,
336 PBOOL lpTimeAdjustmentDisabled )
338 *lpTimeAdjustment = 0;
339 *lpTimeIncrement = 10000000 / sysconf(_SC_CLK_TCK);
340 *lpTimeAdjustmentDisabled = TRUE;
341 return TRUE;
345 /***********************************************************************
346 * SetSystemTime (KERNEL32.@)
348 * Set the system time in utc.
350 * PARAMS
351 * systime [in] The desired system time.
353 * RETURNS
354 * Success: TRUE. The time was set.
355 * Failure: FALSE, if the time was invalid or caller does not have
356 * permission to change the time.
358 BOOL WINAPI SetSystemTime( const SYSTEMTIME *systime )
360 FILETIME ft;
361 LARGE_INTEGER t;
362 NTSTATUS status;
364 if( !SystemTimeToFileTime( systime, &ft ))
365 return FALSE;
366 t.u.LowPart = ft.dwLowDateTime;
367 t.u.HighPart = ft.dwHighDateTime;
368 if ((status = NtSetSystemTime(&t, NULL)))
369 SetLastError( RtlNtStatusToDosError(status) );
370 return !status;
373 /***********************************************************************
374 * SetSystemTimeAdjustment (KERNEL32.@)
376 * Enables or disables the timing adjustments to the system's clock.
378 * PARAMS
379 * dwTimeAdjustment [in] Number of units to add per clock interrupt.
380 * bTimeAdjustmentDisabled [in] Adjustment mode.
382 * RETURNS
383 * Success: TRUE.
384 * Failure: FALSE.
386 BOOL WINAPI SetSystemTimeAdjustment( DWORD dwTimeAdjustment, BOOL bTimeAdjustmentDisabled )
388 /* Fake function for now... */
389 FIXME("(%08x,%d): stub !\n", dwTimeAdjustment, bTimeAdjustmentDisabled);
390 return TRUE;
393 /***********************************************************************
394 * GetTimeZoneInformation (KERNEL32.@)
396 * Get information about the current local time zone.
398 * PARAMS
399 * tzinfo [out] Destination for time zone information.
401 * RETURNS
402 * TIME_ZONE_ID_INVALID An error occurred
403 * TIME_ZONE_ID_UNKNOWN There are no transition time known
404 * TIME_ZONE_ID_STANDARD Current time is standard time
405 * TIME_ZONE_ID_DAYLIGHT Current time is daylight savings time
407 DWORD WINAPI GetTimeZoneInformation( LPTIME_ZONE_INFORMATION tzinfo )
409 NTSTATUS status;
411 status = RtlQueryTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION*)tzinfo );
412 if ( status != STATUS_SUCCESS )
414 SetLastError( RtlNtStatusToDosError(status) );
415 return TIME_ZONE_ID_INVALID;
417 return TIME_ZoneID( tzinfo );
420 /***********************************************************************
421 * SetTimeZoneInformation (KERNEL32.@)
423 * Change the settings of the current local time zone.
425 * PARAMS
426 * tzinfo [in] The new time zone.
428 * RETURNS
429 * Success: TRUE. The time zone was updated with the settings from tzinfo.
430 * Failure: FALSE.
432 BOOL WINAPI SetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
434 NTSTATUS status;
435 status = RtlSetTimeZoneInformation( (const RTL_TIME_ZONE_INFORMATION *)tzinfo );
436 if ( status != STATUS_SUCCESS )
437 SetLastError( RtlNtStatusToDosError(status) );
438 return !status;
441 /***********************************************************************
442 * SystemTimeToTzSpecificLocalTime (KERNEL32.@)
444 * Convert a utc system time to a local time in a given time zone.
446 * PARAMS
447 * lpTimeZoneInformation [in] The desired time zone.
448 * lpUniversalTime [in] The utc time to base local time on.
449 * lpLocalTime [out] The local time in the time zone.
451 * RETURNS
452 * Success: TRUE. lpLocalTime contains the converted time
453 * Failure: FALSE.
456 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
457 const TIME_ZONE_INFORMATION *lpTimeZoneInformation,
458 const SYSTEMTIME *lpUniversalTime, LPSYSTEMTIME lpLocalTime )
460 FILETIME ft;
461 LONG lBias;
462 LONGLONG llTime;
463 TIME_ZONE_INFORMATION tzinfo;
465 if (lpTimeZoneInformation != NULL)
467 tzinfo = *lpTimeZoneInformation;
469 else
471 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
472 return FALSE;
475 if (!SystemTimeToFileTime(lpUniversalTime, &ft))
476 return FALSE;
477 FILETIME2LL( &ft, llTime)
478 if (!TIME_GetTimezoneBias(&tzinfo, &ft, FALSE, &lBias))
479 return FALSE;
480 /* convert minutes to 100-nanoseconds-ticks */
481 llTime -= (LONGLONG)lBias * 600000000;
482 LL2FILETIME( llTime, &ft)
484 return FileTimeToSystemTime(&ft, lpLocalTime);
488 /***********************************************************************
489 * TzSpecificLocalTimeToSystemTime (KERNEL32.@)
491 * Converts a local time to a time in utc.
493 * PARAMS
494 * lpTimeZoneInformation [in] The desired time zone.
495 * lpLocalTime [in] The local time.
496 * lpUniversalTime [out] The calculated utc time.
498 * RETURNS
499 * Success: TRUE. lpUniversalTime contains the converted time.
500 * Failure: FALSE.
502 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
503 const TIME_ZONE_INFORMATION *lpTimeZoneInformation,
504 const SYSTEMTIME *lpLocalTime, LPSYSTEMTIME lpUniversalTime)
506 FILETIME ft;
507 LONG lBias;
508 LONGLONG t;
509 TIME_ZONE_INFORMATION tzinfo;
511 if (lpTimeZoneInformation != NULL)
513 tzinfo = *lpTimeZoneInformation;
515 else
517 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
518 return FALSE;
521 if (!SystemTimeToFileTime(lpLocalTime, &ft))
522 return FALSE;
523 FILETIME2LL( &ft, t)
524 if (!TIME_GetTimezoneBias(&tzinfo, &ft, TRUE, &lBias))
525 return FALSE;
526 /* convert minutes to 100-nanoseconds-ticks */
527 t += (LONGLONG)lBias * 600000000;
528 LL2FILETIME( t, &ft)
529 return FileTimeToSystemTime(&ft, lpUniversalTime);
533 /***********************************************************************
534 * GetSystemTimeAsFileTime (KERNEL32.@)
536 * Get the current time in utc format.
538 * RETURNS
539 * Nothing.
541 VOID WINAPI GetSystemTimeAsFileTime(
542 LPFILETIME time) /* [out] Destination for the current utc time */
544 LARGE_INTEGER t;
545 NtQuerySystemTime( &t );
546 time->dwLowDateTime = t.u.LowPart;
547 time->dwHighDateTime = t.u.HighPart;
551 /***********************************************************************
552 * GetSystemTimePreciseAsFileTime (KERNEL32.@)
554 * Get the current time in utc format, with <1 us precision.
556 * RETURNS
557 * Nothing.
559 VOID WINAPI GetSystemTimePreciseAsFileTime(
560 LPFILETIME time) /* [out] Destination for the current utc time */
562 GetSystemTimeAsFileTime(time);
566 /*********************************************************************
567 * TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
569 * Used by GetProcessTimes to convert clock_t into FILETIME.
571 * Differences to UnixTimeToFileTime:
572 * 1) Divided by CLK_TCK
573 * 2) Time is relative. There is no 'starting date', so there is
574 * no need for offset correction, like in UnixTimeToFileTime
576 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
578 long clocksPerSec = sysconf(_SC_CLK_TCK);
579 ULONGLONG secs = (ULONGLONG)unix_time * 10000000 / clocksPerSec;
580 filetime->dwLowDateTime = (DWORD)secs;
581 filetime->dwHighDateTime = (DWORD)(secs >> 32);
584 /*********************************************************************
585 * GetProcessTimes (KERNEL32.@)
587 * Get the user and kernel execution times of a process,
588 * along with the creation and exit times if known.
590 * PARAMS
591 * hprocess [in] The process to be queried.
592 * lpCreationTime [out] The creation time of the process.
593 * lpExitTime [out] The exit time of the process if exited.
594 * lpKernelTime [out] The time spent in kernel routines in 100's of nanoseconds.
595 * lpUserTime [out] The time spent in user routines in 100's of nanoseconds.
597 * RETURNS
598 * TRUE.
600 * NOTES
601 * olorin@fandra.org:
602 * Would be nice to subtract the cpu time used by Wine at startup.
603 * Also, there is a need to separate times used by different applications.
605 * BUGS
606 * KernelTime and UserTime are always for the current process
608 BOOL WINAPI GetProcessTimes( HANDLE hprocess, LPFILETIME lpCreationTime,
609 LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime )
611 struct tms tms;
612 KERNEL_USER_TIMES pti;
614 times(&tms);
615 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
616 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
617 if (NtQueryInformationProcess( hprocess, ProcessTimes, &pti, sizeof(pti), NULL))
618 return FALSE;
619 LL2FILETIME( pti.CreateTime.QuadPart, lpCreationTime);
620 LL2FILETIME( pti.ExitTime.QuadPart, lpExitTime);
621 return TRUE;
624 /*********************************************************************
625 * GetCalendarInfoA (KERNEL32.@)
628 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
629 LPSTR lpCalData, int cchData, LPDWORD lpValue)
631 int ret, cchDataW = cchData;
632 LPWSTR lpCalDataW = NULL;
634 if (NLS_IsUnicodeOnlyLcid(lcid))
636 SetLastError(ERROR_INVALID_PARAMETER);
637 return 0;
640 if (!cchData && !(CalType & CAL_RETURN_NUMBER))
641 cchDataW = GetCalendarInfoW(lcid, Calendar, CalType, NULL, 0, NULL);
642 if (!(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchDataW*sizeof(WCHAR))))
643 return 0;
645 ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchDataW, lpValue);
646 if(ret && lpCalDataW && lpCalData)
647 ret = WideCharToMultiByte(CP_ACP, 0, lpCalDataW, -1, lpCalData, cchData, NULL, NULL);
648 else if (CalType & CAL_RETURN_NUMBER)
649 ret *= sizeof(WCHAR);
650 HeapFree(GetProcessHeap(), 0, lpCalDataW);
652 return ret;
655 /*********************************************************************
656 * GetCalendarInfoW (KERNEL32.@)
659 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
660 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
662 if (CalType & CAL_NOUSEROVERRIDE)
663 FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
664 if (CalType & CAL_USE_CP_ACP)
665 FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
667 if (CalType & CAL_RETURN_NUMBER) {
668 if (!lpValue)
670 SetLastError( ERROR_INVALID_PARAMETER );
671 return 0;
673 if (lpCalData != NULL)
674 WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
675 if (cchData != 0)
676 WARN("cchData not 0 (%d) when it should!\n", cchData);
677 } else {
678 if (lpValue != NULL)
679 WARN("lpValue not NULL (%p) when it should!\n", lpValue);
682 /* FIXME: No verification is made yet wrt Locale
683 * for the CALTYPES not requiring GetLocaleInfoA */
684 switch (CalType & ~(CAL_NOUSEROVERRIDE|CAL_RETURN_NUMBER|CAL_USE_CP_ACP)) {
685 case CAL_ICALINTVALUE:
686 if (CalType & CAL_RETURN_NUMBER)
687 return GetLocaleInfoW(Locale, LOCALE_RETURN_NUMBER | LOCALE_ICALENDARTYPE,
688 (LPWSTR)lpValue, 2);
689 return GetLocaleInfoW(Locale, LOCALE_ICALENDARTYPE, lpCalData, cchData);
690 case CAL_SCALNAME:
691 FIXME("Unimplemented caltype %d\n", CalType & 0xffff);
692 if (lpCalData) *lpCalData = 0;
693 return 1;
694 case CAL_IYEAROFFSETRANGE:
695 FIXME("Unimplemented caltype %d\n", CalType & 0xffff);
696 return 0;
697 case CAL_SERASTRING:
698 FIXME("Unimplemented caltype %d\n", CalType & 0xffff);
699 return 0;
700 case CAL_SSHORTDATE:
701 return GetLocaleInfoW(Locale, LOCALE_SSHORTDATE, lpCalData, cchData);
702 case CAL_SLONGDATE:
703 return GetLocaleInfoW(Locale, LOCALE_SLONGDATE, lpCalData, cchData);
704 case CAL_SDAYNAME1:
705 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME1, lpCalData, cchData);
706 case CAL_SDAYNAME2:
707 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME2, lpCalData, cchData);
708 case CAL_SDAYNAME3:
709 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME3, lpCalData, cchData);
710 case CAL_SDAYNAME4:
711 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME4, lpCalData, cchData);
712 case CAL_SDAYNAME5:
713 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME5, lpCalData, cchData);
714 case CAL_SDAYNAME6:
715 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME6, lpCalData, cchData);
716 case CAL_SDAYNAME7:
717 return GetLocaleInfoW(Locale, LOCALE_SDAYNAME7, lpCalData, cchData);
718 case CAL_SABBREVDAYNAME1:
719 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME1, lpCalData, cchData);
720 case CAL_SABBREVDAYNAME2:
721 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME2, lpCalData, cchData);
722 case CAL_SABBREVDAYNAME3:
723 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME3, lpCalData, cchData);
724 case CAL_SABBREVDAYNAME4:
725 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME4, lpCalData, cchData);
726 case CAL_SABBREVDAYNAME5:
727 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME5, lpCalData, cchData);
728 case CAL_SABBREVDAYNAME6:
729 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME6, lpCalData, cchData);
730 case CAL_SABBREVDAYNAME7:
731 return GetLocaleInfoW(Locale, LOCALE_SABBREVDAYNAME7, lpCalData, cchData);
732 case CAL_SMONTHNAME1:
733 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME1, lpCalData, cchData);
734 case CAL_SMONTHNAME2:
735 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME2, lpCalData, cchData);
736 case CAL_SMONTHNAME3:
737 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME3, lpCalData, cchData);
738 case CAL_SMONTHNAME4:
739 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME4, lpCalData, cchData);
740 case CAL_SMONTHNAME5:
741 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME5, lpCalData, cchData);
742 case CAL_SMONTHNAME6:
743 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME6, lpCalData, cchData);
744 case CAL_SMONTHNAME7:
745 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME7, lpCalData, cchData);
746 case CAL_SMONTHNAME8:
747 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME8, lpCalData, cchData);
748 case CAL_SMONTHNAME9:
749 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME9, lpCalData, cchData);
750 case CAL_SMONTHNAME10:
751 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME10, lpCalData, cchData);
752 case CAL_SMONTHNAME11:
753 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME11, lpCalData, cchData);
754 case CAL_SMONTHNAME12:
755 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME12, lpCalData, cchData);
756 case CAL_SMONTHNAME13:
757 return GetLocaleInfoW(Locale, LOCALE_SMONTHNAME13, lpCalData, cchData);
758 case CAL_SABBREVMONTHNAME1:
759 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME1, lpCalData, cchData);
760 case CAL_SABBREVMONTHNAME2:
761 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME2, lpCalData, cchData);
762 case CAL_SABBREVMONTHNAME3:
763 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME3, lpCalData, cchData);
764 case CAL_SABBREVMONTHNAME4:
765 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME4, lpCalData, cchData);
766 case CAL_SABBREVMONTHNAME5:
767 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME5, lpCalData, cchData);
768 case CAL_SABBREVMONTHNAME6:
769 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME6, lpCalData, cchData);
770 case CAL_SABBREVMONTHNAME7:
771 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME7, lpCalData, cchData);
772 case CAL_SABBREVMONTHNAME8:
773 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME8, lpCalData, cchData);
774 case CAL_SABBREVMONTHNAME9:
775 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME9, lpCalData, cchData);
776 case CAL_SABBREVMONTHNAME10:
777 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME10, lpCalData, cchData);
778 case CAL_SABBREVMONTHNAME11:
779 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME11, lpCalData, cchData);
780 case CAL_SABBREVMONTHNAME12:
781 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME12, lpCalData, cchData);
782 case CAL_SABBREVMONTHNAME13:
783 return GetLocaleInfoW(Locale, LOCALE_SABBREVMONTHNAME13, lpCalData, cchData);
784 case CAL_SYEARMONTH:
785 return GetLocaleInfoW(Locale, LOCALE_SYEARMONTH, lpCalData, cchData);
786 case CAL_ITWODIGITYEARMAX:
787 if (CalType & CAL_RETURN_NUMBER)
789 *lpValue = CALINFO_MAX_YEAR;
790 return sizeof(DWORD) / sizeof(WCHAR);
792 else
794 static const WCHAR fmtW[] = {'%','u',0};
795 WCHAR buffer[10];
796 int ret = snprintfW( buffer, 10, fmtW, CALINFO_MAX_YEAR ) + 1;
797 if (!lpCalData) return ret;
798 if (ret <= cchData)
800 strcpyW( lpCalData, buffer );
801 return ret;
803 SetLastError( ERROR_INSUFFICIENT_BUFFER );
804 return 0;
806 break;
807 default:
808 FIXME("Unknown caltype %d\n",CalType & 0xffff);
809 SetLastError(ERROR_INVALID_FLAGS);
810 return 0;
812 return 0;
815 /*********************************************************************
816 * GetCalendarInfoEx (KERNEL32.@)
818 int WINAPI GetCalendarInfoEx(LPCWSTR locale, CALID calendar, LPCWSTR lpReserved, CALTYPE caltype,
819 LPWSTR data, int len, DWORD *value)
821 LCID lcid = LocaleNameToLCID(locale, 0);
822 FIXME("(%s, %d, %p, 0x%08x, %p, %d, %p): semi-stub\n", debugstr_w(locale), calendar, lpReserved, caltype,
823 data, len, value);
824 return GetCalendarInfoW(lcid, calendar, caltype, data, len, value);
827 /*********************************************************************
828 * SetCalendarInfoA (KERNEL32.@)
831 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
833 FIXME("(%08x,%08x,%08x,%s): stub\n",
834 Locale, Calendar, CalType, debugstr_a(lpCalData));
835 return 0;
838 /*********************************************************************
839 * SetCalendarInfoW (KERNEL32.@)
843 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
845 FIXME("(%08x,%08x,%08x,%s): stub\n",
846 Locale, Calendar, CalType, debugstr_w(lpCalData));
847 return 0;
850 /*********************************************************************
851 * LocalFileTimeToFileTime (KERNEL32.@)
853 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
855 NTSTATUS status;
856 LARGE_INTEGER local, utc;
858 local.u.LowPart = localft->dwLowDateTime;
859 local.u.HighPart = localft->dwHighDateTime;
860 if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
862 utcft->dwLowDateTime = utc.u.LowPart;
863 utcft->dwHighDateTime = utc.u.HighPart;
865 else SetLastError( RtlNtStatusToDosError(status) );
867 return !status;
870 /*********************************************************************
871 * FileTimeToLocalFileTime (KERNEL32.@)
873 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
875 NTSTATUS status;
876 LARGE_INTEGER local, utc;
878 utc.u.LowPart = utcft->dwLowDateTime;
879 utc.u.HighPart = utcft->dwHighDateTime;
880 if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
882 localft->dwLowDateTime = local.u.LowPart;
883 localft->dwHighDateTime = local.u.HighPart;
885 else SetLastError( RtlNtStatusToDosError(status) );
887 return !status;
890 /*********************************************************************
891 * FileTimeToSystemTime (KERNEL32.@)
893 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
895 TIME_FIELDS tf;
896 LARGE_INTEGER t;
898 t.u.LowPart = ft->dwLowDateTime;
899 t.u.HighPart = ft->dwHighDateTime;
900 RtlTimeToTimeFields(&t, &tf);
902 syst->wYear = tf.Year;
903 syst->wMonth = tf.Month;
904 syst->wDay = tf.Day;
905 syst->wHour = tf.Hour;
906 syst->wMinute = tf.Minute;
907 syst->wSecond = tf.Second;
908 syst->wMilliseconds = tf.Milliseconds;
909 syst->wDayOfWeek = tf.Weekday;
910 return TRUE;
913 /*********************************************************************
914 * SystemTimeToFileTime (KERNEL32.@)
916 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
918 TIME_FIELDS tf;
919 LARGE_INTEGER t;
921 tf.Year = syst->wYear;
922 tf.Month = syst->wMonth;
923 tf.Day = syst->wDay;
924 tf.Hour = syst->wHour;
925 tf.Minute = syst->wMinute;
926 tf.Second = syst->wSecond;
927 tf.Milliseconds = syst->wMilliseconds;
929 if( !RtlTimeFieldsToTime(&tf, &t)) {
930 SetLastError( ERROR_INVALID_PARAMETER);
931 return FALSE;
933 ft->dwLowDateTime = t.u.LowPart;
934 ft->dwHighDateTime = t.u.HighPart;
935 return TRUE;
938 /*********************************************************************
939 * CompareFileTime (KERNEL32.@)
941 * Compare two FILETIME's to each other.
943 * PARAMS
944 * x [I] First time
945 * y [I] time to compare to x
947 * RETURNS
948 * -1, 0, or 1 indicating that x is less than, equal to, or greater
949 * than y respectively.
951 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
953 if (!x || !y) return -1;
955 if (x->dwHighDateTime > y->dwHighDateTime)
956 return 1;
957 if (x->dwHighDateTime < y->dwHighDateTime)
958 return -1;
959 if (x->dwLowDateTime > y->dwLowDateTime)
960 return 1;
961 if (x->dwLowDateTime < y->dwLowDateTime)
962 return -1;
963 return 0;
966 /*********************************************************************
967 * GetLocalTime (KERNEL32.@)
969 * Get the current local time.
971 * PARAMS
972 * systime [O] Destination for current time.
974 * RETURNS
975 * Nothing.
977 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime)
979 FILETIME lft;
980 LARGE_INTEGER ft, ft2;
982 NtQuerySystemTime(&ft);
983 RtlSystemTimeToLocalTime(&ft, &ft2);
984 lft.dwLowDateTime = ft2.u.LowPart;
985 lft.dwHighDateTime = ft2.u.HighPart;
986 FileTimeToSystemTime(&lft, systime);
989 /*********************************************************************
990 * GetSystemTime (KERNEL32.@)
992 * Get the current system time.
994 * PARAMS
995 * systime [O] Destination for current time.
997 * RETURNS
998 * Nothing.
1000 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
1002 FILETIME ft;
1003 LARGE_INTEGER t;
1005 NtQuerySystemTime(&t);
1006 ft.dwLowDateTime = t.u.LowPart;
1007 ft.dwHighDateTime = t.u.HighPart;
1008 FileTimeToSystemTime(&ft, systime);
1011 /*********************************************************************
1012 * GetDaylightFlag (KERNEL32.@)
1014 * Specifies if daylight savings time is in operation.
1016 * NOTES
1017 * This function is called from the Win98's control applet timedate.cpl.
1019 * RETURNS
1020 * TRUE if daylight savings time is in operation.
1021 * FALSE otherwise.
1023 BOOL WINAPI GetDaylightFlag(void)
1025 TIME_ZONE_INFORMATION tzinfo;
1026 return GetTimeZoneInformation( &tzinfo) == TIME_ZONE_ID_DAYLIGHT;
1029 /***********************************************************************
1030 * DosDateTimeToFileTime (KERNEL32.@)
1032 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
1034 struct tm newtm;
1035 #ifndef HAVE_TIMEGM
1036 struct tm *gtm;
1037 time_t time1, time2;
1038 #endif
1040 newtm.tm_sec = (fattime & 0x1f) * 2;
1041 newtm.tm_min = (fattime >> 5) & 0x3f;
1042 newtm.tm_hour = (fattime >> 11);
1043 newtm.tm_mday = (fatdate & 0x1f);
1044 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
1045 newtm.tm_year = (fatdate >> 9) + 80;
1046 newtm.tm_isdst = -1;
1047 #ifdef HAVE_TIMEGM
1048 RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
1049 #else
1050 time1 = mktime(&newtm);
1051 gtm = gmtime(&time1);
1052 time2 = mktime(gtm);
1053 RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
1054 #endif
1055 return TRUE;
1059 /***********************************************************************
1060 * FileTimeToDosDateTime (KERNEL32.@)
1062 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
1063 LPWORD fattime )
1065 LARGE_INTEGER li;
1066 ULONG t;
1067 time_t unixtime;
1068 struct tm* tm;
1070 if (!fatdate || !fattime)
1072 SetLastError(ERROR_INVALID_PARAMETER);
1073 return FALSE;
1075 li.u.LowPart = ft->dwLowDateTime;
1076 li.u.HighPart = ft->dwHighDateTime;
1077 if (!RtlTimeToSecondsSince1970( &li, &t ))
1079 SetLastError(ERROR_INVALID_PARAMETER);
1080 return FALSE;
1082 unixtime = t;
1083 tm = gmtime( &unixtime );
1084 if (fattime)
1085 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
1086 if (fatdate)
1087 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5)
1088 + tm->tm_mday;
1089 return TRUE;
1092 /*********************************************************************
1093 * GetSystemTimes (KERNEL32.@)
1095 * Retrieves system timing information
1097 * PARAMS
1098 * lpIdleTime [O] Destination for idle time.
1099 * lpKernelTime [O] Destination for kernel time.
1100 * lpUserTime [O] Destination for user time.
1102 * RETURNS
1103 * TRUE if success, FALSE otherwise.
1105 BOOL WINAPI GetSystemTimes(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime)
1107 LARGE_INTEGER idle_time, kernel_time, user_time;
1108 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi;
1109 SYSTEM_BASIC_INFORMATION sbi;
1110 NTSTATUS status;
1111 ULONG ret_size;
1112 int i;
1114 TRACE("(%p,%p,%p)\n", lpIdleTime, lpKernelTime, lpUserTime);
1116 status = NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), &ret_size );
1117 if (status != STATUS_SUCCESS)
1119 SetLastError( RtlNtStatusToDosError(status) );
1120 return FALSE;
1123 sppi = HeapAlloc( GetProcessHeap(), 0,
1124 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * sbi.NumberOfProcessors);
1125 if (!sppi)
1127 SetLastError( ERROR_OUTOFMEMORY );
1128 return FALSE;
1131 status = NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, sizeof(*sppi) * sbi.NumberOfProcessors,
1132 &ret_size );
1133 if (status != STATUS_SUCCESS)
1135 HeapFree( GetProcessHeap(), 0, sppi );
1136 SetLastError( RtlNtStatusToDosError(status) );
1137 return FALSE;
1140 idle_time.QuadPart = 0;
1141 kernel_time.QuadPart = 0;
1142 user_time.QuadPart = 0;
1143 for (i = 0; i < sbi.NumberOfProcessors; i++)
1145 idle_time.QuadPart += sppi[i].IdleTime.QuadPart;
1146 kernel_time.QuadPart += sppi[i].KernelTime.QuadPart;
1147 user_time.QuadPart += sppi[i].UserTime.QuadPart;
1150 if (lpIdleTime)
1152 lpIdleTime->dwLowDateTime = idle_time.u.LowPart;
1153 lpIdleTime->dwHighDateTime = idle_time.u.HighPart;
1155 if (lpKernelTime)
1157 lpKernelTime->dwLowDateTime = kernel_time.u.LowPart;
1158 lpKernelTime->dwHighDateTime = kernel_time.u.HighPart;
1160 if (lpUserTime)
1162 lpUserTime->dwLowDateTime = user_time.u.LowPart;
1163 lpUserTime->dwHighDateTime = user_time.u.HighPart;
1166 HeapFree( GetProcessHeap(), 0, sppi );
1167 return TRUE;
1170 /***********************************************************************
1171 * GetDynamicTimeZoneInformation (KERNEL32.@)
1173 DWORD WINAPI GetDynamicTimeZoneInformation(DYNAMIC_TIME_ZONE_INFORMATION *tzinfo)
1175 NTSTATUS status;
1177 status = RtlQueryDynamicTimeZoneInformation( (RTL_DYNAMIC_TIME_ZONE_INFORMATION*)tzinfo );
1178 if ( status != STATUS_SUCCESS )
1180 SetLastError( RtlNtStatusToDosError(status) );
1181 return TIME_ZONE_ID_INVALID;
1183 return TIME_ZoneID( (TIME_ZONE_INFORMATION*)tzinfo );
1186 /***********************************************************************
1187 * QueryThreadCycleTime (KERNEL32.@)
1189 BOOL WINAPI QueryThreadCycleTime(HANDLE thread, PULONG64 cycle)
1191 static int once;
1192 if (!once++)
1193 FIXME("(%p,%p): stub!\n", thread, cycle);
1194 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1195 return FALSE;
1198 /***********************************************************************
1199 * QueryUnbiasedInterruptTime (KERNEL32.@)
1201 BOOL WINAPI QueryUnbiasedInterruptTime(ULONGLONG *time)
1203 TRACE("(%p)\n", time);
1204 if (!time) return FALSE;
1205 RtlQueryUnbiasedInterruptTime(time);
1206 return TRUE;