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
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"
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
47 #include "ntdll_misc.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
51 #define SETTIME_MAX_ADJUST 120
53 /* This structure is used to store strings that represent all of the time zones
54 * in the world. (This is used to help GetTimeZoneInformation)
58 const char *psTZFromUnix;
59 WCHAR psTZWindows[32];
64 static const struct tagTZ_INFO TZ_INFO[] =
67 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
68 'T','i','m','e','\0'}, -720, 0},
70 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
73 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
74 'T','i','m','e','\0'}, 600, 0},
76 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
77 'i','m','e','\0'}, 480, 1},
79 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
80 'i','m','e','\0'}, 420, 1},
82 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
83 'r','d',' ','T','i','m','e','\0'}, 420, 0},
85 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
86 'T','i','m','e','\0'}, 360, 1},
88 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
89 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
91 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
92 'i','m','e','\0'}, 300, 1},
94 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
95 'd',' ','T','i','m','e','\0'}, 300, 0},
97 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
98 'i','m','e','\0'}, 240, 1},
100 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
101 'd',' ','T','i','m','e','\0'}, 300, 0},
103 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
104 'T','i','m','e','\0'}, 180, 1},
106 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
107 'd',' ','T','i','m','e','\0'}, 240, 0},
109 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
110 'd',' ','T','i','m','e','\0'}, 240, 0},
112 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
113 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
115 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
116 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
118 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
119 'd',' ','T','i','m','e','\0'}, 180, 0},
121 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
122 ' ','T','i','m','e','\0'}, 120, 1},
124 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
125 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
127 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
128 'm','e','\0'}, 0, 1},
130 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
131 'd',' ','T','i','m','e','\0'}, 60, 0},
133 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
134 ' ','T','i','m','e','\0'}, 0, 0},
136 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
139 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
142 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
143 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
145 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
146 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
148 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
149 ' ','T','i','m','e','\0'}, -180, 1},
151 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
154 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
155 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
157 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
158 'm','e','\0'}, -120, 0},
160 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
161 'm','e','\0'}, -240, 1},
163 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
166 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
167 'i','m','e','\0'}, -240, 1},
169 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
170 ' ','T','i','m','e','\0'}, -180, 0},
172 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
175 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
176 'i','m','e','\0'}, -240, 0},
178 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
179 'T','i','m','e','\0'}, -300, 1},
181 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
182 'r','d',' ','T','i','m','e','\0'}, -270, 0},
184 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
185 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
187 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
188 ' ','T','i','m','e','\0'}, -300, 0},
190 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
193 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
196 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
197 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
199 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
200 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
202 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
203 ' ','T','i','m','e','\0'}, -360, 0},
205 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
206 'i','m','e','\0'}, -390, 0},
208 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
209 'i','m','e','\0'}, -420, 0},
211 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
212 'd',' ','T','i','m','e','\0'}, -480, 1},
214 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
217 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
218 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
220 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
221 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
223 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
224 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
226 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
229 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
232 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
233 'i','m','e','\0'}, -600, 1},
235 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
236 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
238 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
239 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
241 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
242 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
244 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
245 'r','d',' ','T','i','m','e','\0'}, -660, 1},
247 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
248 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
250 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
251 'r','d',' ','T','i','m','e','\0'}, -720, 0},
253 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
256 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
260 /*********** start of code by Rex Jolliff (rex@lvcablemodem.com) **************/
262 #define TICKSPERSEC 10000000
263 #define TICKSPERMSEC 10000
264 #define SECSPERDAY 86400
265 #define SECSPERHOUR 3600
266 #define SECSPERMIN 60
267 #define MINSPERHOUR 60
268 #define HOURSPERDAY 24
269 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
270 #define DAYSPERWEEK 7
271 #define EPOCHYEAR 1601
272 #define DAYSPERNORMALYEAR 365
273 #define DAYSPERLEAPYEAR 366
274 #define MONSPERYEAR 12
276 /* 1601 to 1970 is 369 years plus 89 leap days */
277 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
278 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
279 /* 1601 to 1980 is 379 years plus 91 leap days */
280 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
281 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
284 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
285 static const int MonthLengths[2][MONSPERYEAR] =
287 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
288 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
291 static inline int IsLeapYear(int Year)
293 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
296 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
298 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
299 *CarryField = (CSHORT) (*CarryField + 1);
302 /***********************************************************************
303 * NTDLL_get_server_timeout
305 * Convert a NTDLL timeout into a timeval struct to send to the server.
307 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
311 if (!timeout) /* infinite timeout */
313 when->sec = when->usec = 0;
315 else if (timeout->QuadPart <= 0) /* relative timeout */
318 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
319 gettimeofday( &tv, 0 );
320 when->sec = tv.tv_sec + sec;
321 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
323 when->usec -= 1000000;
327 else /* absolute time */
329 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
330 TICKSPERSEC, &remainder );
331 when->usec = remainder / 10;
336 /******************************************************************************
337 * RtlTimeToTimeFields [NTDLL.@]
339 * Convert a time into a TIME_FIELDS structure.
342 * liTime [I] Time to convert.
343 * TimeFields [O] Destination for the converted time.
348 VOID WINAPI RtlTimeToTimeFields(
349 const LARGE_INTEGER *liTime,
350 PTIME_FIELDS TimeFields)
353 int SecondsInDay, CurYear;
354 int LeapYear, CurMonth;
356 LONGLONG Time = liTime->QuadPart;
358 /* Extract millisecond from time and convert time into seconds */
359 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
360 Time = Time / TICKSPERSEC;
362 /* The native version of RtlTimeToTimeFields does not take leap seconds
365 /* Split the time into days and seconds within the day */
366 Days = Time / SECSPERDAY;
367 SecondsInDay = Time % SECSPERDAY;
369 /* compute time of day */
370 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
371 SecondsInDay = SecondsInDay % SECSPERHOUR;
372 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
373 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
375 /* compute day of week */
376 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
380 /* FIXME: handle calendar modifications */
382 { LeapYear = IsLeapYear(CurYear);
383 if (Days < (long) YearLengths[LeapYear])
387 Days = Days - (long) YearLengths[LeapYear];
389 TimeFields->Year = (CSHORT) CurYear;
391 /* Compute month of year */
392 Months = MonthLengths[LeapYear];
393 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
394 Days = Days - (long) Months[CurMonth];
395 TimeFields->Month = (CSHORT) (CurMonth + 1);
396 TimeFields->Day = (CSHORT) (Days + 1);
399 /******************************************************************************
400 * RtlTimeFieldsToTime [NTDLL.@]
402 * Convert a TIME_FIELDS structure into a time.
405 * ftTimeFields [I] TIME_FIELDS structure to convert.
406 * Time [O] Destination for the converted time.
412 BOOLEAN WINAPI RtlTimeFieldsToTime(
413 PTIME_FIELDS tfTimeFields,
416 int CurYear, CurMonth;
418 TIME_FIELDS TimeFields = *tfTimeFields;
422 /* FIXME: normalize the TIME_FIELDS structure here */
423 while (TimeFields.Second >= SECSPERMIN)
424 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
426 while (TimeFields.Minute >= MINSPERHOUR)
427 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
429 while (TimeFields.Hour >= HOURSPERDAY)
430 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
432 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
433 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
435 while (TimeFields.Month > MONSPERYEAR)
436 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
439 /* FIXME: handle calendar corrections here */
440 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
441 { rcTime += YearLengths[IsLeapYear(CurYear)];
443 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
444 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
446 rcTime += TimeFields.Day - 1;
447 rcTime *= SECSPERDAY;
448 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
449 rcTime *= TICKSPERSEC;
450 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
451 Time->QuadPart = rcTime;
455 /************ end of code by Rex Jolliff (rex@lvcablemodem.com) ***************/
457 /******************************************************************************
458 * RtlLocalTimeToSystemTime [NTDLL.@]
460 * Convert a local time into system time.
463 * LocalTime [I] Local time to convert.
464 * SystemTime [O] Destination for the converted time.
467 * Success: STATUS_SUCCESS.
468 * Failure: An NTSTATUS error code indicating the problem.
470 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
471 PLARGE_INTEGER SystemTime)
473 TIME_ZONE_INFORMATION tzinfo;
475 TRACE("(%p, %p)\n", LocalTime, SystemTime);
477 RtlQueryTimeZoneInformation(&tzinfo);
478 SystemTime->QuadPart = LocalTime->QuadPart + tzinfo.Bias * 60 * (LONGLONG)10000000;
479 return STATUS_SUCCESS;
482 /******************************************************************************
483 * RtlSystemTimeToLocalTime [NTDLL.@]
485 * Convert a system time into a local time.
488 * SystemTime [I] System time to convert.
489 * LocalTime [O] Destination for the converted time.
492 * Success: STATUS_SUCCESS.
493 * Failure: An NTSTATUS error code indicating the problem.
495 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
496 PLARGE_INTEGER LocalTime )
498 TIME_ZONE_INFORMATION tzinfo;
500 TRACE("(%p, %p)\n", SystemTime, LocalTime);
502 RtlQueryTimeZoneInformation(&tzinfo);
503 LocalTime->QuadPart = SystemTime->QuadPart - tzinfo.Bias * 60 * (LONGLONG)10000000;
504 return STATUS_SUCCESS;
507 /******************************************************************************
508 * RtlTimeToSecondsSince1970 [NTDLL.@]
510 * Convert a time into a count of seconds since 1970.
513 * Time [I] Time to convert.
514 * Seconds [O] Destination for the converted time.
518 * Failure: FALSE, if the resulting value will not fit in a DWORD.
520 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
522 ULONGLONG tmp = ((ULONGLONG)Time->s.HighPart << 32) | Time->s.LowPart;
523 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
524 tmp -= SECS_1601_TO_1970;
525 if (tmp > 0xffffffff) return FALSE;
526 *Seconds = (DWORD)tmp;
530 /******************************************************************************
531 * RtlTimeToSecondsSince1980 [NTDLL.@]
533 * Convert a time into a count of seconds since 1980.
536 * Time [I] Time to convert.
537 * Seconds [O] Destination for the converted time.
541 * Failure: FALSE, if the resulting value will not fit in a DWORD.
543 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
545 ULONGLONG tmp = ((ULONGLONG)Time->s.HighPart << 32) | Time->s.LowPart;
546 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
547 tmp -= SECS_1601_TO_1980;
548 if (tmp > 0xffffffff) return FALSE;
549 *Seconds = (DWORD)tmp;
553 /******************************************************************************
554 * RtlSecondsSince1970ToTime [NTDLL.@]
556 * Convert a count of seconds since 1970 to a time.
559 * Seconds [I] Time to convert.
560 * Time [O] Destination for the converted time.
565 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
567 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
568 Time->s.LowPart = (DWORD)secs;
569 Time->s.HighPart = (DWORD)(secs >> 32);
572 /******************************************************************************
573 * RtlSecondsSince1980ToTime [NTDLL.@]
575 * Convert a count of seconds since 1980 to a time.
578 * Seconds [I] Time to convert.
579 * Time [O] Destination for the converted time.
584 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
586 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
587 Time->s.LowPart = (DWORD)secs;
588 Time->s.HighPart = (DWORD)(secs >> 32);
591 /******************************************************************************
592 * RtlTimeToElapsedTimeFields [NTDLL.@]
594 * Convert a time to a count of elapsed seconds.
597 * Time [I] Time to convert.
598 * TimeFields [O] Destination for the converted time.
603 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
608 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
609 TimeFields->Milliseconds = rem / TICKSPERMSEC;
611 /* time is now in seconds */
612 TimeFields->Year = 0;
613 TimeFields->Month = 0;
614 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
616 /* rem is now the remaining seconds in the last day */
617 TimeFields->Second = rem % 60;
619 TimeFields->Minute = rem % 60;
620 TimeFields->Hour = rem / 60;
623 /***********************************************************************
624 * NtQuerySystemTime [NTDLL.@]
625 * ZwQuerySystemTime [NTDLL.@]
627 * Get the current system time.
630 * Time [O] Destination for the current system time.
633 * Success: STATUS_SUCCESS.
634 * Failure: An NTSTATUS error code indicating the problem.
636 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
640 gettimeofday( &now, 0 );
641 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
642 Time->QuadPart += now.tv_usec * 10;
643 return STATUS_SUCCESS;
646 /***********************************************************************
647 * TIME_GetBias [internal]
649 * Helper function calculates delta local time from UTC.
652 * utc [I] The current utc time.
653 * pdaylight [I] Local daylight.
656 * The bias for the current timezone.
658 static int TIME_GetBias(time_t utc, int *pdaylight)
660 struct tm *ptm = localtime(&utc);
661 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
663 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
664 return (int)(utc-mktime(ptm));
667 /***********************************************************************
668 * TIME_GetTZAsStr [internal]
670 * Helper function that returns the given timezone as a string.
673 * utc [I] The current utc time.
674 * bias [I] The bias of the current timezone.
681 * This could be done with a hash table instead of merely iterating through a
682 * table, however with the small amount of entries (60 or so) I didn't think
685 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
688 struct tm *ptm = localtime(&utc);
691 if (!strftime (psTZName, 7, "%Z", ptm))
694 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
696 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
697 TZ_INFO[i].bias == bias &&
698 TZ_INFO[i].dst == dst
700 return TZ_INFO[i].psTZWindows;
706 /***********************************************************************
707 * RtlQueryTimeZoneInformation [NTDLL.@]
709 * Get information about the current timezone.
712 * tzinfo [O] Destination for the retrieved timezone info.
715 * Success: STATUS_SUCCESS.
716 * Failure: An NTSTATUS error code indicating the problem.
718 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
724 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
727 bias = TIME_GetBias(gmt, &daylight);
729 tzinfo->Bias = -bias / 60;
730 tzinfo->StandardBias = 0;
731 tzinfo->DaylightBias = -60;
732 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
733 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
734 return STATUS_SUCCESS;
737 /***********************************************************************
738 * RtlSetTimeZoneInformation [NTDLL.@]
740 * Set the current time zone information.
743 * tzinfo [I] Timezone information to set.
746 * Success: STATUS_SUCCESS.
747 * Failure: An NTSTATUS error code indicating the problem.
750 * Uses the obsolete unix timezone structure and tz_dsttime member.
752 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
756 tz.tz_minuteswest = tzinfo->Bias;
758 tz.tz_dsttime = DST_NONE;
762 if(!settimeofday(NULL, &tz))
763 return STATUS_SUCCESS;
764 return STATUS_PRIVILEGE_NOT_HELD;
767 /***********************************************************************
768 * NtSetSystemTime [NTDLL.@]
769 * ZwSetSystemTime [NTDLL.@]
771 * Set the system time.
774 * NewTime [I] The time to set.
775 * OldTime [O] Optional destination for the previous system time.
778 * Success: STATUS_SUCCESS.
779 * Failure: An NTSTATUS error code indicating the problem.
781 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
791 /* Return the old time if necessary */
793 NtQuerySystemTime(OldTime);
795 RtlTimeToTimeFields(NewTime, &tf);
797 /* call gettimeofday to get the current timezone */
798 gettimeofday(&tv, &tz);
800 /* get delta local time from utc */
801 bias = TIME_GetBias(oldsec, &dst);
803 /* get the number of seconds */
804 t.tm_sec = tf.Second;
805 t.tm_min = tf.Minute;
808 t.tm_mon = tf.Month - 1;
809 t.tm_year = tf.Year - 1900;
812 /* correct for timezone and daylight */
815 /* set the new time */
817 tv.tv_usec = tf.Milliseconds * 1000;
819 /* error and sanity check*/
820 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
823 #ifdef HAVE_SETTIMEOFDAY
824 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
826 return STATUS_SUCCESS;
832 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
833 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
835 err == -1 ? "No Permission"
836 : sec == (time_t)-1 ? "" : "is too large." );
839 return STATUS_INVALID_PARAMETER;
841 return STATUS_PRIVILEGE_NOT_HELD;
843 return STATUS_NOT_IMPLEMENTED;