regedit: Update the listview path when renaming a treeview node.
[wine.git] / dlls / kernel32 / time.c
blobfbe58118fd838c36782331fc990b19d894417e57
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;
285 /***********************************************************************
286 * TIME_GetSpecificTimeZoneKey
288 * Opens the registry key for the time zone with the given name.
290 * PARAMS
291 * key_name [in] The time zone name.
292 * result [out] The open registry key handle.
294 * RETURNS
295 * TRUE if successful.
297 static BOOL TIME_GetSpecificTimeZoneKey( const WCHAR *key_name, HANDLE *result )
299 static const WCHAR Time_ZonesW[] = { '\\','R','E','G','I','S','T','R','Y','\\',
300 'M','a','c','h','i','n','e','\\',
301 'S','o','f','t','w','a','r','e','\\',
302 'M','i','c','r','o','s','o','f','t','\\',
303 'W','i','n','d','o','w','s',' ','N','T','\\',
304 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
305 'T','i','m','e',' ','Z','o','n','e','s',0 };
306 HANDLE time_zones_key;
307 OBJECT_ATTRIBUTES attr;
308 UNICODE_STRING nameW;
309 NTSTATUS status;
311 attr.Length = sizeof(attr);
312 attr.RootDirectory = 0;
313 attr.ObjectName = &nameW;
314 attr.Attributes = 0;
315 attr.SecurityDescriptor = NULL;
316 attr.SecurityQualityOfService = NULL;
317 RtlInitUnicodeString( &nameW, Time_ZonesW );
318 status = NtOpenKey( &time_zones_key, KEY_READ, &attr );
319 if (status)
321 WARN("Unable to open the time zones key\n");
322 SetLastError( RtlNtStatusToDosError(status) );
323 return FALSE;
326 attr.RootDirectory = time_zones_key;
327 RtlInitUnicodeString( &nameW, key_name );
328 status = NtOpenKey( result, KEY_READ, &attr );
330 NtClose( time_zones_key );
332 if (status)
334 SetLastError( RtlNtStatusToDosError(status) );
335 return FALSE;
338 return TRUE;
341 static BOOL reg_query_value(HKEY hkey, LPCWSTR name, DWORD type, void *data, DWORD count)
343 UNICODE_STRING nameW;
344 char buf[256];
345 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buf;
346 NTSTATUS status;
348 if (count > sizeof(buf) - sizeof(KEY_VALUE_PARTIAL_INFORMATION))
349 return FALSE;
351 RtlInitUnicodeString(&nameW, name);
353 if ((status = NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation,
354 buf, sizeof(buf), &count)))
356 SetLastError( RtlNtStatusToDosError(status) );
357 return FALSE;
360 if (info->Type != type)
362 SetLastError( ERROR_DATATYPE_MISMATCH );
363 return FALSE;
366 memcpy(data, info->Data, info->DataLength);
367 return TRUE;
370 /***********************************************************************
371 * TIME_GetSpecificTimeZoneInfo
373 * Returns time zone information for the given time zone and year.
375 * PARAMS
376 * key_name [in] The time zone name.
377 * year [in] The year, if Dynamic DST is used.
378 * dynamic [in] Whether to use Dynamic DST.
379 * result [out] The time zone information.
381 * RETURNS
382 * TRUE if successful.
384 static BOOL TIME_GetSpecificTimeZoneInfo( const WCHAR *key_name, WORD year,
385 BOOL dynamic, DYNAMIC_TIME_ZONE_INFORMATION *tzinfo )
387 static const WCHAR Dynamic_DstW[] = { 'D','y','n','a','m','i','c',' ','D','S','T',0 };
388 static const WCHAR fmtW[] = { '%','d',0 };
389 static const WCHAR stdW[] = { 'S','t','d',0 };
390 static const WCHAR dltW[] = { 'D','l','t',0 };
391 static const WCHAR tziW[] = { 'T','Z','I',0 };
392 HANDLE time_zone_key, dynamic_dst_key;
393 OBJECT_ATTRIBUTES attr;
394 UNICODE_STRING nameW;
395 WCHAR yearW[16];
396 BOOL got_reg_data = FALSE;
397 struct tz_reg_data
399 LONG bias;
400 LONG std_bias;
401 LONG dlt_bias;
402 SYSTEMTIME std_date;
403 SYSTEMTIME dlt_date;
404 } tz_data;
406 if (!TIME_GetSpecificTimeZoneKey( key_name, &time_zone_key ))
407 return FALSE;
409 if (!reg_query_value( time_zone_key, stdW, REG_SZ, tzinfo->StandardName, sizeof(tzinfo->StandardName)) ||
410 !reg_query_value( time_zone_key, dltW, REG_SZ, tzinfo->DaylightName, sizeof(tzinfo->DaylightName)))
412 NtClose( time_zone_key );
413 return FALSE;
416 lstrcpyW(tzinfo->TimeZoneKeyName, key_name);
418 if (dynamic)
420 attr.Length = sizeof(attr);
421 attr.RootDirectory = time_zone_key;
422 attr.ObjectName = &nameW;
423 attr.Attributes = 0;
424 attr.SecurityDescriptor = NULL;
425 attr.SecurityQualityOfService = NULL;
426 RtlInitUnicodeString( &nameW, Dynamic_DstW );
427 if (!NtOpenKey( &dynamic_dst_key, KEY_READ, &attr ))
429 sprintfW( yearW, fmtW, year );
430 got_reg_data = reg_query_value( dynamic_dst_key, yearW, REG_BINARY, &tz_data, sizeof(tz_data) );
432 NtClose( dynamic_dst_key );
436 if (!got_reg_data)
438 if (!reg_query_value( time_zone_key, tziW, REG_BINARY, &tz_data, sizeof(tz_data) ))
440 NtClose( time_zone_key );
441 return FALSE;
445 tzinfo->Bias = tz_data.bias;
446 tzinfo->StandardBias = tz_data.std_bias;
447 tzinfo->DaylightBias = tz_data.dlt_bias;
448 tzinfo->StandardDate = tz_data.std_date;
449 tzinfo->DaylightDate = tz_data.dlt_date;
451 tzinfo->DynamicDaylightTimeDisabled = !dynamic;
453 NtClose( time_zone_key );
455 return TRUE;
459 /***********************************************************************
460 * SetLocalTime (KERNEL32.@)
462 * Set the local time using current time zone and daylight
463 * savings settings.
465 * PARAMS
466 * systime [in] The desired local time.
468 * RETURNS
469 * Success: TRUE. The time was set.
470 * Failure: FALSE, if the time was invalid or caller does not have
471 * permission to change the time.
473 BOOL WINAPI SetLocalTime( const SYSTEMTIME *systime )
475 FILETIME ft;
476 LARGE_INTEGER st, st2;
477 NTSTATUS status;
479 if( !SystemTimeToFileTime( systime, &ft ))
480 return FALSE;
481 st.u.LowPart = ft.dwLowDateTime;
482 st.u.HighPart = ft.dwHighDateTime;
483 RtlLocalTimeToSystemTime( &st, &st2 );
485 if ((status = NtSetSystemTime(&st2, NULL)))
486 SetLastError( RtlNtStatusToDosError(status) );
487 return !status;
491 /***********************************************************************
492 * GetSystemTimeAdjustment (KERNEL32.@)
494 * Get the period between clock interrupts and the amount the clock
495 * is adjusted each interrupt so as to keep it in sync with an external source.
497 * PARAMS
498 * lpTimeAdjustment [out] The clock adjustment per interrupt in 100's of nanoseconds.
499 * lpTimeIncrement [out] The time between clock interrupts in 100's of nanoseconds.
500 * lpTimeAdjustmentDisabled [out] The clock synchronisation has been disabled.
502 * RETURNS
503 * TRUE.
505 * BUGS
506 * Only the special case of disabled time adjustments is supported.
508 BOOL WINAPI GetSystemTimeAdjustment( PDWORD lpTimeAdjustment, PDWORD lpTimeIncrement,
509 PBOOL lpTimeAdjustmentDisabled )
511 *lpTimeAdjustment = 0;
512 *lpTimeIncrement = 10000000 / sysconf(_SC_CLK_TCK);
513 *lpTimeAdjustmentDisabled = TRUE;
514 return TRUE;
518 /***********************************************************************
519 * SetSystemTime (KERNEL32.@)
521 * Set the system time in utc.
523 * PARAMS
524 * systime [in] The desired system time.
526 * RETURNS
527 * Success: TRUE. The time was set.
528 * Failure: FALSE, if the time was invalid or caller does not have
529 * permission to change the time.
531 BOOL WINAPI SetSystemTime( const SYSTEMTIME *systime )
533 FILETIME ft;
534 LARGE_INTEGER t;
535 NTSTATUS status;
537 if( !SystemTimeToFileTime( systime, &ft ))
538 return FALSE;
539 t.u.LowPart = ft.dwLowDateTime;
540 t.u.HighPart = ft.dwHighDateTime;
541 if ((status = NtSetSystemTime(&t, NULL)))
542 SetLastError( RtlNtStatusToDosError(status) );
543 return !status;
546 /***********************************************************************
547 * SetSystemTimeAdjustment (KERNEL32.@)
549 * Enables or disables the timing adjustments to the system's clock.
551 * PARAMS
552 * dwTimeAdjustment [in] Number of units to add per clock interrupt.
553 * bTimeAdjustmentDisabled [in] Adjustment mode.
555 * RETURNS
556 * Success: TRUE.
557 * Failure: FALSE.
559 BOOL WINAPI SetSystemTimeAdjustment( DWORD dwTimeAdjustment, BOOL bTimeAdjustmentDisabled )
561 /* Fake function for now... */
562 FIXME("(%08x,%d): stub !\n", dwTimeAdjustment, bTimeAdjustmentDisabled);
563 return TRUE;
566 /***********************************************************************
567 * GetTimeZoneInformation (KERNEL32.@)
569 * Get information about the current local time zone.
571 * PARAMS
572 * tzinfo [out] Destination for time zone information.
574 * RETURNS
575 * TIME_ZONE_ID_INVALID An error occurred
576 * TIME_ZONE_ID_UNKNOWN There are no transition time known
577 * TIME_ZONE_ID_STANDARD Current time is standard time
578 * TIME_ZONE_ID_DAYLIGHT Current time is daylight savings time
580 DWORD WINAPI GetTimeZoneInformation( LPTIME_ZONE_INFORMATION tzinfo )
582 NTSTATUS status;
584 status = RtlQueryTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION*)tzinfo );
585 if ( status != STATUS_SUCCESS )
587 SetLastError( RtlNtStatusToDosError(status) );
588 return TIME_ZONE_ID_INVALID;
590 return TIME_ZoneID( tzinfo );
593 /***********************************************************************
594 * GetTimeZoneInformationForYear (KERNEL32.@)
596 BOOL WINAPI GetTimeZoneInformationForYear( USHORT wYear,
597 PDYNAMIC_TIME_ZONE_INFORMATION pdtzi, LPTIME_ZONE_INFORMATION ptzi )
599 DYNAMIC_TIME_ZONE_INFORMATION local_dtzi, result;
601 if (!pdtzi)
603 if (GetDynamicTimeZoneInformation(&local_dtzi) == TIME_ZONE_ID_INVALID)
604 return FALSE;
605 pdtzi = &local_dtzi;
608 if (!TIME_GetSpecificTimeZoneInfo(pdtzi->TimeZoneKeyName, wYear,
609 !pdtzi->DynamicDaylightTimeDisabled, &result))
610 return FALSE;
612 memcpy(ptzi, &result, sizeof(*ptzi));
614 return TRUE;
617 /***********************************************************************
618 * SetTimeZoneInformation (KERNEL32.@)
620 * Change the settings of the current local time zone.
622 * PARAMS
623 * tzinfo [in] The new time zone.
625 * RETURNS
626 * Success: TRUE. The time zone was updated with the settings from tzinfo.
627 * Failure: FALSE.
629 BOOL WINAPI SetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
631 NTSTATUS status;
632 status = RtlSetTimeZoneInformation( (const RTL_TIME_ZONE_INFORMATION *)tzinfo );
633 if ( status != STATUS_SUCCESS )
634 SetLastError( RtlNtStatusToDosError(status) );
635 return !status;
638 /***********************************************************************
639 * SystemTimeToTzSpecificLocalTime (KERNEL32.@)
641 * Convert a utc system time to a local time in a given time zone.
643 * PARAMS
644 * lpTimeZoneInformation [in] The desired time zone.
645 * lpUniversalTime [in] The utc time to base local time on.
646 * lpLocalTime [out] The local time in the time zone.
648 * RETURNS
649 * Success: TRUE. lpLocalTime contains the converted time
650 * Failure: FALSE.
653 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
654 const TIME_ZONE_INFORMATION *lpTimeZoneInformation,
655 const SYSTEMTIME *lpUniversalTime, LPSYSTEMTIME lpLocalTime )
657 FILETIME ft;
658 LONG lBias;
659 LONGLONG llTime;
660 TIME_ZONE_INFORMATION tzinfo;
662 if (lpTimeZoneInformation != NULL)
664 tzinfo = *lpTimeZoneInformation;
666 else
668 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
669 return FALSE;
672 if (!SystemTimeToFileTime(lpUniversalTime, &ft))
673 return FALSE;
674 FILETIME2LL( &ft, llTime)
675 if (!TIME_GetTimezoneBias(&tzinfo, &ft, FALSE, &lBias))
676 return FALSE;
677 /* convert minutes to 100-nanoseconds-ticks */
678 llTime -= (LONGLONG)lBias * 600000000;
679 LL2FILETIME( llTime, &ft)
681 return FileTimeToSystemTime(&ft, lpLocalTime);
685 /***********************************************************************
686 * TzSpecificLocalTimeToSystemTime (KERNEL32.@)
688 * Converts a local time to a time in utc.
690 * PARAMS
691 * lpTimeZoneInformation [in] The desired time zone.
692 * lpLocalTime [in] The local time.
693 * lpUniversalTime [out] The calculated utc time.
695 * RETURNS
696 * Success: TRUE. lpUniversalTime contains the converted time.
697 * Failure: FALSE.
699 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
700 const TIME_ZONE_INFORMATION *lpTimeZoneInformation,
701 const SYSTEMTIME *lpLocalTime, LPSYSTEMTIME lpUniversalTime)
703 FILETIME ft;
704 LONG lBias;
705 LONGLONG t;
706 TIME_ZONE_INFORMATION tzinfo;
708 if (lpTimeZoneInformation != NULL)
710 tzinfo = *lpTimeZoneInformation;
712 else
714 if (GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_INVALID)
715 return FALSE;
718 if (!SystemTimeToFileTime(lpLocalTime, &ft))
719 return FALSE;
720 FILETIME2LL( &ft, t)
721 if (!TIME_GetTimezoneBias(&tzinfo, &ft, TRUE, &lBias))
722 return FALSE;
723 /* convert minutes to 100-nanoseconds-ticks */
724 t += (LONGLONG)lBias * 600000000;
725 LL2FILETIME( t, &ft)
726 return FileTimeToSystemTime(&ft, lpUniversalTime);
730 /***********************************************************************
731 * GetSystemTimeAsFileTime (KERNEL32.@)
733 * Get the current time in utc format.
735 * RETURNS
736 * Nothing.
738 VOID WINAPI GetSystemTimeAsFileTime(
739 LPFILETIME time) /* [out] Destination for the current utc time */
741 LARGE_INTEGER t;
742 NtQuerySystemTime( &t );
743 time->dwLowDateTime = t.u.LowPart;
744 time->dwHighDateTime = t.u.HighPart;
748 /***********************************************************************
749 * GetSystemTimePreciseAsFileTime (KERNEL32.@)
751 * Get the current time in utc format, with <1 us precision.
753 * RETURNS
754 * Nothing.
756 VOID WINAPI GetSystemTimePreciseAsFileTime(
757 LPFILETIME time) /* [out] Destination for the current utc time */
759 GetSystemTimeAsFileTime(time);
763 /*********************************************************************
764 * TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
766 * Used by GetProcessTimes to convert clock_t into FILETIME.
768 * Differences to UnixTimeToFileTime:
769 * 1) Divided by CLK_TCK
770 * 2) Time is relative. There is no 'starting date', so there is
771 * no need for offset correction, like in UnixTimeToFileTime
773 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
775 long clocksPerSec = sysconf(_SC_CLK_TCK);
776 ULONGLONG secs = (ULONGLONG)unix_time * 10000000 / clocksPerSec;
777 filetime->dwLowDateTime = (DWORD)secs;
778 filetime->dwHighDateTime = (DWORD)(secs >> 32);
781 /*********************************************************************
782 * GetProcessTimes (KERNEL32.@)
784 * Get the user and kernel execution times of a process,
785 * along with the creation and exit times if known.
787 * PARAMS
788 * hprocess [in] The process to be queried.
789 * lpCreationTime [out] The creation time of the process.
790 * lpExitTime [out] The exit time of the process if exited.
791 * lpKernelTime [out] The time spent in kernel routines in 100's of nanoseconds.
792 * lpUserTime [out] The time spent in user routines in 100's of nanoseconds.
794 * RETURNS
795 * TRUE.
797 * NOTES
798 * olorin@fandra.org:
799 * Would be nice to subtract the cpu time used by Wine at startup.
800 * Also, there is a need to separate times used by different applications.
802 * BUGS
803 * KernelTime and UserTime are always for the current process
805 BOOL WINAPI GetProcessTimes( HANDLE hprocess, LPFILETIME lpCreationTime,
806 LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime )
808 struct tms tms;
809 KERNEL_USER_TIMES pti;
811 times(&tms);
812 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
813 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
814 if (NtQueryInformationProcess( hprocess, ProcessTimes, &pti, sizeof(pti), NULL))
815 return FALSE;
816 LL2FILETIME( pti.CreateTime.QuadPart, lpCreationTime);
817 LL2FILETIME( pti.ExitTime.QuadPart, lpExitTime);
818 return TRUE;
821 /*********************************************************************
822 * GetCalendarInfoA (KERNEL32.@)
825 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
826 LPSTR lpCalData, int cchData, LPDWORD lpValue)
828 int ret, cchDataW = cchData;
829 LPWSTR lpCalDataW = NULL;
831 if (NLS_IsUnicodeOnlyLcid(lcid))
833 SetLastError(ERROR_INVALID_PARAMETER);
834 return 0;
837 if (!cchData && !(CalType & CAL_RETURN_NUMBER))
838 cchDataW = GetCalendarInfoW(lcid, Calendar, CalType, NULL, 0, NULL);
839 if (!(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchDataW*sizeof(WCHAR))))
840 return 0;
842 ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchDataW, lpValue);
843 if(ret && lpCalDataW && lpCalData)
844 ret = WideCharToMultiByte(CP_ACP, 0, lpCalDataW, -1, lpCalData, cchData, NULL, NULL);
845 else if (CalType & CAL_RETURN_NUMBER)
846 ret *= sizeof(WCHAR);
847 HeapFree(GetProcessHeap(), 0, lpCalDataW);
849 return ret;
852 /*********************************************************************
853 * GetCalendarInfoW (KERNEL32.@)
856 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
857 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
859 static const LCTYPE caltype_lctype_map[] = {
860 0, /* not used */
861 0, /* CAL_ICALINTVALUE */
862 0, /* CAL_SCALNAME */
863 0, /* CAL_IYEAROFFSETRANGE */
864 0, /* CAL_SERASTRING */
865 LOCALE_SSHORTDATE,
866 LOCALE_SLONGDATE,
867 LOCALE_SDAYNAME1,
868 LOCALE_SDAYNAME2,
869 LOCALE_SDAYNAME3,
870 LOCALE_SDAYNAME4,
871 LOCALE_SDAYNAME5,
872 LOCALE_SDAYNAME6,
873 LOCALE_SDAYNAME7,
874 LOCALE_SABBREVDAYNAME1,
875 LOCALE_SABBREVDAYNAME2,
876 LOCALE_SABBREVDAYNAME3,
877 LOCALE_SABBREVDAYNAME4,
878 LOCALE_SABBREVDAYNAME5,
879 LOCALE_SABBREVDAYNAME6,
880 LOCALE_SABBREVDAYNAME7,
881 LOCALE_SMONTHNAME1,
882 LOCALE_SMONTHNAME2,
883 LOCALE_SMONTHNAME3,
884 LOCALE_SMONTHNAME4,
885 LOCALE_SMONTHNAME5,
886 LOCALE_SMONTHNAME6,
887 LOCALE_SMONTHNAME7,
888 LOCALE_SMONTHNAME8,
889 LOCALE_SMONTHNAME9,
890 LOCALE_SMONTHNAME10,
891 LOCALE_SMONTHNAME11,
892 LOCALE_SMONTHNAME12,
893 LOCALE_SMONTHNAME13,
894 LOCALE_SABBREVMONTHNAME1,
895 LOCALE_SABBREVMONTHNAME2,
896 LOCALE_SABBREVMONTHNAME3,
897 LOCALE_SABBREVMONTHNAME4,
898 LOCALE_SABBREVMONTHNAME5,
899 LOCALE_SABBREVMONTHNAME6,
900 LOCALE_SABBREVMONTHNAME7,
901 LOCALE_SABBREVMONTHNAME8,
902 LOCALE_SABBREVMONTHNAME9,
903 LOCALE_SABBREVMONTHNAME10,
904 LOCALE_SABBREVMONTHNAME11,
905 LOCALE_SABBREVMONTHNAME12,
906 LOCALE_SABBREVMONTHNAME13,
907 LOCALE_SYEARMONTH,
908 0, /* CAL_ITWODIGITYEARMAX */
909 LOCALE_SSHORTESTDAYNAME1,
910 LOCALE_SSHORTESTDAYNAME2,
911 LOCALE_SSHORTESTDAYNAME3,
912 LOCALE_SSHORTESTDAYNAME4,
913 LOCALE_SSHORTESTDAYNAME5,
914 LOCALE_SSHORTESTDAYNAME6,
915 LOCALE_SSHORTESTDAYNAME7,
916 LOCALE_SMONTHDAY,
917 0, /* CAL_SABBREVERASTRING */
919 DWORD localeflags = 0;
920 CALTYPE calinfo;
922 if (CalType & CAL_NOUSEROVERRIDE)
923 FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
924 if (CalType & CAL_USE_CP_ACP)
925 FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
927 if (CalType & CAL_RETURN_NUMBER) {
928 if (!lpValue)
930 SetLastError( ERROR_INVALID_PARAMETER );
931 return 0;
933 if (lpCalData != NULL)
934 WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
935 if (cchData != 0)
936 WARN("cchData not 0 (%d) when it should!\n", cchData);
937 } else {
938 if (lpValue != NULL)
939 WARN("lpValue not NULL (%p) when it should!\n", lpValue);
942 /* FIXME: No verification is made yet wrt Locale
943 * for the CALTYPES not requiring GetLocaleInfoA */
945 calinfo = CalType & 0xffff;
947 if (CalType & CAL_RETURN_GENITIVE_NAMES)
948 localeflags |= LOCALE_RETURN_GENITIVE_NAMES;
950 switch (calinfo) {
951 case CAL_ICALINTVALUE:
952 if (CalType & CAL_RETURN_NUMBER)
953 return GetLocaleInfoW(Locale, LOCALE_RETURN_NUMBER | LOCALE_ICALENDARTYPE,
954 (LPWSTR)lpValue, 2);
955 return GetLocaleInfoW(Locale, LOCALE_ICALENDARTYPE, lpCalData, cchData);
956 case CAL_SCALNAME:
957 FIXME("Unimplemented caltype %d\n", calinfo);
958 if (lpCalData) *lpCalData = 0;
959 return 1;
960 case CAL_IYEAROFFSETRANGE:
961 FIXME("Unimplemented caltype %d\n", calinfo);
962 return 0;
963 case CAL_SERASTRING:
964 FIXME("Unimplemented caltype %d\n", calinfo);
965 return 0;
966 case CAL_SSHORTDATE:
967 case CAL_SLONGDATE:
968 case CAL_SDAYNAME1:
969 case CAL_SDAYNAME2:
970 case CAL_SDAYNAME3:
971 case CAL_SDAYNAME4:
972 case CAL_SDAYNAME5:
973 case CAL_SDAYNAME6:
974 case CAL_SDAYNAME7:
975 case CAL_SABBREVDAYNAME1:
976 case CAL_SABBREVDAYNAME2:
977 case CAL_SABBREVDAYNAME3:
978 case CAL_SABBREVDAYNAME4:
979 case CAL_SABBREVDAYNAME5:
980 case CAL_SABBREVDAYNAME6:
981 case CAL_SABBREVDAYNAME7:
982 case CAL_SMONTHNAME1:
983 case CAL_SMONTHNAME2:
984 case CAL_SMONTHNAME3:
985 case CAL_SMONTHNAME4:
986 case CAL_SMONTHNAME5:
987 case CAL_SMONTHNAME6:
988 case CAL_SMONTHNAME7:
989 case CAL_SMONTHNAME8:
990 case CAL_SMONTHNAME9:
991 case CAL_SMONTHNAME10:
992 case CAL_SMONTHNAME11:
993 case CAL_SMONTHNAME12:
994 case CAL_SMONTHNAME13:
995 case CAL_SABBREVMONTHNAME1:
996 case CAL_SABBREVMONTHNAME2:
997 case CAL_SABBREVMONTHNAME3:
998 case CAL_SABBREVMONTHNAME4:
999 case CAL_SABBREVMONTHNAME5:
1000 case CAL_SABBREVMONTHNAME6:
1001 case CAL_SABBREVMONTHNAME7:
1002 case CAL_SABBREVMONTHNAME8:
1003 case CAL_SABBREVMONTHNAME9:
1004 case CAL_SABBREVMONTHNAME10:
1005 case CAL_SABBREVMONTHNAME11:
1006 case CAL_SABBREVMONTHNAME12:
1007 case CAL_SABBREVMONTHNAME13:
1008 case CAL_SYEARMONTH:
1009 return GetLocaleInfoW(Locale, caltype_lctype_map[calinfo] | localeflags, lpCalData, cchData);
1010 case CAL_ITWODIGITYEARMAX:
1011 if (CalType & CAL_RETURN_NUMBER)
1013 *lpValue = CALINFO_MAX_YEAR;
1014 return sizeof(DWORD) / sizeof(WCHAR);
1016 else
1018 static const WCHAR fmtW[] = {'%','u',0};
1019 WCHAR buffer[10];
1020 int ret = snprintfW( buffer, 10, fmtW, CALINFO_MAX_YEAR ) + 1;
1021 if (!lpCalData) return ret;
1022 if (ret <= cchData)
1024 strcpyW( lpCalData, buffer );
1025 return ret;
1027 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1028 return 0;
1030 break;
1031 default:
1032 FIXME("Unknown caltype %d\n", calinfo);
1033 SetLastError(ERROR_INVALID_FLAGS);
1034 return 0;
1036 return 0;
1039 /*********************************************************************
1040 * GetCalendarInfoEx (KERNEL32.@)
1042 int WINAPI GetCalendarInfoEx(LPCWSTR locale, CALID calendar, LPCWSTR lpReserved, CALTYPE caltype,
1043 LPWSTR data, int len, DWORD *value)
1045 static int once;
1047 LCID lcid = LocaleNameToLCID(locale, 0);
1048 if (!once++)
1049 FIXME("(%s, %d, %p, 0x%08x, %p, %d, %p): semi-stub\n", debugstr_w(locale), calendar, lpReserved, caltype,
1050 data, len, value);
1051 return GetCalendarInfoW(lcid, calendar, caltype, data, len, value);
1054 /*********************************************************************
1055 * SetCalendarInfoA (KERNEL32.@)
1058 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
1060 FIXME("(%08x,%08x,%08x,%s): stub\n",
1061 Locale, Calendar, CalType, debugstr_a(lpCalData));
1062 return 0;
1065 /*********************************************************************
1066 * SetCalendarInfoW (KERNEL32.@)
1070 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
1072 FIXME("(%08x,%08x,%08x,%s): stub\n",
1073 Locale, Calendar, CalType, debugstr_w(lpCalData));
1074 return 0;
1077 /*********************************************************************
1078 * LocalFileTimeToFileTime (KERNEL32.@)
1080 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
1082 NTSTATUS status;
1083 LARGE_INTEGER local, utc;
1085 local.u.LowPart = localft->dwLowDateTime;
1086 local.u.HighPart = localft->dwHighDateTime;
1087 if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
1089 utcft->dwLowDateTime = utc.u.LowPart;
1090 utcft->dwHighDateTime = utc.u.HighPart;
1092 else SetLastError( RtlNtStatusToDosError(status) );
1094 return !status;
1097 /*********************************************************************
1098 * FileTimeToLocalFileTime (KERNEL32.@)
1100 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
1102 NTSTATUS status;
1103 LARGE_INTEGER local, utc;
1105 utc.u.LowPart = utcft->dwLowDateTime;
1106 utc.u.HighPart = utcft->dwHighDateTime;
1107 if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
1109 localft->dwLowDateTime = local.u.LowPart;
1110 localft->dwHighDateTime = local.u.HighPart;
1112 else SetLastError( RtlNtStatusToDosError(status) );
1114 return !status;
1117 /*********************************************************************
1118 * FileTimeToSystemTime (KERNEL32.@)
1120 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
1122 TIME_FIELDS tf;
1123 LARGE_INTEGER t;
1125 t.u.LowPart = ft->dwLowDateTime;
1126 t.u.HighPart = ft->dwHighDateTime;
1127 RtlTimeToTimeFields(&t, &tf);
1129 syst->wYear = tf.Year;
1130 syst->wMonth = tf.Month;
1131 syst->wDay = tf.Day;
1132 syst->wHour = tf.Hour;
1133 syst->wMinute = tf.Minute;
1134 syst->wSecond = tf.Second;
1135 syst->wMilliseconds = tf.Milliseconds;
1136 syst->wDayOfWeek = tf.Weekday;
1137 return TRUE;
1140 /*********************************************************************
1141 * SystemTimeToFileTime (KERNEL32.@)
1143 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
1145 TIME_FIELDS tf;
1146 LARGE_INTEGER t;
1148 tf.Year = syst->wYear;
1149 tf.Month = syst->wMonth;
1150 tf.Day = syst->wDay;
1151 tf.Hour = syst->wHour;
1152 tf.Minute = syst->wMinute;
1153 tf.Second = syst->wSecond;
1154 tf.Milliseconds = syst->wMilliseconds;
1156 if( !RtlTimeFieldsToTime(&tf, &t)) {
1157 SetLastError( ERROR_INVALID_PARAMETER);
1158 return FALSE;
1160 ft->dwLowDateTime = t.u.LowPart;
1161 ft->dwHighDateTime = t.u.HighPart;
1162 return TRUE;
1165 /*********************************************************************
1166 * CompareFileTime (KERNEL32.@)
1168 * Compare two FILETIME's to each other.
1170 * PARAMS
1171 * x [I] First time
1172 * y [I] time to compare to x
1174 * RETURNS
1175 * -1, 0, or 1 indicating that x is less than, equal to, or greater
1176 * than y respectively.
1178 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
1180 if (!x || !y) return -1;
1182 if (x->dwHighDateTime > y->dwHighDateTime)
1183 return 1;
1184 if (x->dwHighDateTime < y->dwHighDateTime)
1185 return -1;
1186 if (x->dwLowDateTime > y->dwLowDateTime)
1187 return 1;
1188 if (x->dwLowDateTime < y->dwLowDateTime)
1189 return -1;
1190 return 0;
1193 /*********************************************************************
1194 * GetLocalTime (KERNEL32.@)
1196 * Get the current local time.
1198 * PARAMS
1199 * systime [O] Destination for current time.
1201 * RETURNS
1202 * Nothing.
1204 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime)
1206 FILETIME lft;
1207 LARGE_INTEGER ft, ft2;
1209 NtQuerySystemTime(&ft);
1210 RtlSystemTimeToLocalTime(&ft, &ft2);
1211 lft.dwLowDateTime = ft2.u.LowPart;
1212 lft.dwHighDateTime = ft2.u.HighPart;
1213 FileTimeToSystemTime(&lft, systime);
1216 /*********************************************************************
1217 * GetSystemTime (KERNEL32.@)
1219 * Get the current system time.
1221 * PARAMS
1222 * systime [O] Destination for current time.
1224 * RETURNS
1225 * Nothing.
1227 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
1229 FILETIME ft;
1230 LARGE_INTEGER t;
1232 NtQuerySystemTime(&t);
1233 ft.dwLowDateTime = t.u.LowPart;
1234 ft.dwHighDateTime = t.u.HighPart;
1235 FileTimeToSystemTime(&ft, systime);
1238 /*********************************************************************
1239 * GetDaylightFlag (KERNEL32.@)
1241 * Specifies if daylight savings time is in operation.
1243 * NOTES
1244 * This function is called from the Win98's control applet timedate.cpl.
1246 * RETURNS
1247 * TRUE if daylight savings time is in operation.
1248 * FALSE otherwise.
1250 BOOL WINAPI GetDaylightFlag(void)
1252 TIME_ZONE_INFORMATION tzinfo;
1253 return GetTimeZoneInformation( &tzinfo) == TIME_ZONE_ID_DAYLIGHT;
1256 /***********************************************************************
1257 * DosDateTimeToFileTime (KERNEL32.@)
1259 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
1261 struct tm newtm;
1262 #ifndef HAVE_TIMEGM
1263 struct tm *gtm;
1264 time_t time1, time2;
1265 #endif
1267 newtm.tm_sec = (fattime & 0x1f) * 2;
1268 newtm.tm_min = (fattime >> 5) & 0x3f;
1269 newtm.tm_hour = (fattime >> 11);
1270 newtm.tm_mday = (fatdate & 0x1f);
1271 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
1272 newtm.tm_year = (fatdate >> 9) + 80;
1273 newtm.tm_isdst = -1;
1274 #ifdef HAVE_TIMEGM
1275 RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
1276 #else
1277 time1 = mktime(&newtm);
1278 gtm = gmtime(&time1);
1279 time2 = mktime(gtm);
1280 RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
1281 #endif
1282 return TRUE;
1286 /***********************************************************************
1287 * FileTimeToDosDateTime (KERNEL32.@)
1289 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
1290 LPWORD fattime )
1292 LARGE_INTEGER li;
1293 ULONG t;
1294 time_t unixtime;
1295 struct tm* tm;
1297 if (!fatdate || !fattime)
1299 SetLastError(ERROR_INVALID_PARAMETER);
1300 return FALSE;
1302 li.u.LowPart = ft->dwLowDateTime;
1303 li.u.HighPart = ft->dwHighDateTime;
1304 if (!RtlTimeToSecondsSince1970( &li, &t ))
1306 SetLastError(ERROR_INVALID_PARAMETER);
1307 return FALSE;
1309 unixtime = t;
1310 tm = gmtime( &unixtime );
1311 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
1312 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday;
1313 return TRUE;
1316 /*********************************************************************
1317 * GetSystemTimes (KERNEL32.@)
1319 * Retrieves system timing information
1321 * PARAMS
1322 * lpIdleTime [O] Destination for idle time.
1323 * lpKernelTime [O] Destination for kernel time.
1324 * lpUserTime [O] Destination for user time.
1326 * RETURNS
1327 * TRUE if success, FALSE otherwise.
1329 BOOL WINAPI GetSystemTimes(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime)
1331 LARGE_INTEGER idle_time, kernel_time, user_time;
1332 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi;
1333 SYSTEM_BASIC_INFORMATION sbi;
1334 NTSTATUS status;
1335 ULONG ret_size;
1336 int i;
1338 TRACE("(%p,%p,%p)\n", lpIdleTime, lpKernelTime, lpUserTime);
1340 status = NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), &ret_size );
1341 if (status != STATUS_SUCCESS)
1343 SetLastError( RtlNtStatusToDosError(status) );
1344 return FALSE;
1347 sppi = HeapAlloc( GetProcessHeap(), 0,
1348 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * sbi.NumberOfProcessors);
1349 if (!sppi)
1351 SetLastError( ERROR_OUTOFMEMORY );
1352 return FALSE;
1355 status = NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, sizeof(*sppi) * sbi.NumberOfProcessors,
1356 &ret_size );
1357 if (status != STATUS_SUCCESS)
1359 HeapFree( GetProcessHeap(), 0, sppi );
1360 SetLastError( RtlNtStatusToDosError(status) );
1361 return FALSE;
1364 idle_time.QuadPart = 0;
1365 kernel_time.QuadPart = 0;
1366 user_time.QuadPart = 0;
1367 for (i = 0; i < sbi.NumberOfProcessors; i++)
1369 idle_time.QuadPart += sppi[i].IdleTime.QuadPart;
1370 kernel_time.QuadPart += sppi[i].KernelTime.QuadPart;
1371 user_time.QuadPart += sppi[i].UserTime.QuadPart;
1374 if (lpIdleTime)
1376 lpIdleTime->dwLowDateTime = idle_time.u.LowPart;
1377 lpIdleTime->dwHighDateTime = idle_time.u.HighPart;
1379 if (lpKernelTime)
1381 lpKernelTime->dwLowDateTime = kernel_time.u.LowPart;
1382 lpKernelTime->dwHighDateTime = kernel_time.u.HighPart;
1384 if (lpUserTime)
1386 lpUserTime->dwLowDateTime = user_time.u.LowPart;
1387 lpUserTime->dwHighDateTime = user_time.u.HighPart;
1390 HeapFree( GetProcessHeap(), 0, sppi );
1391 return TRUE;
1394 /***********************************************************************
1395 * GetDynamicTimeZoneInformation (KERNEL32.@)
1397 DWORD WINAPI GetDynamicTimeZoneInformation(DYNAMIC_TIME_ZONE_INFORMATION *tzinfo)
1399 NTSTATUS status;
1401 status = RtlQueryDynamicTimeZoneInformation( (RTL_DYNAMIC_TIME_ZONE_INFORMATION*)tzinfo );
1402 if ( status != STATUS_SUCCESS )
1404 SetLastError( RtlNtStatusToDosError(status) );
1405 return TIME_ZONE_ID_INVALID;
1407 return TIME_ZoneID( (TIME_ZONE_INFORMATION*)tzinfo );
1410 /***********************************************************************
1411 * QueryThreadCycleTime (KERNEL32.@)
1413 BOOL WINAPI QueryThreadCycleTime(HANDLE thread, PULONG64 cycle)
1415 static int once;
1416 if (!once++)
1417 FIXME("(%p,%p): stub!\n", thread, cycle);
1418 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1419 return FALSE;
1422 /***********************************************************************
1423 * QueryUnbiasedInterruptTime (KERNEL32.@)
1425 BOOL WINAPI QueryUnbiasedInterruptTime(ULONGLONG *time)
1427 TRACE("(%p)\n", time);
1428 if (!time) return FALSE;
1429 RtlQueryUnbiasedInterruptTime(time);
1430 return TRUE;