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 static CRITICAL_SECTION TIME_GetBias_section;
52 static CRITICAL_SECTION_DEBUG critsect_debug =
54 0, 0, &TIME_GetBias_section,
55 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
56 0, 0, { 0, (DWORD)(__FILE__ ": TIME_GetBias_section") }
58 static CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
61 #define SETTIME_MAX_ADJUST 120
63 /* This structure is used to store strings that represent all of the time zones
64 * in the world. (This is used to help GetTimeZoneInformation)
68 const char *psTZFromUnix;
69 WCHAR psTZWindows[32];
74 static const struct tagTZ_INFO TZ_INFO[] =
77 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
78 'T','i','m','e','\0'}, -720, 0},
80 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
83 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
84 'T','i','m','e','\0'}, 600, 0},
86 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
87 'i','m','e','\0'}, 480, 1},
89 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
90 'i','m','e','\0'}, 420, 1},
92 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
93 'r','d',' ','T','i','m','e','\0'}, 420, 0},
95 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
96 'T','i','m','e','\0'}, 360, 1},
98 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
99 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
101 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
102 'i','m','e','\0'}, 300, 1},
104 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
105 'd',' ','T','i','m','e','\0'}, 300, 0},
107 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
108 'i','m','e','\0'}, 240, 1},
110 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
111 'd',' ','T','i','m','e','\0'}, 300, 0},
113 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
114 'T','i','m','e','\0'}, 180, 1},
116 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
117 'd',' ','T','i','m','e','\0'}, 240, 0},
119 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
120 'd',' ','T','i','m','e','\0'}, 240, 0},
122 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
123 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
125 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
126 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
128 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
129 'd',' ','T','i','m','e','\0'}, 180, 0},
131 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
132 ' ','T','i','m','e','\0'}, 120, 1},
134 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
135 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
137 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
138 'm','e','\0'}, 0, 1},
140 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
141 'd',' ','T','i','m','e','\0'}, 60, 0},
143 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
144 ' ','T','i','m','e','\0'}, 0, 0},
146 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
149 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
152 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
153 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
155 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
156 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
158 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
159 ' ','T','i','m','e','\0'}, -180, 1},
161 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
164 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
165 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
167 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
168 'm','e','\0'}, -120, 0},
170 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
171 'm','e','\0'}, -240, 1},
173 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
176 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
177 'i','m','e','\0'}, -240, 1},
179 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
180 ' ','T','i','m','e','\0'}, -180, 0},
182 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
185 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
186 'i','m','e','\0'}, -240, 0},
188 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
189 'T','i','m','e','\0'}, -300, 1},
191 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
192 'r','d',' ','T','i','m','e','\0'}, -270, 0},
194 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
195 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
197 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
198 ' ','T','i','m','e','\0'}, -300, 0},
200 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
203 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
206 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
207 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
209 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
210 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
212 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
213 ' ','T','i','m','e','\0'}, -360, 0},
215 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
216 'i','m','e','\0'}, -390, 0},
218 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
219 'i','m','e','\0'}, -420, 0},
221 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
222 'd',' ','T','i','m','e','\0'}, -480, 1},
224 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
227 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
228 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
230 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
231 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
233 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
234 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
236 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
239 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
242 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
243 'i','m','e','\0'}, -600, 1},
245 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
246 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
248 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
249 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
251 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
252 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
254 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
255 'r','d',' ','T','i','m','e','\0'}, -660, 1},
257 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
258 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
260 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
261 'r','d',' ','T','i','m','e','\0'}, -720, 0},
263 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
266 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
270 #define TICKSPERSEC 10000000
271 #define TICKSPERMSEC 10000
272 #define SECSPERDAY 86400
273 #define SECSPERHOUR 3600
274 #define SECSPERMIN 60
275 #define MINSPERHOUR 60
276 #define HOURSPERDAY 24
277 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
278 #define DAYSPERWEEK 7
279 #define EPOCHYEAR 1601
280 #define DAYSPERNORMALYEAR 365
281 #define DAYSPERLEAPYEAR 366
282 #define MONSPERYEAR 12
283 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
284 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
285 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
287 /* 1601 to 1970 is 369 years plus 89 leap days */
288 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
289 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
290 /* 1601 to 1980 is 379 years plus 91 leap days */
291 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
292 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
295 static const int MonthLengths[2][MONSPERYEAR] =
297 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
298 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
301 static const int YearDays[2][MONSPERYEAR+1] =
303 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
304 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
307 static inline int IsLeapYear(int Year)
309 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
312 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
314 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
315 *CarryField = (CSHORT) (*CarryField + 1);
318 /***********************************************************************
319 * NTDLL_get_server_timeout
321 * Convert a NTDLL timeout into a timeval struct to send to the server.
323 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
327 if (!timeout) /* infinite timeout */
329 when->sec = when->usec = 0;
331 else if (timeout->QuadPart <= 0) /* relative timeout */
334 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
335 gettimeofday( &tv, 0 );
336 when->sec = tv.tv_sec + sec;
337 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
339 when->usec -= 1000000;
343 else /* absolute time */
345 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
346 TICKSPERSEC, &remainder );
347 when->usec = remainder / 10;
352 /******************************************************************************
353 * RtlTimeToTimeFields [NTDLL.@]
355 * Convert a time into a TIME_FIELDS structure.
358 * liTime [I] Time to convert.
359 * TimeFields [O] Destination for the converted time.
364 VOID WINAPI RtlTimeToTimeFields(
365 const LARGE_INTEGER *liTime,
366 PTIME_FIELDS TimeFields)
368 int SecondsInDay, DeltaYear;
369 int LeapYear, CurMonth;
371 LONGLONG Time = liTime->QuadPart;
373 /* Extract millisecond from time and convert time into seconds */
374 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
375 Time = Time / TICKSPERSEC;
377 /* The native version of RtlTimeToTimeFields does not take leap seconds
380 /* Split the time into days and seconds within the day */
381 Days = Time / SECSPERDAY;
382 SecondsInDay = Time % SECSPERDAY;
384 /* compute time of day */
385 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
386 SecondsInDay = SecondsInDay % SECSPERHOUR;
387 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
388 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
390 /* compute day of week */
391 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
394 /* FIXME: handle calendar modifications */
395 TimeFields->Year = EPOCHYEAR;
396 DeltaYear = Days / DAYSPERQUADRICENTENNIUM;
397 TimeFields->Year += DeltaYear * 400;
398 Days -= DeltaYear * DAYSPERQUADRICENTENNIUM;
399 DeltaYear = Days / DAYSPERNORMALCENTURY;
400 TimeFields->Year += DeltaYear * 100;
401 Days -= DeltaYear * DAYSPERNORMALCENTURY;
402 DeltaYear = Days / DAYSPERNORMALQUADRENNIUM;
403 TimeFields->Year += DeltaYear * 4;
404 Days -= DeltaYear * DAYSPERNORMALQUADRENNIUM;
405 DeltaYear = Days / DAYSPERNORMALYEAR;
406 TimeFields->Year += DeltaYear;
407 Days -= DeltaYear * DAYSPERNORMALYEAR;
409 LeapYear = IsLeapYear(TimeFields->Year);
411 /* Compute month of year */
413 while (Days >= YearDays[LeapYear][CurMonth]) CurMonth++;
414 TimeFields->Day = Days - YearDays[LeapYear][CurMonth-1] + 1;
415 TimeFields->Month = CurMonth;
418 /******************************************************************************
419 * RtlTimeFieldsToTime [NTDLL.@]
421 * Convert a TIME_FIELDS structure into a time.
424 * ftTimeFields [I] TIME_FIELDS structure to convert.
425 * Time [O] Destination for the converted time.
431 BOOLEAN WINAPI RtlTimeFieldsToTime(
432 PTIME_FIELDS tfTimeFields,
435 int CurYear, DeltaYear;
437 TIME_FIELDS TimeFields = *tfTimeFields;
441 /* FIXME: normalize the TIME_FIELDS structure here */
442 while (TimeFields.Second >= SECSPERMIN)
443 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
445 while (TimeFields.Minute >= MINSPERHOUR)
446 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
448 while (TimeFields.Hour >= HOURSPERDAY)
449 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
451 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
452 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month,
453 MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1]);
455 while (TimeFields.Month > MONSPERYEAR)
456 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
459 /* FIXME: handle calendar corrections here */
460 CurYear = TimeFields.Year - EPOCHYEAR;
461 DeltaYear = CurYear / 400;
462 CurYear -= DeltaYear * 400;
463 rcTime += DeltaYear * DAYSPERQUADRICENTENNIUM;
464 DeltaYear = CurYear / 100;
465 CurYear -= DeltaYear * 100;
466 rcTime += DeltaYear * DAYSPERNORMALCENTURY;
467 DeltaYear = CurYear / 4;
468 CurYear -= DeltaYear * 4;
469 rcTime += DeltaYear * DAYSPERNORMALQUADRENNIUM;
470 rcTime += CurYear * DAYSPERNORMALYEAR;
471 rcTime += YearDays[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1];
472 rcTime += TimeFields.Day - 1;
473 rcTime *= SECSPERDAY;
474 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
475 rcTime *= TICKSPERSEC;
476 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
477 Time->QuadPart = rcTime;
482 /***********************************************************************
483 * TIME_GetBias [internal]
485 * Helper function calculates delta local time from UTC.
488 * utc [I] The current utc time.
489 * pdaylight [I] Local daylight.
492 * The bias for the current timezone.
494 static int TIME_GetBias(time_t utc, int *pdaylight)
497 static time_t last_utc;
498 static int last_bias;
499 static int last_daylight;
502 RtlEnterCriticalSection( &TIME_GetBias_section );
505 *pdaylight = last_daylight;
509 ptm = localtime(&utc);
510 *pdaylight = last_daylight =
511 ptm->tm_isdst; /* daylight for local timezone */
513 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
515 ret = last_bias = (int)(utc-mktime(ptm));
517 RtlLeaveCriticalSection( &TIME_GetBias_section );
521 /******************************************************************************
522 * RtlLocalTimeToSystemTime [NTDLL.@]
524 * Convert a local time into system time.
527 * LocalTime [I] Local time to convert.
528 * SystemTime [O] Destination for the converted time.
531 * Success: STATUS_SUCCESS.
532 * Failure: An NTSTATUS error code indicating the problem.
534 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
535 PLARGE_INTEGER SystemTime)
540 TRACE("(%p, %p)\n", LocalTime, SystemTime);
543 bias = TIME_GetBias(gmt, &daylight);
545 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)10000000;
546 return STATUS_SUCCESS;
549 /******************************************************************************
550 * RtlSystemTimeToLocalTime [NTDLL.@]
552 * Convert a system time into a local time.
555 * SystemTime [I] System time to convert.
556 * LocalTime [O] Destination for the converted time.
559 * Success: STATUS_SUCCESS.
560 * Failure: An NTSTATUS error code indicating the problem.
562 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
563 PLARGE_INTEGER LocalTime )
568 TRACE("(%p, %p)\n", SystemTime, LocalTime);
571 bias = TIME_GetBias(gmt, &daylight);
573 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)10000000;
574 return STATUS_SUCCESS;
577 /******************************************************************************
578 * RtlTimeToSecondsSince1970 [NTDLL.@]
580 * Convert a time into a count of seconds since 1970.
583 * Time [I] Time to convert.
584 * Seconds [O] Destination for the converted time.
588 * Failure: FALSE, if the resulting value will not fit in a DWORD.
590 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
592 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
593 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
594 tmp -= SECS_1601_TO_1970;
595 if (tmp > 0xffffffff) return FALSE;
596 *Seconds = (DWORD)tmp;
600 /******************************************************************************
601 * RtlTimeToSecondsSince1980 [NTDLL.@]
603 * Convert a time into a count of seconds since 1980.
606 * Time [I] Time to convert.
607 * Seconds [O] Destination for the converted time.
611 * Failure: FALSE, if the resulting value will not fit in a DWORD.
613 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
615 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
616 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
617 tmp -= SECS_1601_TO_1980;
618 if (tmp > 0xffffffff) return FALSE;
619 *Seconds = (DWORD)tmp;
623 /******************************************************************************
624 * RtlSecondsSince1970ToTime [NTDLL.@]
626 * Convert a count of seconds since 1970 to a time.
629 * Seconds [I] Time to convert.
630 * Time [O] Destination for the converted time.
635 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
637 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
638 Time->u.LowPart = (DWORD)secs;
639 Time->u.HighPart = (DWORD)(secs >> 32);
642 /******************************************************************************
643 * RtlSecondsSince1980ToTime [NTDLL.@]
645 * Convert a count of seconds since 1980 to a time.
648 * Seconds [I] Time to convert.
649 * Time [O] Destination for the converted time.
654 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
656 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
657 Time->u.LowPart = (DWORD)secs;
658 Time->u.HighPart = (DWORD)(secs >> 32);
661 /******************************************************************************
662 * RtlTimeToElapsedTimeFields [NTDLL.@]
664 * Convert a time to a count of elapsed seconds.
667 * Time [I] Time to convert.
668 * TimeFields [O] Destination for the converted time.
673 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
678 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
679 TimeFields->Milliseconds = rem / TICKSPERMSEC;
681 /* time is now in seconds */
682 TimeFields->Year = 0;
683 TimeFields->Month = 0;
684 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
686 /* rem is now the remaining seconds in the last day */
687 TimeFields->Second = rem % 60;
689 TimeFields->Minute = rem % 60;
690 TimeFields->Hour = rem / 60;
693 /***********************************************************************
694 * NtQuerySystemTime [NTDLL.@]
695 * ZwQuerySystemTime [NTDLL.@]
697 * Get the current system time.
700 * Time [O] Destination for the current system time.
703 * Success: STATUS_SUCCESS.
704 * Failure: An NTSTATUS error code indicating the problem.
706 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
710 gettimeofday( &now, 0 );
711 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
712 Time->QuadPart += now.tv_usec * 10;
713 return STATUS_SUCCESS;
716 /***********************************************************************
717 * TIME_GetTZAsStr [internal]
719 * Helper function that returns the given timezone as a string.
722 * utc [I] The current utc time.
723 * bias [I] The bias of the current timezone.
730 * This could be done with a hash table instead of merely iterating through a
731 * table, however with the small amount of entries (60 or so) I didn't think
734 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
737 struct tm *ptm = localtime(&utc);
740 if (!strftime (psTZName, 7, "%Z", ptm))
743 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
745 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
746 TZ_INFO[i].bias == bias &&
747 TZ_INFO[i].dst == dst
749 return TZ_INFO[i].psTZWindows;
755 /***********************************************************************
756 * RtlQueryTimeZoneInformation [NTDLL.@]
758 * Get information about the current timezone.
761 * tzinfo [O] Destination for the retrieved timezone info.
764 * Success: STATUS_SUCCESS.
765 * Failure: An NTSTATUS error code indicating the problem.
767 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
773 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
776 bias = TIME_GetBias(gmt, &daylight);
778 tzinfo->Bias = -bias / 60;
779 tzinfo->StandardBias = 0;
780 tzinfo->DaylightBias = -60;
781 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
782 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
783 return STATUS_SUCCESS;
786 /***********************************************************************
787 * RtlSetTimeZoneInformation [NTDLL.@]
789 * Set the current time zone information.
792 * tzinfo [I] Timezone information to set.
795 * Success: STATUS_SUCCESS.
796 * Failure: An NTSTATUS error code indicating the problem.
799 * Uses the obsolete unix timezone structure and tz_dsttime member.
801 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
803 #ifdef HAVE_SETTIMEOFDAY
806 tz.tz_minuteswest = tzinfo->Bias;
808 tz.tz_dsttime = DST_NONE;
812 if(!settimeofday(NULL, &tz))
813 return STATUS_SUCCESS;
815 return STATUS_PRIVILEGE_NOT_HELD;
818 /***********************************************************************
819 * NtSetSystemTime [NTDLL.@]
820 * ZwSetSystemTime [NTDLL.@]
822 * Set the system time.
825 * NewTime [I] The time to set.
826 * OldTime [O] Optional destination for the previous system time.
829 * Success: STATUS_SUCCESS.
830 * Failure: An NTSTATUS error code indicating the problem.
832 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
842 /* Return the old time if necessary */
844 NtQuerySystemTime(OldTime);
846 RtlTimeToTimeFields(NewTime, &tf);
848 /* call gettimeofday to get the current timezone */
849 gettimeofday(&tv, &tz);
851 /* get delta local time from utc */
852 bias = TIME_GetBias(oldsec, &dst);
854 /* get the number of seconds */
855 t.tm_sec = tf.Second;
856 t.tm_min = tf.Minute;
859 t.tm_mon = tf.Month - 1;
860 t.tm_year = tf.Year - 1900;
863 /* correct for timezone and daylight */
866 /* set the new time */
868 tv.tv_usec = tf.Milliseconds * 1000;
870 /* error and sanity check*/
871 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
874 #ifdef HAVE_SETTIMEOFDAY
875 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
877 return STATUS_SUCCESS;
883 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
884 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
886 err == -1 ? "No Permission"
887 : sec == (time_t)-1 ? "" : "is too large." );
890 return STATUS_INVALID_PARAMETER;
892 return STATUS_PRIVILEGE_NOT_HELD;
894 return STATUS_NOT_IMPLEMENTED;