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"
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
40 #define NONAMELESSUNION
41 #define NONAMELESSSTRUCT
46 #include "wine/unicode.h"
47 #include "wine/debug.h"
48 #include "ntdll_misc.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
52 static CRITICAL_SECTION TIME_GetBias_section;
53 static CRITICAL_SECTION_DEBUG critsect_debug =
55 0, 0, &TIME_GetBias_section,
56 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
57 0, 0, { 0, (DWORD)(__FILE__ ": TIME_GetBias_section") }
59 static CRITICAL_SECTION TIME_GetBias_section = { &critsect_debug, -1, 0, 0, 0, 0 };
62 #define SETTIME_MAX_ADJUST 120
64 /* This structure is used to store strings that represent all of the time zones
65 * in the world. (This is used to help GetTimeZoneInformation)
69 const char *psTZFromUnix;
70 WCHAR psTZWindows[32];
75 static const struct tagTZ_INFO TZ_INFO[] =
78 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
79 'T','i','m','e','\0'}, -720, 0},
81 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
84 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
85 'T','i','m','e','\0'}, 600, 0},
87 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
88 'i','m','e','\0'}, 480, 1},
90 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
91 'i','m','e','\0'}, 420, 1},
93 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
94 'r','d',' ','T','i','m','e','\0'}, 420, 0},
96 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
97 'T','i','m','e','\0'}, 360, 1},
99 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
100 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
102 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
103 'i','m','e','\0'}, 300, 1},
105 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
106 'd',' ','T','i','m','e','\0'}, 300, 0},
108 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
109 'i','m','e','\0'}, 240, 1},
111 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
112 'd',' ','T','i','m','e','\0'}, 300, 0},
114 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
115 'T','i','m','e','\0'}, 180, 1},
117 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
118 'd',' ','T','i','m','e','\0'}, 240, 0},
120 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
121 'd',' ','T','i','m','e','\0'}, 240, 0},
123 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
124 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
126 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
127 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
129 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
130 'd',' ','T','i','m','e','\0'}, 180, 0},
132 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
133 ' ','T','i','m','e','\0'}, 120, 1},
135 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
136 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
138 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
139 'm','e','\0'}, 0, 1},
141 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
142 'd',' ','T','i','m','e','\0'}, 60, 0},
144 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
145 ' ','T','i','m','e','\0'}, 0, 0},
147 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
150 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
153 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
156 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
157 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
159 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
160 'n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
162 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
163 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
165 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
166 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
168 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
169 ' ','T','i','m','e','\0'}, -180, 1},
171 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
174 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
175 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
177 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
178 'm','e','\0'}, -120, 0},
180 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
181 'm','e','\0'}, -240, 1},
183 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
186 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
187 'i','m','e','\0'}, -240, 1},
189 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
190 ' ','T','i','m','e','\0'}, -180, 0},
192 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
195 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
196 'i','m','e','\0'}, -240, 0},
198 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
199 'T','i','m','e','\0'}, -300, 1},
201 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
202 'r','d',' ','T','i','m','e','\0'}, -270, 0},
204 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
205 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
207 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
208 ' ','T','i','m','e','\0'}, -300, 0},
210 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
213 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
216 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
217 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
219 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
220 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
222 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
223 ' ','T','i','m','e','\0'}, -360, 0},
225 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
226 'i','m','e','\0'}, -390, 0},
228 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
229 'i','m','e','\0'}, -420, 0},
231 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
232 'd',' ','T','i','m','e','\0'}, -480, 1},
234 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
237 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
238 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
240 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
241 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
243 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
244 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
246 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
249 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
252 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
253 'i','m','e','\0'}, -600, 1},
255 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
256 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
258 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
259 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
261 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
262 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
264 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
265 'r','d',' ','T','i','m','e','\0'}, -660, 1},
267 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
268 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
270 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
271 'r','d',' ','T','i','m','e','\0'}, -720, 0},
273 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
276 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
280 #define TICKSPERSEC 10000000
281 #define TICKSPERMSEC 10000
282 #define SECSPERDAY 86400
283 #define SECSPERHOUR 3600
284 #define SECSPERMIN 60
285 #define MINSPERHOUR 60
286 #define HOURSPERDAY 24
287 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
288 #define DAYSPERWEEK 7
289 #define EPOCHYEAR 1601
290 #define DAYSPERNORMALYEAR 365
291 #define DAYSPERLEAPYEAR 366
292 #define MONSPERYEAR 12
293 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
294 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
295 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
297 /* 1601 to 1970 is 369 years plus 89 leap days */
298 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
299 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
300 /* 1601 to 1980 is 379 years plus 91 leap days */
301 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
302 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
303 /* max ticks that can be represented as Unix time */
304 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
307 static const int MonthLengths[2][MONSPERYEAR] =
309 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
310 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
313 static const int YearDays[2][MONSPERYEAR+1] =
315 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
316 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
319 static inline int IsLeapYear(int Year)
321 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
324 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
326 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
327 *CarryField = (CSHORT) (*CarryField + 1);
330 /***********************************************************************
331 * NTDLL_get_server_timeout
333 * Convert a NTDLL timeout into a timeval struct to send to the server.
335 void NTDLL_get_server_timeout( abs_time_t *when, const LARGE_INTEGER *timeout )
339 if (!timeout) /* infinite timeout */
341 when->sec = when->usec = 0;
343 else if (timeout->QuadPart <= 0) /* relative timeout */
347 if (-timeout->QuadPart > (LONGLONG)INT_MAX * TICKSPERSEC)
348 when->sec = when->usec = INT_MAX;
351 ULONG sec = RtlEnlargedUnsignedDivide( -timeout->QuadPart, TICKSPERSEC, &remainder );
352 gettimeofday( &tv, 0 );
353 when->sec = tv.tv_sec + sec;
354 if ((when->usec = tv.tv_usec + (remainder / 10)) >= 1000000)
356 when->usec -= 1000000;
359 if (when->sec < tv.tv_sec) /* overflow */
360 when->sec = when->usec = INT_MAX;
363 else /* absolute time */
365 if (timeout->QuadPart < TICKS_1601_TO_1970)
366 when->sec = when->usec = 0;
367 else if (timeout->QuadPart > TICKS_1601_TO_UNIX_MAX)
368 when->sec = when->usec = INT_MAX;
371 when->sec = RtlEnlargedUnsignedDivide( timeout->QuadPart - TICKS_1601_TO_1970,
372 TICKSPERSEC, &remainder );
373 when->usec = remainder / 10;
379 /******************************************************************************
380 * RtlTimeToTimeFields [NTDLL.@]
382 * Convert a time into a TIME_FIELDS structure.
385 * liTime [I] Time to convert.
386 * TimeFields [O] Destination for the converted time.
391 VOID WINAPI RtlTimeToTimeFields(
392 const LARGE_INTEGER *liTime,
393 PTIME_FIELDS TimeFields)
395 int SecondsInDay, DeltaYear;
396 int LeapYear, CurMonth;
398 LONGLONG Time = liTime->QuadPart;
400 /* Extract millisecond from time and convert time into seconds */
401 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
402 Time = Time / TICKSPERSEC;
404 /* The native version of RtlTimeToTimeFields does not take leap seconds
407 /* Split the time into days and seconds within the day */
408 Days = Time / SECSPERDAY;
409 SecondsInDay = Time % SECSPERDAY;
411 /* compute time of day */
412 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
413 SecondsInDay = SecondsInDay % SECSPERHOUR;
414 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
415 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
417 /* compute day of week */
418 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
421 /* FIXME: handle calendar modifications */
422 TimeFields->Year = EPOCHYEAR;
423 DeltaYear = Days / DAYSPERQUADRICENTENNIUM;
424 TimeFields->Year += DeltaYear * 400;
425 Days -= DeltaYear * DAYSPERQUADRICENTENNIUM;
426 DeltaYear = Days / DAYSPERNORMALCENTURY;
427 TimeFields->Year += DeltaYear * 100;
428 Days -= DeltaYear * DAYSPERNORMALCENTURY;
429 DeltaYear = Days / DAYSPERNORMALQUADRENNIUM;
430 TimeFields->Year += DeltaYear * 4;
431 Days -= DeltaYear * DAYSPERNORMALQUADRENNIUM;
432 DeltaYear = Days / DAYSPERNORMALYEAR;
433 TimeFields->Year += DeltaYear;
434 Days -= DeltaYear * DAYSPERNORMALYEAR;
436 LeapYear = IsLeapYear(TimeFields->Year);
438 /* Compute month of year */
440 while (Days >= YearDays[LeapYear][CurMonth]) CurMonth++;
441 TimeFields->Day = Days - YearDays[LeapYear][CurMonth-1] + 1;
442 TimeFields->Month = CurMonth;
445 /******************************************************************************
446 * RtlTimeFieldsToTime [NTDLL.@]
448 * Convert a TIME_FIELDS structure into a time.
451 * ftTimeFields [I] TIME_FIELDS structure to convert.
452 * Time [O] Destination for the converted time.
458 BOOLEAN WINAPI RtlTimeFieldsToTime(
459 PTIME_FIELDS tfTimeFields,
462 int CurYear, DeltaYear;
464 TIME_FIELDS TimeFields = *tfTimeFields;
468 /* FIXME: normalize the TIME_FIELDS structure here */
469 while (TimeFields.Second >= SECSPERMIN)
470 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
472 while (TimeFields.Minute >= MINSPERHOUR)
473 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
475 while (TimeFields.Hour >= HOURSPERDAY)
476 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
478 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
479 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month,
480 MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1]);
482 while (TimeFields.Month > MONSPERYEAR)
483 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
486 /* FIXME: handle calendar corrections here */
487 CurYear = TimeFields.Year - EPOCHYEAR;
488 DeltaYear = CurYear / 400;
489 CurYear -= DeltaYear * 400;
490 rcTime += DeltaYear * DAYSPERQUADRICENTENNIUM;
491 DeltaYear = CurYear / 100;
492 CurYear -= DeltaYear * 100;
493 rcTime += DeltaYear * DAYSPERNORMALCENTURY;
494 DeltaYear = CurYear / 4;
495 CurYear -= DeltaYear * 4;
496 rcTime += DeltaYear * DAYSPERNORMALQUADRENNIUM;
497 rcTime += CurYear * DAYSPERNORMALYEAR;
498 rcTime += YearDays[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1];
499 rcTime += TimeFields.Day - 1;
500 rcTime *= SECSPERDAY;
501 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
502 rcTime *= TICKSPERSEC;
503 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
504 Time->QuadPart = rcTime;
509 /***********************************************************************
510 * TIME_GetBias [internal]
512 * Helper function calculates delta local time from UTC.
515 * utc [I] The current utc time.
516 * pdaylight [I] Local daylight.
519 * The bias for the current timezone.
521 static int TIME_GetBias(time_t utc, int *pdaylight)
524 static time_t last_utc;
525 static int last_bias;
526 static int last_daylight;
529 RtlEnterCriticalSection( &TIME_GetBias_section );
532 *pdaylight = last_daylight;
536 ptm = localtime(&utc);
537 *pdaylight = last_daylight =
538 ptm->tm_isdst; /* daylight for local timezone */
540 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
542 ret = last_bias = (int)(utc-mktime(ptm));
544 RtlLeaveCriticalSection( &TIME_GetBias_section );
548 /******************************************************************************
549 * RtlLocalTimeToSystemTime [NTDLL.@]
551 * Convert a local time into system time.
554 * LocalTime [I] Local time to convert.
555 * SystemTime [O] Destination for the converted time.
558 * Success: STATUS_SUCCESS.
559 * Failure: An NTSTATUS error code indicating the problem.
561 NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
562 PLARGE_INTEGER SystemTime)
567 TRACE("(%p, %p)\n", LocalTime, SystemTime);
570 bias = TIME_GetBias(gmt, &daylight);
572 SystemTime->QuadPart = LocalTime->QuadPart - bias * (LONGLONG)10000000;
573 return STATUS_SUCCESS;
576 /******************************************************************************
577 * RtlSystemTimeToLocalTime [NTDLL.@]
579 * Convert a system time into a local time.
582 * SystemTime [I] System time to convert.
583 * LocalTime [O] Destination for the converted time.
586 * Success: STATUS_SUCCESS.
587 * Failure: An NTSTATUS error code indicating the problem.
589 NTSTATUS WINAPI RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
590 PLARGE_INTEGER LocalTime )
595 TRACE("(%p, %p)\n", SystemTime, LocalTime);
598 bias = TIME_GetBias(gmt, &daylight);
600 LocalTime->QuadPart = SystemTime->QuadPart + bias * (LONGLONG)10000000;
601 return STATUS_SUCCESS;
604 /******************************************************************************
605 * RtlTimeToSecondsSince1970 [NTDLL.@]
607 * Convert a time into a count of seconds since 1970.
610 * Time [I] Time to convert.
611 * Seconds [O] Destination for the converted time.
615 * Failure: FALSE, if the resulting value will not fit in a DWORD.
617 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, LPDWORD Seconds )
619 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
620 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
621 tmp -= SECS_1601_TO_1970;
622 if (tmp > 0xffffffff) return FALSE;
623 *Seconds = (DWORD)tmp;
627 /******************************************************************************
628 * RtlTimeToSecondsSince1980 [NTDLL.@]
630 * Convert a time into a count of seconds since 1980.
633 * Time [I] Time to convert.
634 * Seconds [O] Destination for the converted time.
638 * Failure: FALSE, if the resulting value will not fit in a DWORD.
640 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *Time, LPDWORD Seconds )
642 ULONGLONG tmp = ((ULONGLONG)Time->u.HighPart << 32) | Time->u.LowPart;
643 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
644 tmp -= SECS_1601_TO_1980;
645 if (tmp > 0xffffffff) return FALSE;
646 *Seconds = (DWORD)tmp;
650 /******************************************************************************
651 * RtlSecondsSince1970ToTime [NTDLL.@]
653 * Convert a count of seconds since 1970 to a time.
656 * Seconds [I] Time to convert.
657 * Time [O] Destination for the converted time.
662 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
664 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
665 Time->u.LowPart = (DWORD)secs;
666 Time->u.HighPart = (DWORD)(secs >> 32);
669 /******************************************************************************
670 * RtlSecondsSince1980ToTime [NTDLL.@]
672 * Convert a count of seconds since 1980 to a time.
675 * Seconds [I] Time to convert.
676 * Time [O] Destination for the converted time.
681 void WINAPI RtlSecondsSince1980ToTime( DWORD Seconds, LARGE_INTEGER *Time )
683 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1980;
684 Time->u.LowPart = (DWORD)secs;
685 Time->u.HighPart = (DWORD)(secs >> 32);
688 /******************************************************************************
689 * RtlTimeToElapsedTimeFields [NTDLL.@]
691 * Convert a time to a count of elapsed seconds.
694 * Time [I] Time to convert.
695 * TimeFields [O] Destination for the converted time.
700 void WINAPI RtlTimeToElapsedTimeFields( const LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
705 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
706 TimeFields->Milliseconds = rem / TICKSPERMSEC;
708 /* time is now in seconds */
709 TimeFields->Year = 0;
710 TimeFields->Month = 0;
711 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
713 /* rem is now the remaining seconds in the last day */
714 TimeFields->Second = rem % 60;
716 TimeFields->Minute = rem % 60;
717 TimeFields->Hour = rem / 60;
720 /***********************************************************************
721 * NtQuerySystemTime [NTDLL.@]
722 * ZwQuerySystemTime [NTDLL.@]
724 * Get the current system time.
727 * Time [O] Destination for the current system time.
730 * Success: STATUS_SUCCESS.
731 * Failure: An NTSTATUS error code indicating the problem.
733 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER Time )
737 gettimeofday( &now, 0 );
738 Time->QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
739 Time->QuadPart += now.tv_usec * 10;
740 return STATUS_SUCCESS;
743 /***********************************************************************
744 * TIME_GetTZAsStr [internal]
746 * Helper function that returns the given timezone as a string.
749 * utc [I] The current utc time.
750 * bias [I] The bias of the current timezone.
757 * This could be done with a hash table instead of merely iterating through a
758 * table, however with the small amount of entries (60 or so) I didn't think
761 static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
764 struct tm *ptm = localtime(&utc);
767 if (!strftime (psTZName, 7, "%Z", ptm))
770 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
772 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
773 TZ_INFO[i].bias == bias &&
774 TZ_INFO[i].dst == dst
776 return TZ_INFO[i].psTZWindows;
778 FIXME("Can't match system time zone name \"%s\" to an entry in TZ_INFO\n",psTZName);
779 FIXME(" Please add appropriate entry to TZ_INFO and submit as patch to wine-patches\n");
783 /***********************************************************************
784 * RtlQueryTimeZoneInformation [NTDLL.@]
786 * Get information about the current timezone.
789 * tzinfo [O] Destination for the retrieved timezone info.
792 * Success: STATUS_SUCCESS.
793 * Failure: An NTSTATUS error code indicating the problem.
795 NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
801 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
804 bias = TIME_GetBias(gmt, &daylight);
806 tzinfo->Bias = -bias / 60;
807 tzinfo->StandardBias = 0;
808 tzinfo->DaylightBias = -60;
809 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
810 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
811 return STATUS_SUCCESS;
814 /***********************************************************************
815 * RtlSetTimeZoneInformation [NTDLL.@]
817 * Set the current time zone information.
820 * tzinfo [I] Timezone information to set.
823 * Success: STATUS_SUCCESS.
824 * Failure: An NTSTATUS error code indicating the problem.
827 * Uses the obsolete unix timezone structure and tz_dsttime member.
829 NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
831 #ifdef HAVE_SETTIMEOFDAY
834 tz.tz_minuteswest = tzinfo->Bias;
836 tz.tz_dsttime = DST_NONE;
840 if(!settimeofday(NULL, &tz))
841 return STATUS_SUCCESS;
843 return STATUS_PRIVILEGE_NOT_HELD;
846 /***********************************************************************
847 * NtSetSystemTime [NTDLL.@]
848 * ZwSetSystemTime [NTDLL.@]
850 * Set the system time.
853 * NewTime [I] The time to set.
854 * OldTime [O] Optional destination for the previous system time.
857 * Success: STATUS_SUCCESS.
858 * Failure: An NTSTATUS error code indicating the problem.
860 NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
870 /* Return the old time if necessary */
872 NtQuerySystemTime(OldTime);
874 RtlTimeToTimeFields(NewTime, &tf);
876 /* call gettimeofday to get the current timezone */
877 gettimeofday(&tv, &tz);
879 /* get delta local time from utc */
880 bias = TIME_GetBias(oldsec, &dst);
882 /* get the number of seconds */
883 t.tm_sec = tf.Second;
884 t.tm_min = tf.Minute;
887 t.tm_mon = tf.Month - 1;
888 t.tm_year = tf.Year - 1900;
891 /* correct for timezone and daylight */
894 /* set the new time */
896 tv.tv_usec = tf.Milliseconds * 1000;
898 /* error and sanity check*/
899 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
902 #ifdef HAVE_SETTIMEOFDAY
903 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
905 return STATUS_SUCCESS;
911 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
912 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
914 err == -1 ? "No Permission"
915 : sec == (time_t)-1 ? "" : "is too large." );
918 return STATUS_INVALID_PARAMETER;
920 return STATUS_PRIVILEGE_NOT_HELD;
922 return STATUS_NOT_IMPLEMENTED;