4 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5 * adapted to wine with special permissions of the author. This code is
6 * Copyright 2002 Rex Jolliff (rex@lvcablemodem.com)
8 * Copyright 1999 Juergen Schmied
9 * Copyright 2007 Dmitry Timoshkov
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/port.h"
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
42 # include <mach/mach_time.h>
46 #define WIN32_NO_STATUS
49 #include "wine/unicode.h"
50 #include "wine/debug.h"
51 #include "ntdll_misc.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
55 static int init_tz_info(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
);
57 static RTL_CRITICAL_SECTION TIME_tz_section
;
58 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
60 0, 0, &TIME_tz_section
,
61 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
62 0, 0, { (DWORD_PTR
)(__FILE__
": TIME_tz_section") }
64 static RTL_CRITICAL_SECTION TIME_tz_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
66 #define TICKSPERSEC 10000000
67 #define TICKSPERMSEC 10000
68 #define SECSPERDAY 86400
69 #define SECSPERHOUR 3600
71 #define MINSPERHOUR 60
72 #define HOURSPERDAY 24
73 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
75 #define MONSPERYEAR 12
76 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
77 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
79 /* 1601 to 1970 is 369 years plus 89 leap days */
80 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
81 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
82 /* 1601 to 1980 is 379 years plus 91 leap days */
83 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
84 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
87 static const int MonthLengths
[2][MONSPERYEAR
] =
89 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
90 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
93 static inline BOOL
IsLeapYear(int Year
)
95 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0);
98 /* return a monotonic time counter, in Win32 ticks */
99 static ULONGLONG
monotonic_counter(void)
103 #ifdef HAVE_CLOCK_GETTIME
105 #ifdef CLOCK_MONOTONIC_RAW
106 if (!clock_gettime( CLOCK_MONOTONIC_RAW
, &ts
))
107 return ts
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ ts
.tv_nsec
/ 100;
109 if (!clock_gettime( CLOCK_MONOTONIC
, &ts
))
110 return ts
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ ts
.tv_nsec
/ 100;
111 #elif defined(__APPLE__)
112 static mach_timebase_info_data_t timebase
;
114 if (!timebase
.denom
) mach_timebase_info( &timebase
);
115 return mach_absolute_time() * timebase
.numer
/ timebase
.denom
/ 100;
118 gettimeofday( &now
, 0 );
119 return now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ now
.tv_usec
* 10 + TICKS_1601_TO_1970
- server_start_time
;
122 /******************************************************************************
123 * RtlTimeToTimeFields [NTDLL.@]
125 * Convert a time into a TIME_FIELDS structure.
128 * liTime [I] Time to convert.
129 * TimeFields [O] Destination for the converted time.
134 VOID WINAPI
RtlTimeToTimeFields(
135 const LARGE_INTEGER
*liTime
,
136 PTIME_FIELDS TimeFields
)
139 long int cleaps
, years
, yearday
, months
;
143 /* Extract millisecond from time and convert time into seconds */
144 TimeFields
->Milliseconds
=
145 (CSHORT
) (( liTime
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
146 Time
= liTime
->QuadPart
/ TICKSPERSEC
;
148 /* The native version of RtlTimeToTimeFields does not take leap seconds
151 /* Split the time into days and seconds within the day */
152 Days
= Time
/ SECSPERDAY
;
153 SecondsInDay
= Time
% SECSPERDAY
;
155 /* compute time of day */
156 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
157 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
158 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
159 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
161 /* compute day of week */
162 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
164 /* compute year, month and day of month. */
165 cleaps
=( 3 * ((4 * Days
+ 1227) / DAYSPERQUADRICENTENNIUM
) + 3 ) / 4;
166 Days
+= 28188 + cleaps
;
167 years
= (20 * Days
- 2442) / (5 * DAYSPERNORMALQUADRENNIUM
);
168 yearday
= Days
- (years
* DAYSPERNORMALQUADRENNIUM
)/4;
169 months
= (64 * yearday
) / 1959;
170 /* the result is based on a year starting on March.
171 * To convert take 12 from Januari and Februari and
172 * increase the year by one. */
174 TimeFields
->Month
= months
- 1;
175 TimeFields
->Year
= years
+ 1524;
177 TimeFields
->Month
= months
- 13;
178 TimeFields
->Year
= years
+ 1525;
180 /* calculation of day of month is based on the wonderful
181 * sequence of INT( n * 30.6): it reproduces the
182 * 31-30-31-30-31-31 month lengths exactly for small n's */
183 TimeFields
->Day
= yearday
- (1959 * months
) / 64 ;
187 /******************************************************************************
188 * RtlTimeFieldsToTime [NTDLL.@]
190 * Convert a TIME_FIELDS structure into a time.
193 * ftTimeFields [I] TIME_FIELDS structure to convert.
194 * Time [O] Destination for the converted time.
200 BOOLEAN WINAPI
RtlTimeFieldsToTime(
201 PTIME_FIELDS tfTimeFields
,
204 int month
, year
, cleaps
, day
;
206 /* FIXME: normalize the TIME_FIELDS structure here */
207 /* No, native just returns 0 (error) if the fields are not */
208 if( tfTimeFields
->Milliseconds
< 0 || tfTimeFields
->Milliseconds
> 999 ||
209 tfTimeFields
->Second
< 0 || tfTimeFields
->Second
> 59 ||
210 tfTimeFields
->Minute
< 0 || tfTimeFields
->Minute
> 59 ||
211 tfTimeFields
->Hour
< 0 || tfTimeFields
->Hour
> 23 ||
212 tfTimeFields
->Month
< 1 || tfTimeFields
->Month
> 12 ||
213 tfTimeFields
->Day
< 1 ||
214 tfTimeFields
->Day
> MonthLengths
215 [ tfTimeFields
->Month
==2 || IsLeapYear(tfTimeFields
->Year
)]
216 [ tfTimeFields
->Month
- 1] ||
217 tfTimeFields
->Year
< 1601 )
220 /* now calculate a day count from the date
221 * First start counting years from March. This way the leap days
222 * are added at the end of the year, not somewhere in the middle.
223 * Formula's become so much less complicate that way.
224 * To convert: add 12 to the month numbers of Jan and Feb, and
225 * take 1 from the year */
226 if(tfTimeFields
->Month
< 3) {
227 month
= tfTimeFields
->Month
+ 13;
228 year
= tfTimeFields
->Year
- 1;
230 month
= tfTimeFields
->Month
+ 1;
231 year
= tfTimeFields
->Year
;
233 cleaps
= (3 * (year
/ 100) + 3) / 4; /* nr of "century leap years"*/
234 day
= (36525 * year
) / 100 - cleaps
+ /* year * dayperyr, corrected */
235 (1959 * month
) / 64 + /* months * daypermonth */
236 tfTimeFields
->Day
- /* day of the month */
237 584817 ; /* zero that on 1601-01-01 */
240 Time
->QuadPart
= (((((LONGLONG
) day
* HOURSPERDAY
+
241 tfTimeFields
->Hour
) * MINSPERHOUR
+
242 tfTimeFields
->Minute
) * SECSPERMIN
+
243 tfTimeFields
->Second
) * 1000 +
244 tfTimeFields
->Milliseconds
) * TICKSPERMSEC
;
249 /***********************************************************************
250 * TIME_GetBias [internal]
252 * Helper function calculates delta local time from UTC.
255 * utc [I] The current utc time.
256 * pdaylight [I] Local daylight.
259 * The bias for the current timezone.
261 static LONG
TIME_GetBias(void)
263 static time_t last_utc
;
264 static LONG last_bias
;
270 RtlEnterCriticalSection( &TIME_tz_section
);
273 RTL_DYNAMIC_TIME_ZONE_INFORMATION tzi
;
274 int is_dst
= init_tz_info( &tzi
);
277 last_bias
= tzi
.Bias
;
278 last_bias
+= is_dst
? tzi
.DaylightBias
: tzi
.StandardBias
;
279 last_bias
*= SECSPERMIN
;
284 RtlLeaveCriticalSection( &TIME_tz_section
);
288 /******************************************************************************
289 * RtlLocalTimeToSystemTime [NTDLL.@]
291 * Convert a local time into system time.
294 * LocalTime [I] Local time to convert.
295 * SystemTime [O] Destination for the converted time.
298 * Success: STATUS_SUCCESS.
299 * Failure: An NTSTATUS error code indicating the problem.
301 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
302 PLARGE_INTEGER SystemTime
)
306 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
308 bias
= TIME_GetBias();
309 SystemTime
->QuadPart
= LocalTime
->QuadPart
+ bias
* (LONGLONG
)TICKSPERSEC
;
310 return STATUS_SUCCESS
;
313 /******************************************************************************
314 * RtlSystemTimeToLocalTime [NTDLL.@]
316 * Convert a system time into a local time.
319 * SystemTime [I] System time to convert.
320 * LocalTime [O] Destination for the converted time.
323 * Success: STATUS_SUCCESS.
324 * Failure: An NTSTATUS error code indicating the problem.
326 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
327 PLARGE_INTEGER LocalTime
)
331 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
333 bias
= TIME_GetBias();
334 LocalTime
->QuadPart
= SystemTime
->QuadPart
- bias
* (LONGLONG
)TICKSPERSEC
;
335 return STATUS_SUCCESS
;
338 /******************************************************************************
339 * RtlTimeToSecondsSince1970 [NTDLL.@]
341 * Convert a time into a count of seconds since 1970.
344 * Time [I] Time to convert.
345 * Seconds [O] Destination for the converted time.
349 * Failure: FALSE, if the resulting value will not fit in a DWORD.
351 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
353 ULONGLONG tmp
= Time
->QuadPart
/ TICKSPERSEC
- SECS_1601_TO_1970
;
354 if (tmp
> 0xffffffff) return FALSE
;
359 /******************************************************************************
360 * RtlTimeToSecondsSince1980 [NTDLL.@]
362 * Convert a time into a count of seconds since 1980.
365 * Time [I] Time to convert.
366 * Seconds [O] Destination for the converted time.
370 * Failure: FALSE, if the resulting value will not fit in a DWORD.
372 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
374 ULONGLONG tmp
= Time
->QuadPart
/ TICKSPERSEC
- SECS_1601_TO_1980
;
375 if (tmp
> 0xffffffff) return FALSE
;
380 /******************************************************************************
381 * RtlSecondsSince1970ToTime [NTDLL.@]
383 * Convert a count of seconds since 1970 to a time.
386 * Seconds [I] Time to convert.
387 * Time [O] Destination for the converted time.
392 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
394 Time
->QuadPart
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
397 /******************************************************************************
398 * RtlSecondsSince1980ToTime [NTDLL.@]
400 * Convert a count of seconds since 1980 to a time.
403 * Seconds [I] Time to convert.
404 * Time [O] Destination for the converted time.
409 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
411 Time
->QuadPart
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
414 /******************************************************************************
415 * RtlTimeToElapsedTimeFields [NTDLL.@]
417 * Convert a time to a count of elapsed seconds.
420 * Time [I] Time to convert.
421 * TimeFields [O] Destination for the converted time.
426 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
431 time
= Time
->QuadPart
/ TICKSPERSEC
;
432 TimeFields
->Milliseconds
= (Time
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
;
434 /* time is now in seconds */
435 TimeFields
->Year
= 0;
436 TimeFields
->Month
= 0;
437 TimeFields
->Day
= time
/ SECSPERDAY
;
439 /* rem is now the remaining seconds in the last day */
440 rem
= time
% SECSPERDAY
;
441 TimeFields
->Second
= rem
% 60;
443 TimeFields
->Minute
= rem
% 60;
444 TimeFields
->Hour
= rem
/ 60;
447 /***********************************************************************
448 * NtQuerySystemTime [NTDLL.@]
449 * ZwQuerySystemTime [NTDLL.@]
451 * Get the current system time.
454 * Time [O] Destination for the current system time.
457 * Success: STATUS_SUCCESS.
458 * Failure: An NTSTATUS error code indicating the problem.
460 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
464 gettimeofday( &now
, 0 );
465 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
466 Time
->QuadPart
+= now
.tv_usec
* 10;
467 return STATUS_SUCCESS
;
470 /******************************************************************************
471 * NtQueryPerformanceCounter [NTDLL.@]
473 NTSTATUS WINAPI
NtQueryPerformanceCounter( LARGE_INTEGER
*counter
, LARGE_INTEGER
*frequency
)
475 if (!counter
) return STATUS_ACCESS_VIOLATION
;
477 counter
->QuadPart
= monotonic_counter();
478 if (frequency
) frequency
->QuadPart
= TICKSPERSEC
;
479 return STATUS_SUCCESS
;
483 /******************************************************************************
484 * NtGetTickCount (NTDLL.@)
485 * ZwGetTickCount (NTDLL.@)
487 ULONG WINAPI
NtGetTickCount(void)
489 return monotonic_counter() / TICKSPERMSEC
;
492 /* calculate the mday of dst change date, so that for instance Sun 5 Oct 2007
493 * (last Sunday in October of 2007) becomes Sun Oct 28 2007
495 * Note: year, day and month must be in unix format.
497 static int weekday_to_mday(int year
, int day
, int mon
, int day_of_week
)
503 /* find first day in the month matching week day of the date */
504 memset(&date
, 0, sizeof(date
));
513 } while (date
.tm_wday
!= day_of_week
|| date
.tm_mon
!= mon
);
517 /* find number of week days in the month matching week day of the date */
518 wday
= 1; /* 1 - 1st, ...., 5 - last */
525 tm
= localtime(&tmp
);
526 if (tm
->tm_mon
!= mon
)
535 static BOOL
match_tz_date(const RTL_SYSTEM_TIME
*st
, const RTL_SYSTEM_TIME
*reg_st
)
539 if (st
->wMonth
!= reg_st
->wMonth
) return FALSE
;
541 if (!st
->wMonth
) return TRUE
; /* no transition dates */
544 if (!reg_st
->wYear
) /* date in a day-of-week format */
545 wDay
= weekday_to_mday(st
->wYear
- 1900, reg_st
->wDay
, reg_st
->wMonth
- 1, reg_st
->wDayOfWeek
);
547 if (st
->wDay
!= wDay
||
548 st
->wHour
!= reg_st
->wHour
||
549 st
->wMinute
!= reg_st
->wMinute
||
550 st
->wSecond
!= reg_st
->wSecond
||
551 st
->wMilliseconds
!= reg_st
->wMilliseconds
) return FALSE
;
556 static BOOL
match_tz_info(const RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
, const RTL_DYNAMIC_TIME_ZONE_INFORMATION
*reg_tzi
)
558 if (tzi
->Bias
== reg_tzi
->Bias
&&
559 match_tz_date(&tzi
->StandardDate
, ®_tzi
->StandardDate
) &&
560 match_tz_date(&tzi
->DaylightDate
, ®_tzi
->DaylightDate
))
566 static BOOL
reg_query_value(HKEY hkey
, LPCWSTR name
, DWORD type
, void *data
, DWORD count
)
568 UNICODE_STRING nameW
;
570 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf
;
572 if (count
> sizeof(buf
) - sizeof(KEY_VALUE_PARTIAL_INFORMATION
))
575 RtlInitUnicodeString(&nameW
, name
);
577 if (NtQueryValueKey(hkey
, &nameW
, KeyValuePartialInformation
,
578 buf
, sizeof(buf
), &count
))
581 if (info
->Type
!= type
) return FALSE
;
583 memcpy(data
, info
->Data
, info
->DataLength
);
587 static void find_reg_tz_info(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
, int year
)
589 static const WCHAR Time_ZonesW
[] = { 'M','a','c','h','i','n','e','\\',
590 'S','o','f','t','w','a','r','e','\\',
591 'M','i','c','r','o','s','o','f','t','\\',
592 'W','i','n','d','o','w','s',' ','N','T','\\',
593 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
594 'T','i','m','e',' ','Z','o','n','e','s',0 };
595 static const WCHAR Dynamic_DstW
[] = { 'D','y','n','a','m','i','c',' ','D','S','T',0 };
596 static const WCHAR fmtW
[] = { '%','d',0 };
599 OBJECT_ATTRIBUTES attr
, attrDynamic
;
600 UNICODE_STRING nameW
, nameDynamicW
;
601 WCHAR buf
[128], yearW
[16];
603 sprintfW(yearW
, fmtW
, year
);
605 attrDynamic
.Length
= sizeof(attrDynamic
);
606 attrDynamic
.RootDirectory
= 0; /* will be replaced later */
607 attrDynamic
.ObjectName
= &nameDynamicW
;
608 attrDynamic
.Attributes
= 0;
609 attrDynamic
.SecurityDescriptor
= NULL
;
610 attrDynamic
.SecurityQualityOfService
= NULL
;
611 RtlInitUnicodeString(&nameDynamicW
, Dynamic_DstW
);
613 attr
.Length
= sizeof(attr
);
614 attr
.RootDirectory
= 0;
615 attr
.ObjectName
= &nameW
;
617 attr
.SecurityDescriptor
= NULL
;
618 attr
.SecurityQualityOfService
= NULL
;
619 RtlInitUnicodeString(&nameW
, Time_ZonesW
);
620 if (NtOpenKey(&hkey
, KEY_READ
, &attr
))
622 WARN("Unable to open the time zones key\n");
628 nameW
.Length
= sizeof(buf
);
629 nameW
.MaximumLength
= sizeof(buf
);
631 while (!RtlpNtEnumerateSubKey(hkey
, &nameW
, idx
++))
633 static const WCHAR stdW
[] = { 'S','t','d',0 };
634 static const WCHAR dltW
[] = { 'D','l','t',0 };
635 static const WCHAR tziW
[] = { 'T','Z','I',0 };
636 RTL_DYNAMIC_TIME_ZONE_INFORMATION reg_tzi
;
637 HANDLE hSubkey
, hSubkeyDynamicDST
;
638 BOOL is_dynamic
= FALSE
;
645 RTL_SYSTEM_TIME std_date
;
646 RTL_SYSTEM_TIME dlt_date
;
649 attr
.Length
= sizeof(attr
);
650 attr
.RootDirectory
= hkey
;
651 attr
.ObjectName
= &nameW
;
653 attr
.SecurityDescriptor
= NULL
;
654 attr
.SecurityQualityOfService
= NULL
;
655 if (NtOpenKey(&hSubkey
, KEY_READ
, &attr
))
657 WARN("Unable to open subkey %s\n", debugstr_wn(nameW
.Buffer
, nameW
.Length
/sizeof(WCHAR
)));
661 #define get_value(hkey, name, type, data, len) \
662 if (!reg_query_value(hkey, name, type, data, len)) \
664 WARN("can't read data from %s\n", debugstr_w(name)); \
669 get_value(hSubkey
, stdW
, REG_SZ
, reg_tzi
.StandardName
, sizeof(reg_tzi
.StandardName
));
670 get_value(hSubkey
, dltW
, REG_SZ
, reg_tzi
.DaylightName
, sizeof(reg_tzi
.DaylightName
));
671 memcpy(reg_tzi
.TimeZoneKeyName
, nameW
.Buffer
, nameW
.Length
);
672 reg_tzi
.TimeZoneKeyName
[nameW
.Length
/sizeof(WCHAR
)] = 0;
674 /* Check for Dynamic DST entry first */
675 attrDynamic
.RootDirectory
= hSubkey
;
676 if (!NtOpenKey(&hSubkeyDynamicDST
, KEY_READ
, &attrDynamic
))
678 is_dynamic
= reg_query_value(hSubkeyDynamicDST
, yearW
, REG_BINARY
, &tz_data
, sizeof(tz_data
));
679 NtClose(hSubkeyDynamicDST
);
683 get_value(hSubkey
, tziW
, REG_BINARY
, &tz_data
, sizeof(tz_data
));
687 reg_tzi
.Bias
= tz_data
.bias
;
688 reg_tzi
.StandardBias
= tz_data
.std_bias
;
689 reg_tzi
.DaylightBias
= tz_data
.dlt_bias
;
690 reg_tzi
.StandardDate
= tz_data
.std_date
;
691 reg_tzi
.DaylightDate
= tz_data
.dlt_date
;
693 TRACE("%s: bias %d\n", debugstr_wn(nameW
.Buffer
, nameW
.Length
/sizeof(WCHAR
)), reg_tzi
.Bias
);
694 TRACE("std (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
695 reg_tzi
.StandardDate
.wDay
, reg_tzi
.StandardDate
.wMonth
,
696 reg_tzi
.StandardDate
.wYear
, reg_tzi
.StandardDate
.wDayOfWeek
,
697 reg_tzi
.StandardDate
.wHour
, reg_tzi
.StandardDate
.wMinute
,
698 reg_tzi
.StandardDate
.wSecond
, reg_tzi
.StandardDate
.wMilliseconds
,
699 reg_tzi
.StandardBias
);
700 TRACE("dst (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
701 reg_tzi
.DaylightDate
.wDay
, reg_tzi
.DaylightDate
.wMonth
,
702 reg_tzi
.DaylightDate
.wYear
, reg_tzi
.DaylightDate
.wDayOfWeek
,
703 reg_tzi
.DaylightDate
.wHour
, reg_tzi
.DaylightDate
.wMinute
,
704 reg_tzi
.DaylightDate
.wSecond
, reg_tzi
.DaylightDate
.wMilliseconds
,
705 reg_tzi
.DaylightBias
);
709 if (match_tz_info(tzi
, ®_tzi
))
717 nameW
.Length
= sizeof(buf
);
718 nameW
.MaximumLength
= sizeof(buf
);
723 FIXME("Can't find matching timezone information in the registry for "
724 "bias %d, std (d/m/y): %u/%02u/%04u, dlt (d/m/y): %u/%02u/%04u\n",
726 tzi
->StandardDate
.wDay
, tzi
->StandardDate
.wMonth
, tzi
->StandardDate
.wYear
,
727 tzi
->DaylightDate
.wDay
, tzi
->DaylightDate
.wMonth
, tzi
->DaylightDate
.wYear
);
730 static time_t find_dst_change(unsigned long min
, unsigned long max
, int *is_dst
)
736 tm
= localtime(&start
);
737 *is_dst
= !tm
->tm_isdst
;
738 TRACE("starting date isdst %d, %s", !*is_dst
, ctime(&start
));
742 time_t pos
= (min
+ max
) / 2;
743 tm
= localtime(&pos
);
745 if (tm
->tm_isdst
!= *is_dst
)
753 static int init_tz_info(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
)
755 static RTL_DYNAMIC_TIME_ZONE_INFORMATION cached_tzi
;
756 static int current_year
= -1, current_bias
= 65535;
758 time_t year_start
, year_end
, tmp
, dlt
= 0, std
= 0;
759 int is_dst
, current_is_dst
, bias
;
761 RtlEnterCriticalSection( &TIME_tz_section
);
763 year_start
= time(NULL
);
764 tm
= gmtime(&year_start
);
765 bias
= (LONG
)(mktime(tm
) - year_start
) / 60;
767 tm
= localtime(&year_start
);
768 current_is_dst
= tm
->tm_isdst
;
769 if (current_year
== tm
->tm_year
&& current_bias
== bias
)
772 RtlLeaveCriticalSection( &TIME_tz_section
);
773 return current_is_dst
;
776 memset(tzi
, 0, sizeof(*tzi
));
778 TRACE("tz data will be valid through year %d, bias %d\n", tm
->tm_year
+ 1900, bias
);
779 current_year
= tm
->tm_year
;
786 tm
->tm_mon
= tm
->tm_hour
= tm
->tm_min
= tm
->tm_sec
= tm
->tm_wday
= tm
->tm_yday
= 0;
787 year_start
= mktime(tm
);
788 TRACE("year_start: %s", ctime(&year_start
));
790 tm
->tm_mday
= tm
->tm_wday
= tm
->tm_yday
= 0;
793 tm
->tm_min
= tm
->tm_sec
= 59;
794 year_end
= mktime(tm
);
795 TRACE("year_end: %s", ctime(&year_end
));
797 tmp
= find_dst_change(year_start
, year_end
, &is_dst
);
803 tmp
= find_dst_change(tmp
, year_end
, &is_dst
);
809 TRACE("std: %s", ctime(&std
));
810 TRACE("dlt: %s", ctime(&dlt
));
812 if (dlt
== std
|| !dlt
|| !std
)
813 TRACE("there is no daylight saving rules in this time zone\n");
816 tmp
= dlt
- tzi
->Bias
* 60;
818 TRACE("dlt gmtime: %s", asctime(tm
));
820 tzi
->DaylightBias
= -60;
821 tzi
->DaylightDate
.wYear
= tm
->tm_year
+ 1900;
822 tzi
->DaylightDate
.wMonth
= tm
->tm_mon
+ 1;
823 tzi
->DaylightDate
.wDayOfWeek
= tm
->tm_wday
;
824 tzi
->DaylightDate
.wDay
= tm
->tm_mday
;
825 tzi
->DaylightDate
.wHour
= tm
->tm_hour
;
826 tzi
->DaylightDate
.wMinute
= tm
->tm_min
;
827 tzi
->DaylightDate
.wSecond
= tm
->tm_sec
;
828 tzi
->DaylightDate
.wMilliseconds
= 0;
830 TRACE("daylight (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
831 tzi
->DaylightDate
.wDay
, tzi
->DaylightDate
.wMonth
,
832 tzi
->DaylightDate
.wYear
, tzi
->DaylightDate
.wDayOfWeek
,
833 tzi
->DaylightDate
.wHour
, tzi
->DaylightDate
.wMinute
,
834 tzi
->DaylightDate
.wSecond
, tzi
->DaylightDate
.wMilliseconds
,
837 tmp
= std
- tzi
->Bias
* 60 - tzi
->DaylightBias
* 60;
839 TRACE("std gmtime: %s", asctime(tm
));
841 tzi
->StandardBias
= 0;
842 tzi
->StandardDate
.wYear
= tm
->tm_year
+ 1900;
843 tzi
->StandardDate
.wMonth
= tm
->tm_mon
+ 1;
844 tzi
->StandardDate
.wDayOfWeek
= tm
->tm_wday
;
845 tzi
->StandardDate
.wDay
= tm
->tm_mday
;
846 tzi
->StandardDate
.wHour
= tm
->tm_hour
;
847 tzi
->StandardDate
.wMinute
= tm
->tm_min
;
848 tzi
->StandardDate
.wSecond
= tm
->tm_sec
;
849 tzi
->StandardDate
.wMilliseconds
= 0;
851 TRACE("standard (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
852 tzi
->StandardDate
.wDay
, tzi
->StandardDate
.wMonth
,
853 tzi
->StandardDate
.wYear
, tzi
->StandardDate
.wDayOfWeek
,
854 tzi
->StandardDate
.wHour
, tzi
->StandardDate
.wMinute
,
855 tzi
->StandardDate
.wSecond
, tzi
->StandardDate
.wMilliseconds
,
859 find_reg_tz_info(tzi
, current_year
+ 1900);
862 RtlLeaveCriticalSection( &TIME_tz_section
);
864 return current_is_dst
;
867 /***********************************************************************
868 * RtlQueryTimeZoneInformation [NTDLL.@]
870 * Get information about the current timezone.
873 * tzinfo [O] Destination for the retrieved timezone info.
876 * Success: STATUS_SUCCESS.
877 * Failure: An NTSTATUS error code indicating the problem.
879 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION
*ret
)
881 RTL_DYNAMIC_TIME_ZONE_INFORMATION tzinfo
;
883 init_tz_info( &tzinfo
);
884 memcpy( ret
, &tzinfo
, sizeof(*ret
) );
885 return STATUS_SUCCESS
;
888 /***********************************************************************
889 * RtlQueryDynamicTimeZoneInformation [NTDLL.@]
891 * Get information about the current timezone.
894 * tzinfo [O] Destination for the retrieved timezone info.
897 * Success: STATUS_SUCCESS.
898 * Failure: An NTSTATUS error code indicating the problem.
900 NTSTATUS WINAPI
RtlQueryDynamicTimeZoneInformation(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzinfo
)
902 init_tz_info( tzinfo
);
904 return STATUS_SUCCESS
;
907 /***********************************************************************
908 * RtlSetTimeZoneInformation [NTDLL.@]
910 * Set the current time zone information.
913 * tzinfo [I] Timezone information to set.
916 * Success: STATUS_SUCCESS.
917 * Failure: An NTSTATUS error code indicating the problem.
920 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION
*tzinfo
)
922 return STATUS_PRIVILEGE_NOT_HELD
;
925 /***********************************************************************
926 * NtSetSystemTime [NTDLL.@]
927 * ZwSetSystemTime [NTDLL.@]
929 * Set the system time.
932 * NewTime [I] The time to set.
933 * OldTime [O] Optional destination for the previous system time.
936 * Success: STATUS_SUCCESS.
937 * Failure: An NTSTATUS error code indicating the problem.
939 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
946 /* Return the old time if necessary */
947 if (!OldTime
) OldTime
= &tm
;
949 NtQuerySystemTime( OldTime
);
950 RtlTimeToSecondsSince1970( OldTime
, &oldsec
);
952 RtlTimeToSecondsSince1970( NewTime
, &sec
);
954 /* set the new time */
958 #ifdef HAVE_SETTIMEOFDAY
959 if (!settimeofday(&tv
, NULL
)) /* 0 is OK, -1 is error */
960 return STATUS_SUCCESS
;
962 ERR("Cannot set time to %s, time adjustment %ld: %s\n",
963 ctime(&tm_t
), (long)(sec
-oldsec
), strerror(errno
));
965 return STATUS_PRIVILEGE_NOT_HELD
;
967 return STATUS_INVALID_PARAMETER
;
970 FIXME("setting time to %s not implemented for missing settimeofday\n",
972 return STATUS_NOT_IMPLEMENTED
;
976 /***********************************************************************
977 * RtlQueryUnbiasedInterruptTime [NTDLL.@]
979 NTSTATUS WINAPI
RtlQueryUnbiasedInterruptTime(ULONGLONG
*time
)
981 *time
= monotonic_counter();
982 return STATUS_SUCCESS
;