wined3d: Split clears for different size resources in ffp_blitter_clear().
[wine.git] / dlls / kernel32 / time.c
blobc425eb8280f1cae6a9b121761c8def5cc94bfec5
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 ;
62 static const WCHAR mui_stdW[] = { 'M','U','I','_','S','t','d',0 };
63 static const WCHAR mui_dltW[] = { 'M','U','I','_','D','l','t',0 };
65 static const int MonthLengths[2][12] =
67 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
68 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
71 static inline BOOL IsLeapYear(int Year)
73 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
76 /***********************************************************************
77 * TIME_DayLightCompareDate
79 * Compares two dates without looking at the year.
81 * PARAMS
82 * date [in] The local time to compare.
83 * compareDate [in] The daylight savings begin or end date.
85 * RETURNS
87 * -1 if date < compareDate
88 * 0 if date == compareDate
89 * 1 if date > compareDate
90 * -2 if an error occurs
92 static int TIME_DayLightCompareDate( const SYSTEMTIME *date,
93 const SYSTEMTIME *compareDate )
95 int limit_day, dayinsecs;
97 if (date->wMonth < compareDate->wMonth)
98 return -1; /* We are in a month before the date limit. */
100 if (date->wMonth > compareDate->wMonth)
101 return 1; /* We are in a month after the date limit. */
103 /* if year is 0 then date is in day-of-week format, otherwise
104 * it's absolute date.
106 if (compareDate->wYear == 0)
108 WORD First;
109 /* compareDate->wDay is interpreted as number of the week in the month
110 * 5 means: the last week in the month */
111 int weekofmonth = compareDate->wDay;
112 /* calculate the day of the first DayOfWeek in the month */
113 First = ( 6 + compareDate->wDayOfWeek - date->wDayOfWeek + date->wDay
114 ) % 7 + 1;
115 limit_day = First + 7 * (weekofmonth - 1);
116 /* check needed for the 5th weekday of the month */
117 if(limit_day > MonthLengths[date->wMonth==2 && IsLeapYear(date->wYear)]
118 [date->wMonth - 1])
119 limit_day -= 7;
121 else
123 limit_day = compareDate->wDay;
126 /* convert to seconds */
127 limit_day = ((limit_day * 24 + compareDate->wHour) * 60 +
128 compareDate->wMinute ) * 60;
129 dayinsecs = ((date->wDay * 24 + date->wHour) * 60 +
130 date->wMinute ) * 60 + date->wSecond;
131 /* and compare */
132 return dayinsecs < limit_day ? -1 :
133 dayinsecs > limit_day ? 1 :
134 0; /* date is equal to the date limit. */
137 /***********************************************************************
138 * TIME_CompTimeZoneID
140 * Computes the local time bias for a given time and time zone.
142 * PARAMS
143 * pTZinfo [in] The time zone data.
144 * lpFileTime [in] The system or local time.
145 * islocal [in] it is local time.
147 * RETURNS
148 * TIME_ZONE_ID_INVALID An error occurred
149 * TIME_ZONE_ID_UNKNOWN There are no transition time known
150 * TIME_ZONE_ID_STANDARD Current time is standard time
151 * TIME_ZONE_ID_DAYLIGHT Current time is daylight savings time
153 static DWORD TIME_CompTimeZoneID ( const TIME_ZONE_INFORMATION *pTZinfo,
154 FILETIME *lpFileTime, BOOL islocal )
156 int ret, year;
157 BOOL beforeStandardDate, afterDaylightDate;
158 DWORD retval = TIME_ZONE_ID_INVALID;
159 LONGLONG llTime = 0; /* initialized to prevent gcc complaining */
160 SYSTEMTIME SysTime;
161 FILETIME ftTemp;
163 if (pTZinfo->DaylightDate.wMonth != 0)
165 /* if year is 0 then date is in day-of-week format, otherwise
166 * it's absolute date.
168 if (pTZinfo->StandardDate.wMonth == 0 ||
169 (pTZinfo->StandardDate.wYear == 0 &&
170 (pTZinfo->StandardDate.wDay<1 ||
171 pTZinfo->StandardDate.wDay>5 ||
172 pTZinfo->DaylightDate.wDay<1 ||
173 pTZinfo->DaylightDate.wDay>5)))
175 SetLastError(ERROR_INVALID_PARAMETER);
176 return TIME_ZONE_ID_INVALID;
179 if (!islocal) {
180 FILETIME2LL( lpFileTime, llTime );
181 llTime -= pTZinfo->Bias * (LONGLONG)600000000;
182 LL2FILETIME( llTime, &ftTemp)
183 lpFileTime = &ftTemp;
186 FileTimeToSystemTime(lpFileTime, &SysTime);
187 year = SysTime.wYear;
189 if (!islocal) {
190 llTime -= pTZinfo->DaylightBias * (LONGLONG)600000000;
191 LL2FILETIME( llTime, &ftTemp)
192 FileTimeToSystemTime(lpFileTime, &SysTime);
195 /* check for daylight savings */
196 if(year == SysTime.wYear) {
197 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->StandardDate);
198 if (ret == -2)
199 return TIME_ZONE_ID_INVALID;
201 beforeStandardDate = ret < 0;
202 } else
203 beforeStandardDate = SysTime.wYear < year;
205 if (!islocal) {
206 llTime -= ( pTZinfo->StandardBias - pTZinfo->DaylightBias )
207 * (LONGLONG)600000000;
208 LL2FILETIME( llTime, &ftTemp)
209 FileTimeToSystemTime(lpFileTime, &SysTime);
212 if(year == SysTime.wYear) {
213 ret = TIME_DayLightCompareDate( &SysTime, &pTZinfo->DaylightDate);
214 if (ret == -2)
215 return TIME_ZONE_ID_INVALID;
217 afterDaylightDate = ret >= 0;
218 } else
219 afterDaylightDate = SysTime.wYear > year;
221 retval = TIME_ZONE_ID_STANDARD;
222 if( pTZinfo->DaylightDate.wMonth < pTZinfo->StandardDate.wMonth ) {
223 /* Northern hemisphere */
224 if( beforeStandardDate && afterDaylightDate )
225 retval = TIME_ZONE_ID_DAYLIGHT;
226 } else /* Down south */
227 if( beforeStandardDate || afterDaylightDate )
228 retval = TIME_ZONE_ID_DAYLIGHT;
229 } else
230 /* No transition date */
231 retval = TIME_ZONE_ID_UNKNOWN;
233 return retval;
236 /***********************************************************************
237 * TIME_TimeZoneID
239 * Calculates whether daylight savings is on now.
241 * PARAMS
242 * pTzi [in] Timezone info.
244 * RETURNS
245 * TIME_ZONE_ID_INVALID An error occurred
246 * TIME_ZONE_ID_UNKNOWN There are no transition time known
247 * TIME_ZONE_ID_STANDARD Current time is standard time
248 * TIME_ZONE_ID_DAYLIGHT Current time is daylight savings time
250 static DWORD TIME_ZoneID( const TIME_ZONE_INFORMATION *pTzi )
252 FILETIME ftTime;
253 GetSystemTimeAsFileTime( &ftTime);
254 return TIME_CompTimeZoneID( pTzi, &ftTime, FALSE);
257 /***********************************************************************
258 * TIME_GetTimezoneBias
260 * Calculates the local time bias for a given time zone.
262 * PARAMS
263 * pTZinfo [in] The time zone data.
264 * lpFileTime [in] The system or local time.
265 * islocal [in] It is local time.
266 * pBias [out] The calculated bias in minutes.
268 * RETURNS
269 * TRUE when the time zone bias was calculated.
271 static BOOL TIME_GetTimezoneBias( const TIME_ZONE_INFORMATION *pTZinfo,
272 FILETIME *lpFileTime, BOOL islocal, LONG *pBias )
274 LONG bias = pTZinfo->Bias;
275 DWORD tzid = TIME_CompTimeZoneID( pTZinfo, lpFileTime, islocal);
277 if( tzid == TIME_ZONE_ID_INVALID)
278 return FALSE;
279 if (tzid == TIME_ZONE_ID_DAYLIGHT)
280 bias += pTZinfo->DaylightBias;
281 else if (tzid == TIME_ZONE_ID_STANDARD)
282 bias += pTZinfo->StandardBias;
283 *pBias = bias;
284 return TRUE;
287 /***********************************************************************
288 * TIME_GetSpecificTimeZoneKey
290 * Opens the registry key for the time zone with the given name.
292 * PARAMS
293 * key_name [in] The time zone name.
294 * result [out] The open registry key handle.
296 * RETURNS
297 * TRUE if successful.
299 static BOOL TIME_GetSpecificTimeZoneKey( const WCHAR *key_name, HANDLE *result )
301 static const WCHAR Time_ZonesW[] = { '\\','R','E','G','I','S','T','R','Y','\\',
302 'M','a','c','h','i','n','e','\\',
303 'S','o','f','t','w','a','r','e','\\',
304 'M','i','c','r','o','s','o','f','t','\\',
305 'W','i','n','d','o','w','s',' ','N','T','\\',
306 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
307 'T','i','m','e',' ','Z','o','n','e','s',0 };
308 HANDLE time_zones_key;
309 OBJECT_ATTRIBUTES attr;
310 UNICODE_STRING nameW;
311 NTSTATUS status;
313 attr.Length = sizeof(attr);
314 attr.RootDirectory = 0;
315 attr.ObjectName = &nameW;
316 attr.Attributes = 0;
317 attr.SecurityDescriptor = NULL;
318 attr.SecurityQualityOfService = NULL;
319 RtlInitUnicodeString( &nameW, Time_ZonesW );
320 status = NtOpenKey( &time_zones_key, KEY_READ, &attr );
321 if (status)
323 WARN("Unable to open the time zones key\n");
324 SetLastError( RtlNtStatusToDosError(status) );
325 return FALSE;
328 attr.RootDirectory = time_zones_key;
329 RtlInitUnicodeString( &nameW, key_name );
330 status = NtOpenKey( result, KEY_READ, &attr );
332 NtClose( time_zones_key );
334 if (status)
336 SetLastError( RtlNtStatusToDosError(status) );
337 return FALSE;
340 return TRUE;
343 static BOOL reg_query_value(HKEY hkey, LPCWSTR name, DWORD type, void *data, DWORD count)
345 UNICODE_STRING nameW;
346 char buf[256];
347 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buf;
348 NTSTATUS status;
350 if (count > sizeof(buf) - sizeof(KEY_VALUE_PARTIAL_INFORMATION))
351 return FALSE;
353 RtlInitUnicodeString(&nameW, name);
355 if ((status = NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation,
356 buf, sizeof(buf), &count)))
358 SetLastError( RtlNtStatusToDosError(status) );
359 return FALSE;
362 if (info->Type != type)
364 SetLastError( ERROR_DATATYPE_MISMATCH );
365 return FALSE;
368 memcpy(data, info->Data, info->DataLength);
369 return TRUE;
372 static BOOL reg_load_mui_string(HKEY hkey, LPCWSTR value, LPWSTR buffer, DWORD size)
374 static const WCHAR advapi32W[] = {'a','d','v','a','p','i','3','2','.','d','l','l',0};
375 DWORD (WINAPI *pRegLoadMUIStringW)(HKEY, LPCWSTR, LPWSTR, DWORD, DWORD *, DWORD, LPCWSTR);
376 HMODULE hDll;
377 BOOL ret = FALSE;
379 hDll = LoadLibraryExW(advapi32W, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
380 if (hDll) {
381 pRegLoadMUIStringW = (void *)GetProcAddress(hDll, "RegLoadMUIStringW");
382 if (pRegLoadMUIStringW &&
383 !pRegLoadMUIStringW(hkey, value, buffer, size, NULL, 0, NULL))
384 ret = TRUE;
385 FreeLibrary(hDll);
387 return ret;
390 /***********************************************************************
391 * TIME_GetSpecificTimeZoneInfo
393 * Returns time zone information for the given time zone and year.
395 * PARAMS
396 * key_name [in] The time zone name.
397 * year [in] The year, if Dynamic DST is used.
398 * dynamic [in] Whether to use Dynamic DST.
399 * result [out] The time zone information.
401 * RETURNS
402 * TRUE if successful.
404 static BOOL TIME_GetSpecificTimeZoneInfo( const WCHAR *key_name, WORD year,
405 BOOL dynamic, DYNAMIC_TIME_ZONE_INFORMATION *tzinfo )
407 static const WCHAR Dynamic_DstW[] = { 'D','y','n','a','m','i','c',' ','D','S','T',0 };
408 static const WCHAR fmtW[] = { '%','d',0 };
409 static const WCHAR stdW[] = { 'S','t','d',0 };
410 static const WCHAR dltW[] = { 'D','l','t',0 };
411 static const WCHAR tziW[] = { 'T','Z','I',0 };
412 HANDLE time_zone_key, dynamic_dst_key;
413 OBJECT_ATTRIBUTES attr;
414 UNICODE_STRING nameW;
415 WCHAR yearW[16];
416 BOOL got_reg_data = FALSE;
417 struct tz_reg_data
419 LONG bias;
420 LONG std_bias;
421 LONG dlt_bias;
422 SYSTEMTIME std_date;
423 SYSTEMTIME dlt_date;
424 } tz_data;
426 if (!TIME_GetSpecificTimeZoneKey( key_name, &time_zone_key ))
427 return FALSE;
429 if (!reg_load_mui_string( time_zone_key, mui_stdW, tzinfo->StandardName, sizeof(tzinfo->StandardName) ) &&
430 !reg_query_value( time_zone_key, stdW, REG_SZ, tzinfo->StandardName, sizeof(tzinfo->StandardName) ))
432 NtClose( time_zone_key );
433 return FALSE;
436 if (!reg_load_mui_string( time_zone_key, mui_dltW, tzinfo->DaylightName, sizeof(tzinfo->DaylightName) ) &&
437 !reg_query_value( time_zone_key, dltW, REG_SZ, tzinfo->DaylightName, sizeof(tzinfo->DaylightName) ))
439 NtClose( time_zone_key );
440 return FALSE;
443 lstrcpyW(tzinfo->TimeZoneKeyName, key_name);
445 if (dynamic)
447 attr.Length = sizeof(attr);
448 attr.RootDirectory = time_zone_key;
449 attr.ObjectName = &nameW;
450 attr.Attributes = 0;
451 attr.SecurityDescriptor = NULL;
452 attr.SecurityQualityOfService = NULL;
453 RtlInitUnicodeString( &nameW, Dynamic_DstW );
454 if (!NtOpenKey( &dynamic_dst_key, KEY_READ, &attr ))
456 sprintfW( yearW, fmtW, year );
457 got_reg_data = reg_query_value( dynamic_dst_key, yearW, REG_BINARY, &tz_data, sizeof(tz_data) );
459 NtClose( dynamic_dst_key );
463 if (!got_reg_data)
465 if (!reg_query_value( time_zone_key, tziW, REG_BINARY, &tz_data, sizeof(tz_data) ))
467 NtClose( time_zone_key );
468 return FALSE;
472 tzinfo->Bias = tz_data.bias;
473 tzinfo->StandardBias = tz_data.std_bias;
474 tzinfo->DaylightBias = tz_data.dlt_bias;
475 tzinfo->StandardDate = tz_data.std_date;
476 tzinfo->DaylightDate = tz_data.dlt_date;
478 tzinfo->DynamicDaylightTimeDisabled = !dynamic;
480 NtClose( time_zone_key );
482 return TRUE;
486 /***********************************************************************
487 * SetLocalTime (KERNEL32.@)
489 * Set the local time using current time zone and daylight
490 * savings settings.
492 * PARAMS
493 * systime [in] The desired local time.
495 * RETURNS
496 * Success: TRUE. The time was set.
497 * Failure: FALSE, if the time was invalid or caller does not have
498 * permission to change the time.
500 BOOL WINAPI SetLocalTime( const SYSTEMTIME *systime )
502 FILETIME ft;
503 LARGE_INTEGER st, st2;
504 NTSTATUS status;
506 if( !SystemTimeToFileTime( systime, &ft ))
507 return FALSE;
508 st.u.LowPart = ft.dwLowDateTime;
509 st.u.HighPart = ft.dwHighDateTime;
510 RtlLocalTimeToSystemTime( &st, &st2 );
512 if ((status = NtSetSystemTime(&st2, NULL)))
513 SetLastError( RtlNtStatusToDosError(status) );
514 return !status;
518 /***********************************************************************
519 * GetSystemTimeAdjustment (KERNEL32.@)
521 * Get the period between clock interrupts and the amount the clock
522 * is adjusted each interrupt so as to keep it in sync with an external source.
524 * PARAMS
525 * lpTimeAdjustment [out] The clock adjustment per interrupt in 100's of nanoseconds.
526 * lpTimeIncrement [out] The time between clock interrupts in 100's of nanoseconds.
527 * lpTimeAdjustmentDisabled [out] The clock synchronisation has been disabled.
529 * RETURNS
530 * TRUE.
532 * BUGS
533 * Only the special case of disabled time adjustments is supported.
535 BOOL WINAPI GetSystemTimeAdjustment( PDWORD lpTimeAdjustment, PDWORD lpTimeIncrement,
536 PBOOL lpTimeAdjustmentDisabled )
538 *lpTimeAdjustment = 0;
539 *lpTimeIncrement = 10000000 / sysconf(_SC_CLK_TCK);
540 *lpTimeAdjustmentDisabled = TRUE;
541 return TRUE;
545 /***********************************************************************
546 * SetSystemTime (KERNEL32.@)
548 * Set the system time in utc.
550 * PARAMS
551 * systime [in] The desired system time.
553 * RETURNS
554 * Success: TRUE. The time was set.
555 * Failure: FALSE, if the time was invalid or caller does not have
556 * permission to change the time.
558 BOOL WINAPI SetSystemTime( const SYSTEMTIME *systime )
560 FILETIME ft;
561 LARGE_INTEGER t;
562 NTSTATUS status;
564 if( !SystemTimeToFileTime( systime, &ft ))
565 return FALSE;
566 t.u.LowPart = ft.dwLowDateTime;
567 t.u.HighPart = ft.dwHighDateTime;
568 if ((status = NtSetSystemTime(&t, NULL)))
569 SetLastError( RtlNtStatusToDosError(status) );
570 return !status;
573 /***********************************************************************
574 * SetSystemTimeAdjustment (KERNEL32.@)
576 * Enables or disables the timing adjustments to the system's clock.
578 * PARAMS
579 * dwTimeAdjustment [in] Number of units to add per clock interrupt.
580 * bTimeAdjustmentDisabled [in] Adjustment mode.
582 * RETURNS
583 * Success: TRUE.
584 * Failure: FALSE.
586 BOOL WINAPI SetSystemTimeAdjustment( DWORD dwTimeAdjustment, BOOL bTimeAdjustmentDisabled )
588 /* Fake function for now... */
589 FIXME("(%08x,%d): stub !\n", dwTimeAdjustment, bTimeAdjustmentDisabled);
590 return TRUE;
593 /***********************************************************************
594 * GetTimeZoneInformation (KERNEL32.@)
596 * Get information about the current local time zone.
598 * PARAMS
599 * tzinfo [out] Destination for time zone information.
601 * RETURNS
602 * TIME_ZONE_ID_INVALID An error occurred
603 * TIME_ZONE_ID_UNKNOWN There are no transition time known
604 * TIME_ZONE_ID_STANDARD Current time is standard time
605 * TIME_ZONE_ID_DAYLIGHT Current time is daylight savings time
607 DWORD WINAPI GetTimeZoneInformation( LPTIME_ZONE_INFORMATION ret )
609 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
610 DWORD time_zone_id;
612 time_zone_id = GetDynamicTimeZoneInformation( &tzinfo );
613 memcpy( ret, &tzinfo, sizeof(*ret) );
614 return time_zone_id;
617 /***********************************************************************
618 * GetTimeZoneInformationForYear (KERNEL32.@)
620 BOOL WINAPI GetTimeZoneInformationForYear( USHORT wYear,
621 PDYNAMIC_TIME_ZONE_INFORMATION pdtzi, LPTIME_ZONE_INFORMATION ptzi )
623 DYNAMIC_TIME_ZONE_INFORMATION local_dtzi, result;
625 if (!pdtzi)
627 if (GetDynamicTimeZoneInformation(&local_dtzi) == TIME_ZONE_ID_INVALID)
628 return FALSE;
629 pdtzi = &local_dtzi;
632 if (!TIME_GetSpecificTimeZoneInfo(pdtzi->TimeZoneKeyName, wYear,
633 !pdtzi->DynamicDaylightTimeDisabled, &result))
634 return FALSE;
636 memcpy(ptzi, &result, sizeof(*ptzi));
638 return TRUE;
641 /***********************************************************************
642 * SetTimeZoneInformation (KERNEL32.@)
644 * Change the settings of the current local time zone.
646 * PARAMS
647 * tzinfo [in] The new time zone.
649 * RETURNS
650 * Success: TRUE. The time zone was updated with the settings from tzinfo.
651 * Failure: FALSE.
653 BOOL WINAPI SetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
655 NTSTATUS status;
656 status = RtlSetTimeZoneInformation( (const RTL_TIME_ZONE_INFORMATION *)tzinfo );
657 if ( status != STATUS_SUCCESS )
658 SetLastError( RtlNtStatusToDosError(status) );
659 return !status;
662 /***********************************************************************
663 * SystemTimeToTzSpecificLocalTime (KERNEL32.@)
665 * Convert a utc system time to a local time in a given time zone.
667 * PARAMS
668 * lpTimeZoneInformation [in] The desired time zone.
669 * lpUniversalTime [in] The utc time to base local time on.
670 * lpLocalTime [out] The local time in the time zone.
672 * RETURNS
673 * Success: TRUE. lpLocalTime contains the converted time
674 * Failure: FALSE.
677 BOOL WINAPI SystemTimeToTzSpecificLocalTime(
678 const TIME_ZONE_INFORMATION *lpTimeZoneInformation,
679 const SYSTEMTIME *lpUniversalTime, LPSYSTEMTIME lpLocalTime )
681 FILETIME ft;
682 LONG lBias;
683 LONGLONG llTime;
684 TIME_ZONE_INFORMATION tzinfo;
686 if (lpTimeZoneInformation != NULL)
688 tzinfo = *lpTimeZoneInformation;
690 else
692 RtlQueryTimeZoneInformation((RTL_TIME_ZONE_INFORMATION *)&tzinfo);
695 if (!SystemTimeToFileTime(lpUniversalTime, &ft))
696 return FALSE;
697 FILETIME2LL( &ft, llTime)
698 if (!TIME_GetTimezoneBias(&tzinfo, &ft, FALSE, &lBias))
699 return FALSE;
700 /* convert minutes to 100-nanoseconds-ticks */
701 llTime -= (LONGLONG)lBias * 600000000;
702 LL2FILETIME( llTime, &ft)
704 return FileTimeToSystemTime(&ft, lpLocalTime);
708 /***********************************************************************
709 * TzSpecificLocalTimeToSystemTime (KERNEL32.@)
711 * Converts a local time to a time in utc.
713 * PARAMS
714 * lpTimeZoneInformation [in] The desired time zone.
715 * lpLocalTime [in] The local time.
716 * lpUniversalTime [out] The calculated utc time.
718 * RETURNS
719 * Success: TRUE. lpUniversalTime contains the converted time.
720 * Failure: FALSE.
722 BOOL WINAPI TzSpecificLocalTimeToSystemTime(
723 const TIME_ZONE_INFORMATION *lpTimeZoneInformation,
724 const SYSTEMTIME *lpLocalTime, LPSYSTEMTIME lpUniversalTime)
726 FILETIME ft;
727 LONG lBias;
728 LONGLONG t;
729 TIME_ZONE_INFORMATION tzinfo;
731 if (lpTimeZoneInformation != NULL)
733 tzinfo = *lpTimeZoneInformation;
735 else
737 RtlQueryTimeZoneInformation((RTL_TIME_ZONE_INFORMATION *)&tzinfo);
740 if (!SystemTimeToFileTime(lpLocalTime, &ft))
741 return FALSE;
742 FILETIME2LL( &ft, t)
743 if (!TIME_GetTimezoneBias(&tzinfo, &ft, TRUE, &lBias))
744 return FALSE;
745 /* convert minutes to 100-nanoseconds-ticks */
746 t += (LONGLONG)lBias * 600000000;
747 LL2FILETIME( t, &ft)
748 return FileTimeToSystemTime(&ft, lpUniversalTime);
752 /***********************************************************************
753 * GetSystemTimeAsFileTime (KERNEL32.@)
755 * Get the current time in utc format.
757 * RETURNS
758 * Nothing.
760 VOID WINAPI GetSystemTimeAsFileTime(
761 LPFILETIME time) /* [out] Destination for the current utc time */
763 LARGE_INTEGER t;
764 NtQuerySystemTime( &t );
765 time->dwLowDateTime = t.u.LowPart;
766 time->dwHighDateTime = t.u.HighPart;
770 /***********************************************************************
771 * GetSystemTimePreciseAsFileTime (KERNEL32.@)
773 * Get the current time in utc format, with <1 us precision.
775 * RETURNS
776 * Nothing.
778 VOID WINAPI GetSystemTimePreciseAsFileTime(
779 LPFILETIME time) /* [out] Destination for the current utc time */
781 GetSystemTimeAsFileTime(time);
785 /*********************************************************************
786 * TIME_ClockTimeToFileTime (olorin@fandra.org, 20-Sep-1998)
788 * Used by GetProcessTimes to convert clock_t into FILETIME.
790 * Differences to UnixTimeToFileTime:
791 * 1) Divided by CLK_TCK
792 * 2) Time is relative. There is no 'starting date', so there is
793 * no need for offset correction, like in UnixTimeToFileTime
795 static void TIME_ClockTimeToFileTime(clock_t unix_time, LPFILETIME filetime)
797 long clocksPerSec = sysconf(_SC_CLK_TCK);
798 ULONGLONG secs = (ULONGLONG)unix_time * 10000000 / clocksPerSec;
799 filetime->dwLowDateTime = (DWORD)secs;
800 filetime->dwHighDateTime = (DWORD)(secs >> 32);
803 /***********************************************************************
804 * TIMEZONE_InitRegistry
806 * Update registry contents on startup if the user timezone has changed.
807 * This simulates the action of the Windows control panel.
809 void TIMEZONE_InitRegistry(void)
811 static const WCHAR timezoneInformationW[] = {
812 'M','a','c','h','i','n','e','\\','S','y','s','t','e','m','\\',
813 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
814 'C','o','n','t','r','o','l','\\',
815 'T','i','m','e','Z','o','n','e','I','n','f','o','r','m','a','t','i','o','n','\0'
817 static const WCHAR standardNameW[] = {'S','t','a','n','d','a','r','d','N','a','m','e','\0'};
818 static const WCHAR timezoneKeyNameW[] = {'T','i','m','e','Z','o','n','e','K','e','y','N','a','m','e','\0'};
819 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
820 UNICODE_STRING name;
821 OBJECT_ATTRIBUTES attr;
822 HANDLE hkey;
823 DWORD tzid;
825 tzid = GetDynamicTimeZoneInformation(&tzinfo);
826 if (tzid == TIME_ZONE_ID_INVALID) return;
828 RtlInitUnicodeString(&name, timezoneInformationW);
829 InitializeObjectAttributes(&attr, &name, 0, 0, NULL);
830 if (NtCreateKey(&hkey, KEY_ALL_ACCESS, &attr, 0, NULL, 0, NULL) != STATUS_SUCCESS) return;
832 RtlInitUnicodeString(&name, standardNameW);
833 NtSetValueKey(hkey, &name, 0, REG_SZ, tzinfo.StandardName,
834 (strlenW(tzinfo.StandardName) + 1) * sizeof(WCHAR));
836 RtlInitUnicodeString(&name, timezoneKeyNameW);
837 NtSetValueKey(hkey, &name, 0, REG_SZ, tzinfo.TimeZoneKeyName,
838 (strlenW(tzinfo.TimeZoneKeyName) + 1) * sizeof(WCHAR));
840 NtClose( hkey );
843 /*********************************************************************
844 * GetProcessTimes (KERNEL32.@)
846 * Get the user and kernel execution times of a process,
847 * along with the creation and exit times if known.
849 * PARAMS
850 * hprocess [in] The process to be queried.
851 * lpCreationTime [out] The creation time of the process.
852 * lpExitTime [out] The exit time of the process if exited.
853 * lpKernelTime [out] The time spent in kernel routines in 100's of nanoseconds.
854 * lpUserTime [out] The time spent in user routines in 100's of nanoseconds.
856 * RETURNS
857 * TRUE.
859 * NOTES
860 * olorin@fandra.org:
861 * Would be nice to subtract the cpu time used by Wine at startup.
862 * Also, there is a need to separate times used by different applications.
864 * BUGS
865 * KernelTime and UserTime are always for the current process
867 BOOL WINAPI GetProcessTimes( HANDLE hprocess, LPFILETIME lpCreationTime,
868 LPFILETIME lpExitTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime )
870 struct tms tms;
871 KERNEL_USER_TIMES pti;
873 times(&tms);
874 TIME_ClockTimeToFileTime(tms.tms_utime,lpUserTime);
875 TIME_ClockTimeToFileTime(tms.tms_stime,lpKernelTime);
876 if (NtQueryInformationProcess( hprocess, ProcessTimes, &pti, sizeof(pti), NULL))
877 return FALSE;
878 LL2FILETIME( pti.CreateTime.QuadPart, lpCreationTime);
879 LL2FILETIME( pti.ExitTime.QuadPart, lpExitTime);
880 return TRUE;
883 /*********************************************************************
884 * GetCalendarInfoA (KERNEL32.@)
887 int WINAPI GetCalendarInfoA(LCID lcid, CALID Calendar, CALTYPE CalType,
888 LPSTR lpCalData, int cchData, LPDWORD lpValue)
890 int ret, cchDataW = cchData;
891 LPWSTR lpCalDataW = NULL;
893 if (NLS_IsUnicodeOnlyLcid(lcid))
895 SetLastError(ERROR_INVALID_PARAMETER);
896 return 0;
899 if (!cchData && !(CalType & CAL_RETURN_NUMBER))
900 cchDataW = GetCalendarInfoW(lcid, Calendar, CalType, NULL, 0, NULL);
901 if (!(lpCalDataW = HeapAlloc(GetProcessHeap(), 0, cchDataW*sizeof(WCHAR))))
902 return 0;
904 ret = GetCalendarInfoW(lcid, Calendar, CalType, lpCalDataW, cchDataW, lpValue);
905 if(ret && lpCalDataW && lpCalData)
906 ret = WideCharToMultiByte(CP_ACP, 0, lpCalDataW, -1, lpCalData, cchData, NULL, NULL);
907 else if (CalType & CAL_RETURN_NUMBER)
908 ret *= sizeof(WCHAR);
909 HeapFree(GetProcessHeap(), 0, lpCalDataW);
911 return ret;
914 /*********************************************************************
915 * GetCalendarInfoW (KERNEL32.@)
918 int WINAPI GetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType,
919 LPWSTR lpCalData, int cchData, LPDWORD lpValue)
921 static const LCTYPE caltype_lctype_map[] = {
922 0, /* not used */
923 0, /* CAL_ICALINTVALUE */
924 0, /* CAL_SCALNAME */
925 0, /* CAL_IYEAROFFSETRANGE */
926 0, /* CAL_SERASTRING */
927 LOCALE_SSHORTDATE,
928 LOCALE_SLONGDATE,
929 LOCALE_SDAYNAME1,
930 LOCALE_SDAYNAME2,
931 LOCALE_SDAYNAME3,
932 LOCALE_SDAYNAME4,
933 LOCALE_SDAYNAME5,
934 LOCALE_SDAYNAME6,
935 LOCALE_SDAYNAME7,
936 LOCALE_SABBREVDAYNAME1,
937 LOCALE_SABBREVDAYNAME2,
938 LOCALE_SABBREVDAYNAME3,
939 LOCALE_SABBREVDAYNAME4,
940 LOCALE_SABBREVDAYNAME5,
941 LOCALE_SABBREVDAYNAME6,
942 LOCALE_SABBREVDAYNAME7,
943 LOCALE_SMONTHNAME1,
944 LOCALE_SMONTHNAME2,
945 LOCALE_SMONTHNAME3,
946 LOCALE_SMONTHNAME4,
947 LOCALE_SMONTHNAME5,
948 LOCALE_SMONTHNAME6,
949 LOCALE_SMONTHNAME7,
950 LOCALE_SMONTHNAME8,
951 LOCALE_SMONTHNAME9,
952 LOCALE_SMONTHNAME10,
953 LOCALE_SMONTHNAME11,
954 LOCALE_SMONTHNAME12,
955 LOCALE_SMONTHNAME13,
956 LOCALE_SABBREVMONTHNAME1,
957 LOCALE_SABBREVMONTHNAME2,
958 LOCALE_SABBREVMONTHNAME3,
959 LOCALE_SABBREVMONTHNAME4,
960 LOCALE_SABBREVMONTHNAME5,
961 LOCALE_SABBREVMONTHNAME6,
962 LOCALE_SABBREVMONTHNAME7,
963 LOCALE_SABBREVMONTHNAME8,
964 LOCALE_SABBREVMONTHNAME9,
965 LOCALE_SABBREVMONTHNAME10,
966 LOCALE_SABBREVMONTHNAME11,
967 LOCALE_SABBREVMONTHNAME12,
968 LOCALE_SABBREVMONTHNAME13,
969 LOCALE_SYEARMONTH,
970 0, /* CAL_ITWODIGITYEARMAX */
971 LOCALE_SSHORTESTDAYNAME1,
972 LOCALE_SSHORTESTDAYNAME2,
973 LOCALE_SSHORTESTDAYNAME3,
974 LOCALE_SSHORTESTDAYNAME4,
975 LOCALE_SSHORTESTDAYNAME5,
976 LOCALE_SSHORTESTDAYNAME6,
977 LOCALE_SSHORTESTDAYNAME7,
978 LOCALE_SMONTHDAY,
979 0, /* CAL_SABBREVERASTRING */
981 DWORD localeflags = 0;
982 CALTYPE calinfo;
984 if (CalType & CAL_NOUSEROVERRIDE)
985 FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
986 if (CalType & CAL_USE_CP_ACP)
987 FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
989 if (CalType & CAL_RETURN_NUMBER) {
990 if (!lpValue)
992 SetLastError( ERROR_INVALID_PARAMETER );
993 return 0;
995 if (lpCalData != NULL)
996 WARN("lpCalData not NULL (%p) when it should!\n", lpCalData);
997 if (cchData != 0)
998 WARN("cchData not 0 (%d) when it should!\n", cchData);
999 } else {
1000 if (lpValue != NULL)
1001 WARN("lpValue not NULL (%p) when it should!\n", lpValue);
1004 /* FIXME: No verification is made yet wrt Locale
1005 * for the CALTYPES not requiring GetLocaleInfoA */
1007 calinfo = CalType & 0xffff;
1009 if (CalType & CAL_RETURN_GENITIVE_NAMES)
1010 localeflags |= LOCALE_RETURN_GENITIVE_NAMES;
1012 switch (calinfo) {
1013 case CAL_ICALINTVALUE:
1014 if (CalType & CAL_RETURN_NUMBER)
1015 return GetLocaleInfoW(Locale, LOCALE_RETURN_NUMBER | LOCALE_ICALENDARTYPE,
1016 (LPWSTR)lpValue, 2);
1017 return GetLocaleInfoW(Locale, LOCALE_ICALENDARTYPE, lpCalData, cchData);
1018 case CAL_SCALNAME:
1019 FIXME("Unimplemented caltype %d\n", calinfo);
1020 if (lpCalData) *lpCalData = 0;
1021 return 1;
1022 case CAL_IYEAROFFSETRANGE:
1023 FIXME("Unimplemented caltype %d\n", calinfo);
1024 return 0;
1025 case CAL_SERASTRING:
1026 FIXME("Unimplemented caltype %d\n", calinfo);
1027 return 0;
1028 case CAL_SSHORTDATE:
1029 case CAL_SLONGDATE:
1030 case CAL_SDAYNAME1:
1031 case CAL_SDAYNAME2:
1032 case CAL_SDAYNAME3:
1033 case CAL_SDAYNAME4:
1034 case CAL_SDAYNAME5:
1035 case CAL_SDAYNAME6:
1036 case CAL_SDAYNAME7:
1037 case CAL_SABBREVDAYNAME1:
1038 case CAL_SABBREVDAYNAME2:
1039 case CAL_SABBREVDAYNAME3:
1040 case CAL_SABBREVDAYNAME4:
1041 case CAL_SABBREVDAYNAME5:
1042 case CAL_SABBREVDAYNAME6:
1043 case CAL_SABBREVDAYNAME7:
1044 case CAL_SMONTHNAME1:
1045 case CAL_SMONTHNAME2:
1046 case CAL_SMONTHNAME3:
1047 case CAL_SMONTHNAME4:
1048 case CAL_SMONTHNAME5:
1049 case CAL_SMONTHNAME6:
1050 case CAL_SMONTHNAME7:
1051 case CAL_SMONTHNAME8:
1052 case CAL_SMONTHNAME9:
1053 case CAL_SMONTHNAME10:
1054 case CAL_SMONTHNAME11:
1055 case CAL_SMONTHNAME12:
1056 case CAL_SMONTHNAME13:
1057 case CAL_SABBREVMONTHNAME1:
1058 case CAL_SABBREVMONTHNAME2:
1059 case CAL_SABBREVMONTHNAME3:
1060 case CAL_SABBREVMONTHNAME4:
1061 case CAL_SABBREVMONTHNAME5:
1062 case CAL_SABBREVMONTHNAME6:
1063 case CAL_SABBREVMONTHNAME7:
1064 case CAL_SABBREVMONTHNAME8:
1065 case CAL_SABBREVMONTHNAME9:
1066 case CAL_SABBREVMONTHNAME10:
1067 case CAL_SABBREVMONTHNAME11:
1068 case CAL_SABBREVMONTHNAME12:
1069 case CAL_SABBREVMONTHNAME13:
1070 case CAL_SYEARMONTH:
1071 return GetLocaleInfoW(Locale, caltype_lctype_map[calinfo] | localeflags, lpCalData, cchData);
1072 case CAL_ITWODIGITYEARMAX:
1073 if (CalType & CAL_RETURN_NUMBER)
1075 *lpValue = CALINFO_MAX_YEAR;
1076 return sizeof(DWORD) / sizeof(WCHAR);
1078 else
1080 static const WCHAR fmtW[] = {'%','u',0};
1081 WCHAR buffer[10];
1082 int ret = snprintfW( buffer, 10, fmtW, CALINFO_MAX_YEAR ) + 1;
1083 if (!lpCalData) return ret;
1084 if (ret <= cchData)
1086 strcpyW( lpCalData, buffer );
1087 return ret;
1089 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1090 return 0;
1092 break;
1093 default:
1094 FIXME("Unknown caltype %d\n", calinfo);
1095 SetLastError(ERROR_INVALID_FLAGS);
1096 return 0;
1098 return 0;
1101 /*********************************************************************
1102 * GetCalendarInfoEx (KERNEL32.@)
1104 int WINAPI GetCalendarInfoEx(LPCWSTR locale, CALID calendar, LPCWSTR lpReserved, CALTYPE caltype,
1105 LPWSTR data, int len, DWORD *value)
1107 static int once;
1109 LCID lcid = LocaleNameToLCID(locale, 0);
1110 if (!once++)
1111 FIXME("(%s, %d, %p, 0x%08x, %p, %d, %p): semi-stub\n", debugstr_w(locale), calendar, lpReserved, caltype,
1112 data, len, value);
1113 return GetCalendarInfoW(lcid, calendar, caltype, data, len, value);
1116 /*********************************************************************
1117 * SetCalendarInfoA (KERNEL32.@)
1120 int WINAPI SetCalendarInfoA(LCID Locale, CALID Calendar, CALTYPE CalType, LPCSTR lpCalData)
1122 FIXME("(%08x,%08x,%08x,%s): stub\n",
1123 Locale, Calendar, CalType, debugstr_a(lpCalData));
1124 return 0;
1127 /*********************************************************************
1128 * SetCalendarInfoW (KERNEL32.@)
1132 int WINAPI SetCalendarInfoW(LCID Locale, CALID Calendar, CALTYPE CalType, LPCWSTR lpCalData)
1134 FIXME("(%08x,%08x,%08x,%s): stub\n",
1135 Locale, Calendar, CalType, debugstr_w(lpCalData));
1136 return 0;
1139 /*********************************************************************
1140 * LocalFileTimeToFileTime (KERNEL32.@)
1142 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, LPFILETIME utcft )
1144 NTSTATUS status;
1145 LARGE_INTEGER local, utc;
1147 local.u.LowPart = localft->dwLowDateTime;
1148 local.u.HighPart = localft->dwHighDateTime;
1149 if (!(status = RtlLocalTimeToSystemTime( &local, &utc )))
1151 utcft->dwLowDateTime = utc.u.LowPart;
1152 utcft->dwHighDateTime = utc.u.HighPart;
1154 else SetLastError( RtlNtStatusToDosError(status) );
1156 return !status;
1159 /*********************************************************************
1160 * FileTimeToLocalFileTime (KERNEL32.@)
1162 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, LPFILETIME localft )
1164 NTSTATUS status;
1165 LARGE_INTEGER local, utc;
1167 utc.u.LowPart = utcft->dwLowDateTime;
1168 utc.u.HighPart = utcft->dwHighDateTime;
1169 if (!(status = RtlSystemTimeToLocalTime( &utc, &local )))
1171 localft->dwLowDateTime = local.u.LowPart;
1172 localft->dwHighDateTime = local.u.HighPart;
1174 else SetLastError( RtlNtStatusToDosError(status) );
1176 return !status;
1179 /*********************************************************************
1180 * FileTimeToSystemTime (KERNEL32.@)
1182 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, LPSYSTEMTIME syst )
1184 TIME_FIELDS tf;
1185 LARGE_INTEGER t;
1187 t.u.LowPart = ft->dwLowDateTime;
1188 t.u.HighPart = ft->dwHighDateTime;
1189 RtlTimeToTimeFields(&t, &tf);
1191 syst->wYear = tf.Year;
1192 syst->wMonth = tf.Month;
1193 syst->wDay = tf.Day;
1194 syst->wHour = tf.Hour;
1195 syst->wMinute = tf.Minute;
1196 syst->wSecond = tf.Second;
1197 syst->wMilliseconds = tf.Milliseconds;
1198 syst->wDayOfWeek = tf.Weekday;
1199 return TRUE;
1202 /*********************************************************************
1203 * SystemTimeToFileTime (KERNEL32.@)
1205 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, LPFILETIME ft )
1207 TIME_FIELDS tf;
1208 LARGE_INTEGER t;
1210 tf.Year = syst->wYear;
1211 tf.Month = syst->wMonth;
1212 tf.Day = syst->wDay;
1213 tf.Hour = syst->wHour;
1214 tf.Minute = syst->wMinute;
1215 tf.Second = syst->wSecond;
1216 tf.Milliseconds = syst->wMilliseconds;
1218 if( !RtlTimeFieldsToTime(&tf, &t)) {
1219 SetLastError( ERROR_INVALID_PARAMETER);
1220 return FALSE;
1222 ft->dwLowDateTime = t.u.LowPart;
1223 ft->dwHighDateTime = t.u.HighPart;
1224 return TRUE;
1227 /*********************************************************************
1228 * CompareFileTime (KERNEL32.@)
1230 * Compare two FILETIME's to each other.
1232 * PARAMS
1233 * x [I] First time
1234 * y [I] time to compare to x
1236 * RETURNS
1237 * -1, 0, or 1 indicating that x is less than, equal to, or greater
1238 * than y respectively.
1240 INT WINAPI CompareFileTime( const FILETIME *x, const FILETIME *y )
1242 if (!x || !y) return -1;
1244 if (x->dwHighDateTime > y->dwHighDateTime)
1245 return 1;
1246 if (x->dwHighDateTime < y->dwHighDateTime)
1247 return -1;
1248 if (x->dwLowDateTime > y->dwLowDateTime)
1249 return 1;
1250 if (x->dwLowDateTime < y->dwLowDateTime)
1251 return -1;
1252 return 0;
1255 /*********************************************************************
1256 * GetLocalTime (KERNEL32.@)
1258 * Get the current local time.
1260 * PARAMS
1261 * systime [O] Destination for current time.
1263 * RETURNS
1264 * Nothing.
1266 VOID WINAPI GetLocalTime(LPSYSTEMTIME systime)
1268 FILETIME lft;
1269 LARGE_INTEGER ft, ft2;
1271 NtQuerySystemTime(&ft);
1272 RtlSystemTimeToLocalTime(&ft, &ft2);
1273 lft.dwLowDateTime = ft2.u.LowPart;
1274 lft.dwHighDateTime = ft2.u.HighPart;
1275 FileTimeToSystemTime(&lft, systime);
1278 /*********************************************************************
1279 * GetSystemTime (KERNEL32.@)
1281 * Get the current system time.
1283 * PARAMS
1284 * systime [O] Destination for current time.
1286 * RETURNS
1287 * Nothing.
1289 VOID WINAPI GetSystemTime(LPSYSTEMTIME systime)
1291 FILETIME ft;
1292 LARGE_INTEGER t;
1294 NtQuerySystemTime(&t);
1295 ft.dwLowDateTime = t.u.LowPart;
1296 ft.dwHighDateTime = t.u.HighPart;
1297 FileTimeToSystemTime(&ft, systime);
1300 /*********************************************************************
1301 * GetDaylightFlag (KERNEL32.@)
1303 * Specifies if daylight savings time is in operation.
1305 * NOTES
1306 * This function is called from the Win98's control applet timedate.cpl.
1308 * RETURNS
1309 * TRUE if daylight savings time is in operation.
1310 * FALSE otherwise.
1312 BOOL WINAPI GetDaylightFlag(void)
1314 TIME_ZONE_INFORMATION tzinfo;
1315 RtlQueryTimeZoneInformation((RTL_TIME_ZONE_INFORMATION *)&tzinfo);
1316 return (TIME_ZoneID(&tzinfo) == TIME_ZONE_ID_DAYLIGHT);
1319 /***********************************************************************
1320 * DosDateTimeToFileTime (KERNEL32.@)
1322 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, LPFILETIME ft)
1324 struct tm newtm;
1325 #ifndef HAVE_TIMEGM
1326 struct tm *gtm;
1327 time_t time1, time2;
1328 #endif
1330 newtm.tm_sec = (fattime & 0x1f) * 2;
1331 newtm.tm_min = (fattime >> 5) & 0x3f;
1332 newtm.tm_hour = (fattime >> 11);
1333 newtm.tm_mday = (fatdate & 0x1f);
1334 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
1335 newtm.tm_year = (fatdate >> 9) + 80;
1336 newtm.tm_isdst = -1;
1337 #ifdef HAVE_TIMEGM
1338 RtlSecondsSince1970ToTime( timegm(&newtm), (LARGE_INTEGER *)ft );
1339 #else
1340 time1 = mktime(&newtm);
1341 gtm = gmtime(&time1);
1342 time2 = mktime(gtm);
1343 RtlSecondsSince1970ToTime( 2*time1-time2, (LARGE_INTEGER *)ft );
1344 #endif
1345 return TRUE;
1349 /***********************************************************************
1350 * FileTimeToDosDateTime (KERNEL32.@)
1352 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, LPWORD fatdate,
1353 LPWORD fattime )
1355 LARGE_INTEGER li;
1356 ULONG t;
1357 time_t unixtime;
1358 struct tm* tm;
1360 if (!fatdate || !fattime)
1362 SetLastError(ERROR_INVALID_PARAMETER);
1363 return FALSE;
1365 li.u.LowPart = ft->dwLowDateTime;
1366 li.u.HighPart = ft->dwHighDateTime;
1367 if (!RtlTimeToSecondsSince1970( &li, &t ))
1369 SetLastError(ERROR_INVALID_PARAMETER);
1370 return FALSE;
1372 unixtime = t;
1373 tm = gmtime( &unixtime );
1374 *fattime = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
1375 *fatdate = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday;
1376 return TRUE;
1379 /*********************************************************************
1380 * GetSystemTimes (KERNEL32.@)
1382 * Retrieves system timing information
1384 * PARAMS
1385 * lpIdleTime [O] Destination for idle time.
1386 * lpKernelTime [O] Destination for kernel time.
1387 * lpUserTime [O] Destination for user time.
1389 * RETURNS
1390 * TRUE if success, FALSE otherwise.
1392 BOOL WINAPI GetSystemTimes(LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime)
1394 LARGE_INTEGER idle_time, kernel_time, user_time;
1395 SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *sppi;
1396 SYSTEM_BASIC_INFORMATION sbi;
1397 NTSTATUS status;
1398 ULONG ret_size;
1399 int i;
1401 TRACE("(%p,%p,%p)\n", lpIdleTime, lpKernelTime, lpUserTime);
1403 status = NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), &ret_size );
1404 if (status != STATUS_SUCCESS)
1406 SetLastError( RtlNtStatusToDosError(status) );
1407 return FALSE;
1410 sppi = HeapAlloc( GetProcessHeap(), 0,
1411 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * sbi.NumberOfProcessors);
1412 if (!sppi)
1414 SetLastError( ERROR_OUTOFMEMORY );
1415 return FALSE;
1418 status = NtQuerySystemInformation( SystemProcessorPerformanceInformation, sppi, sizeof(*sppi) * sbi.NumberOfProcessors,
1419 &ret_size );
1420 if (status != STATUS_SUCCESS)
1422 HeapFree( GetProcessHeap(), 0, sppi );
1423 SetLastError( RtlNtStatusToDosError(status) );
1424 return FALSE;
1427 idle_time.QuadPart = 0;
1428 kernel_time.QuadPart = 0;
1429 user_time.QuadPart = 0;
1430 for (i = 0; i < sbi.NumberOfProcessors; i++)
1432 idle_time.QuadPart += sppi[i].IdleTime.QuadPart;
1433 kernel_time.QuadPart += sppi[i].KernelTime.QuadPart;
1434 user_time.QuadPart += sppi[i].UserTime.QuadPart;
1437 if (lpIdleTime)
1439 lpIdleTime->dwLowDateTime = idle_time.u.LowPart;
1440 lpIdleTime->dwHighDateTime = idle_time.u.HighPart;
1442 if (lpKernelTime)
1444 lpKernelTime->dwLowDateTime = kernel_time.u.LowPart;
1445 lpKernelTime->dwHighDateTime = kernel_time.u.HighPart;
1447 if (lpUserTime)
1449 lpUserTime->dwLowDateTime = user_time.u.LowPart;
1450 lpUserTime->dwHighDateTime = user_time.u.HighPart;
1453 HeapFree( GetProcessHeap(), 0, sppi );
1454 return TRUE;
1457 /***********************************************************************
1458 * GetDynamicTimeZoneInformation (KERNEL32.@)
1460 DWORD WINAPI GetDynamicTimeZoneInformation(DYNAMIC_TIME_ZONE_INFORMATION *tzinfo)
1462 NTSTATUS status;
1463 HANDLE time_zone_key;
1465 status = RtlQueryDynamicTimeZoneInformation( (RTL_DYNAMIC_TIME_ZONE_INFORMATION*)tzinfo );
1466 if ( status != STATUS_SUCCESS )
1468 SetLastError( RtlNtStatusToDosError(status) );
1469 return TIME_ZONE_ID_INVALID;
1472 if (!TIME_GetSpecificTimeZoneKey( tzinfo->TimeZoneKeyName, &time_zone_key ))
1473 return TIME_ZONE_ID_INVALID;
1474 reg_load_mui_string( time_zone_key, mui_stdW, tzinfo->StandardName, sizeof(tzinfo->StandardName) );
1475 reg_load_mui_string( time_zone_key, mui_dltW, tzinfo->DaylightName, sizeof(tzinfo->DaylightName) );
1476 NtClose( time_zone_key );
1478 return TIME_ZoneID( (TIME_ZONE_INFORMATION*)tzinfo );
1481 /***********************************************************************
1482 * GetDynamicTimeZoneInformationEffectiveYears (KERNEL32.@)
1484 DWORD WINAPI GetDynamicTimeZoneInformationEffectiveYears(DYNAMIC_TIME_ZONE_INFORMATION *tzinfo, DWORD *first_year, DWORD *last_year)
1486 FIXME("(%p, %p, %p): stub!\n", tzinfo, first_year, last_year);
1487 return ERROR_FILE_NOT_FOUND;
1490 /***********************************************************************
1491 * QueryProcessCycleTime (KERNEL32.@)
1493 BOOL WINAPI QueryProcessCycleTime(HANDLE process, PULONG64 cycle)
1495 static int once;
1496 if (!once++)
1497 FIXME("(%p,%p): stub!\n", process, cycle);
1498 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1499 return FALSE;
1502 /***********************************************************************
1503 * QueryThreadCycleTime (KERNEL32.@)
1505 BOOL WINAPI QueryThreadCycleTime(HANDLE thread, PULONG64 cycle)
1507 static int once;
1508 if (!once++)
1509 FIXME("(%p,%p): stub!\n", thread, cycle);
1510 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1511 return FALSE;
1514 /***********************************************************************
1515 * QueryUnbiasedInterruptTime (KERNEL32.@)
1517 BOOL WINAPI QueryUnbiasedInterruptTime(ULONGLONG *time)
1519 TRACE("(%p)\n", time);
1520 if (!time) return FALSE;
1521 RtlQueryUnbiasedInterruptTime(time);
1522 return TRUE;