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 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
154 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
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'}, -60, 0},
159 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','D','a','y',
160 'l','i','g','h','t',' ','T','i','m','e','\0'}, -120, 1},
162 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
163 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
165 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
166 ' ','T','i','m','e','\0'}, -180, 1},
168 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
171 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
172 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
174 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
175 'm','e','\0'}, -120, 0},
177 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
178 'm','e','\0'}, -240, 1},
180 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
183 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
184 'i','m','e','\0'}, -240, 1},
186 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
187 ' ','T','i','m','e','\0'}, -180, 0},
189 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
192 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
193 'i','m','e','\0'}, -240, 0},
195 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
196 'T','i','m','e','\0'}, -300, 1},
198 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
199 'r','d',' ','T','i','m','e','\0'}, -270, 0},
201 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
202 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
204 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
205 ' ','T','i','m','e','\0'}, -300, 0},
207 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
210 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
213 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
214 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
216 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
217 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
219 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
220 ' ','T','i','m','e','\0'}, -360, 0},
222 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
223 'i','m','e','\0'}, -390, 0},
225 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
226 'i','m','e','\0'}, -420, 0},
228 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
229 'd',' ','T','i','m','e','\0'}, -480, 1},
231 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
234 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
235 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
237 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
238 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
240 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
241 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
243 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
246 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
249 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
250 'i','m','e','\0'}, -600, 1},
252 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
253 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
255 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
256 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
258 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
259 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
261 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
262 'r','d',' ','T','i','m','e','\0'}, -660, 1},
264 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
265 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
267 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
268 'r','d',' ','T','i','m','e','\0'}, -720, 0},
270 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
273 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
277 #define TICKSPERSEC 10000000
278 #define TICKSPERMSEC 10000
279 #define SECSPERDAY 86400
280 #define SECSPERHOUR 3600
281 #define SECSPERMIN 60
282 #define MINSPERHOUR 60
283 #define HOURSPERDAY 24
284 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
285 #define DAYSPERWEEK 7
286 #define EPOCHYEAR 1601
287 #define DAYSPERNORMALYEAR 365
288 #define DAYSPERLEAPYEAR 366
289 #define MONSPERYEAR 12
290 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
291 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
292 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
294 /* 1601 to 1970 is 369 years plus 89 leap days */
295 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
296 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
297 /* 1601 to 1980 is 379 years plus 91 leap days */
298 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
299 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
300 /* max ticks that can be represented as Unix time */
301 #define TICKS_1601_TO_UNIX_MAX ((SECS_1601_TO_1970 + INT_MAX) * TICKSPERSEC)
304 static const int MonthLengths
[2][MONSPERYEAR
] =
306 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
307 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
310 static const int YearDays
[2][MONSPERYEAR
+1] =
312 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
313 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
316 static inline int IsLeapYear(int Year
)
318 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
321 static inline void NormalizeTimeFields(CSHORT
*FieldToNormalize
, CSHORT
*CarryField
,int Modulus
)
323 *FieldToNormalize
= (CSHORT
) (*FieldToNormalize
- Modulus
);
324 *CarryField
= (CSHORT
) (*CarryField
+ 1);
327 /***********************************************************************
328 * NTDLL_get_server_timeout
330 * Convert a NTDLL timeout into a timeval struct to send to the server.
332 void NTDLL_get_server_timeout( abs_time_t
*when
, const LARGE_INTEGER
*timeout
)
336 if (!timeout
) /* infinite timeout */
338 when
->sec
= when
->usec
= 0;
340 else if (timeout
->QuadPart
<= 0) /* relative timeout */
344 if (-timeout
->QuadPart
> (LONGLONG
)INT_MAX
* TICKSPERSEC
)
345 when
->sec
= when
->usec
= INT_MAX
;
348 ULONG sec
= RtlEnlargedUnsignedDivide( -timeout
->QuadPart
, TICKSPERSEC
, &remainder
);
349 gettimeofday( &tv
, 0 );
350 when
->sec
= tv
.tv_sec
+ sec
;
351 if ((when
->usec
= tv
.tv_usec
+ (remainder
/ 10)) >= 1000000)
353 when
->usec
-= 1000000;
356 if (when
->sec
< tv
.tv_sec
) /* overflow */
357 when
->sec
= when
->usec
= INT_MAX
;
360 else /* absolute time */
362 if (timeout
->QuadPart
< TICKS_1601_TO_1970
)
363 when
->sec
= when
->usec
= 0;
364 else if (timeout
->QuadPart
> TICKS_1601_TO_UNIX_MAX
)
365 when
->sec
= when
->usec
= INT_MAX
;
368 when
->sec
= RtlEnlargedUnsignedDivide( timeout
->QuadPart
- TICKS_1601_TO_1970
,
369 TICKSPERSEC
, &remainder
);
370 when
->usec
= remainder
/ 10;
376 /******************************************************************************
377 * RtlTimeToTimeFields [NTDLL.@]
379 * Convert a time into a TIME_FIELDS structure.
382 * liTime [I] Time to convert.
383 * TimeFields [O] Destination for the converted time.
388 VOID WINAPI
RtlTimeToTimeFields(
389 const LARGE_INTEGER
*liTime
,
390 PTIME_FIELDS TimeFields
)
392 int SecondsInDay
, DeltaYear
;
393 int LeapYear
, CurMonth
;
395 LONGLONG Time
= liTime
->QuadPart
;
397 /* Extract millisecond from time and convert time into seconds */
398 TimeFields
->Milliseconds
= (CSHORT
) ((Time
% TICKSPERSEC
) / TICKSPERMSEC
);
399 Time
= Time
/ TICKSPERSEC
;
401 /* The native version of RtlTimeToTimeFields does not take leap seconds
404 /* Split the time into days and seconds within the day */
405 Days
= Time
/ SECSPERDAY
;
406 SecondsInDay
= Time
% SECSPERDAY
;
408 /* compute time of day */
409 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
410 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
411 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
412 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
414 /* compute day of week */
415 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
418 /* FIXME: handle calendar modifications */
419 TimeFields
->Year
= EPOCHYEAR
;
420 DeltaYear
= Days
/ DAYSPERQUADRICENTENNIUM
;
421 TimeFields
->Year
+= DeltaYear
* 400;
422 Days
-= DeltaYear
* DAYSPERQUADRICENTENNIUM
;
423 DeltaYear
= Days
/ DAYSPERNORMALCENTURY
;
424 TimeFields
->Year
+= DeltaYear
* 100;
425 Days
-= DeltaYear
* DAYSPERNORMALCENTURY
;
426 DeltaYear
= Days
/ DAYSPERNORMALQUADRENNIUM
;
427 TimeFields
->Year
+= DeltaYear
* 4;
428 Days
-= DeltaYear
* DAYSPERNORMALQUADRENNIUM
;
429 DeltaYear
= Days
/ DAYSPERNORMALYEAR
;
430 TimeFields
->Year
+= DeltaYear
;
431 Days
-= DeltaYear
* DAYSPERNORMALYEAR
;
433 LeapYear
= IsLeapYear(TimeFields
->Year
);
435 /* Compute month of year */
437 while (Days
>= YearDays
[LeapYear
][CurMonth
]) CurMonth
++;
438 TimeFields
->Day
= Days
- YearDays
[LeapYear
][CurMonth
-1] + 1;
439 TimeFields
->Month
= CurMonth
;
442 /******************************************************************************
443 * RtlTimeFieldsToTime [NTDLL.@]
445 * Convert a TIME_FIELDS structure into a time.
448 * ftTimeFields [I] TIME_FIELDS structure to convert.
449 * Time [O] Destination for the converted time.
455 BOOLEAN WINAPI
RtlTimeFieldsToTime(
456 PTIME_FIELDS tfTimeFields
,
459 int CurYear
, DeltaYear
;
461 TIME_FIELDS TimeFields
= *tfTimeFields
;
465 /* FIXME: normalize the TIME_FIELDS structure here */
466 while (TimeFields
.Second
>= SECSPERMIN
)
467 { NormalizeTimeFields(&TimeFields
.Second
, &TimeFields
.Minute
, SECSPERMIN
);
469 while (TimeFields
.Minute
>= MINSPERHOUR
)
470 { NormalizeTimeFields(&TimeFields
.Minute
, &TimeFields
.Hour
, MINSPERHOUR
);
472 while (TimeFields
.Hour
>= HOURSPERDAY
)
473 { NormalizeTimeFields(&TimeFields
.Hour
, &TimeFields
.Day
, HOURSPERDAY
);
475 while (TimeFields
.Day
> MonthLengths
[IsLeapYear(TimeFields
.Year
)][TimeFields
.Month
- 1])
476 { NormalizeTimeFields(&TimeFields
.Day
, &TimeFields
.Month
,
477 MonthLengths
[IsLeapYear(TimeFields
.Year
)][TimeFields
.Month
- 1]);
479 while (TimeFields
.Month
> MONSPERYEAR
)
480 { NormalizeTimeFields(&TimeFields
.Month
, &TimeFields
.Year
, MONSPERYEAR
);
483 /* FIXME: handle calendar corrections here */
484 CurYear
= TimeFields
.Year
- EPOCHYEAR
;
485 DeltaYear
= CurYear
/ 400;
486 CurYear
-= DeltaYear
* 400;
487 rcTime
+= DeltaYear
* DAYSPERQUADRICENTENNIUM
;
488 DeltaYear
= CurYear
/ 100;
489 CurYear
-= DeltaYear
* 100;
490 rcTime
+= DeltaYear
* DAYSPERNORMALCENTURY
;
491 DeltaYear
= CurYear
/ 4;
492 CurYear
-= DeltaYear
* 4;
493 rcTime
+= DeltaYear
* DAYSPERNORMALQUADRENNIUM
;
494 rcTime
+= CurYear
* DAYSPERNORMALYEAR
;
495 rcTime
+= YearDays
[IsLeapYear(TimeFields
.Year
)][TimeFields
.Month
- 1];
496 rcTime
+= TimeFields
.Day
- 1;
497 rcTime
*= SECSPERDAY
;
498 rcTime
+= TimeFields
.Hour
* SECSPERHOUR
+ TimeFields
.Minute
* SECSPERMIN
+ TimeFields
.Second
;
499 rcTime
*= TICKSPERSEC
;
500 rcTime
+= TimeFields
.Milliseconds
* TICKSPERMSEC
;
501 Time
->QuadPart
= rcTime
;
506 /***********************************************************************
507 * TIME_GetBias [internal]
509 * Helper function calculates delta local time from UTC.
512 * utc [I] The current utc time.
513 * pdaylight [I] Local daylight.
516 * The bias for the current timezone.
518 static int TIME_GetBias(time_t utc
, int *pdaylight
)
521 static time_t last_utc
;
522 static int last_bias
;
523 static int last_daylight
;
526 RtlEnterCriticalSection( &TIME_GetBias_section
);
529 *pdaylight
= last_daylight
;
533 ptm
= localtime(&utc
);
534 *pdaylight
= last_daylight
=
535 ptm
->tm_isdst
; /* daylight for local timezone */
537 ptm
->tm_isdst
= *pdaylight
; /* use local daylight, not that of Greenwich */
539 ret
= last_bias
= (int)(utc
-mktime(ptm
));
541 RtlLeaveCriticalSection( &TIME_GetBias_section
);
545 /******************************************************************************
546 * RtlLocalTimeToSystemTime [NTDLL.@]
548 * Convert a local time into system time.
551 * LocalTime [I] Local time to convert.
552 * SystemTime [O] Destination for the converted time.
555 * Success: STATUS_SUCCESS.
556 * Failure: An NTSTATUS error code indicating the problem.
558 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
559 PLARGE_INTEGER SystemTime
)
564 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
567 bias
= TIME_GetBias(gmt
, &daylight
);
569 SystemTime
->QuadPart
= LocalTime
->QuadPart
- bias
* (LONGLONG
)10000000;
570 return STATUS_SUCCESS
;
573 /******************************************************************************
574 * RtlSystemTimeToLocalTime [NTDLL.@]
576 * Convert a system time into a local time.
579 * SystemTime [I] System time to convert.
580 * LocalTime [O] Destination for the converted time.
583 * Success: STATUS_SUCCESS.
584 * Failure: An NTSTATUS error code indicating the problem.
586 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
587 PLARGE_INTEGER LocalTime
)
592 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
595 bias
= TIME_GetBias(gmt
, &daylight
);
597 LocalTime
->QuadPart
= SystemTime
->QuadPart
+ bias
* (LONGLONG
)10000000;
598 return STATUS_SUCCESS
;
601 /******************************************************************************
602 * RtlTimeToSecondsSince1970 [NTDLL.@]
604 * Convert a time into a count of seconds since 1970.
607 * Time [I] Time to convert.
608 * Seconds [O] Destination for the converted time.
612 * Failure: FALSE, if the resulting value will not fit in a DWORD.
614 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
616 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
617 tmp
= RtlLargeIntegerDivide( tmp
, 10000000, NULL
);
618 tmp
-= SECS_1601_TO_1970
;
619 if (tmp
> 0xffffffff) return FALSE
;
620 *Seconds
= (DWORD
)tmp
;
624 /******************************************************************************
625 * RtlTimeToSecondsSince1980 [NTDLL.@]
627 * Convert a time into a count of seconds since 1980.
630 * Time [I] Time to convert.
631 * Seconds [O] Destination for the converted time.
635 * Failure: FALSE, if the resulting value will not fit in a DWORD.
637 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
639 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
640 tmp
= RtlLargeIntegerDivide( tmp
, 10000000, NULL
);
641 tmp
-= SECS_1601_TO_1980
;
642 if (tmp
> 0xffffffff) return FALSE
;
643 *Seconds
= (DWORD
)tmp
;
647 /******************************************************************************
648 * RtlSecondsSince1970ToTime [NTDLL.@]
650 * Convert a count of seconds since 1970 to a time.
653 * Seconds [I] Time to convert.
654 * Time [O] Destination for the converted time.
659 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
661 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
662 Time
->u
.LowPart
= (DWORD
)secs
;
663 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
666 /******************************************************************************
667 * RtlSecondsSince1980ToTime [NTDLL.@]
669 * Convert a count of seconds since 1980 to a time.
672 * Seconds [I] Time to convert.
673 * Time [O] Destination for the converted time.
678 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
680 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
681 Time
->u
.LowPart
= (DWORD
)secs
;
682 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
685 /******************************************************************************
686 * RtlTimeToElapsedTimeFields [NTDLL.@]
688 * Convert a time to a count of elapsed seconds.
691 * Time [I] Time to convert.
692 * TimeFields [O] Destination for the converted time.
697 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
702 time
= RtlExtendedLargeIntegerDivide( Time
->QuadPart
, TICKSPERSEC
, &rem
);
703 TimeFields
->Milliseconds
= rem
/ TICKSPERMSEC
;
705 /* time is now in seconds */
706 TimeFields
->Year
= 0;
707 TimeFields
->Month
= 0;
708 TimeFields
->Day
= RtlExtendedLargeIntegerDivide( time
, SECSPERDAY
, &rem
);
710 /* rem is now the remaining seconds in the last day */
711 TimeFields
->Second
= rem
% 60;
713 TimeFields
->Minute
= rem
% 60;
714 TimeFields
->Hour
= rem
/ 60;
717 /***********************************************************************
718 * NtQuerySystemTime [NTDLL.@]
719 * ZwQuerySystemTime [NTDLL.@]
721 * Get the current system time.
724 * Time [O] Destination for the current system time.
727 * Success: STATUS_SUCCESS.
728 * Failure: An NTSTATUS error code indicating the problem.
730 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
734 gettimeofday( &now
, 0 );
735 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
736 Time
->QuadPart
+= now
.tv_usec
* 10;
737 return STATUS_SUCCESS
;
740 /***********************************************************************
741 * TIME_GetTZAsStr [internal]
743 * Helper function that returns the given timezone as a string.
746 * utc [I] The current utc time.
747 * bias [I] The bias of the current timezone.
754 * This could be done with a hash table instead of merely iterating through a
755 * table, however with the small amount of entries (60 or so) I didn't think
758 static const WCHAR
* TIME_GetTZAsStr (time_t utc
, int bias
, int dst
)
761 struct tm
*ptm
= localtime(&utc
);
764 if (!strftime (psTZName
, 7, "%Z", ptm
))
767 for (i
=0; i
<(sizeof(TZ_INFO
) / sizeof(struct tagTZ_INFO
)); i
++)
769 if ( strcmp(TZ_INFO
[i
].psTZFromUnix
, psTZName
) == 0 &&
770 TZ_INFO
[i
].bias
== bias
&&
771 TZ_INFO
[i
].dst
== dst
773 return TZ_INFO
[i
].psTZWindows
;
775 FIXME("Can't match system time zone name \"%s\" to an entry in TZ_INFO\n",psTZName
);
776 FIXME(" Please add appropriate entry to TZ_INFO and submit as patch to wine-patches\n");
780 /***********************************************************************
781 * RtlQueryTimeZoneInformation [NTDLL.@]
783 * Get information about the current timezone.
786 * tzinfo [O] Destination for the retrieved timezone info.
789 * Success: STATUS_SUCCESS.
790 * Failure: An NTSTATUS error code indicating the problem.
792 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo
)
798 memset(tzinfo
, 0, sizeof(TIME_ZONE_INFORMATION
));
801 bias
= TIME_GetBias(gmt
, &daylight
);
803 tzinfo
->Bias
= -bias
/ 60;
804 tzinfo
->StandardBias
= 0;
805 tzinfo
->DaylightBias
= -60;
806 psTZ
= TIME_GetTZAsStr (gmt
, (-bias
/60), daylight
);
807 if (psTZ
) strcpyW( tzinfo
->StandardName
, psTZ
);
808 return STATUS_SUCCESS
;
811 /***********************************************************************
812 * RtlSetTimeZoneInformation [NTDLL.@]
814 * Set the current time zone information.
817 * tzinfo [I] Timezone information to set.
820 * Success: STATUS_SUCCESS.
821 * Failure: An NTSTATUS error code indicating the problem.
824 * Uses the obsolete unix timezone structure and tz_dsttime member.
826 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION
*tzinfo
)
828 #ifdef HAVE_SETTIMEOFDAY
831 tz
.tz_minuteswest
= tzinfo
->Bias
;
833 tz
.tz_dsttime
= DST_NONE
;
837 if(!settimeofday(NULL
, &tz
))
838 return STATUS_SUCCESS
;
840 return STATUS_PRIVILEGE_NOT_HELD
;
843 /***********************************************************************
844 * NtSetSystemTime [NTDLL.@]
845 * ZwSetSystemTime [NTDLL.@]
847 * Set the system time.
850 * NewTime [I] The time to set.
851 * OldTime [O] Optional destination for the previous system time.
854 * Success: STATUS_SUCCESS.
855 * Failure: An NTSTATUS error code indicating the problem.
857 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
867 /* Return the old time if necessary */
869 NtQuerySystemTime(OldTime
);
871 RtlTimeToTimeFields(NewTime
, &tf
);
873 /* call gettimeofday to get the current timezone */
874 gettimeofday(&tv
, &tz
);
876 /* get delta local time from utc */
877 bias
= TIME_GetBias(oldsec
, &dst
);
879 /* get the number of seconds */
880 t
.tm_sec
= tf
.Second
;
881 t
.tm_min
= tf
.Minute
;
884 t
.tm_mon
= tf
.Month
- 1;
885 t
.tm_year
= tf
.Year
- 1900;
888 /* correct for timezone and daylight */
891 /* set the new time */
893 tv
.tv_usec
= tf
.Milliseconds
* 1000;
895 /* error and sanity check*/
896 if(sec
== (time_t)-1 || abs((int)(sec
-oldsec
)) > SETTIME_MAX_ADJUST
) {
899 #ifdef HAVE_SETTIMEOFDAY
900 err
= settimeofday(&tv
, NULL
); /* 0 is OK, -1 is error */
902 return STATUS_SUCCESS
;
908 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
909 tf
.Year
, tf
.Month
, tf
.Day
, tf
.Hour
, tf
.Minute
, tf
.Second
,
911 err
== -1 ? "No Permission"
912 : sec
== (time_t)-1 ? "" : "is too large." );
915 return STATUS_INVALID_PARAMETER
;
917 return STATUS_PRIVILEGE_NOT_HELD
;
919 return STATUS_NOT_IMPLEMENTED
;