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>
45 #define NONAMELESSUNION
46 #define NONAMELESSSTRUCT
48 #define WIN32_NO_STATUS
51 #include "wine/unicode.h"
52 #include "wine/debug.h"
53 #include "ntdll_misc.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
57 static int init_tz_info(RTL_TIME_ZONE_INFORMATION
*tzi
);
59 static RTL_CRITICAL_SECTION TIME_tz_section
;
60 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
62 0, 0, &TIME_tz_section
,
63 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
64 0, 0, { (DWORD_PTR
)(__FILE__
": TIME_tz_section") }
66 static RTL_CRITICAL_SECTION TIME_tz_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
68 #define TICKSPERSEC 10000000
69 #define TICKSPERMSEC 10000
70 #define SECSPERDAY 86400
71 #define SECSPERHOUR 3600
73 #define MINSPERHOUR 60
74 #define HOURSPERDAY 24
75 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
77 #define EPOCHYEAR 1601
78 #define DAYSPERNORMALYEAR 365
79 #define DAYSPERLEAPYEAR 366
80 #define MONSPERYEAR 12
81 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
82 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
83 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
85 /* 1601 to 1970 is 369 years plus 89 leap days */
86 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
87 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
88 /* 1601 to 1980 is 379 years plus 91 leap days */
89 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
90 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
93 static const int MonthLengths
[2][MONSPERYEAR
] =
95 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
96 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
99 static inline BOOL
IsLeapYear(int Year
)
101 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0);
104 /* return a monotonic time counter, in Win32 ticks */
105 static ULONGLONG
monotonic_counter(void)
109 #ifdef 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;
117 #elif defined(__APPLE__)
118 static mach_timebase_info_data_t timebase
;
120 if (!timebase
.denom
) mach_timebase_info( &timebase
);
121 return mach_absolute_time() * timebase
.numer
/ timebase
.denom
/ 100;
124 gettimeofday( &now
, 0 );
125 return now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ now
.tv_usec
* 10 + TICKS_1601_TO_1970
- server_start_time
;
128 /******************************************************************************
129 * RtlTimeToTimeFields [NTDLL.@]
131 * Convert a time into a TIME_FIELDS structure.
134 * liTime [I] Time to convert.
135 * TimeFields [O] Destination for the converted time.
140 VOID WINAPI
RtlTimeToTimeFields(
141 const LARGE_INTEGER
*liTime
,
142 PTIME_FIELDS TimeFields
)
145 long int cleaps
, years
, yearday
, months
;
149 /* Extract millisecond from time and convert time into seconds */
150 TimeFields
->Milliseconds
=
151 (CSHORT
) (( liTime
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
152 Time
= liTime
->QuadPart
/ TICKSPERSEC
;
154 /* The native version of RtlTimeToTimeFields does not take leap seconds
157 /* Split the time into days and seconds within the day */
158 Days
= Time
/ SECSPERDAY
;
159 SecondsInDay
= Time
% SECSPERDAY
;
161 /* compute time of day */
162 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
163 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
164 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
165 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
167 /* compute day of week */
168 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
170 /* compute year, month and day of month. */
171 cleaps
=( 3 * ((4 * Days
+ 1227) / DAYSPERQUADRICENTENNIUM
) + 3 ) / 4;
172 Days
+= 28188 + cleaps
;
173 years
= (20 * Days
- 2442) / (5 * DAYSPERNORMALQUADRENNIUM
);
174 yearday
= Days
- (years
* DAYSPERNORMALQUADRENNIUM
)/4;
175 months
= (64 * yearday
) / 1959;
176 /* the result is based on a year starting on March.
177 * To convert take 12 from Januari and Februari and
178 * increase the year by one. */
180 TimeFields
->Month
= months
- 1;
181 TimeFields
->Year
= years
+ 1524;
183 TimeFields
->Month
= months
- 13;
184 TimeFields
->Year
= years
+ 1525;
186 /* calculation of day of month is based on the wonderful
187 * sequence of INT( n * 30.6): it reproduces the
188 * 31-30-31-30-31-31 month lengths exactly for small n's */
189 TimeFields
->Day
= yearday
- (1959 * months
) / 64 ;
193 /******************************************************************************
194 * RtlTimeFieldsToTime [NTDLL.@]
196 * Convert a TIME_FIELDS structure into a time.
199 * ftTimeFields [I] TIME_FIELDS structure to convert.
200 * Time [O] Destination for the converted time.
206 BOOLEAN WINAPI
RtlTimeFieldsToTime(
207 PTIME_FIELDS tfTimeFields
,
210 int month
, year
, cleaps
, day
;
212 /* FIXME: normalize the TIME_FIELDS structure here */
213 /* No, native just returns 0 (error) if the fields are not */
214 if( tfTimeFields
->Milliseconds
< 0 || tfTimeFields
->Milliseconds
> 999 ||
215 tfTimeFields
->Second
< 0 || tfTimeFields
->Second
> 59 ||
216 tfTimeFields
->Minute
< 0 || tfTimeFields
->Minute
> 59 ||
217 tfTimeFields
->Hour
< 0 || tfTimeFields
->Hour
> 23 ||
218 tfTimeFields
->Month
< 1 || tfTimeFields
->Month
> 12 ||
219 tfTimeFields
->Day
< 1 ||
220 tfTimeFields
->Day
> MonthLengths
221 [ tfTimeFields
->Month
==2 || IsLeapYear(tfTimeFields
->Year
)]
222 [ tfTimeFields
->Month
- 1] ||
223 tfTimeFields
->Year
< 1601 )
226 /* now calculate a day count from the date
227 * First start counting years from March. This way the leap days
228 * are added at the end of the year, not somewhere in the middle.
229 * Formula's become so much less complicate that way.
230 * To convert: add 12 to the month numbers of Jan and Feb, and
231 * take 1 from the year */
232 if(tfTimeFields
->Month
< 3) {
233 month
= tfTimeFields
->Month
+ 13;
234 year
= tfTimeFields
->Year
- 1;
236 month
= tfTimeFields
->Month
+ 1;
237 year
= tfTimeFields
->Year
;
239 cleaps
= (3 * (year
/ 100) + 3) / 4; /* nr of "century leap years"*/
240 day
= (36525 * year
) / 100 - cleaps
+ /* year * dayperyr, corrected */
241 (1959 * month
) / 64 + /* months * daypermonth */
242 tfTimeFields
->Day
- /* day of the month */
243 584817 ; /* zero that on 1601-01-01 */
246 Time
->QuadPart
= (((((LONGLONG
) day
* HOURSPERDAY
+
247 tfTimeFields
->Hour
) * MINSPERHOUR
+
248 tfTimeFields
->Minute
) * SECSPERMIN
+
249 tfTimeFields
->Second
) * 1000 +
250 tfTimeFields
->Milliseconds
) * TICKSPERMSEC
;
255 /***********************************************************************
256 * TIME_GetBias [internal]
258 * Helper function calculates delta local time from UTC.
261 * utc [I] The current utc time.
262 * pdaylight [I] Local daylight.
265 * The bias for the current timezone.
267 static LONG
TIME_GetBias(void)
269 static time_t last_utc
;
270 static LONG last_bias
;
276 RtlEnterCriticalSection( &TIME_tz_section
);
279 RTL_TIME_ZONE_INFORMATION tzi
;
280 int is_dst
= init_tz_info( &tzi
);
283 last_bias
= tzi
.Bias
;
284 last_bias
+= is_dst
? tzi
.DaylightBias
: tzi
.StandardBias
;
285 last_bias
*= SECSPERMIN
;
290 RtlLeaveCriticalSection( &TIME_tz_section
);
294 /******************************************************************************
295 * RtlLocalTimeToSystemTime [NTDLL.@]
297 * Convert a local time into system time.
300 * LocalTime [I] Local time to convert.
301 * SystemTime [O] Destination for the converted time.
304 * Success: STATUS_SUCCESS.
305 * Failure: An NTSTATUS error code indicating the problem.
307 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
308 PLARGE_INTEGER SystemTime
)
312 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
314 bias
= TIME_GetBias();
315 SystemTime
->QuadPart
= LocalTime
->QuadPart
+ bias
* (LONGLONG
)TICKSPERSEC
;
316 return STATUS_SUCCESS
;
319 /******************************************************************************
320 * RtlSystemTimeToLocalTime [NTDLL.@]
322 * Convert a system time into a local time.
325 * SystemTime [I] System time to convert.
326 * LocalTime [O] Destination for the converted time.
329 * Success: STATUS_SUCCESS.
330 * Failure: An NTSTATUS error code indicating the problem.
332 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
333 PLARGE_INTEGER LocalTime
)
337 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
339 bias
= TIME_GetBias();
340 LocalTime
->QuadPart
= SystemTime
->QuadPart
- bias
* (LONGLONG
)TICKSPERSEC
;
341 return STATUS_SUCCESS
;
344 /******************************************************************************
345 * RtlTimeToSecondsSince1970 [NTDLL.@]
347 * Convert a time into a count of seconds since 1970.
350 * Time [I] Time to convert.
351 * Seconds [O] Destination for the converted time.
355 * Failure: FALSE, if the resulting value will not fit in a DWORD.
357 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
359 ULONGLONG tmp
= Time
->QuadPart
/ TICKSPERSEC
- SECS_1601_TO_1970
;
360 if (tmp
> 0xffffffff) return FALSE
;
365 /******************************************************************************
366 * RtlTimeToSecondsSince1980 [NTDLL.@]
368 * Convert a time into a count of seconds since 1980.
371 * Time [I] Time to convert.
372 * Seconds [O] Destination for the converted time.
376 * Failure: FALSE, if the resulting value will not fit in a DWORD.
378 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
380 ULONGLONG tmp
= Time
->QuadPart
/ TICKSPERSEC
- SECS_1601_TO_1980
;
381 if (tmp
> 0xffffffff) return FALSE
;
386 /******************************************************************************
387 * RtlSecondsSince1970ToTime [NTDLL.@]
389 * Convert a count of seconds since 1970 to a time.
392 * Seconds [I] Time to convert.
393 * Time [O] Destination for the converted time.
398 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
400 Time
->QuadPart
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
403 /******************************************************************************
404 * RtlSecondsSince1980ToTime [NTDLL.@]
406 * Convert a count of seconds since 1980 to a time.
409 * Seconds [I] Time to convert.
410 * Time [O] Destination for the converted time.
415 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
417 Time
->QuadPart
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
420 /******************************************************************************
421 * RtlTimeToElapsedTimeFields [NTDLL.@]
423 * Convert a time to a count of elapsed seconds.
426 * Time [I] Time to convert.
427 * TimeFields [O] Destination for the converted time.
432 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
437 time
= Time
->QuadPart
/ TICKSPERSEC
;
438 TimeFields
->Milliseconds
= (Time
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
;
440 /* time is now in seconds */
441 TimeFields
->Year
= 0;
442 TimeFields
->Month
= 0;
443 TimeFields
->Day
= time
/ SECSPERDAY
;
445 /* rem is now the remaining seconds in the last day */
446 rem
= time
% SECSPERDAY
;
447 TimeFields
->Second
= rem
% 60;
449 TimeFields
->Minute
= rem
% 60;
450 TimeFields
->Hour
= rem
/ 60;
453 /***********************************************************************
454 * NtQuerySystemTime [NTDLL.@]
455 * ZwQuerySystemTime [NTDLL.@]
457 * Get the current system time.
460 * Time [O] Destination for the current system time.
463 * Success: STATUS_SUCCESS.
464 * Failure: An NTSTATUS error code indicating the problem.
466 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
470 gettimeofday( &now
, 0 );
471 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
472 Time
->QuadPart
+= now
.tv_usec
* 10;
473 return STATUS_SUCCESS
;
476 /******************************************************************************
477 * NtQueryPerformanceCounter [NTDLL.@]
479 NTSTATUS WINAPI
NtQueryPerformanceCounter( LARGE_INTEGER
*counter
, LARGE_INTEGER
*frequency
)
481 if (!counter
) return STATUS_ACCESS_VIOLATION
;
483 counter
->QuadPart
= monotonic_counter();
484 if (frequency
) frequency
->QuadPart
= TICKSPERSEC
;
485 return STATUS_SUCCESS
;
489 /******************************************************************************
490 * NtGetTickCount (NTDLL.@)
491 * ZwGetTickCount (NTDLL.@)
493 ULONG WINAPI
NtGetTickCount(void)
495 return monotonic_counter() / TICKSPERMSEC
;
498 /* calculate the mday of dst change date, so that for instance Sun 5 Oct 2007
499 * (last Sunday in October of 2007) becomes Sun Oct 28 2007
501 * Note: year, day and month must be in unix format.
503 static int weekday_to_mday(int year
, int day
, int mon
, int day_of_week
)
509 /* find first day in the month matching week day of the date */
510 memset(&date
, 0, sizeof(date
));
519 } while (date
.tm_wday
!= day_of_week
|| date
.tm_mon
!= mon
);
523 /* find number of week days in the month matching week day of the date */
524 wday
= 1; /* 1 - 1st, ...., 5 - last */
531 tm
= localtime(&tmp
);
532 if (tm
->tm_mon
!= mon
)
541 static BOOL
match_tz_date(const RTL_SYSTEM_TIME
*st
, const RTL_SYSTEM_TIME
*reg_st
)
545 if (st
->wMonth
!= reg_st
->wMonth
) return FALSE
;
547 if (!st
->wMonth
) return TRUE
; /* no transition dates */
550 if (!reg_st
->wYear
) /* date in a day-of-week format */
551 wDay
= weekday_to_mday(st
->wYear
- 1900, reg_st
->wDay
, reg_st
->wMonth
- 1, reg_st
->wDayOfWeek
);
553 if (st
->wDay
!= wDay
||
554 st
->wHour
!= reg_st
->wHour
||
555 st
->wMinute
!= reg_st
->wMinute
||
556 st
->wSecond
!= reg_st
->wSecond
||
557 st
->wMilliseconds
!= reg_st
->wMilliseconds
) return FALSE
;
562 static BOOL
match_tz_info(const RTL_TIME_ZONE_INFORMATION
*tzi
, const RTL_TIME_ZONE_INFORMATION
*reg_tzi
)
564 if (tzi
->Bias
== reg_tzi
->Bias
&&
565 match_tz_date(&tzi
->StandardDate
, ®_tzi
->StandardDate
) &&
566 match_tz_date(&tzi
->DaylightDate
, ®_tzi
->DaylightDate
))
572 static BOOL
reg_query_value(HKEY hkey
, LPCWSTR name
, DWORD type
, void *data
, DWORD count
)
574 UNICODE_STRING nameW
;
576 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buf
;
578 if (count
> sizeof(buf
) - sizeof(KEY_VALUE_PARTIAL_INFORMATION
))
581 RtlInitUnicodeString(&nameW
, name
);
583 if (NtQueryValueKey(hkey
, &nameW
, KeyValuePartialInformation
,
584 buf
, sizeof(buf
), &count
))
587 if (info
->Type
!= type
) return FALSE
;
589 memcpy(data
, info
->Data
, info
->DataLength
);
593 static void find_reg_tz_info(RTL_TIME_ZONE_INFORMATION
*tzi
)
595 static const WCHAR Time_ZonesW
[] = { 'M','a','c','h','i','n','e','\\',
596 'S','o','f','t','w','a','r','e','\\',
597 'M','i','c','r','o','s','o','f','t','\\',
598 'W','i','n','d','o','w','s',' ','N','T','\\',
599 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
600 'T','i','m','e',' ','Z','o','n','e','s',0 };
603 OBJECT_ATTRIBUTES attr
;
604 UNICODE_STRING nameW
;
607 attr
.Length
= sizeof(attr
);
608 attr
.RootDirectory
= 0;
609 attr
.ObjectName
= &nameW
;
611 attr
.SecurityDescriptor
= NULL
;
612 attr
.SecurityQualityOfService
= NULL
;
613 RtlInitUnicodeString(&nameW
, Time_ZonesW
);
614 if (NtOpenKey(&hkey
, KEY_READ
, &attr
))
616 WARN("Unable to open the time zones key\n");
622 nameW
.Length
= sizeof(buf
);
623 nameW
.MaximumLength
= sizeof(buf
);
625 while (!RtlpNtEnumerateSubKey(hkey
, &nameW
, idx
++))
627 static const WCHAR stdW
[] = { 'S','t','d',0 };
628 static const WCHAR dltW
[] = { 'D','l','t',0 };
629 static const WCHAR tziW
[] = { 'T','Z','I',0 };
630 RTL_TIME_ZONE_INFORMATION reg_tzi
;
637 RTL_SYSTEM_TIME std_date
;
638 RTL_SYSTEM_TIME dlt_date
;
641 attr
.Length
= sizeof(attr
);
642 attr
.RootDirectory
= hkey
;
643 attr
.ObjectName
= &nameW
;
645 attr
.SecurityDescriptor
= NULL
;
646 attr
.SecurityQualityOfService
= NULL
;
647 if (NtOpenKey(&hSubkey
, KEY_READ
, &attr
))
649 WARN("Unable to open subkey %s\n", debugstr_wn(nameW
.Buffer
, nameW
.Length
/sizeof(WCHAR
)));
653 #define get_value(hkey, name, type, data, len) \
654 if (!reg_query_value(hkey, name, type, data, len)) \
656 WARN("can't read data from %s\n", debugstr_w(name)); \
661 get_value(hSubkey
, stdW
, REG_SZ
, reg_tzi
.StandardName
, sizeof(reg_tzi
.StandardName
));
662 get_value(hSubkey
, dltW
, REG_SZ
, reg_tzi
.DaylightName
, sizeof(reg_tzi
.DaylightName
));
663 get_value(hSubkey
, tziW
, REG_BINARY
, &tz_data
, sizeof(tz_data
));
667 reg_tzi
.Bias
= tz_data
.bias
;
668 reg_tzi
.StandardBias
= tz_data
.std_bias
;
669 reg_tzi
.DaylightBias
= tz_data
.dlt_bias
;
670 reg_tzi
.StandardDate
= tz_data
.std_date
;
671 reg_tzi
.DaylightDate
= tz_data
.dlt_date
;
673 TRACE("%s: bias %d\n", debugstr_wn(nameW
.Buffer
, nameW
.Length
/sizeof(WCHAR
)), reg_tzi
.Bias
);
674 TRACE("std (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
675 reg_tzi
.StandardDate
.wDay
, reg_tzi
.StandardDate
.wMonth
,
676 reg_tzi
.StandardDate
.wYear
, reg_tzi
.StandardDate
.wDayOfWeek
,
677 reg_tzi
.StandardDate
.wHour
, reg_tzi
.StandardDate
.wMinute
,
678 reg_tzi
.StandardDate
.wSecond
, reg_tzi
.StandardDate
.wMilliseconds
,
679 reg_tzi
.StandardBias
);
680 TRACE("dst (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
681 reg_tzi
.DaylightDate
.wDay
, reg_tzi
.DaylightDate
.wMonth
,
682 reg_tzi
.DaylightDate
.wYear
, reg_tzi
.DaylightDate
.wDayOfWeek
,
683 reg_tzi
.DaylightDate
.wHour
, reg_tzi
.DaylightDate
.wMinute
,
684 reg_tzi
.DaylightDate
.wSecond
, reg_tzi
.DaylightDate
.wMilliseconds
,
685 reg_tzi
.DaylightBias
);
689 if (match_tz_info(tzi
, ®_tzi
))
697 nameW
.Length
= sizeof(buf
);
698 nameW
.MaximumLength
= sizeof(buf
);
703 FIXME("Can't find matching timezone information in the registry for "
704 "bias %d, std (d/m/y): %u/%02u/%04u, dlt (d/m/y): %u/%02u/%04u\n",
706 tzi
->StandardDate
.wDay
, tzi
->StandardDate
.wMonth
, tzi
->StandardDate
.wYear
,
707 tzi
->DaylightDate
.wDay
, tzi
->DaylightDate
.wMonth
, tzi
->DaylightDate
.wYear
);
710 static time_t find_dst_change(unsigned long min
, unsigned long max
, int *is_dst
)
716 tm
= localtime(&start
);
717 *is_dst
= !tm
->tm_isdst
;
718 TRACE("starting date isdst %d, %s", !*is_dst
, ctime(&start
));
722 time_t pos
= (min
+ max
) / 2;
723 tm
= localtime(&pos
);
725 if (tm
->tm_isdst
!= *is_dst
)
733 static int init_tz_info(RTL_TIME_ZONE_INFORMATION
*tzi
)
735 static RTL_TIME_ZONE_INFORMATION cached_tzi
;
736 static int current_year
= -1, current_bias
= 65535;
738 time_t year_start
, year_end
, tmp
, dlt
= 0, std
= 0;
739 int is_dst
, current_is_dst
, bias
;
741 RtlEnterCriticalSection( &TIME_tz_section
);
743 year_start
= time(NULL
);
744 tm
= gmtime(&year_start
);
745 bias
= (LONG
)(mktime(tm
) - year_start
) / 60;
747 tm
= localtime(&year_start
);
748 current_is_dst
= tm
->tm_isdst
;
749 if (current_year
== tm
->tm_year
&& current_bias
== bias
)
752 RtlLeaveCriticalSection( &TIME_tz_section
);
753 return current_is_dst
;
756 memset(tzi
, 0, sizeof(*tzi
));
758 TRACE("tz data will be valid through year %d, bias %d\n", tm
->tm_year
+ 1900, bias
);
759 current_year
= tm
->tm_year
;
766 tm
->tm_mon
= tm
->tm_hour
= tm
->tm_min
= tm
->tm_sec
= tm
->tm_wday
= tm
->tm_yday
= 0;
767 year_start
= mktime(tm
);
768 TRACE("year_start: %s", ctime(&year_start
));
770 tm
->tm_mday
= tm
->tm_wday
= tm
->tm_yday
= 0;
773 tm
->tm_min
= tm
->tm_sec
= 59;
774 year_end
= mktime(tm
);
775 TRACE("year_end: %s", ctime(&year_end
));
777 tmp
= find_dst_change(year_start
, year_end
, &is_dst
);
783 tmp
= find_dst_change(tmp
, year_end
, &is_dst
);
789 TRACE("std: %s", ctime(&std
));
790 TRACE("dlt: %s", ctime(&dlt
));
792 if (dlt
== std
|| !dlt
|| !std
)
793 TRACE("there is no daylight saving rules in this time zone\n");
796 tmp
= dlt
- tzi
->Bias
* 60;
798 TRACE("dlt gmtime: %s", asctime(tm
));
800 tzi
->DaylightBias
= -60;
801 tzi
->DaylightDate
.wYear
= tm
->tm_year
+ 1900;
802 tzi
->DaylightDate
.wMonth
= tm
->tm_mon
+ 1;
803 tzi
->DaylightDate
.wDayOfWeek
= tm
->tm_wday
;
804 tzi
->DaylightDate
.wDay
= tm
->tm_mday
;
805 tzi
->DaylightDate
.wHour
= tm
->tm_hour
;
806 tzi
->DaylightDate
.wMinute
= tm
->tm_min
;
807 tzi
->DaylightDate
.wSecond
= tm
->tm_sec
;
808 tzi
->DaylightDate
.wMilliseconds
= 0;
810 TRACE("daylight (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
811 tzi
->DaylightDate
.wDay
, tzi
->DaylightDate
.wMonth
,
812 tzi
->DaylightDate
.wYear
, tzi
->DaylightDate
.wDayOfWeek
,
813 tzi
->DaylightDate
.wHour
, tzi
->DaylightDate
.wMinute
,
814 tzi
->DaylightDate
.wSecond
, tzi
->DaylightDate
.wMilliseconds
,
817 tmp
= std
- tzi
->Bias
* 60 - tzi
->DaylightBias
* 60;
819 TRACE("std gmtime: %s", asctime(tm
));
821 tzi
->StandardBias
= 0;
822 tzi
->StandardDate
.wYear
= tm
->tm_year
+ 1900;
823 tzi
->StandardDate
.wMonth
= tm
->tm_mon
+ 1;
824 tzi
->StandardDate
.wDayOfWeek
= tm
->tm_wday
;
825 tzi
->StandardDate
.wDay
= tm
->tm_mday
;
826 tzi
->StandardDate
.wHour
= tm
->tm_hour
;
827 tzi
->StandardDate
.wMinute
= tm
->tm_min
;
828 tzi
->StandardDate
.wSecond
= tm
->tm_sec
;
829 tzi
->StandardDate
.wMilliseconds
= 0;
831 TRACE("standard (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
832 tzi
->StandardDate
.wDay
, tzi
->StandardDate
.wMonth
,
833 tzi
->StandardDate
.wYear
, tzi
->StandardDate
.wDayOfWeek
,
834 tzi
->StandardDate
.wHour
, tzi
->StandardDate
.wMinute
,
835 tzi
->StandardDate
.wSecond
, tzi
->StandardDate
.wMilliseconds
,
839 find_reg_tz_info(tzi
);
842 RtlLeaveCriticalSection( &TIME_tz_section
);
844 return current_is_dst
;
847 /***********************************************************************
848 * RtlQueryTimeZoneInformation [NTDLL.@]
850 * Get information about the current timezone.
853 * tzinfo [O] Destination for the retrieved timezone info.
856 * Success: STATUS_SUCCESS.
857 * Failure: An NTSTATUS error code indicating the problem.
859 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION
*tzinfo
)
861 init_tz_info( tzinfo
);
863 return STATUS_SUCCESS
;
866 /***********************************************************************
867 * RtlSetTimeZoneInformation [NTDLL.@]
869 * Set the current time zone information.
872 * tzinfo [I] Timezone information to set.
875 * Success: STATUS_SUCCESS.
876 * Failure: An NTSTATUS error code indicating the problem.
879 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION
*tzinfo
)
881 return STATUS_PRIVILEGE_NOT_HELD
;
884 /***********************************************************************
885 * NtSetSystemTime [NTDLL.@]
886 * ZwSetSystemTime [NTDLL.@]
888 * Set the system time.
891 * NewTime [I] The time to set.
892 * OldTime [O] Optional destination for the previous system time.
895 * Success: STATUS_SUCCESS.
896 * Failure: An NTSTATUS error code indicating the problem.
898 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
905 /* Return the old time if necessary */
906 if (!OldTime
) OldTime
= &tm
;
908 NtQuerySystemTime( OldTime
);
909 RtlTimeToSecondsSince1970( OldTime
, &oldsec
);
911 RtlTimeToSecondsSince1970( NewTime
, &sec
);
913 /* set the new time */
917 #ifdef HAVE_SETTIMEOFDAY
918 if (!settimeofday(&tv
, NULL
)) /* 0 is OK, -1 is error */
919 return STATUS_SUCCESS
;
921 ERR("Cannot set time to %s, time adjustment %ld: %s\n",
922 ctime(&tm_t
), (long)(sec
-oldsec
), strerror(errno
));
924 return STATUS_PRIVILEGE_NOT_HELD
;
926 return STATUS_INVALID_PARAMETER
;
929 FIXME("setting time to %s not implemented for missing settimeofday\n",
931 return STATUS_NOT_IMPLEMENTED
;