4 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5 * adapted to wine with special permissions of the author
6 * Rex Jolliff (rex@lvcablemodem.com)
8 * Copyright 1999 Juergen Schmied
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
31 #ifdef HAVE_SYS_TIME_H
32 # include <sys/time.h>
38 #define NONAMELESSUNION
39 #define NONAMELESSSTRUCT
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43 #include "ntdll_misc.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
47 #define SETTIME_MAX_ADJUST 120
49 /* This structure is used to store strings that represent all of the time zones
50 * in the world. (This is used to help GetTimeZoneInformation)
54 const char *psTZFromUnix;
55 WCHAR psTZWindows[32];
60 static const struct tagTZ_INFO TZ_INFO[] =
63 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
64 'T','i','m','e','\0'}, -720, 0},
66 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
69 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
70 'T','i','m','e','\0'}, 600, 0},
72 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
73 'i','m','e','\0'}, 480, 1},
75 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
76 'i','m','e','\0'}, 420, 1},
78 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
79 'r','d',' ','T','i','m','e','\0'}, 420, 0},
81 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
82 'T','i','m','e','\0'}, 360, 1},
84 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
85 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
87 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
88 'i','m','e','\0'}, 300, 1},
90 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
91 'd',' ','T','i','m','e','\0'}, 300, 0},
93 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
94 'i','m','e','\0'}, 240, 1},
96 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
97 'd',' ','T','i','m','e','\0'}, 300, 0},
99 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
100 'T','i','m','e','\0'}, 180, 1},
102 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
103 'd',' ','T','i','m','e','\0'}, 240, 0},
105 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
106 'd',' ','T','i','m','e','\0'}, 240, 0},
108 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
109 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
111 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
112 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
114 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
115 'd',' ','T','i','m','e','\0'}, 180, 0},
117 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
118 ' ','T','i','m','e','\0'}, 120, 1},
120 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
121 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
123 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
124 'm','e','\0'}, 0, 1},
126 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
127 'd',' ','T','i','m','e','\0'}, 60, 0},
129 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
130 ' ','T','i','m','e','\0'}, 0, 0},
132 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
135 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
138 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
139 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
141 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
142 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
144 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
145 ' ','T','i','m','e','\0'}, -180, 1},
147 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
150 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
151 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
153 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
154 'm','e','\0'}, -120, 0},
156 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
157 'm','e','\0'}, -240, 1},
159 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
162 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
163 'i','m','e','\0'}, -240, 1},
165 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
166 ' ','T','i','m','e','\0'}, -180, 0},
168 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
171 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
172 'i','m','e','\0'}, -240, 0},
174 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
175 'T','i','m','e','\0'}, -300, 1},
177 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
178 'r','d',' ','T','i','m','e','\0'}, -270, 0},
180 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
181 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
183 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
184 ' ','T','i','m','e','\0'}, -300, 0},
186 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
189 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
192 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
193 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
195 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
196 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
198 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
199 ' ','T','i','m','e','\0'}, -360, 0},
201 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
202 'i','m','e','\0'}, -390, 0},
204 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
205 'i','m','e','\0'}, -420, 0},
207 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
208 'd',' ','T','i','m','e','\0'}, -480, 1},
210 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
213 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
214 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
216 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
217 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
219 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
220 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
222 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
225 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
228 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
229 'i','m','e','\0'}, -600, 1},
231 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
232 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
234 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
235 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
237 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
238 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
240 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
241 'r','d',' ','T','i','m','e','\0'}, -660, 1},
243 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
244 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
246 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
247 'r','d',' ','T','i','m','e','\0'}, -720, 0},
249 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
252 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
256 /*********** start of code by Rex Jolliff (rex@lvcablemodem.com) **************/
258 #define TICKSPERSEC 10000000
259 #define TICKSPERMSEC 10000
260 #define SECSPERDAY 86400
261 #define SECSPERHOUR 3600
262 #define SECSPERMIN 60
263 #define MINSPERHOUR 60
264 #define HOURSPERDAY 24
265 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
266 #define DAYSPERWEEK 7
267 #define EPOCHYEAR 1601
268 #define DAYSPERNORMALYEAR 365
269 #define DAYSPERLEAPYEAR 366
270 #define MONSPERYEAR 12
272 /* 1601 to 1970 is 369 years plus 89 leap days */
273 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
274 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
275 /* 1601 to 1980 is 379 years plus 91 leap days */
276 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
277 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
280 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
281 static const int MonthLengths[2][MONSPERYEAR] =
283 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
284 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
287 static inline int IsLeapYear(int Year)
289 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
292 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
294 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
295 *CarryField = (CSHORT) (*CarryField + 1);
298 /***********************************************************************
299 * NTDLL_get_server_timeout
301 * Convert a NTDLL timeout into a timeval struct to send to the server.
303 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
307 if (!timeout) /* infinite timeout */
309 when->sec = when->usec = 0;
311 else if (timeout->QuadPart <= 0) /* relative timeout */
314 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
315 gettimeofday( &tv, 0 );
316 when->sec = tv.tv_sec + sec;
317 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
319 when->usec -= 1000000;
323 else /* absolute time */
325 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
326 TICKSPERSEC, &remainder );
327 when->usec = remainder / 10;
332 /******************************************************************************
333 * RtlTimeToTimeFields [NTDLL.@]
335 * Parses Time into a TimeFields structure.
338 * liTime [I] Time to convert to timefields.
339 * TimeFields [O] Pointer to TIME_FIELDS structure to hold parsed info.
344 VOID WINAPI RtlTimeToTimeFields(
345 const LARGE_INTEGER *liTime,
346 PTIME_FIELDS TimeFields)
349 int SecondsInDay, CurYear;
350 int LeapYear, CurMonth;
352 LONGLONG Time = liTime->QuadPart;
354 /* Extract millisecond from time and convert time into seconds */
355 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
356 Time = Time / TICKSPERSEC;
358 /* The native version of RtlTimeToTimeFields does not take leap seconds
361 /* Split the time into days and seconds within the day */
362 Days = Time / SECSPERDAY;
363 SecondsInDay = Time % SECSPERDAY;
365 /* compute time of day */
366 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
367 SecondsInDay = SecondsInDay % SECSPERHOUR;
368 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
369 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
371 /* compute day of week */
372 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
376 /* FIXME: handle calendar modifications */
378 { LeapYear = IsLeapYear(CurYear);
379 if (Days < (long) YearLengths[LeapYear])
383 Days = Days - (long) YearLengths[LeapYear];
385 TimeFields->Year = (CSHORT) CurYear;
387 /* Compute month of year */
388 Months = MonthLengths[LeapYear];
389 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
390 Days = Days - (long) Months[CurMonth];
391 TimeFields->Month = (CSHORT) (CurMonth + 1);
392 TimeFields->Day = (CSHORT) (Days + 1);
395 /******************************************************************************
396 * RtlTimeFieldsToTime [NTDLL.@]
398 * Converts a TIME_FIELDS structure to time.
401 * ftTimeFields [I] Time fields structure to convert.
402 * Time [O] Converted time.
408 BOOLEAN WINAPI RtlTimeFieldsToTime(
409 PTIME_FIELDS tfTimeFields,
412 int CurYear, CurMonth;
414 TIME_FIELDS TimeFields = *tfTimeFields;
418 /* FIXME: normalize the TIME_FIELDS structure here */
419 while (TimeFields.Second >= SECSPERMIN)
420 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
422 while (TimeFields.Minute >= MINSPERHOUR)
423 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
425 while (TimeFields.Hour >= HOURSPERDAY)
426 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
428 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
429 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
431 while (TimeFields.Month > MONSPERYEAR)
432 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
435 /* FIXME: handle calendar corrections here */
436 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
437 { rcTime += YearLengths[IsLeapYear(CurYear)];
439 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
440 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
442 rcTime += TimeFields.Day - 1;
443 rcTime *= SECSPERDAY;
444 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
445 rcTime *= TICKSPERSEC;
446 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
447 Time->QuadPart = rcTime;
451 /************ end of code by Rex Jolliff (rex@lvcablemodem.com) ***************/
453 /******************************************************************************
454 * RtlLocalTimeToSystemTime [NTDLL.@]
456 * Converts local time to system time.
459 * LocalTime [I] Localtime to convert.
460 * SystemTime [O] SystemTime of the supplied localtime.
465 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
466 PLARGE_INTEGER SystemTime)
468 TIME_ZONE_INFORMATION tzinfo;
470 TRACE("(%p, %p)\n", LocalTime, SystemTime);
472 RtlQueryTimeZoneInformation(&tzinfo);
473 SystemTime->QuadPart = LocalTime->QuadPart + tzinfo.Bias * 60 * (LONGLONG)10000000;
474 return STATUS_SUCCESS;
477 /******************************************************************************
478 * RtlSystemTimeToLocalTime [NTDLL.@]
480 * Converts system Time to local time.
483 * SystemTime [I] System time to convert.
484 * LocalTime [O] Local time of the supplied system time.
489 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
490 PLARGE_INTEGER LocalTime )
492 TIME_ZONE_INFORMATION tzinfo;
494 TRACE("(%p, %p)\n", SystemTime, LocalTime);
496 RtlQueryTimeZoneInformation(&tzinfo);
497 LocalTime->QuadPart = SystemTime->QuadPart - tzinfo.Bias * 60 * (LONGLONG)10000000;
498 return STATUS_SUCCESS;
501 /******************************************************************************
502 * RtlTimeToSecondsSince1970 [NTDLL.@]
504 * Converts Time to seconds since 1970.
507 * time [I] Time to convert.
508 * res [O] Pointer to a LONG to recieve the seconds since 1970.
514 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *time, PULONG res )
516 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
517 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
518 tmp -= SECS_1601_TO_1970;
519 if (tmp > 0xffffffff) return FALSE;
524 /******************************************************************************
525 * RtlTimeToSecondsSince1980 [NTDLL.@]
527 * Converts Time to seconds since 1980.
530 * time [I] Time to convert.
531 * res [O] Pointer to a integer to recieve the time since 1980.
537 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *time, LPDWORD res )
539 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
540 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
541 tmp -= SECS_1601_TO_1980;
542 if (tmp > 0xffffffff) return FALSE;
547 /******************************************************************************
548 * RtlSecondsSince1970ToTime [NTDLL.@]
550 * Converts seconds since 1970 to time.
553 * time [I] Seconds since 1970 to convert.
554 * res [O] Seconds since 1970 in Time.
559 void WINAPI RtlSecondsSince1970ToTime( DWORD time, LARGE_INTEGER *res )
561 ULONGLONG secs = time * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
562 res->s.LowPart = (DWORD)secs;
563 res->s.HighPart = (DWORD)(secs >> 32);
566 /******************************************************************************
567 * RtlSecondsSince1980ToTime [NTDLL.@]
569 * Converts seconds since 1980 to time.
572 * time [I] Seconds since 1980 to convert.
573 * res [O] Seconds since 1980 in Time.
578 void WINAPI RtlSecondsSince1980ToTime( DWORD time, LARGE_INTEGER *res )
580 ULONGLONG secs = time * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
581 res->s.LowPart = (DWORD)secs;
582 res->s.HighPart = (DWORD)(secs >> 32);
585 /******************************************************************************
586 * RtlTimeToElapsedTimeFields [NTDLL.@]
591 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
596 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
597 TimeFields->Milliseconds = rem / TICKSPERMSEC;
599 /* time is now in seconds */
600 TimeFields->Year = 0;
601 TimeFields->Month = 0;
602 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
604 /* rem is now the remaining seconds in the last day */
605 TimeFields->Second = rem % 60;
607 TimeFields->Minute = rem % 60;
608 TimeFields->Hour = rem / 60;
611 /***********************************************************************
612 * NtQuerySystemTime [NTDLL.@]
613 * ZwQuerySystemTime [NTDLL.@]
615 * Gets the current system time.
618 * time [O] The current system time.
623 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER time )
627 gettimeofday( &now, 0 );
628 time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
629 time->QuadPart += now.tv_usec * 10;
630 return STATUS_SUCCESS;
633 /***********************************************************************
634 * TIME_GetBias [internal]
636 * Helper function calculates delta local time from UTC.
639 * utc [I] The current utc time.
640 * pdaylight [I] Local daylight.
643 * The bias for the current timezone.
645 static int TIME_GetBias(time_t utc, int *pdaylight)
647 struct tm *ptm = localtime(&utc);
648 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
650 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
651 return (int)(utc-mktime(ptm));
654 /***********************************************************************
655 * TIME_GetTZAsStr [internal]
657 * Helper function that returns the given timezone as a string.
660 * utc [I] The current utc time.
661 * bias [I] The bias of the current timezone.
668 * This could be done with a hash table instead of merely iterating through a
669 * table, however with the small amount of entries (60 or so) I didn't think
672 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
675 struct tm *ptm = localtime(&utc);
678 if (!strftime (psTZName, 7, "%Z", ptm))
681 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
683 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
684 TZ_INFO[i].bias == bias &&
685 TZ_INFO[i].dst == dst
687 return TZ_INFO[i].psTZWindows;
693 /***********************************************************************
694 * RtlQueryTimeZoneInformation [NTDLL.@]
696 * Returns the timezone.
699 * tzinfo [O] Retrieves the timezone info.
704 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
710 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
713 bias = TIME_GetBias(gmt, &daylight);
715 tzinfo->Bias = -bias / 60;
716 tzinfo->StandardBias = 0;
717 tzinfo->DaylightBias = -60;
718 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
719 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
720 return STATUS_SUCCESS;
723 /***********************************************************************
724 * RtlSetTimeZoneInformation [NTDLL.@]
726 * Sets the current time zone.
729 * tzinfo [I] Timezone information used to set timezone.
735 * Uses the obsolete unix timezone structure and tz_dsttime member.
737 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
741 tz.tz_minuteswest = tzinfo->Bias;
743 tz.tz_dsttime = DST_NONE;
747 if(!settimeofday(NULL, &tz))
748 return STATUS_SUCCESS;
749 return STATUS_PRIVILEGE_NOT_HELD;
752 /***********************************************************************
753 * NtSetSystemTime [NTDLL.@]
754 * ZwSetSystemTime [NTDLL.@]
756 * Sets the system time.
759 * NewTime [I] The time to set the system time to.
760 * OldTime [O] Optional (ie. can be NULL). Old Time.
765 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
775 /* Return the old time if necessary */
777 NtQuerySystemTime(OldTime);
779 RtlTimeToTimeFields(NewTime, &tf);
781 /* call gettimeofday to get the current timezone */
782 gettimeofday(&tv, &tz);
784 /* get delta local time from utc */
785 bias = TIME_GetBias(oldsec, &dst);
787 /* get the number of seconds */
788 t.tm_sec = tf.Second;
789 t.tm_min = tf.Minute;
792 t.tm_mon = tf.Month - 1;
793 t.tm_year = tf.Year - 1900;
796 /* correct for timezone and daylight */
799 /* set the new time */
801 tv.tv_usec = tf.Milliseconds * 1000;
803 /* error and sanity check*/
804 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
807 #ifdef HAVE_SETTIMEOFDAY
808 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
810 return STATUS_SUCCESS;
816 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
817 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
818 sec-oldsec, err == -1 ? "No Permission" :
819 sec == (time_t)-1 ? "" : "is too large." );
822 return STATUS_INVALID_PARAMETER;
824 return STATUS_PRIVILEGE_NOT_HELD;
826 return STATUS_NOT_IMPLEMENTED;