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 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
45 #define WIN32_NO_STATUS
48 #include "wine/unicode.h"
49 #include "wine/debug.h"
50 #include "ntdll_misc.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
54 static int init_tz_info(RTL_TIME_ZONE_INFORMATION *tzi);
56 static RTL_CRITICAL_SECTION TIME_tz_section;
57 static RTL_CRITICAL_SECTION_DEBUG critsect_debug =
59 0, 0, &TIME_tz_section,
60 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
61 0, 0, { (DWORD_PTR)(__FILE__ ": TIME_tz_section") }
63 static RTL_CRITICAL_SECTION TIME_tz_section = { &critsect_debug, -1, 0, 0, 0, 0 };
65 #define SETTIME_MAX_ADJUST 120
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 EPOCHYEAR 1601
77 #define DAYSPERNORMALYEAR 365
78 #define DAYSPERLEAPYEAR 366
79 #define MONSPERYEAR 12
80 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
81 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
82 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
84 /* 1601 to 1970 is 369 years plus 89 leap days */
85 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
86 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
87 /* 1601 to 1980 is 379 years plus 91 leap days */
88 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
89 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
90 /* max ticks that can be represented as Unix time */
91 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
94 static const int MonthLengths[2][MONSPERYEAR] =
96 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
97 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
100 static inline int IsLeapYear(int Year)
102 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
105 /******************************************************************************
106 * RtlTimeToTimeFields [NTDLL.@]
108 * Convert a time into a TIME_FIELDS structure.
111 * liTime [I] Time to convert.
112 * TimeFields [O] Destination for the converted time.
117 VOID WINAPI RtlTimeToTimeFields(
118 const LARGE_INTEGER *liTime,
119 PTIME_FIELDS TimeFields)
122 long int cleaps, years, yearday, months;
126 /* Extract millisecond from time and convert time into seconds */
127 TimeFields->Milliseconds =
128 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
129 Time = liTime->QuadPart / TICKSPERSEC;
131 /* The native version of RtlTimeToTimeFields does not take leap seconds
134 /* Split the time into days and seconds within the day */
135 Days = Time / SECSPERDAY;
136 SecondsInDay = Time % SECSPERDAY;
138 /* compute time of day */
139 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
140 SecondsInDay = SecondsInDay % SECSPERHOUR;
141 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
142 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
144 /* compute day of week */
145 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
147 /* compute year, month and day of month. */
148 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
149 Days += 28188 + cleaps;
150 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
151 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
152 months = (64 * yearday) / 1959;
153 /* the result is based on a year starting on March.
154 * To convert take 12 from Januari and Februari and
155 * increase the year by one. */
157 TimeFields->Month = months - 1;
158 TimeFields->Year = years + 1524;
160 TimeFields->Month = months - 13;
161 TimeFields->Year = years + 1525;
163 /* calculation of day of month is based on the wonderful
164 * sequence of INT( n * 30.6): it reproduces the
165 * 31-30-31-30-31-31 month lengths exactly for small n's */
166 TimeFields->Day = yearday - (1959 * months) / 64 ;
170 /******************************************************************************
171 * RtlTimeFieldsToTime [NTDLL.@]
173 * Convert a TIME_FIELDS structure into a time.
176 * ftTimeFields [I] TIME_FIELDS structure to convert.
177 * Time [O] Destination for the converted time.
183 BOOLEAN WINAPI RtlTimeFieldsToTime(
184 PTIME_FIELDS tfTimeFields,
187 int month, year, cleaps, day;
189 /* FIXME: normalize the TIME_FIELDS structure here */
190 /* No, native just returns 0 (error) if the fields are not */
191 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
192 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
193 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
194 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
195 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
196 tfTimeFields->Day < 1 ||
197 tfTimeFields->Day > MonthLengths
198 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
199 [ tfTimeFields->Month - 1] ||
200 tfTimeFields->Year < 1601 )
203 /* now calculate a day count from the date
204 * First start counting years from March. This way the leap days
205 * are added at the end of the year, not somewhere in the middle.
206 * Formula's become so much less complicate that way.
207 * To convert: add 12 to the month numbers of Jan and Feb, and
208 * take 1 from the year */
209 if(tfTimeFields->Month < 3) {
210 month = tfTimeFields->Month + 13;
211 year = tfTimeFields->Year - 1;
213 month = tfTimeFields->Month + 1;
214 year = tfTimeFields->Year;
216 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
217 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
218 (1959 * month) / 64 + /* months * daypermonth */
219 tfTimeFields->Day - /* day of the month */
220 584817 ; /* zero that on 1601-01-01 */
223 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
224 tfTimeFields->Hour) * MINSPERHOUR +
225 tfTimeFields->Minute) * SECSPERMIN +
226 tfTimeFields->Second ) * 1000 +
227 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
232 /***********************************************************************
233 * TIME_GetBias [internal]
235 * Helper function calculates delta local time from UTC.
238 * utc [I] The current utc time.
239 * pdaylight [I] Local daylight.
242 * The bias for the current timezone.
244 static LONG TIME_GetBias(void)
246 static time_t last_utc;
247 static LONG last_bias;
253 RtlEnterCriticalSection( &TIME_tz_section );
256 RTL_TIME_ZONE_INFORMATION tzi;
257 int is_dst = init_tz_info( &tzi );
260 last_bias = tzi.Bias;
261 last_bias += is_dst ? tzi.DaylightBias : tzi.StandardBias;
262 last_bias *= SECSPERMIN;
267 RtlLeaveCriticalSection( &TIME_tz_section );
271 /******************************************************************************
272 * RtlLocalTimeToSystemTime [NTDLL.@]
274 * Convert a local time into system time.
277 * LocalTime [I] Local time to convert.
278 * SystemTime [O] Destination for the converted time.
281 * Success: STATUS_SUCCESS.
282 * Failure: An NTSTATUS error code indicating the problem.
284 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
285 PLARGE_INTEGER SystemTime)
289 TRACE("(%p, %p)\n", LocalTime, SystemTime);
291 bias = TIME_GetBias();
292 SystemTime->QuadPart = LocalTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
293 return STATUS_SUCCESS;
296 /******************************************************************************
297 * RtlSystemTimeToLocalTime [NTDLL.@]
299 * Convert a system time into a local time.
302 * SystemTime [I] System time to convert.
303 * LocalTime [O] Destination for the converted time.
306 * Success: STATUS_SUCCESS.
307 * Failure: An NTSTATUS error code indicating the problem.
309 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
310 PLARGE_INTEGER LocalTime )
314 TRACE("(%p, %p)\n", SystemTime, LocalTime);
316 bias = TIME_GetBias();
317 LocalTime->QuadPart = SystemTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
318 return STATUS_SUCCESS;
321 /******************************************************************************
322 * RtlTimeToSecondsSince1970 [NTDLL.@]
324 * Convert a time into a count of seconds since 1970.
327 * Time [I] Time to convert.
328 * Seconds [O] Destination for the converted time.
332 * Failure: FALSE, if the resulting value will not fit in a DWORD.
334 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
336 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
337 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
338 tmp -= SECS_1601_TO_1970;
339 if (tmp > 0xffffffff) return FALSE;
340 *Seconds = (DWORD)tmp;
344 /******************************************************************************
345 * RtlTimeToSecondsSince1980 [NTDLL.@]
347 * Convert a time into a count of seconds since 1980.
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 RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
359 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
360 tmp = RtlLargeIntegerDivide( tmp, TICKSPERSEC, NULL );
361 tmp -= SECS_1601_TO_1980;
362 if (tmp > 0xffffffff) return FALSE;
363 *Seconds = (DWORD)tmp;
367 /******************************************************************************
368 * RtlSecondsSince1970ToTime [NTDLL.@]
370 * Convert a count of seconds since 1970 to a time.
373 * Seconds [I] Time to convert.
374 * Time [O] Destination for the converted time.
379 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
381 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
382 Time->u.LowPart = (DWORD)secs;
383 Time->u.HighPart = (DWORD)(secs >> 32);
386 /******************************************************************************
387 * RtlSecondsSince1980ToTime [NTDLL.@]
389 * Convert a count of seconds since 1980 to a time.
392 * Seconds [I] Time to convert.
393 * Time [O] Destination for the converted time.
398 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
400 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
401 Time->u.LowPart = (DWORD)secs;
402 Time->u.HighPart = (DWORD)(secs >> 32);
405 /******************************************************************************
406 * RtlTimeToElapsedTimeFields [NTDLL.@]
408 * Convert a time to a count of elapsed seconds.
411 * Time [I] Time to convert.
412 * TimeFields [O] Destination for the converted time.
417 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
422 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
423 TimeFields->Milliseconds = rem / TICKSPERMSEC;
425 /* time is now in seconds */
426 TimeFields->Year = 0;
427 TimeFields->Month = 0;
428 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
430 /* rem is now the remaining seconds in the last day */
431 TimeFields->Second = rem % 60;
433 TimeFields->Minute = rem % 60;
434 TimeFields->Hour = rem / 60;
437 /***********************************************************************
438 * NtQuerySystemTime [NTDLL.@]
439 * ZwQuerySystemTime [NTDLL.@]
441 * Get the current system time.
444 * Time [O] Destination for the current system time.
447 * Success: STATUS_SUCCESS.
448 * Failure: An NTSTATUS error code indicating the problem.
450 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
454 gettimeofday( &now, 0 );
455 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
456 Time->QuadPart += now.tv_usec * 10;
457 return STATUS_SUCCESS;
460 /******************************************************************************
461 * NtQueryPerformanceCounter [NTDLL.@]
463 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
464 * good number of applications that crash when the returned frequency is either
465 * lower or higher than what Windows gives. Also too high counter values are
466 * reported to give problems.
468 NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
472 if (!Counter) return STATUS_ACCESS_VIOLATION;
474 /* convert a counter that increments at a rate of 10 MHz
475 * to one of 1.193182 MHz, with some care for arithmetic
476 * overflow and good accuracy (21/176 = 0.11931818) */
477 NtQuerySystemTime( &now );
478 Counter->QuadPart = ((now.QuadPart - server_start_time) * 21) / 176;
479 if (Frequency) Frequency->QuadPart = 1193182;
480 return STATUS_SUCCESS;
484 /******************************************************************************
485 * NtGetTickCount (NTDLL.@)
486 * ZwGetTickCount (NTDLL.@)
488 ULONG WINAPI NtGetTickCount(void)
492 NtQuerySystemTime( &now );
493 return (now.QuadPart - server_start_time) / 10000;
496 /* calculate the mday of dst change date, so that for instance Sun 5 Oct 2007
497 * (last Sunday in October of 2007) becomes Sun Oct 28 2007
499 * Note: year, day and month must be in unix format.
501 static int weekday_to_mday(int year, int day, int mon, int day_of_week)
507 /* find first day in the month matching week day of the date */
508 memset(&date, 0, sizeof(date));
517 } while (date.tm_wday != day_of_week || date.tm_mon != mon);
521 /* find number of week days in the month matching week day of the date */
522 wday = 1; /* 1 - 1st, ...., 5 - last */
529 tm = localtime(&tmp);
530 if (tm->tm_mon != mon)
539 static BOOL match_tz_date(const RTL_SYSTEM_TIME *st, const RTL_SYSTEM_TIME *reg_st)
543 if (st->wMonth != reg_st->wMonth) return FALSE;
545 if (!st->wMonth) return TRUE; /* no transition dates */
548 if (!reg_st->wYear) /* date in a day-of-week format */
549 wDay = weekday_to_mday(st->wYear - 1900, reg_st->wDay, reg_st->wMonth - 1, reg_st->wDayOfWeek);
551 if (st->wDay != wDay ||
552 st->wHour != reg_st->wHour ||
553 st->wMinute != reg_st->wMinute ||
554 st->wSecond != reg_st->wSecond ||
555 st->wMilliseconds != reg_st->wMilliseconds) return FALSE;
560 static BOOL match_tz_info(const RTL_TIME_ZONE_INFORMATION *tzi, const RTL_TIME_ZONE_INFORMATION *reg_tzi)
562 if (tzi->Bias == reg_tzi->Bias &&
563 match_tz_date(&tzi->StandardDate, ®_tzi->StandardDate) &&
564 match_tz_date(&tzi->DaylightDate, ®_tzi->DaylightDate))
570 static BOOL reg_query_value(HKEY hkey, LPCWSTR name, DWORD type, void *data, DWORD count)
572 UNICODE_STRING nameW;
574 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buf;
576 if (count > sizeof(buf) - sizeof(KEY_VALUE_PARTIAL_INFORMATION))
579 RtlInitUnicodeString(&nameW, name);
581 if (NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation,
582 buf, sizeof(buf), &count))
585 if (info->Type != type) return FALSE;
587 memcpy(data, info->Data, info->DataLength);
591 static void find_reg_tz_info(RTL_TIME_ZONE_INFORMATION *tzi)
593 static const WCHAR Time_ZonesW[] = { 'M','a','c','h','i','n','e','\\',
594 'S','o','f','t','w','a','r','e','\\',
595 'M','i','c','r','o','s','o','f','t','\\',
596 'W','i','n','d','o','w','s',' ','N','T','\\',
597 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
598 'T','i','m','e',' ','Z','o','n','e','s',0 };
601 OBJECT_ATTRIBUTES attr;
602 UNICODE_STRING nameW;
605 attr.Length = sizeof(attr);
606 attr.RootDirectory = 0;
607 attr.ObjectName = &nameW;
609 attr.SecurityDescriptor = NULL;
610 attr.SecurityQualityOfService = NULL;
611 RtlInitUnicodeString(&nameW, Time_ZonesW);
612 if (NtOpenKey(&hkey, KEY_READ, &attr))
614 WARN("Unable to open the time zones key\n");
620 nameW.Length = sizeof(buf);
621 nameW.MaximumLength = sizeof(buf);
623 while (!RtlpNtEnumerateSubKey(hkey, &nameW, idx++))
625 static const WCHAR stdW[] = { 'S','t','d',0 };
626 static const WCHAR dltW[] = { 'D','l','t',0 };
627 static const WCHAR tziW[] = { 'T','Z','I',0 };
628 RTL_TIME_ZONE_INFORMATION reg_tzi;
635 RTL_SYSTEM_TIME std_date;
636 RTL_SYSTEM_TIME dlt_date;
639 attr.Length = sizeof(attr);
640 attr.RootDirectory = hkey;
641 attr.ObjectName = &nameW;
643 attr.SecurityDescriptor = NULL;
644 attr.SecurityQualityOfService = NULL;
645 if (NtOpenKey(&hSubkey, KEY_READ, &attr))
647 WARN("Unable to open subkey %s\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)));
651 #define get_value(hkey, name, type, data, len) \
652 if (!reg_query_value(hkey, name, type, data, len)) \
654 WARN("can't read data from %s\n", debugstr_w(name)); \
659 get_value(hSubkey, stdW, REG_SZ, reg_tzi.StandardName, sizeof(reg_tzi.StandardName));
660 get_value(hSubkey, dltW, REG_SZ, reg_tzi.DaylightName, sizeof(reg_tzi.DaylightName));
661 get_value(hSubkey, tziW, REG_BINARY, &tz_data, sizeof(tz_data));
665 reg_tzi.Bias = tz_data.bias;
666 reg_tzi.StandardBias = tz_data.std_bias;
667 reg_tzi.DaylightBias = tz_data.dlt_bias;
668 reg_tzi.StandardDate = tz_data.std_date;
669 reg_tzi.DaylightDate = tz_data.dlt_date;
671 TRACE("%s: bias %d\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)), reg_tzi.Bias);
672 TRACE("std (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
673 reg_tzi.StandardDate.wDay, reg_tzi.StandardDate.wMonth,
674 reg_tzi.StandardDate.wYear, reg_tzi.StandardDate.wDayOfWeek,
675 reg_tzi.StandardDate.wHour, reg_tzi.StandardDate.wMinute,
676 reg_tzi.StandardDate.wSecond, reg_tzi.StandardDate.wMilliseconds,
677 reg_tzi.StandardBias);
678 TRACE("dst (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
679 reg_tzi.DaylightDate.wDay, reg_tzi.DaylightDate.wMonth,
680 reg_tzi.DaylightDate.wYear, reg_tzi.DaylightDate.wDayOfWeek,
681 reg_tzi.DaylightDate.wHour, reg_tzi.DaylightDate.wMinute,
682 reg_tzi.DaylightDate.wSecond, reg_tzi.DaylightDate.wMilliseconds,
683 reg_tzi.DaylightBias);
687 if (match_tz_info(tzi, ®_tzi))
689 memcpy(tzi, ®_tzi, sizeof(*tzi));
695 nameW.Length = sizeof(buf);
696 nameW.MaximumLength = sizeof(buf);
701 FIXME("Can't find matching timezone information in the registry for "
702 "bias %d, std (d/m/y): %u/%02u/%04u, dlt (d/m/y): %u/%02u/%04u\n",
704 tzi->StandardDate.wDay, tzi->StandardDate.wMonth, tzi->StandardDate.wYear,
705 tzi->DaylightDate.wDay, tzi->DaylightDate.wMonth, tzi->DaylightDate.wYear);
708 static time_t find_dst_change(unsigned long min, unsigned long max, int *is_dst)
714 tm = localtime(&start);
715 *is_dst = !tm->tm_isdst;
716 TRACE("starting date isdst %d, %s", !*is_dst, ctime(&start));
720 time_t pos = (min + max) / 2;
721 tm = localtime(&pos);
723 if (tm->tm_isdst != *is_dst)
731 static int init_tz_info(RTL_TIME_ZONE_INFORMATION *tzi)
733 static RTL_TIME_ZONE_INFORMATION cached_tzi;
734 static int current_year = -1;
736 time_t year_start, year_end, tmp, dlt = 0, std = 0;
737 int is_dst, current_is_dst;
739 RtlEnterCriticalSection( &TIME_tz_section );
741 year_start = time(NULL);
742 tm = localtime(&year_start);
744 current_is_dst = tm->tm_isdst;
745 if (current_year == tm->tm_year)
748 RtlLeaveCriticalSection( &TIME_tz_section );
749 return current_is_dst;
752 memset(tzi, 0, sizeof(*tzi));
754 TRACE("tz data will be valid through year %d\n", tm->tm_year + 1900);
755 current_year = tm->tm_year;
759 tm->tm_mon = tm->tm_hour = tm->tm_min = tm->tm_sec = tm->tm_wday = tm->tm_yday = 0;
760 year_start = mktime(tm);
761 TRACE("year_start: %s", ctime(&year_start));
763 tm->tm_mday = tm->tm_wday = tm->tm_yday = 0;
766 tm->tm_min = tm->tm_sec = 59;
767 year_end = mktime(tm);
768 TRACE("year_end: %s", ctime(&year_end));
770 tm = gmtime(&year_start);
771 tzi->Bias = (LONG)(mktime(tm) - year_start) / 60;
772 TRACE("bias: %d\n", tzi->Bias);
774 tmp = find_dst_change(year_start, year_end, &is_dst);
780 tmp = find_dst_change(tmp, year_end, &is_dst);
786 TRACE("std: %s", ctime(&std));
787 TRACE("dlt: %s", ctime(&dlt));
789 if (dlt == std || !dlt || !std)
791 RtlLeaveCriticalSection( &TIME_tz_section );
792 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,
838 find_reg_tz_info(tzi);
841 RtlLeaveCriticalSection( &TIME_tz_section );
843 return current_is_dst;
846 /***********************************************************************
847 * RtlQueryTimeZoneInformation [NTDLL.@]
849 * Get information about the current timezone.
852 * tzinfo [O] Destination for the retrieved timezone info.
855 * Success: STATUS_SUCCESS.
856 * Failure: An NTSTATUS error code indicating the problem.
858 NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
860 init_tz_info( tzinfo );
862 return STATUS_SUCCESS;
865 /***********************************************************************
866 * RtlSetTimeZoneInformation [NTDLL.@]
868 * Set the current time zone information.
871 * tzinfo [I] Timezone information to set.
874 * Success: STATUS_SUCCESS.
875 * Failure: An NTSTATUS error code indicating the problem.
878 NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
880 return STATUS_PRIVILEGE_NOT_HELD;
883 /***********************************************************************
884 * NtSetSystemTime [NTDLL.@]
885 * ZwSetSystemTime [NTDLL.@]
887 * Set the system time.
890 * NewTime [I] The time to set.
891 * OldTime [O] Optional destination for the previous system time.
894 * Success: STATUS_SUCCESS.
895 * Failure: An NTSTATUS error code indicating the problem.
897 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 /* error and sanity check*/
918 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
921 #ifdef HAVE_SETTIMEOFDAY
922 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
924 return STATUS_SUCCESS;
931 ERR("Cannot set time to %s Time adjustment %ld %s\n",
934 err == -1 ? "No Permission"
935 : sec == (time_t)-1 ? "" : "is too large." );
938 return STATUS_INVALID_PARAMETER;
940 return STATUS_PRIVILEGE_NOT_HELD;
942 return STATUS_NOT_IMPLEMENTED;