shell32: Initialize fAnyOperationsAborted in SHFileOperation.
[wine.git] / dlls / ntdll / time.c
bloba8b13768450d9c759e504fb0f2f7479be4497c18
1 /*
2 * Nt time functions.
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
26 #include "config.h"
27 #include "wine/port.h"
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <time.h>
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "ntstatus.h"
45 #define WIN32_NO_STATUS
46 #include "windef.h"
47 #include "winternl.h"
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 TICKSPERSEC 10000000
66 #define TICKSPERMSEC 10000
67 #define SECSPERDAY 86400
68 #define SECSPERHOUR 3600
69 #define SECSPERMIN 60
70 #define MINSPERHOUR 60
71 #define HOURSPERDAY 24
72 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
73 #define DAYSPERWEEK 7
74 #define EPOCHYEAR 1601
75 #define DAYSPERNORMALYEAR 365
76 #define DAYSPERLEAPYEAR 366
77 #define MONSPERYEAR 12
78 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
79 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
80 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
82 /* 1601 to 1970 is 369 years plus 89 leap days */
83 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
84 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
85 /* 1601 to 1980 is 379 years plus 91 leap days */
86 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
87 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
88 /* max ticks that can be represented as Unix time */
89 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
92 static const int MonthLengths[2][MONSPERYEAR] =
94 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
95 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
98 static inline int IsLeapYear(int Year)
100 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
103 /******************************************************************************
104 * RtlTimeToTimeFields [NTDLL.@]
106 * Convert a time into a TIME_FIELDS structure.
108 * PARAMS
109 * liTime [I] Time to convert.
110 * TimeFields [O] Destination for the converted time.
112 * RETURNS
113 * Nothing.
115 VOID WINAPI RtlTimeToTimeFields(
116 const LARGE_INTEGER *liTime,
117 PTIME_FIELDS TimeFields)
119 int SecondsInDay;
120 long int cleaps, years, yearday, months;
121 long int Days;
122 LONGLONG Time;
124 /* Extract millisecond from time and convert time into seconds */
125 TimeFields->Milliseconds =
126 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
127 Time = liTime->QuadPart / TICKSPERSEC;
129 /* The native version of RtlTimeToTimeFields does not take leap seconds
130 * into account */
132 /* Split the time into days and seconds within the day */
133 Days = Time / SECSPERDAY;
134 SecondsInDay = Time % SECSPERDAY;
136 /* compute time of day */
137 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
138 SecondsInDay = SecondsInDay % SECSPERHOUR;
139 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
140 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
142 /* compute day of week */
143 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
145 /* compute year, month and day of month. */
146 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
147 Days += 28188 + cleaps;
148 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
149 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
150 months = (64 * yearday) / 1959;
151 /* the result is based on a year starting on March.
152 * To convert take 12 from Januari and Februari and
153 * increase the year by one. */
154 if( months < 14 ) {
155 TimeFields->Month = months - 1;
156 TimeFields->Year = years + 1524;
157 } else {
158 TimeFields->Month = months - 13;
159 TimeFields->Year = years + 1525;
161 /* calculation of day of month is based on the wonderful
162 * sequence of INT( n * 30.6): it reproduces the
163 * 31-30-31-30-31-31 month lengths exactly for small n's */
164 TimeFields->Day = yearday - (1959 * months) / 64 ;
165 return;
168 /******************************************************************************
169 * RtlTimeFieldsToTime [NTDLL.@]
171 * Convert a TIME_FIELDS structure into a time.
173 * PARAMS
174 * ftTimeFields [I] TIME_FIELDS structure to convert.
175 * Time [O] Destination for the converted time.
177 * RETURNS
178 * Success: TRUE.
179 * Failure: FALSE.
181 BOOLEAN WINAPI RtlTimeFieldsToTime(
182 PTIME_FIELDS tfTimeFields,
183 PLARGE_INTEGER Time)
185 int month, year, cleaps, day;
187 /* FIXME: normalize the TIME_FIELDS structure here */
188 /* No, native just returns 0 (error) if the fields are not */
189 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
190 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
191 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
192 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
193 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
194 tfTimeFields->Day < 1 ||
195 tfTimeFields->Day > MonthLengths
196 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
197 [ tfTimeFields->Month - 1] ||
198 tfTimeFields->Year < 1601 )
199 return FALSE;
201 /* now calculate a day count from the date
202 * First start counting years from March. This way the leap days
203 * are added at the end of the year, not somewhere in the middle.
204 * Formula's become so much less complicate that way.
205 * To convert: add 12 to the month numbers of Jan and Feb, and
206 * take 1 from the year */
207 if(tfTimeFields->Month < 3) {
208 month = tfTimeFields->Month + 13;
209 year = tfTimeFields->Year - 1;
210 } else {
211 month = tfTimeFields->Month + 1;
212 year = tfTimeFields->Year;
214 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
215 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
216 (1959 * month) / 64 + /* months * daypermonth */
217 tfTimeFields->Day - /* day of the month */
218 584817 ; /* zero that on 1601-01-01 */
219 /* done */
221 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
222 tfTimeFields->Hour) * MINSPERHOUR +
223 tfTimeFields->Minute) * SECSPERMIN +
224 tfTimeFields->Second ) * 1000 +
225 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
227 return TRUE;
230 /***********************************************************************
231 * TIME_GetBias [internal]
233 * Helper function calculates delta local time from UTC.
235 * PARAMS
236 * utc [I] The current utc time.
237 * pdaylight [I] Local daylight.
239 * RETURNS
240 * The bias for the current timezone.
242 static LONG TIME_GetBias(void)
244 static time_t last_utc;
245 static LONG last_bias;
246 LONG ret;
247 time_t utc;
249 utc = time( NULL );
251 RtlEnterCriticalSection( &TIME_tz_section );
252 if (utc != last_utc)
254 RTL_TIME_ZONE_INFORMATION tzi;
255 int is_dst = init_tz_info( &tzi );
257 last_utc = utc;
258 last_bias = tzi.Bias;
259 last_bias += is_dst ? tzi.DaylightBias : tzi.StandardBias;
260 last_bias *= SECSPERMIN;
263 ret = last_bias;
265 RtlLeaveCriticalSection( &TIME_tz_section );
266 return ret;
269 /******************************************************************************
270 * RtlLocalTimeToSystemTime [NTDLL.@]
272 * Convert a local time into system time.
274 * PARAMS
275 * LocalTime [I] Local time to convert.
276 * SystemTime [O] Destination for the converted time.
278 * RETURNS
279 * Success: STATUS_SUCCESS.
280 * Failure: An NTSTATUS error code indicating the problem.
282 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
283 PLARGE_INTEGER SystemTime)
285 LONG bias;
287 TRACE("(%p, %p)\n", LocalTime, SystemTime);
289 bias = TIME_GetBias();
290 SystemTime->QuadPart = LocalTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
291 return STATUS_SUCCESS;
294 /******************************************************************************
295 * RtlSystemTimeToLocalTime [NTDLL.@]
297 * Convert a system time into a local time.
299 * PARAMS
300 * SystemTime [I] System time to convert.
301 * LocalTime [O] Destination for the converted time.
303 * RETURNS
304 * Success: STATUS_SUCCESS.
305 * Failure: An NTSTATUS error code indicating the problem.
307 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
308 PLARGE_INTEGER LocalTime )
310 LONG bias;
312 TRACE("(%p, %p)\n", SystemTime, LocalTime);
314 bias = TIME_GetBias();
315 LocalTime->QuadPart = SystemTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
316 return STATUS_SUCCESS;
319 /******************************************************************************
320 * RtlTimeToSecondsSince1970 [NTDLL.@]
322 * Convert a time into a count of seconds since 1970.
324 * PARAMS
325 * Time [I] Time to convert.
326 * Seconds [O] Destination for the converted time.
328 * RETURNS
329 * Success: TRUE.
330 * Failure: FALSE, if the resulting value will not fit in a DWORD.
332 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
334 ULONGLONG tmp = Time->QuadPart / TICKSPERSEC - SECS_1601_TO_1970;
335 if (tmp > 0xffffffff) return FALSE;
336 *Seconds = tmp;
337 return TRUE;
340 /******************************************************************************
341 * RtlTimeToSecondsSince1980 [NTDLL.@]
343 * Convert a time into a count of seconds since 1980.
345 * PARAMS
346 * Time [I] Time to convert.
347 * Seconds [O] Destination for the converted time.
349 * RETURNS
350 * Success: TRUE.
351 * Failure: FALSE, if the resulting value will not fit in a DWORD.
353 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
355 ULONGLONG tmp = Time->QuadPart / TICKSPERSEC - SECS_1601_TO_1980;
356 if (tmp > 0xffffffff) return FALSE;
357 *Seconds = tmp;
358 return TRUE;
361 /******************************************************************************
362 * RtlSecondsSince1970ToTime [NTDLL.@]
364 * Convert a count of seconds since 1970 to a time.
366 * PARAMS
367 * Seconds [I] Time to convert.
368 * Time [O] Destination for the converted time.
370 * RETURNS
371 * Nothing.
373 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
375 Time->QuadPart = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
378 /******************************************************************************
379 * RtlSecondsSince1980ToTime [NTDLL.@]
381 * Convert a count of seconds since 1980 to a time.
383 * PARAMS
384 * Seconds [I] Time to convert.
385 * Time [O] Destination for the converted time.
387 * RETURNS
388 * Nothing.
390 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
392 Time->QuadPart = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
395 /******************************************************************************
396 * RtlTimeToElapsedTimeFields [NTDLL.@]
398 * Convert a time to a count of elapsed seconds.
400 * PARAMS
401 * Time [I] Time to convert.
402 * TimeFields [O] Destination for the converted time.
404 * RETURNS
405 * Nothing.
407 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
409 LONGLONG time;
410 INT rem;
412 time = Time->QuadPart / TICKSPERSEC;
413 TimeFields->Milliseconds = (Time->QuadPart % TICKSPERSEC) / TICKSPERMSEC;
415 /* time is now in seconds */
416 TimeFields->Year = 0;
417 TimeFields->Month = 0;
418 TimeFields->Day = time / SECSPERDAY;
420 /* rem is now the remaining seconds in the last day */
421 rem = time % SECSPERDAY;
422 TimeFields->Second = rem % 60;
423 rem /= 60;
424 TimeFields->Minute = rem % 60;
425 TimeFields->Hour = rem / 60;
428 /***********************************************************************
429 * NtQuerySystemTime [NTDLL.@]
430 * ZwQuerySystemTime [NTDLL.@]
432 * Get the current system time.
434 * PARAMS
435 * Time [O] Destination for the current system time.
437 * RETURNS
438 * Success: STATUS_SUCCESS.
439 * Failure: An NTSTATUS error code indicating the problem.
441 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
443 struct timeval now;
445 gettimeofday( &now, 0 );
446 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
447 Time->QuadPart += now.tv_usec * 10;
448 return STATUS_SUCCESS;
451 /******************************************************************************
452 * NtQueryPerformanceCounter [NTDLL.@]
454 * Note: Windows uses a timer clocked at a multiple of 1193182 Hz. There is a
455 * good number of applications that crash when the returned frequency is either
456 * lower or higher than what Windows gives. Also too high counter values are
457 * reported to give problems.
459 NTSTATUS WINAPI NtQueryPerformanceCounter( PLARGE_INTEGER Counter, PLARGE_INTEGER Frequency )
461 LARGE_INTEGER now;
463 if (!Counter) return STATUS_ACCESS_VIOLATION;
465 /* convert a counter that increments at a rate of 10 MHz
466 * to one of 1.193182 MHz, with some care for arithmetic
467 * overflow and good accuracy (21/176 = 0.11931818) */
468 NtQuerySystemTime( &now );
469 Counter->QuadPart = ((now.QuadPart - server_start_time) * 21) / 176;
470 if (Frequency) Frequency->QuadPart = 1193182;
471 return STATUS_SUCCESS;
475 /******************************************************************************
476 * NtGetTickCount (NTDLL.@)
477 * ZwGetTickCount (NTDLL.@)
479 ULONG WINAPI NtGetTickCount(void)
481 LARGE_INTEGER now;
483 NtQuerySystemTime( &now );
484 return (now.QuadPart - server_start_time) / 10000;
487 /* calculate the mday of dst change date, so that for instance Sun 5 Oct 2007
488 * (last Sunday in October of 2007) becomes Sun Oct 28 2007
490 * Note: year, day and month must be in unix format.
492 static int weekday_to_mday(int year, int day, int mon, int day_of_week)
494 struct tm date;
495 time_t tmp;
496 int wday, mday;
498 /* find first day in the month matching week day of the date */
499 memset(&date, 0, sizeof(date));
500 date.tm_year = year;
501 date.tm_mon = mon;
502 date.tm_mday = -1;
503 date.tm_wday = -1;
506 date.tm_mday++;
507 tmp = mktime(&date);
508 } while (date.tm_wday != day_of_week || date.tm_mon != mon);
510 mday = date.tm_mday;
512 /* find number of week days in the month matching week day of the date */
513 wday = 1; /* 1 - 1st, ...., 5 - last */
514 while (wday < day)
516 struct tm *tm;
518 date.tm_mday += 7;
519 tmp = mktime(&date);
520 tm = localtime(&tmp);
521 if (tm->tm_mon != mon)
522 break;
523 mday = tm->tm_mday;
524 wday++;
527 return mday;
530 static BOOL match_tz_date(const RTL_SYSTEM_TIME *st, const RTL_SYSTEM_TIME *reg_st)
532 WORD wDay;
534 if (st->wMonth != reg_st->wMonth) return FALSE;
536 if (!st->wMonth) return TRUE; /* no transition dates */
538 wDay = reg_st->wDay;
539 if (!reg_st->wYear) /* date in a day-of-week format */
540 wDay = weekday_to_mday(st->wYear - 1900, reg_st->wDay, reg_st->wMonth - 1, reg_st->wDayOfWeek);
542 if (st->wDay != wDay ||
543 st->wHour != reg_st->wHour ||
544 st->wMinute != reg_st->wMinute ||
545 st->wSecond != reg_st->wSecond ||
546 st->wMilliseconds != reg_st->wMilliseconds) return FALSE;
548 return TRUE;
551 static BOOL match_tz_info(const RTL_TIME_ZONE_INFORMATION *tzi, const RTL_TIME_ZONE_INFORMATION *reg_tzi)
553 if (tzi->Bias == reg_tzi->Bias &&
554 match_tz_date(&tzi->StandardDate, &reg_tzi->StandardDate) &&
555 match_tz_date(&tzi->DaylightDate, &reg_tzi->DaylightDate))
556 return TRUE;
558 return FALSE;
561 static BOOL reg_query_value(HKEY hkey, LPCWSTR name, DWORD type, void *data, DWORD count)
563 UNICODE_STRING nameW;
564 char buf[256];
565 KEY_VALUE_PARTIAL_INFORMATION *info = (KEY_VALUE_PARTIAL_INFORMATION *)buf;
567 if (count > sizeof(buf) - sizeof(KEY_VALUE_PARTIAL_INFORMATION))
568 return FALSE;
570 RtlInitUnicodeString(&nameW, name);
572 if (NtQueryValueKey(hkey, &nameW, KeyValuePartialInformation,
573 buf, sizeof(buf), &count))
574 return FALSE;
576 if (info->Type != type) return FALSE;
578 memcpy(data, info->Data, info->DataLength);
579 return TRUE;
582 static void find_reg_tz_info(RTL_TIME_ZONE_INFORMATION *tzi)
584 static const WCHAR Time_ZonesW[] = { 'M','a','c','h','i','n','e','\\',
585 'S','o','f','t','w','a','r','e','\\',
586 'M','i','c','r','o','s','o','f','t','\\',
587 'W','i','n','d','o','w','s',' ','N','T','\\',
588 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
589 'T','i','m','e',' ','Z','o','n','e','s',0 };
590 HANDLE hkey;
591 ULONG idx;
592 OBJECT_ATTRIBUTES attr;
593 UNICODE_STRING nameW;
594 WCHAR buf[128];
596 attr.Length = sizeof(attr);
597 attr.RootDirectory = 0;
598 attr.ObjectName = &nameW;
599 attr.Attributes = 0;
600 attr.SecurityDescriptor = NULL;
601 attr.SecurityQualityOfService = NULL;
602 RtlInitUnicodeString(&nameW, Time_ZonesW);
603 if (NtOpenKey(&hkey, KEY_READ, &attr))
605 WARN("Unable to open the time zones key\n");
606 return;
609 idx = 0;
610 nameW.Buffer = buf;
611 nameW.Length = sizeof(buf);
612 nameW.MaximumLength = sizeof(buf);
614 while (!RtlpNtEnumerateSubKey(hkey, &nameW, idx++))
616 static const WCHAR stdW[] = { 'S','t','d',0 };
617 static const WCHAR dltW[] = { 'D','l','t',0 };
618 static const WCHAR tziW[] = { 'T','Z','I',0 };
619 RTL_TIME_ZONE_INFORMATION reg_tzi;
620 HANDLE hSubkey;
621 struct tz_reg_data
623 LONG bias;
624 LONG std_bias;
625 LONG dlt_bias;
626 RTL_SYSTEM_TIME std_date;
627 RTL_SYSTEM_TIME dlt_date;
628 } tz_data;
630 attr.Length = sizeof(attr);
631 attr.RootDirectory = hkey;
632 attr.ObjectName = &nameW;
633 attr.Attributes = 0;
634 attr.SecurityDescriptor = NULL;
635 attr.SecurityQualityOfService = NULL;
636 if (NtOpenKey(&hSubkey, KEY_READ, &attr))
638 WARN("Unable to open subkey %s\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)));
639 continue;
642 #define get_value(hkey, name, type, data, len) \
643 if (!reg_query_value(hkey, name, type, data, len)) \
645 WARN("can't read data from %s\n", debugstr_w(name)); \
646 NtClose(hkey); \
647 continue; \
650 get_value(hSubkey, stdW, REG_SZ, reg_tzi.StandardName, sizeof(reg_tzi.StandardName));
651 get_value(hSubkey, dltW, REG_SZ, reg_tzi.DaylightName, sizeof(reg_tzi.DaylightName));
652 get_value(hSubkey, tziW, REG_BINARY, &tz_data, sizeof(tz_data));
654 #undef get_value
656 reg_tzi.Bias = tz_data.bias;
657 reg_tzi.StandardBias = tz_data.std_bias;
658 reg_tzi.DaylightBias = tz_data.dlt_bias;
659 reg_tzi.StandardDate = tz_data.std_date;
660 reg_tzi.DaylightDate = tz_data.dlt_date;
662 TRACE("%s: bias %d\n", debugstr_wn(nameW.Buffer, nameW.Length/sizeof(WCHAR)), reg_tzi.Bias);
663 TRACE("std (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
664 reg_tzi.StandardDate.wDay, reg_tzi.StandardDate.wMonth,
665 reg_tzi.StandardDate.wYear, reg_tzi.StandardDate.wDayOfWeek,
666 reg_tzi.StandardDate.wHour, reg_tzi.StandardDate.wMinute,
667 reg_tzi.StandardDate.wSecond, reg_tzi.StandardDate.wMilliseconds,
668 reg_tzi.StandardBias);
669 TRACE("dst (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
670 reg_tzi.DaylightDate.wDay, reg_tzi.DaylightDate.wMonth,
671 reg_tzi.DaylightDate.wYear, reg_tzi.DaylightDate.wDayOfWeek,
672 reg_tzi.DaylightDate.wHour, reg_tzi.DaylightDate.wMinute,
673 reg_tzi.DaylightDate.wSecond, reg_tzi.DaylightDate.wMilliseconds,
674 reg_tzi.DaylightBias);
676 NtClose(hSubkey);
678 if (match_tz_info(tzi, &reg_tzi))
680 *tzi = reg_tzi;
681 NtClose(hkey);
682 return;
685 /* reset len */
686 nameW.Length = sizeof(buf);
687 nameW.MaximumLength = sizeof(buf);
690 NtClose(hkey);
692 FIXME("Can't find matching timezone information in the registry for "
693 "bias %d, std (d/m/y): %u/%02u/%04u, dlt (d/m/y): %u/%02u/%04u\n",
694 tzi->Bias,
695 tzi->StandardDate.wDay, tzi->StandardDate.wMonth, tzi->StandardDate.wYear,
696 tzi->DaylightDate.wDay, tzi->DaylightDate.wMonth, tzi->DaylightDate.wYear);
699 static time_t find_dst_change(unsigned long min, unsigned long max, int *is_dst)
701 time_t start;
702 struct tm *tm;
704 start = min;
705 tm = localtime(&start);
706 *is_dst = !tm->tm_isdst;
707 TRACE("starting date isdst %d, %s", !*is_dst, ctime(&start));
709 while (min <= max)
711 time_t pos = (min + max) / 2;
712 tm = localtime(&pos);
714 if (tm->tm_isdst != *is_dst)
715 min = pos + 1;
716 else
717 max = pos - 1;
719 return min;
722 static int init_tz_info(RTL_TIME_ZONE_INFORMATION *tzi)
724 static RTL_TIME_ZONE_INFORMATION cached_tzi;
725 static int current_year = -1, current_bias = 65535;
726 struct tm *tm;
727 time_t year_start, year_end, tmp, dlt = 0, std = 0;
728 int is_dst, current_is_dst, bias;
730 RtlEnterCriticalSection( &TIME_tz_section );
732 year_start = time(NULL);
733 tm = gmtime(&year_start);
734 bias = (LONG)(mktime(tm) - year_start) / 60;
736 tm = localtime(&year_start);
737 current_is_dst = tm->tm_isdst;
738 if (current_year == tm->tm_year && current_bias == bias)
740 *tzi = cached_tzi;
741 RtlLeaveCriticalSection( &TIME_tz_section );
742 return current_is_dst;
745 memset(tzi, 0, sizeof(*tzi));
747 TRACE("tz data will be valid through year %d, bias %d\n", tm->tm_year + 1900, bias);
748 current_year = tm->tm_year;
749 current_bias = bias;
751 tzi->Bias = bias;
753 tm->tm_isdst = 0;
754 tm->tm_mday = 1;
755 tm->tm_mon = tm->tm_hour = tm->tm_min = tm->tm_sec = tm->tm_wday = tm->tm_yday = 0;
756 year_start = mktime(tm);
757 TRACE("year_start: %s", ctime(&year_start));
759 tm->tm_mday = tm->tm_wday = tm->tm_yday = 0;
760 tm->tm_mon = 12;
761 tm->tm_hour = 23;
762 tm->tm_min = tm->tm_sec = 59;
763 year_end = mktime(tm);
764 TRACE("year_end: %s", ctime(&year_end));
766 tmp = find_dst_change(year_start, year_end, &is_dst);
767 if (is_dst)
768 dlt = tmp;
769 else
770 std = tmp;
772 tmp = find_dst_change(tmp, year_end, &is_dst);
773 if (is_dst)
774 dlt = tmp;
775 else
776 std = tmp;
778 TRACE("std: %s", ctime(&std));
779 TRACE("dlt: %s", ctime(&dlt));
781 if (dlt == std || !dlt || !std)
782 TRACE("there is no daylight saving rules in this time zone\n");
783 else
785 tmp = dlt - tzi->Bias * 60;
786 tm = gmtime(&tmp);
787 TRACE("dlt gmtime: %s", asctime(tm));
789 tzi->DaylightBias = -60;
790 tzi->DaylightDate.wYear = tm->tm_year + 1900;
791 tzi->DaylightDate.wMonth = tm->tm_mon + 1;
792 tzi->DaylightDate.wDayOfWeek = tm->tm_wday;
793 tzi->DaylightDate.wDay = tm->tm_mday;
794 tzi->DaylightDate.wHour = tm->tm_hour;
795 tzi->DaylightDate.wMinute = tm->tm_min;
796 tzi->DaylightDate.wSecond = tm->tm_sec;
797 tzi->DaylightDate.wMilliseconds = 0;
799 TRACE("daylight (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
800 tzi->DaylightDate.wDay, tzi->DaylightDate.wMonth,
801 tzi->DaylightDate.wYear, tzi->DaylightDate.wDayOfWeek,
802 tzi->DaylightDate.wHour, tzi->DaylightDate.wMinute,
803 tzi->DaylightDate.wSecond, tzi->DaylightDate.wMilliseconds,
804 tzi->DaylightBias);
806 tmp = std - tzi->Bias * 60 - tzi->DaylightBias * 60;
807 tm = gmtime(&tmp);
808 TRACE("std gmtime: %s", asctime(tm));
810 tzi->StandardBias = 0;
811 tzi->StandardDate.wYear = tm->tm_year + 1900;
812 tzi->StandardDate.wMonth = tm->tm_mon + 1;
813 tzi->StandardDate.wDayOfWeek = tm->tm_wday;
814 tzi->StandardDate.wDay = tm->tm_mday;
815 tzi->StandardDate.wHour = tm->tm_hour;
816 tzi->StandardDate.wMinute = tm->tm_min;
817 tzi->StandardDate.wSecond = tm->tm_sec;
818 tzi->StandardDate.wMilliseconds = 0;
820 TRACE("standard (d/m/y): %u/%02u/%04u day of week %u %u:%02u:%02u.%03u bias %d\n",
821 tzi->StandardDate.wDay, tzi->StandardDate.wMonth,
822 tzi->StandardDate.wYear, tzi->StandardDate.wDayOfWeek,
823 tzi->StandardDate.wHour, tzi->StandardDate.wMinute,
824 tzi->StandardDate.wSecond, tzi->StandardDate.wMilliseconds,
825 tzi->StandardBias);
828 find_reg_tz_info(tzi);
829 cached_tzi = *tzi;
831 RtlLeaveCriticalSection( &TIME_tz_section );
833 return current_is_dst;
836 /***********************************************************************
837 * RtlQueryTimeZoneInformation [NTDLL.@]
839 * Get information about the current timezone.
841 * PARAMS
842 * tzinfo [O] Destination for the retrieved timezone info.
844 * RETURNS
845 * Success: STATUS_SUCCESS.
846 * Failure: An NTSTATUS error code indicating the problem.
848 NTSTATUS WINAPI RtlQueryTimeZoneInformation(RTL_TIME_ZONE_INFORMATION *tzinfo)
850 init_tz_info( tzinfo );
852 return STATUS_SUCCESS;
855 /***********************************************************************
856 * RtlSetTimeZoneInformation [NTDLL.@]
858 * Set the current time zone information.
860 * PARAMS
861 * tzinfo [I] Timezone information to set.
863 * RETURNS
864 * Success: STATUS_SUCCESS.
865 * Failure: An NTSTATUS error code indicating the problem.
868 NTSTATUS WINAPI RtlSetTimeZoneInformation( const RTL_TIME_ZONE_INFORMATION *tzinfo )
870 return STATUS_PRIVILEGE_NOT_HELD;
873 /***********************************************************************
874 * NtSetSystemTime [NTDLL.@]
875 * ZwSetSystemTime [NTDLL.@]
877 * Set the system time.
879 * PARAMS
880 * NewTime [I] The time to set.
881 * OldTime [O] Optional destination for the previous system time.
883 * RETURNS
884 * Success: STATUS_SUCCESS.
885 * Failure: An NTSTATUS error code indicating the problem.
887 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
889 struct timeval tv;
890 time_t tm_t;
891 DWORD sec, oldsec;
892 LARGE_INTEGER tm;
894 /* Return the old time if necessary */
895 if (!OldTime) OldTime = &tm;
897 NtQuerySystemTime( OldTime );
898 RtlTimeToSecondsSince1970( OldTime, &oldsec );
900 RtlTimeToSecondsSince1970( NewTime, &sec );
902 /* set the new time */
903 tv.tv_sec = sec;
904 tv.tv_usec = 0;
906 #ifdef HAVE_SETTIMEOFDAY
907 if (!settimeofday(&tv, NULL)) /* 0 is OK, -1 is error */
908 return STATUS_SUCCESS;
909 tm_t = sec;
910 ERR("Cannot set time to %s, time adjustment %ld: %s\n",
911 ctime(&tm_t), (long)(sec-oldsec), strerror(errno));
912 if (errno == EPERM)
913 return STATUS_PRIVILEGE_NOT_HELD;
914 else
915 return STATUS_INVALID_PARAMETER;
916 #else
917 tm_t = sec;
918 FIXME("setting time to %s not implemented for missing settimeofday\n",
919 ctime(&tm_t));
920 return STATUS_NOT_IMPLEMENTED;
921 #endif