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/exception.h"
50 #include "wine/unicode.h"
51 #include "wine/debug.h"
52 #include "ntdll_misc.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
56 static int init_tz_info(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
);
58 static RTL_CRITICAL_SECTION TIME_tz_section
;
59 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
61 0, 0, &TIME_tz_section
,
62 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
63 0, 0, { (DWORD_PTR
)(__FILE__
": TIME_tz_section") }
65 static RTL_CRITICAL_SECTION TIME_tz_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
67 #define TICKSPERSEC 10000000
68 #define TICKSPERMSEC 10000
69 #define SECSPERDAY 86400
70 #define SECSPERHOUR 3600
72 #define MINSPERHOUR 60
73 #define HOURSPERDAY 24
74 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
76 #define MONSPERYEAR 12
77 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
78 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
80 /* 1601 to 1970 is 369 years plus 89 leap days */
81 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
82 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
83 /* 1601 to 1980 is 379 years plus 91 leap days */
84 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
85 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
88 static const int MonthLengths
[2][MONSPERYEAR
] =
90 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
91 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
94 static inline BOOL
IsLeapYear(int Year
)
96 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0);
99 /* return a monotonic time counter, in Win32 ticks */
100 static ULONGLONG
monotonic_counter(void)
105 static mach_timebase_info_data_t timebase
;
107 if (!timebase
.denom
) mach_timebase_info( &timebase
);
108 return mach_absolute_time() * timebase
.numer
/ timebase
.denom
/ 100;
109 #elif defined(HAVE_CLOCK_GETTIME)
111 #ifdef CLOCK_MONOTONIC_RAW
112 if (!clock_gettime( CLOCK_MONOTONIC_RAW
, &ts
))
113 return ts
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ ts
.tv_nsec
/ 100;
115 if (!clock_gettime( CLOCK_MONOTONIC
, &ts
))
116 return ts
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ ts
.tv_nsec
/ 100;
119 gettimeofday( &now
, 0 );
120 return now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ now
.tv_usec
* 10 + TICKS_1601_TO_1970
- server_start_time
;
123 /******************************************************************************
124 * RtlTimeToTimeFields [NTDLL.@]
126 * Convert a time into a TIME_FIELDS structure.
129 * liTime [I] Time to convert.
130 * TimeFields [O] Destination for the converted time.
135 VOID WINAPI
RtlTimeToTimeFields(
136 const LARGE_INTEGER
*liTime
,
137 PTIME_FIELDS TimeFields
)
140 long int cleaps
, years
, yearday
, months
;
144 /* Extract millisecond from time and convert time into seconds */
145 TimeFields
->Milliseconds
=
146 (CSHORT
) (( liTime
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
147 Time
= liTime
->QuadPart
/ TICKSPERSEC
;
149 /* The native version of RtlTimeToTimeFields does not take leap seconds
152 /* Split the time into days and seconds within the day */
153 Days
= Time
/ SECSPERDAY
;
154 SecondsInDay
= Time
% SECSPERDAY
;
156 /* compute time of day */
157 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
158 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
159 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
160 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
162 /* compute day of week */
163 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
165 /* compute year, month and day of month. */
166 cleaps
=( 3 * ((4 * Days
+ 1227) / DAYSPERQUADRICENTENNIUM
) + 3 ) / 4;
167 Days
+= 28188 + cleaps
;
168 years
= (20 * Days
- 2442) / (5 * DAYSPERNORMALQUADRENNIUM
);
169 yearday
= Days
- (years
* DAYSPERNORMALQUADRENNIUM
)/4;
170 months
= (64 * yearday
) / 1959;
171 /* the result is based on a year starting on March.
172 * To convert take 12 from Januari and Februari and
173 * increase the year by one. */
175 TimeFields
->Month
= months
- 1;
176 TimeFields
->Year
= years
+ 1524;
178 TimeFields
->Month
= months
- 13;
179 TimeFields
->Year
= years
+ 1525;
181 /* calculation of day of month is based on the wonderful
182 * sequence of INT( n * 30.6): it reproduces the
183 * 31-30-31-30-31-31 month lengths exactly for small n's */
184 TimeFields
->Day
= yearday
- (1959 * months
) / 64 ;
188 /******************************************************************************
189 * RtlTimeFieldsToTime [NTDLL.@]
191 * Convert a TIME_FIELDS structure into a time.
194 * ftTimeFields [I] TIME_FIELDS structure to convert.
195 * Time [O] Destination for the converted time.
201 BOOLEAN WINAPI
RtlTimeFieldsToTime(
202 PTIME_FIELDS tfTimeFields
,
205 int month
, year
, cleaps
, day
;
207 /* FIXME: normalize the TIME_FIELDS structure here */
208 /* No, native just returns 0 (error) if the fields are not */
209 if( tfTimeFields
->Milliseconds
< 0 || tfTimeFields
->Milliseconds
> 999 ||
210 tfTimeFields
->Second
< 0 || tfTimeFields
->Second
> 59 ||
211 tfTimeFields
->Minute
< 0 || tfTimeFields
->Minute
> 59 ||
212 tfTimeFields
->Hour
< 0 || tfTimeFields
->Hour
> 23 ||
213 tfTimeFields
->Month
< 1 || tfTimeFields
->Month
> 12 ||
214 tfTimeFields
->Day
< 1 ||
215 tfTimeFields
->Day
> MonthLengths
216 [ tfTimeFields
->Month
==2 || IsLeapYear(tfTimeFields
->Year
)]
217 [ tfTimeFields
->Month
- 1] ||
218 tfTimeFields
->Year
< 1601 )
221 /* now calculate a day count from the date
222 * First start counting years from March. This way the leap days
223 * are added at the end of the year, not somewhere in the middle.
224 * Formula's become so much less complicate that way.
225 * To convert: add 12 to the month numbers of Jan and Feb, and
226 * take 1 from the year */
227 if(tfTimeFields
->Month
< 3) {
228 month
= tfTimeFields
->Month
+ 13;
229 year
= tfTimeFields
->Year
- 1;
231 month
= tfTimeFields
->Month
+ 1;
232 year
= tfTimeFields
->Year
;
234 cleaps
= (3 * (year
/ 100) + 3) / 4; /* nr of "century leap years"*/
235 day
= (36525 * year
) / 100 - cleaps
+ /* year * dayperyr, corrected */
236 (1959 * month
) / 64 + /* months * daypermonth */
237 tfTimeFields
->Day
- /* day of the month */
238 584817 ; /* zero that on 1601-01-01 */
241 Time
->QuadPart
= (((((LONGLONG
) day
* HOURSPERDAY
+
242 tfTimeFields
->Hour
) * MINSPERHOUR
+
243 tfTimeFields
->Minute
) * SECSPERMIN
+
244 tfTimeFields
->Second
) * 1000 +
245 tfTimeFields
->Milliseconds
) * TICKSPERMSEC
;
250 /***********************************************************************
251 * TIME_GetBias [internal]
253 * Helper function calculates delta local time from UTC.
256 * utc [I] The current utc time.
257 * pdaylight [I] Local daylight.
260 * The bias for the current timezone.
262 static LONG
TIME_GetBias(void)
264 static time_t last_utc
;
265 static LONG last_bias
;
271 RtlEnterCriticalSection( &TIME_tz_section
);
274 RTL_DYNAMIC_TIME_ZONE_INFORMATION tzi
;
275 int is_dst
= init_tz_info( &tzi
);
278 last_bias
= tzi
.Bias
;
279 last_bias
+= is_dst
? tzi
.DaylightBias
: tzi
.StandardBias
;
280 last_bias
*= SECSPERMIN
;
285 RtlLeaveCriticalSection( &TIME_tz_section
);
289 /******************************************************************************
290 * RtlLocalTimeToSystemTime [NTDLL.@]
292 * Convert a local time into system time.
295 * LocalTime [I] Local time to convert.
296 * SystemTime [O] Destination for the converted time.
299 * Success: STATUS_SUCCESS.
300 * Failure: An NTSTATUS error code indicating the problem.
302 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
303 PLARGE_INTEGER SystemTime
)
307 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
309 bias
= TIME_GetBias();
310 SystemTime
->QuadPart
= LocalTime
->QuadPart
+ bias
* (LONGLONG
)TICKSPERSEC
;
311 return STATUS_SUCCESS
;
314 /******************************************************************************
315 * RtlSystemTimeToLocalTime [NTDLL.@]
317 * Convert a system time into a local time.
320 * SystemTime [I] System time to convert.
321 * LocalTime [O] Destination for the converted time.
324 * Success: STATUS_SUCCESS.
325 * Failure: An NTSTATUS error code indicating the problem.
327 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
328 PLARGE_INTEGER LocalTime
)
332 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
334 bias
= TIME_GetBias();
335 LocalTime
->QuadPart
= SystemTime
->QuadPart
- bias
* (LONGLONG
)TICKSPERSEC
;
336 return STATUS_SUCCESS
;
339 /******************************************************************************
340 * RtlTimeToSecondsSince1970 [NTDLL.@]
342 * Convert a time into a count of seconds since 1970.
345 * Time [I] Time to convert.
346 * Seconds [O] Destination for the converted time.
350 * Failure: FALSE, if the resulting value will not fit in a DWORD.
352 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
354 ULONGLONG tmp
= Time
->QuadPart
/ TICKSPERSEC
- SECS_1601_TO_1970
;
355 if (tmp
> 0xffffffff) return FALSE
;
360 /******************************************************************************
361 * RtlTimeToSecondsSince1980 [NTDLL.@]
363 * Convert a time into a count of seconds since 1980.
366 * Time [I] Time to convert.
367 * Seconds [O] Destination for the converted time.
371 * Failure: FALSE, if the resulting value will not fit in a DWORD.
373 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
375 ULONGLONG tmp
= Time
->QuadPart
/ TICKSPERSEC
- SECS_1601_TO_1980
;
376 if (tmp
> 0xffffffff) return FALSE
;
381 /******************************************************************************
382 * RtlSecondsSince1970ToTime [NTDLL.@]
384 * Convert a count of seconds since 1970 to a time.
387 * Seconds [I] Time to convert.
388 * Time [O] Destination for the converted time.
393 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
395 Time
->QuadPart
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
398 /******************************************************************************
399 * RtlSecondsSince1980ToTime [NTDLL.@]
401 * Convert a count of seconds since 1980 to a time.
404 * Seconds [I] Time to convert.
405 * Time [O] Destination for the converted time.
410 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
412 Time
->QuadPart
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
415 /******************************************************************************
416 * RtlTimeToElapsedTimeFields [NTDLL.@]
418 * Convert a time to a count of elapsed seconds.
421 * Time [I] Time to convert.
422 * TimeFields [O] Destination for the converted time.
427 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
432 time
= Time
->QuadPart
/ TICKSPERSEC
;
433 TimeFields
->Milliseconds
= (Time
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
;
435 /* time is now in seconds */
436 TimeFields
->Year
= 0;
437 TimeFields
->Month
= 0;
438 TimeFields
->Day
= time
/ SECSPERDAY
;
440 /* rem is now the remaining seconds in the last day */
441 rem
= time
% SECSPERDAY
;
442 TimeFields
->Second
= rem
% 60;
444 TimeFields
->Minute
= rem
% 60;
445 TimeFields
->Hour
= rem
/ 60;
448 /***********************************************************************
449 * NtQuerySystemTime [NTDLL.@]
450 * ZwQuerySystemTime [NTDLL.@]
452 * Get the current system time.
455 * Time [O] Destination for the current system time.
458 * Success: STATUS_SUCCESS.
459 * Failure: An NTSTATUS error code indicating the problem.
461 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
465 gettimeofday( &now
, 0 );
466 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
467 Time
->QuadPart
+= now
.tv_usec
* 10;
468 return STATUS_SUCCESS
;
471 /******************************************************************************
472 * NtQueryPerformanceCounter [NTDLL.@]
474 NTSTATUS WINAPI
NtQueryPerformanceCounter( LARGE_INTEGER
*counter
, LARGE_INTEGER
*frequency
)
478 counter
->QuadPart
= monotonic_counter();
479 if (frequency
) frequency
->QuadPart
= TICKSPERSEC
;
483 return STATUS_ACCESS_VIOLATION
;
487 return STATUS_SUCCESS
;
491 /******************************************************************************
492 * NtGetTickCount (NTDLL.@)
493 * ZwGetTickCount (NTDLL.@)
495 ULONG WINAPI
NtGetTickCount(void)
497 return monotonic_counter() / TICKSPERMSEC
;
500 /* calculate the mday of dst change date, so that for instance Sun 5 Oct 2007
501 * (last Sunday in October of 2007) becomes Sun Oct 28 2007
503 * Note: year, day and month must be in unix format.
505 static int weekday_to_mday(int year
, int day
, int mon
, int day_of_week
)
511 /* find first day in the month matching week day of the date */
512 memset(&date
, 0, sizeof(date
));
521 } while (date
.tm_wday
!= day_of_week
|| date
.tm_mon
!= mon
);
525 /* find number of week days in the month matching week day of the date */
526 wday
= 1; /* 1 - 1st, ...., 5 - last */
533 tm
= localtime(&tmp
);
534 if (tm
->tm_mon
!= mon
)
543 static BOOL
match_tz_date(const RTL_SYSTEM_TIME
*st
, const RTL_SYSTEM_TIME
*reg_st
)
547 if (st
->wMonth
!= reg_st
->wMonth
) return FALSE
;
549 if (!st
->wMonth
) return TRUE
; /* no transition dates */
552 if (!reg_st
->wYear
) /* date in a day-of-week format */
553 wDay
= weekday_to_mday(st
->wYear
- 1900, reg_st
->wDay
, reg_st
->wMonth
- 1, reg_st
->wDayOfWeek
);
555 if (st
->wDay
!= wDay
||
556 st
->wHour
!= reg_st
->wHour
||
557 st
->wMinute
!= reg_st
->wMinute
||
558 st
->wSecond
!= reg_st
->wSecond
||
559 st
->wMilliseconds
!= reg_st
->wMilliseconds
) return FALSE
;
564 static BOOL
match_tz_info(const RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
, const RTL_DYNAMIC_TIME_ZONE_INFORMATION
*reg_tzi
)
566 if (tzi
->Bias
== reg_tzi
->Bias
&&
567 match_tz_date(&tzi
->StandardDate
, ®_tzi
->StandardDate
) &&
568 match_tz_date(&tzi
->DaylightDate
, ®_tzi
->DaylightDate
))
574 static BOOL
reg_query_value(HKEY hkey
, LPCWSTR name
, DWORD type
, void *data
, DWORD count
)
576 UNICODE_STRING nameW
;
578 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf
;
580 if (count
> sizeof(buf
) - sizeof(KEY_VALUE_PARTIAL_INFORMATION
))
583 RtlInitUnicodeString(&nameW
, name
);
585 if (NtQueryValueKey(hkey
, &nameW
, KeyValuePartialInformation
,
586 buf
, sizeof(buf
), &count
))
589 if (info
->Type
!= type
) return FALSE
;
591 memcpy(data
, info
->Data
, info
->DataLength
);
595 static void find_reg_tz_info(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
, int year
)
597 static const WCHAR Time_ZonesW
[] = { 'M','a','c','h','i','n','e','\\',
598 'S','o','f','t','w','a','r','e','\\',
599 'M','i','c','r','o','s','o','f','t','\\',
600 'W','i','n','d','o','w','s',' ','N','T','\\',
601 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
602 'T','i','m','e',' ','Z','o','n','e','s',0 };
603 static const WCHAR Dynamic_DstW
[] = { 'D','y','n','a','m','i','c',' ','D','S','T',0 };
604 static const WCHAR fmtW
[] = { '%','d',0 };
607 OBJECT_ATTRIBUTES attr
, attrDynamic
;
608 UNICODE_STRING nameW
, nameDynamicW
;
609 WCHAR buf
[128], yearW
[16];
611 sprintfW(yearW
, fmtW
, year
);
613 attrDynamic
.Length
= sizeof(attrDynamic
);
614 attrDynamic
.RootDirectory
= 0; /* will be replaced later */
615 attrDynamic
.ObjectName
= &nameDynamicW
;
616 attrDynamic
.Attributes
= 0;
617 attrDynamic
.SecurityDescriptor
= NULL
;
618 attrDynamic
.SecurityQualityOfService
= NULL
;
619 RtlInitUnicodeString(&nameDynamicW
, Dynamic_DstW
);
621 attr
.Length
= sizeof(attr
);
622 attr
.RootDirectory
= 0;
623 attr
.ObjectName
= &nameW
;
625 attr
.SecurityDescriptor
= NULL
;
626 attr
.SecurityQualityOfService
= NULL
;
627 RtlInitUnicodeString(&nameW
, Time_ZonesW
);
628 if (NtOpenKey(&hkey
, KEY_READ
, &attr
))
630 WARN("Unable to open the time zones key\n");
636 nameW
.Length
= sizeof(buf
);
637 nameW
.MaximumLength
= sizeof(buf
);
639 while (!RtlpNtEnumerateSubKey(hkey
, &nameW
, idx
++))
641 static const WCHAR stdW
[] = { 'S','t','d',0 };
642 static const WCHAR dltW
[] = { 'D','l','t',0 };
643 static const WCHAR tziW
[] = { 'T','Z','I',0 };
644 RTL_DYNAMIC_TIME_ZONE_INFORMATION reg_tzi
;
645 HANDLE hSubkey
, hSubkeyDynamicDST
;
646 BOOL is_dynamic
= FALSE
;
653 RTL_SYSTEM_TIME std_date
;
654 RTL_SYSTEM_TIME dlt_date
;
657 attr
.Length
= sizeof(attr
);
658 attr
.RootDirectory
= hkey
;
659 attr
.ObjectName
= &nameW
;
661 attr
.SecurityDescriptor
= NULL
;
662 attr
.SecurityQualityOfService
= NULL
;
663 if (NtOpenKey(&hSubkey
, KEY_READ
, &attr
))
665 WARN("Unable to open subkey %s\n", debugstr_wn(nameW
.Buffer
, nameW
.Length
/sizeof(WCHAR
)));
669 #define get_value(hkey, name, type, data, len) \
670 if (!reg_query_value(hkey, name, type, data, len)) \
672 WARN("can't read data from %s\n", debugstr_w(name)); \
677 get_value(hSubkey
, stdW
, REG_SZ
, reg_tzi
.StandardName
, sizeof(reg_tzi
.StandardName
));
678 get_value(hSubkey
, dltW
, REG_SZ
, reg_tzi
.DaylightName
, sizeof(reg_tzi
.DaylightName
));
679 memcpy(reg_tzi
.TimeZoneKeyName
, nameW
.Buffer
, nameW
.Length
);
680 reg_tzi
.TimeZoneKeyName
[nameW
.Length
/sizeof(WCHAR
)] = 0;
682 /* Check for Dynamic DST entry first */
683 attrDynamic
.RootDirectory
= hSubkey
;
684 if (!NtOpenKey(&hSubkeyDynamicDST
, KEY_READ
, &attrDynamic
))
686 is_dynamic
= reg_query_value(hSubkeyDynamicDST
, yearW
, REG_BINARY
, &tz_data
, sizeof(tz_data
));
687 NtClose(hSubkeyDynamicDST
);
691 get_value(hSubkey
, tziW
, REG_BINARY
, &tz_data
, sizeof(tz_data
));
695 reg_tzi
.Bias
= tz_data
.bias
;
696 reg_tzi
.StandardBias
= tz_data
.std_bias
;
697 reg_tzi
.DaylightBias
= tz_data
.dlt_bias
;
698 reg_tzi
.StandardDate
= tz_data
.std_date
;
699 reg_tzi
.DaylightDate
= tz_data
.dlt_date
;
701 TRACE("%s: bias %d\n", debugstr_wn(nameW
.Buffer
, nameW
.Length
/sizeof(WCHAR
)), reg_tzi
.Bias
);
702 TRACE("std (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
703 reg_tzi
.StandardDate
.wDay
, reg_tzi
.StandardDate
.wMonth
,
704 reg_tzi
.StandardDate
.wYear
, reg_tzi
.StandardDate
.wDayOfWeek
,
705 reg_tzi
.StandardDate
.wHour
, reg_tzi
.StandardDate
.wMinute
,
706 reg_tzi
.StandardDate
.wSecond
, reg_tzi
.StandardDate
.wMilliseconds
,
707 reg_tzi
.StandardBias
);
708 TRACE("dst (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
709 reg_tzi
.DaylightDate
.wDay
, reg_tzi
.DaylightDate
.wMonth
,
710 reg_tzi
.DaylightDate
.wYear
, reg_tzi
.DaylightDate
.wDayOfWeek
,
711 reg_tzi
.DaylightDate
.wHour
, reg_tzi
.DaylightDate
.wMinute
,
712 reg_tzi
.DaylightDate
.wSecond
, reg_tzi
.DaylightDate
.wMilliseconds
,
713 reg_tzi
.DaylightBias
);
717 if (match_tz_info(tzi
, ®_tzi
))
725 nameW
.Length
= sizeof(buf
);
726 nameW
.MaximumLength
= sizeof(buf
);
731 FIXME("Can't find matching timezone information in the registry for "
732 "bias %d, std (d/m/y): %u/%02u/%04u, dlt (d/m/y): %u/%02u/%04u\n",
734 tzi
->StandardDate
.wDay
, tzi
->StandardDate
.wMonth
, tzi
->StandardDate
.wYear
,
735 tzi
->DaylightDate
.wDay
, tzi
->DaylightDate
.wMonth
, tzi
->DaylightDate
.wYear
);
738 static time_t find_dst_change(unsigned long min
, unsigned long max
, int *is_dst
)
744 tm
= localtime(&start
);
745 *is_dst
= !tm
->tm_isdst
;
746 TRACE("starting date isdst %d, %s", !*is_dst
, ctime(&start
));
750 time_t pos
= (min
+ max
) / 2;
751 tm
= localtime(&pos
);
753 if (tm
->tm_isdst
!= *is_dst
)
761 static int init_tz_info(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzi
)
763 static RTL_DYNAMIC_TIME_ZONE_INFORMATION cached_tzi
;
764 static int current_year
= -1, current_bias
= 65535;
766 time_t year_start
, year_end
, tmp
, dlt
= 0, std
= 0;
767 int is_dst
, current_is_dst
, bias
;
769 RtlEnterCriticalSection( &TIME_tz_section
);
771 year_start
= time(NULL
);
772 tm
= gmtime(&year_start
);
773 bias
= (LONG
)(mktime(tm
) - year_start
) / 60;
775 tm
= localtime(&year_start
);
776 current_is_dst
= tm
->tm_isdst
;
777 if (current_year
== tm
->tm_year
&& current_bias
== bias
)
780 RtlLeaveCriticalSection( &TIME_tz_section
);
781 return current_is_dst
;
784 memset(tzi
, 0, sizeof(*tzi
));
786 TRACE("tz data will be valid through year %d, bias %d\n", tm
->tm_year
+ 1900, bias
);
787 current_year
= tm
->tm_year
;
794 tm
->tm_mon
= tm
->tm_hour
= tm
->tm_min
= tm
->tm_sec
= tm
->tm_wday
= tm
->tm_yday
= 0;
795 year_start
= mktime(tm
);
796 TRACE("year_start: %s", ctime(&year_start
));
798 tm
->tm_mday
= tm
->tm_wday
= tm
->tm_yday
= 0;
801 tm
->tm_min
= tm
->tm_sec
= 59;
802 year_end
= mktime(tm
);
803 TRACE("year_end: %s", ctime(&year_end
));
805 tmp
= find_dst_change(year_start
, year_end
, &is_dst
);
811 tmp
= find_dst_change(tmp
, year_end
, &is_dst
);
817 TRACE("std: %s", ctime(&std
));
818 TRACE("dlt: %s", ctime(&dlt
));
820 if (dlt
== std
|| !dlt
|| !std
)
821 TRACE("there is no daylight saving rules in this time zone\n");
824 tmp
= dlt
- tzi
->Bias
* 60;
826 TRACE("dlt gmtime: %s", asctime(tm
));
828 tzi
->DaylightBias
= -60;
829 tzi
->DaylightDate
.wYear
= tm
->tm_year
+ 1900;
830 tzi
->DaylightDate
.wMonth
= tm
->tm_mon
+ 1;
831 tzi
->DaylightDate
.wDayOfWeek
= tm
->tm_wday
;
832 tzi
->DaylightDate
.wDay
= tm
->tm_mday
;
833 tzi
->DaylightDate
.wHour
= tm
->tm_hour
;
834 tzi
->DaylightDate
.wMinute
= tm
->tm_min
;
835 tzi
->DaylightDate
.wSecond
= tm
->tm_sec
;
836 tzi
->DaylightDate
.wMilliseconds
= 0;
838 TRACE("daylight (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
839 tzi
->DaylightDate
.wDay
, tzi
->DaylightDate
.wMonth
,
840 tzi
->DaylightDate
.wYear
, tzi
->DaylightDate
.wDayOfWeek
,
841 tzi
->DaylightDate
.wHour
, tzi
->DaylightDate
.wMinute
,
842 tzi
->DaylightDate
.wSecond
, tzi
->DaylightDate
.wMilliseconds
,
845 tmp
= std
- tzi
->Bias
* 60 - tzi
->DaylightBias
* 60;
847 TRACE("std gmtime: %s", asctime(tm
));
849 tzi
->StandardBias
= 0;
850 tzi
->StandardDate
.wYear
= tm
->tm_year
+ 1900;
851 tzi
->StandardDate
.wMonth
= tm
->tm_mon
+ 1;
852 tzi
->StandardDate
.wDayOfWeek
= tm
->tm_wday
;
853 tzi
->StandardDate
.wDay
= tm
->tm_mday
;
854 tzi
->StandardDate
.wHour
= tm
->tm_hour
;
855 tzi
->StandardDate
.wMinute
= tm
->tm_min
;
856 tzi
->StandardDate
.wSecond
= tm
->tm_sec
;
857 tzi
->StandardDate
.wMilliseconds
= 0;
859 TRACE("standard (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
860 tzi
->StandardDate
.wDay
, tzi
->StandardDate
.wMonth
,
861 tzi
->StandardDate
.wYear
, tzi
->StandardDate
.wDayOfWeek
,
862 tzi
->StandardDate
.wHour
, tzi
->StandardDate
.wMinute
,
863 tzi
->StandardDate
.wSecond
, tzi
->StandardDate
.wMilliseconds
,
867 find_reg_tz_info(tzi
, current_year
+ 1900);
870 RtlLeaveCriticalSection( &TIME_tz_section
);
872 return current_is_dst
;
875 /***********************************************************************
876 * RtlQueryTimeZoneInformation [NTDLL.@]
878 * Get information about the current timezone.
881 * tzinfo [O] Destination for the retrieved timezone info.
884 * Success: STATUS_SUCCESS.
885 * Failure: An NTSTATUS error code indicating the problem.
887 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION
*ret
)
889 RTL_DYNAMIC_TIME_ZONE_INFORMATION tzinfo
;
891 init_tz_info( &tzinfo
);
892 memcpy( ret
, &tzinfo
, sizeof(*ret
) );
893 return STATUS_SUCCESS
;
896 /***********************************************************************
897 * RtlQueryDynamicTimeZoneInformation [NTDLL.@]
899 * Get information about the current timezone.
902 * tzinfo [O] Destination for the retrieved timezone info.
905 * Success: STATUS_SUCCESS.
906 * Failure: An NTSTATUS error code indicating the problem.
908 NTSTATUS WINAPI
RtlQueryDynamicTimeZoneInformation(RTL_DYNAMIC_TIME_ZONE_INFORMATION
*tzinfo
)
910 init_tz_info( tzinfo
);
912 return STATUS_SUCCESS
;
915 /***********************************************************************
916 * RtlSetTimeZoneInformation [NTDLL.@]
918 * Set the current time zone information.
921 * tzinfo [I] Timezone information to set.
924 * Success: STATUS_SUCCESS.
925 * Failure: An NTSTATUS error code indicating the problem.
928 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION
*tzinfo
)
930 return STATUS_PRIVILEGE_NOT_HELD
;
933 /***********************************************************************
934 * NtSetSystemTime [NTDLL.@]
935 * ZwSetSystemTime [NTDLL.@]
937 * Set the system time.
940 * NewTime [I] The time to set.
941 * OldTime [O] Optional destination for the previous system time.
944 * Success: STATUS_SUCCESS.
945 * Failure: An NTSTATUS error code indicating the problem.
947 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
954 /* Return the old time if necessary */
955 if (!OldTime
) OldTime
= &tm
;
957 NtQuerySystemTime( OldTime
);
958 RtlTimeToSecondsSince1970( OldTime
, &oldsec
);
960 RtlTimeToSecondsSince1970( NewTime
, &sec
);
962 /* fake success if time didn't change */
964 return STATUS_SUCCESS
;
966 /* set the new time */
970 #ifdef HAVE_SETTIMEOFDAY
972 if (!settimeofday(&tv
, NULL
)) /* 0 is OK, -1 is error */
974 TRACE("OS time changed to %s\n", ctime(&tm_t
));
975 return STATUS_SUCCESS
;
977 ERR("Cannot set time to %s, time adjustment %ld: %s\n",
978 ctime(&tm_t
), (long)(sec
-oldsec
), strerror(errno
));
980 return STATUS_PRIVILEGE_NOT_HELD
;
982 return STATUS_INVALID_PARAMETER
;
985 FIXME("setting time to %s not implemented for missing settimeofday\n",
987 return STATUS_NOT_IMPLEMENTED
;
991 /***********************************************************************
992 * RtlQueryUnbiasedInterruptTime [NTDLL.@]
994 NTSTATUS WINAPI
RtlQueryUnbiasedInterruptTime(ULONGLONG
*time
)
996 *time
= monotonic_counter();
997 return STATUS_SUCCESS
;