user32/tests: Reorganize the DPI tests.
[wine.git] / dlls / kernel32 / time.c
blob91b2c006c7c770de42f25f4c53017f3ee6c16812
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 * TIMEZONE_InitRegistry
784 * Update registry contents on startup if the user timezone has changed.
785 * This simulates the action of the Windows control panel.
787 void TIMEZONE_InitRegistry(void)
789 static const WCHAR timezoneInformationW[] = {
790 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
791 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
792 'C','o','n','t','r','o','l','\\',
793 'T','i','m','e','Z','o','n','e','I','n','f','o','r','m','a','t','i','o','n','\0'
795 static const WCHAR standardNameW[] = {'S','t','a','n','d','a','r','d','N','a','m','e','\0'};
796 static const WCHAR timezoneKeyNameW[] = {'T','i','m','e','Z','o','n','e','K','e','y','N','a','m','e','\0'};
797 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
798 UNICODE_STRING name;
799 OBJECT_ATTRIBUTES attr;
800 HANDLE hkey;
801 DWORD tzid;
803 tzid = GetDynamicTimeZoneInformation(&tzinfo);
804 if (tzid == TIME_ZONE_ID_INVALID) return;
806 RtlInitUnicodeString(&name, timezoneInformationW);
807 InitializeObjectAttributes(&attr, &name, 0, 0, NULL);
808 if (NtCreateKey(&hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL) != STATUS_SUCCESS) return;
810 RtlInitUnicodeString(&name, standardNameW);
811 NtSetValueKey(hkey, &name, 0, REG_SZ, tzinfo.StandardName,
812 (strlenW(tzinfo.StandardName) + 1) * sizeof(WCHAR));
814 RtlInitUnicodeString(&name, timezoneKeyNameW);
815 NtSetValueKey(hkey, &name, 0, REG_SZ, tzinfo.TimeZoneKeyName,
816 (strlenW(tzinfo.TimeZoneKeyName) + 1) * sizeof(WCHAR));
818 NtClose( hkey );
821 /*********************************************************************
822 * GetProcessTimes (KERNEL32.@)
824 * Get the user and kernel execution times of a process,
825 * along with the creation and exit times if known.
827 * PARAMS
828 * hprocess [in] The process to be queried.
829 * lpCreationTime [out] The creation time of the process.
830 * lpExitTime [out] The exit time of the process if exited.
831 * lpKernelTime [out] The time spent in kernel routines in 100's of nanoseconds.
832 * lpUserTime [out] The time spent in user routines in 100's of nanoseconds.
834 * RETURNS
835 * TRUE.
837 * NOTES
838 * olorin@fandra.org:
839 * Would be nice to subtract the cpu time used by Wine at startup.
840 * Also, there is a need to separate times used by different applications.
842 * BUGS
843 * KernelTime and UserTime are always for the current process
845 BOOL WINAPI GetProcessTimes( HANDLE hprocess, LPFILETIME lpCreationTime,
846 LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime )
848 struct tms tms;
849 KERNEL_USER_TIMES pti;
851 times(&tms);
852 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
853 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
854 if (NtQueryInformationProcess( hprocess, ProcessTimes, &pti, sizeof(pti), NULL))
855 return FALSE;
856 LL2FILETIME( pti.CreateTime.QuadPart, lpCreationTime);
857 LL2FILETIME( pti.ExitTime.QuadPart, lpExitTime);
858 return TRUE;
861 /*********************************************************************
862 * GetCalendarInfoA (KERNEL32.@)
865 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
866 LPSTR lpCalData, int cchData, LPDWORD lpValue)
868 int ret, cchDataW = cchData;
869 LPWSTR lpCalDataW = NULL;
871 if (NLS_IsUnicodeOnlyLcid(lcid))
873 SetLastError(ERROR_INVALID_PARAMETER);
874 return 0;
877 if (!cchData && !(CalType & CAL_RETURN_NUMBER))
878 cchDataW = GetCalendarInfoW(lcid, Calendar, CalType, NULL, 0, NULL);
879 if (!(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchDataW*sizeof(WCHAR))))
880 return 0;
882 ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchDataW, lpValue);
883 if(ret && lpCalDataW && lpCalData)
884 ret = WideCharToMultiByte(CP_ACP, 0, lpCalDataW, -1, lpCalData, cchData, NULL, NULL);
885 else if (CalType & CAL_RETURN_NUMBER)
886 ret *= sizeof(WCHAR);
887 HeapFree(GetProcessHeap(), 0, lpCalDataW);
889 return ret;
892 /*********************************************************************
893 * GetCalendarInfoW (KERNEL32.@)
896 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
897 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
899 static const LCTYPE caltype_lctype_map[] = {
900 0, /* not used */
901 0, /* CAL_ICALINTVALUE */
902 0, /* CAL_SCALNAME */
903 0, /* CAL_IYEAROFFSETRANGE */
904 0, /* CAL_SERASTRING */
905 LOCALE_SSHORTDATE,
906 LOCALE_SLONGDATE,
907 LOCALE_SDAYNAME1,
908 LOCALE_SDAYNAME2,
909 LOCALE_SDAYNAME3,
910 LOCALE_SDAYNAME4,
911 LOCALE_SDAYNAME5,
912 LOCALE_SDAYNAME6,
913 LOCALE_SDAYNAME7,
914 LOCALE_SABBREVDAYNAME1,
915 LOCALE_SABBREVDAYNAME2,
916 LOCALE_SABBREVDAYNAME3,
917 LOCALE_SABBREVDAYNAME4,
918 LOCALE_SABBREVDAYNAME5,
919 LOCALE_SABBREVDAYNAME6,
920 LOCALE_SABBREVDAYNAME7,
921 LOCALE_SMONTHNAME1,
922 LOCALE_SMONTHNAME2,
923 LOCALE_SMONTHNAME3,
924 LOCALE_SMONTHNAME4,
925 LOCALE_SMONTHNAME5,
926 LOCALE_SMONTHNAME6,
927 LOCALE_SMONTHNAME7,
928 LOCALE_SMONTHNAME8,
929 LOCALE_SMONTHNAME9,
930 LOCALE_SMONTHNAME10,
931 LOCALE_SMONTHNAME11,
932 LOCALE_SMONTHNAME12,
933 LOCALE_SMONTHNAME13,
934 LOCALE_SABBREVMONTHNAME1,
935 LOCALE_SABBREVMONTHNAME2,
936 LOCALE_SABBREVMONTHNAME3,
937 LOCALE_SABBREVMONTHNAME4,
938 LOCALE_SABBREVMONTHNAME5,
939 LOCALE_SABBREVMONTHNAME6,
940 LOCALE_SABBREVMONTHNAME7,
941 LOCALE_SABBREVMONTHNAME8,
942 LOCALE_SABBREVMONTHNAME9,
943 LOCALE_SABBREVMONTHNAME10,
944 LOCALE_SABBREVMONTHNAME11,
945 LOCALE_SABBREVMONTHNAME12,
946 LOCALE_SABBREVMONTHNAME13,
947 LOCALE_SYEARMONTH,
948 0, /* CAL_ITWODIGITYEARMAX */
949 LOCALE_SSHORTESTDAYNAME1,
950 LOCALE_SSHORTESTDAYNAME2,
951 LOCALE_SSHORTESTDAYNAME3,
952 LOCALE_SSHORTESTDAYNAME4,
953 LOCALE_SSHORTESTDAYNAME5,
954 LOCALE_SSHORTESTDAYNAME6,
955 LOCALE_SSHORTESTDAYNAME7,
956 LOCALE_SMONTHDAY,
957 0, /* CAL_SABBREVERASTRING */
959 DWORD localeflags = 0;
960 CALTYPE calinfo;
962 if (CalType & CAL_NOUSEROVERRIDE)
963 FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
964 if (CalType & CAL_USE_CP_ACP)
965 FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
967 if (CalType & CAL_RETURN_NUMBER) {
968 if (!lpValue)
970 SetLastError( ERROR_INVALID_PARAMETER );
971 return 0;
973 if (lpCalData != NULL)
974 WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
975 if (cchData != 0)
976 WARN("cchData not 0 (%d) when it should!\n", cchData);
977 } else {
978 if (lpValue != NULL)
979 WARN("lpValue not NULL (%p) when it should!\n", lpValue);
982 /* FIXME: No verification is made yet wrt Locale
983 * for the CALTYPES not requiring GetLocaleInfoA */
985 calinfo = CalType & 0xffff;
987 if (CalType & CAL_RETURN_GENITIVE_NAMES)
988 localeflags |= LOCALE_RETURN_GENITIVE_NAMES;
990 switch (calinfo) {
991 case CAL_ICALINTVALUE:
992 if (CalType & CAL_RETURN_NUMBER)
993 return GetLocaleInfoW(Locale, LOCALE_RETURN_NUMBER | LOCALE_ICALENDARTYPE,
994 (LPWSTR)lpValue, 2);
995 return GetLocaleInfoW(Locale, LOCALE_ICALENDARTYPE, lpCalData, cchData);
996 case CAL_SCALNAME:
997 FIXME("Unimplemented caltype %d\n", calinfo);
998 if (lpCalData) *lpCalData = 0;
999 return 1;
1000 case CAL_IYEAROFFSETRANGE:
1001 FIXME("Unimplemented caltype %d\n", calinfo);
1002 return 0;
1003 case CAL_SERASTRING:
1004 FIXME("Unimplemented caltype %d\n", calinfo);
1005 return 0;
1006 case CAL_SSHORTDATE:
1007 case CAL_SLONGDATE:
1008 case CAL_SDAYNAME1:
1009 case CAL_SDAYNAME2:
1010 case CAL_SDAYNAME3:
1011 case CAL_SDAYNAME4:
1012 case CAL_SDAYNAME5:
1013 case CAL_SDAYNAME6:
1014 case CAL_SDAYNAME7:
1015 case CAL_SABBREVDAYNAME1:
1016 case CAL_SABBREVDAYNAME2:
1017 case CAL_SABBREVDAYNAME3:
1018 case CAL_SABBREVDAYNAME4:
1019 case CAL_SABBREVDAYNAME5:
1020 case CAL_SABBREVDAYNAME6:
1021 case CAL_SABBREVDAYNAME7:
1022 case CAL_SMONTHNAME1:
1023 case CAL_SMONTHNAME2:
1024 case CAL_SMONTHNAME3:
1025 case CAL_SMONTHNAME4:
1026 case CAL_SMONTHNAME5:
1027 case CAL_SMONTHNAME6:
1028 case CAL_SMONTHNAME7:
1029 case CAL_SMONTHNAME8:
1030 case CAL_SMONTHNAME9:
1031 case CAL_SMONTHNAME10:
1032 case CAL_SMONTHNAME11:
1033 case CAL_SMONTHNAME12:
1034 case CAL_SMONTHNAME13:
1035 case CAL_SABBREVMONTHNAME1:
1036 case CAL_SABBREVMONTHNAME2:
1037 case CAL_SABBREVMONTHNAME3:
1038 case CAL_SABBREVMONTHNAME4:
1039 case CAL_SABBREVMONTHNAME5:
1040 case CAL_SABBREVMONTHNAME6:
1041 case CAL_SABBREVMONTHNAME7:
1042 case CAL_SABBREVMONTHNAME8:
1043 case CAL_SABBREVMONTHNAME9:
1044 case CAL_SABBREVMONTHNAME10:
1045 case CAL_SABBREVMONTHNAME11:
1046 case CAL_SABBREVMONTHNAME12:
1047 case CAL_SABBREVMONTHNAME13:
1048 case CAL_SYEARMONTH:
1049 return GetLocaleInfoW(Locale, caltype_lctype_map[calinfo] | localeflags, lpCalData, cchData);
1050 case CAL_ITWODIGITYEARMAX:
1051 if (CalType & CAL_RETURN_NUMBER)
1053 *lpValue = CALINFO_MAX_YEAR;
1054 return sizeof(DWORD) / sizeof(WCHAR);
1056 else
1058 static const WCHAR fmtW[] = {'%','u',0};
1059 WCHAR buffer[10];
1060 int ret = snprintfW( buffer, 10, fmtW, CALINFO_MAX_YEAR ) + 1;
1061 if (!lpCalData) return ret;
1062 if (ret <= cchData)
1064 strcpyW( lpCalData, buffer );
1065 return ret;
1067 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1068 return 0;
1070 break;
1071 default:
1072 FIXME("Unknown caltype %d\n", calinfo);
1073 SetLastError(ERROR_INVALID_FLAGS);
1074 return 0;
1076 return 0;
1079 /*********************************************************************
1080 * GetCalendarInfoEx (KERNEL32.@)
1082 int WINAPI GetCalendarInfoEx(LPCWSTR locale, CALID calendar, LPCWSTR lpReserved, CALTYPE caltype,
1083 LPWSTR data, int len, DWORD *value)
1085 static int once;
1087 LCID lcid = LocaleNameToLCID(locale, 0);
1088 if (!once++)
1089 FIXME("(%s, %d, %p, 0x%08x, %p, %d, %p): semi-stub\n", debugstr_w(locale), calendar, lpReserved, caltype,
1090 data, len, value);
1091 return GetCalendarInfoW(lcid, calendar, caltype, data, len, value);
1094 /*********************************************************************
1095 * SetCalendarInfoA (KERNEL32.@)
1098 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
1100 FIXME("(%08x,%08x,%08x,%s): stub\n",
1101 Locale, Calendar, CalType, debugstr_a(lpCalData));
1102 return 0;
1105 /*********************************************************************
1106 * SetCalendarInfoW (KERNEL32.@)
1110 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
1112 FIXME("(%08x,%08x,%08x,%s): stub\n",
1113 Locale, Calendar, CalType, debugstr_w(lpCalData));
1114 return 0;
1117 /*********************************************************************
1118 * LocalFileTimeToFileTime (KERNEL32.@)
1120 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
1122 NTSTATUS status;
1123 LARGE_INTEGER local, utc;
1125 local.u.LowPart = localft->dwLowDateTime;
1126 local.u.HighPart = localft->dwHighDateTime;
1127 if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
1129 utcft->dwLowDateTime = utc.u.LowPart;
1130 utcft->dwHighDateTime = utc.u.HighPart;
1132 else SetLastError( RtlNtStatusToDosError(status) );
1134 return !status;
1137 /*********************************************************************
1138 * FileTimeToLocalFileTime (KERNEL32.@)
1140 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
1142 NTSTATUS status;
1143 LARGE_INTEGER local, utc;
1145 utc.u.LowPart = utcft->dwLowDateTime;
1146 utc.u.HighPart = utcft->dwHighDateTime;
1147 if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
1149 localft->dwLowDateTime = local.u.LowPart;
1150 localft->dwHighDateTime = local.u.HighPart;
1152 else SetLastError( RtlNtStatusToDosError(status) );
1154 return !status;
1157 /*********************************************************************
1158 * FileTimeToSystemTime (KERNEL32.@)
1160 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
1162 TIME_FIELDS tf;
1163 LARGE_INTEGER t;
1165 t.u.LowPart = ft->dwLowDateTime;
1166 t.u.HighPart = ft->dwHighDateTime;
1167 RtlTimeToTimeFields(&t, &tf);
1169 syst->wYear = tf.Year;
1170 syst->wMonth = tf.Month;
1171 syst->wDay = tf.Day;
1172 syst->wHour = tf.Hour;
1173 syst->wMinute = tf.Minute;
1174 syst->wSecond = tf.Second;
1175 syst->wMilliseconds = tf.Milliseconds;
1176 syst->wDayOfWeek = tf.Weekday;
1177 return TRUE;
1180 /*********************************************************************
1181 * SystemTimeToFileTime (KERNEL32.@)
1183 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
1185 TIME_FIELDS tf;
1186 LARGE_INTEGER t;
1188 tf.Year = syst->wYear;
1189 tf.Month = syst->wMonth;
1190 tf.Day = syst->wDay;
1191 tf.Hour = syst->wHour;
1192 tf.Minute = syst->wMinute;
1193 tf.Second = syst->wSecond;
1194 tf.Milliseconds = syst->wMilliseconds;
1196 if( !RtlTimeFieldsToTime(&tf, &t)) {
1197 SetLastError( ERROR_INVALID_PARAMETER);
1198 return FALSE;
1200 ft->dwLowDateTime = t.u.LowPart;
1201 ft->dwHighDateTime = t.u.HighPart;
1202 return TRUE;
1205 /*********************************************************************
1206 * CompareFileTime (KERNEL32.@)
1208 * Compare two FILETIME's to each other.
1210 * PARAMS
1211 * x [I] First time
1212 * y [I] time to compare to x
1214 * RETURNS
1215 * -1, 0, or 1 indicating that x is less than, equal to, or greater
1216 * than y respectively.
1218 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
1220 if (!x || !y) return -1;
1222 if (x->dwHighDateTime > y->dwHighDateTime)
1223 return 1;
1224 if (x->dwHighDateTime < y->dwHighDateTime)
1225 return -1;
1226 if (x->dwLowDateTime > y->dwLowDateTime)
1227 return 1;
1228 if (x->dwLowDateTime < y->dwLowDateTime)
1229 return -1;
1230 return 0;
1233 /*********************************************************************
1234 * GetLocalTime (KERNEL32.@)
1236 * Get the current local time.
1238 * PARAMS
1239 * systime [O] Destination for current time.
1241 * RETURNS
1242 * Nothing.
1244 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime)
1246 FILETIME lft;
1247 LARGE_INTEGER ft, ft2;
1249 NtQuerySystemTime(&ft);
1250 RtlSystemTimeToLocalTime(&ft, &ft2);
1251 lft.dwLowDateTime = ft2.u.LowPart;
1252 lft.dwHighDateTime = ft2.u.HighPart;
1253 FileTimeToSystemTime(&lft, systime);
1256 /*********************************************************************
1257 * GetSystemTime (KERNEL32.@)
1259 * Get the current system time.
1261 * PARAMS
1262 * systime [O] Destination for current time.
1264 * RETURNS
1265 * Nothing.
1267 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
1269 FILETIME ft;
1270 LARGE_INTEGER t;
1272 NtQuerySystemTime(&t);
1273 ft.dwLowDateTime = t.u.LowPart;
1274 ft.dwHighDateTime = t.u.HighPart;
1275 FileTimeToSystemTime(&ft, systime);
1278 /*********************************************************************
1279 * GetDaylightFlag (KERNEL32.@)
1281 * Specifies if daylight savings time is in operation.
1283 * NOTES
1284 * This function is called from the Win98's control applet timedate.cpl.
1286 * RETURNS
1287 * TRUE if daylight savings time is in operation.
1288 * FALSE otherwise.
1290 BOOL WINAPI GetDaylightFlag(void)
1292 TIME_ZONE_INFORMATION tzinfo;
1293 return GetTimeZoneInformation( &tzinfo) == TIME_ZONE_ID_DAYLIGHT;
1296 /***********************************************************************
1297 * DosDateTimeToFileTime (KERNEL32.@)
1299 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
1301 struct tm newtm;
1302 #ifndef HAVE_TIMEGM
1303 struct tm *gtm;
1304 time_t time1, time2;
1305 #endif
1307 newtm.tm_sec = (fattime & 0x1f) * 2;
1308 newtm.tm_min = (fattime >> 5) & 0x3f;
1309 newtm.tm_hour = (fattime >> 11);
1310 newtm.tm_mday = (fatdate & 0x1f);
1311 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
1312 newtm.tm_year = (fatdate >> 9) + 80;
1313 newtm.tm_isdst = -1;
1314 #ifdef HAVE_TIMEGM
1315 RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
1316 #else
1317 time1 = mktime(&newtm);
1318 gtm = gmtime(&time1);
1319 time2 = mktime(gtm);
1320 RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
1321 #endif
1322 return TRUE;
1326 /***********************************************************************
1327 * FileTimeToDosDateTime (KERNEL32.@)
1329 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
1330 LPWORD fattime )
1332 LARGE_INTEGER li;
1333 ULONG t;
1334 time_t unixtime;
1335 struct tm* tm;
1337 if (!fatdate || !fattime)
1339 SetLastError(ERROR_INVALID_PARAMETER);
1340 return FALSE;
1342 li.u.LowPart = ft->dwLowDateTime;
1343 li.u.HighPart = ft->dwHighDateTime;
1344 if (!RtlTimeToSecondsSince1970( &li, &t ))
1346 SetLastError(ERROR_INVALID_PARAMETER);
1347 return FALSE;
1349 unixtime = t;
1350 tm = gmtime( &unixtime );
1351 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
1352 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday;
1353 return TRUE;
1356 /*********************************************************************
1357 * GetSystemTimes (KERNEL32.@)
1359 * Retrieves system timing information
1361 * PARAMS
1362 * lpIdleTime [O] Destination for idle time.
1363 * lpKernelTime [O] Destination for kernel time.
1364 * lpUserTime [O] Destination for user time.
1366 * RETURNS
1367 * TRUE if success, FALSE otherwise.
1369 BOOL WINAPI GetSystemTimes(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime)
1371 LARGE_INTEGER idle_time, kernel_time, user_time;
1372 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi;
1373 SYSTEM_BASIC_INFORMATION sbi;
1374 NTSTATUS status;
1375 ULONG ret_size;
1376 int i;
1378 TRACE("(%p,%p,%p)\n", lpIdleTime, lpKernelTime, lpUserTime);
1380 status = NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), &ret_size );
1381 if (status != STATUS_SUCCESS)
1383 SetLastError( RtlNtStatusToDosError(status) );
1384 return FALSE;
1387 sppi = HeapAlloc( GetProcessHeap(), 0,
1388 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * sbi.NumberOfProcessors);
1389 if (!sppi)
1391 SetLastError( ERROR_OUTOFMEMORY );
1392 return FALSE;
1395 status = NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, sizeof(*sppi) * sbi.NumberOfProcessors,
1396 &ret_size );
1397 if (status != STATUS_SUCCESS)
1399 HeapFree( GetProcessHeap(), 0, sppi );
1400 SetLastError( RtlNtStatusToDosError(status) );
1401 return FALSE;
1404 idle_time.QuadPart = 0;
1405 kernel_time.QuadPart = 0;
1406 user_time.QuadPart = 0;
1407 for (i = 0; i < sbi.NumberOfProcessors; i++)
1409 idle_time.QuadPart += sppi[i].IdleTime.QuadPart;
1410 kernel_time.QuadPart += sppi[i].KernelTime.QuadPart;
1411 user_time.QuadPart += sppi[i].UserTime.QuadPart;
1414 if (lpIdleTime)
1416 lpIdleTime->dwLowDateTime = idle_time.u.LowPart;
1417 lpIdleTime->dwHighDateTime = idle_time.u.HighPart;
1419 if (lpKernelTime)
1421 lpKernelTime->dwLowDateTime = kernel_time.u.LowPart;
1422 lpKernelTime->dwHighDateTime = kernel_time.u.HighPart;
1424 if (lpUserTime)
1426 lpUserTime->dwLowDateTime = user_time.u.LowPart;
1427 lpUserTime->dwHighDateTime = user_time.u.HighPart;
1430 HeapFree( GetProcessHeap(), 0, sppi );
1431 return TRUE;
1434 /***********************************************************************
1435 * GetDynamicTimeZoneInformation (KERNEL32.@)
1437 DWORD WINAPI GetDynamicTimeZoneInformation(DYNAMIC_TIME_ZONE_INFORMATION *tzinfo)
1439 NTSTATUS status;
1441 status = RtlQueryDynamicTimeZoneInformation( (RTL_DYNAMIC_TIME_ZONE_INFORMATION*)tzinfo );
1442 if ( status != STATUS_SUCCESS )
1444 SetLastError( RtlNtStatusToDosError(status) );
1445 return TIME_ZONE_ID_INVALID;
1447 return TIME_ZoneID( (TIME_ZONE_INFORMATION*)tzinfo );
1450 /***********************************************************************
1451 * QueryProcessCycleTime (KERNEL32.@)
1453 BOOL WINAPI QueryProcessCycleTime(HANDLE process, PULONG64 cycle)
1455 static int once;
1456 if (!once++)
1457 FIXME("(%p,%p): stub!\n", process, cycle);
1458 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1459 return FALSE;
1462 /***********************************************************************
1463 * QueryThreadCycleTime (KERNEL32.@)
1465 BOOL WINAPI QueryThreadCycleTime(HANDLE thread, PULONG64 cycle)
1467 static int once;
1468 if (!once++)
1469 FIXME("(%p,%p): stub!\n", thread, cycle);
1470 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1471 return FALSE;
1474 /***********************************************************************
1475 * QueryUnbiasedInterruptTime (KERNEL32.@)
1477 BOOL WINAPI QueryUnbiasedInterruptTime(ULONGLONG *time)
1479 TRACE("(%p)\n", time);
1480 if (!time) return FALSE;
1481 RtlQueryUnbiasedInterruptTime(time);
1482 return TRUE;