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
275 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
276 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
277 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
279 /* 1601 to 1970 is 369 years plus 89 leap days */
280 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
281 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
282 /* 1601 to 1980 is 379 years plus 91 leap days */
283 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
284 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
287 static const int YearLengths
[2] = {DAYSPERNORMALYEAR
, DAYSPERLEAPYEAR
};
288 static const int MonthLengths
[2][MONSPERYEAR
] =
290 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
291 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
294 static inline int IsLeapYear(int Year
)
296 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
299 static inline void NormalizeTimeFields(CSHORT
*FieldToNormalize
, CSHORT
*CarryField
,int Modulus
)
301 *FieldToNormalize
= (CSHORT
) (*FieldToNormalize
- Modulus
);
302 *CarryField
= (CSHORT
) (*CarryField
+ 1);
305 /***********************************************************************
306 * NTDLL_get_server_timeout
308 * Convert a NTDLL timeout into a timeval struct to send to the server.
310 void NTDLL_get_server_timeout( abs_time_t
*when
, const LARGE_INTEGER
*timeout
)
314 if (!timeout
) /* infinite timeout */
316 when
->sec
= when
->usec
= 0;
318 else if (timeout
->QuadPart
<= 0) /* relative timeout */
321 ULONG sec
= RtlEnlargedUnsignedDivide( -timeout
->QuadPart
, TICKSPERSEC
, &remainder
);
322 gettimeofday( &tv
, 0 );
323 when
->sec
= tv
.tv_sec
+ sec
;
324 if ((when
->usec
= tv
.tv_usec
+ (remainder
/ 10)) >= 1000000)
326 when
->usec
-= 1000000;
330 else /* absolute time */
332 when
->sec
= RtlEnlargedUnsignedDivide( timeout
->QuadPart
- TICKS_1601_TO_1970
,
333 TICKSPERSEC
, &remainder
);
334 when
->usec
= remainder
/ 10;
339 /******************************************************************************
340 * RtlTimeToTimeFields [NTDLL.@]
342 * Convert a time into a TIME_FIELDS structure.
345 * liTime [I] Time to convert.
346 * TimeFields [O] Destination for the converted time.
351 VOID WINAPI
RtlTimeToTimeFields(
352 const LARGE_INTEGER
*liTime
,
353 PTIME_FIELDS TimeFields
)
356 int SecondsInDay
, DeltaYear
;
357 int LeapYear
, CurMonth
;
359 LONGLONG Time
= liTime
->QuadPart
;
361 /* Extract millisecond from time and convert time into seconds */
362 TimeFields
->Milliseconds
= (CSHORT
) ((Time
% TICKSPERSEC
) / TICKSPERMSEC
);
363 Time
= Time
/ TICKSPERSEC
;
365 /* The native version of RtlTimeToTimeFields does not take leap seconds
368 /* Split the time into days and seconds within the day */
369 Days
= Time
/ SECSPERDAY
;
370 SecondsInDay
= Time
% SECSPERDAY
;
372 /* compute time of day */
373 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
374 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
375 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
376 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
378 /* compute day of week */
379 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
382 /* FIXME: handle calendar modifications */
383 TimeFields
->Year
= EPOCHYEAR
;
384 DeltaYear
= Days
/ DAYSPERQUADRICENTENNIUM
;
385 TimeFields
->Year
+= DeltaYear
* 400;
386 Days
-= DeltaYear
* DAYSPERQUADRICENTENNIUM
;
387 DeltaYear
= Days
/ DAYSPERNORMALCENTURY
;
388 TimeFields
->Year
+= DeltaYear
* 100;
389 Days
-= DeltaYear
* DAYSPERNORMALCENTURY
;
390 DeltaYear
= Days
/ DAYSPERNORMALQUADRENNIUM
;
391 TimeFields
->Year
+= DeltaYear
* 4;
392 Days
-= DeltaYear
* DAYSPERNORMALQUADRENNIUM
;
393 DeltaYear
= Days
/ DAYSPERNORMALYEAR
;
394 TimeFields
->Year
+= DeltaYear
;
395 Days
-= DeltaYear
* DAYSPERNORMALYEAR
;
397 LeapYear
= IsLeapYear(TimeFields
->Year
);
399 /* Compute month of year */
400 Months
= MonthLengths
[LeapYear
];
401 for (CurMonth
= 0; Days
>= (long) Months
[CurMonth
]; CurMonth
++)
402 Days
= Days
- (long) Months
[CurMonth
];
403 TimeFields
->Month
= (CSHORT
) (CurMonth
+ 1);
404 TimeFields
->Day
= (CSHORT
) (Days
+ 1);
407 /******************************************************************************
408 * RtlTimeFieldsToTime [NTDLL.@]
410 * Convert a TIME_FIELDS structure into a time.
413 * ftTimeFields [I] TIME_FIELDS structure to convert.
414 * Time [O] Destination for the converted time.
420 BOOLEAN WINAPI
RtlTimeFieldsToTime(
421 PTIME_FIELDS tfTimeFields
,
424 int CurYear
, CurMonth
;
426 TIME_FIELDS TimeFields
= *tfTimeFields
;
430 /* FIXME: normalize the TIME_FIELDS structure here */
431 while (TimeFields
.Second
>= SECSPERMIN
)
432 { NormalizeTimeFields(&TimeFields
.Second
, &TimeFields
.Minute
, SECSPERMIN
);
434 while (TimeFields
.Minute
>= MINSPERHOUR
)
435 { NormalizeTimeFields(&TimeFields
.Minute
, &TimeFields
.Hour
, MINSPERHOUR
);
437 while (TimeFields
.Hour
>= HOURSPERDAY
)
438 { NormalizeTimeFields(&TimeFields
.Hour
, &TimeFields
.Day
, HOURSPERDAY
);
440 while (TimeFields
.Day
> MonthLengths
[IsLeapYear(TimeFields
.Year
)][TimeFields
.Month
- 1])
441 { NormalizeTimeFields(&TimeFields
.Day
, &TimeFields
.Month
, SECSPERMIN
);
443 while (TimeFields
.Month
> MONSPERYEAR
)
444 { NormalizeTimeFields(&TimeFields
.Month
, &TimeFields
.Year
, MONSPERYEAR
);
447 /* FIXME: handle calendar corrections here */
448 for (CurYear
= EPOCHYEAR
; CurYear
< TimeFields
.Year
; CurYear
++)
449 { rcTime
+= YearLengths
[IsLeapYear(CurYear
)];
451 for (CurMonth
= 1; CurMonth
< TimeFields
.Month
; CurMonth
++)
452 { rcTime
+= MonthLengths
[IsLeapYear(CurYear
)][CurMonth
- 1];
454 rcTime
+= TimeFields
.Day
- 1;
455 rcTime
*= SECSPERDAY
;
456 rcTime
+= TimeFields
.Hour
* SECSPERHOUR
+ TimeFields
.Minute
* SECSPERMIN
+ TimeFields
.Second
;
457 rcTime
*= TICKSPERSEC
;
458 rcTime
+= TimeFields
.Milliseconds
* TICKSPERMSEC
;
459 Time
->QuadPart
= rcTime
;
463 /************ end of code by Rex Jolliff (rex@lvcablemodem.com) ***************/
465 /******************************************************************************
466 * RtlLocalTimeToSystemTime [NTDLL.@]
468 * Convert a local time into system time.
471 * LocalTime [I] Local time to convert.
472 * SystemTime [O] Destination for the converted time.
475 * Success: STATUS_SUCCESS.
476 * Failure: An NTSTATUS error code indicating the problem.
478 NTSTATUS WINAPI
RtlLocalTimeToSystemTime( const LARGE_INTEGER
*LocalTime
,
479 PLARGE_INTEGER SystemTime
)
481 TIME_ZONE_INFORMATION tzinfo
;
483 TRACE("(%p, %p)\n", LocalTime
, SystemTime
);
485 RtlQueryTimeZoneInformation(&tzinfo
);
486 SystemTime
->QuadPart
= LocalTime
->QuadPart
+ tzinfo
.Bias
* 60 * (LONGLONG
)10000000;
487 return STATUS_SUCCESS
;
490 /******************************************************************************
491 * RtlSystemTimeToLocalTime [NTDLL.@]
493 * Convert a system time into a local time.
496 * SystemTime [I] System time to convert.
497 * LocalTime [O] Destination for the converted time.
500 * Success: STATUS_SUCCESS.
501 * Failure: An NTSTATUS error code indicating the problem.
503 NTSTATUS WINAPI
RtlSystemTimeToLocalTime( const LARGE_INTEGER
*SystemTime
,
504 PLARGE_INTEGER LocalTime
)
506 TIME_ZONE_INFORMATION tzinfo
;
508 TRACE("(%p, %p)\n", SystemTime
, LocalTime
);
510 RtlQueryTimeZoneInformation(&tzinfo
);
511 LocalTime
->QuadPart
= SystemTime
->QuadPart
- tzinfo
.Bias
* 60 * (LONGLONG
)10000000;
512 return STATUS_SUCCESS
;
515 /******************************************************************************
516 * RtlTimeToSecondsSince1970 [NTDLL.@]
518 * Convert a time into a count of seconds since 1970.
521 * Time [I] Time to convert.
522 * Seconds [O] Destination for the converted time.
526 * Failure: FALSE, if the resulting value will not fit in a DWORD.
528 BOOLEAN WINAPI
RtlTimeToSecondsSince1970( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
530 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
531 tmp
= RtlLargeIntegerDivide( tmp
, 10000000, NULL
);
532 tmp
-= SECS_1601_TO_1970
;
533 if (tmp
> 0xffffffff) return FALSE
;
534 *Seconds
= (DWORD
)tmp
;
538 /******************************************************************************
539 * RtlTimeToSecondsSince1980 [NTDLL.@]
541 * Convert a time into a count of seconds since 1980.
544 * Time [I] Time to convert.
545 * Seconds [O] Destination for the converted time.
549 * Failure: FALSE, if the resulting value will not fit in a DWORD.
551 BOOLEAN WINAPI
RtlTimeToSecondsSince1980( const LARGE_INTEGER
*Time
, LPDWORD Seconds
)
553 ULONGLONG tmp
= ((ULONGLONG
)Time
->u
.HighPart
<< 32) | Time
->u
.LowPart
;
554 tmp
= RtlLargeIntegerDivide( tmp
, 10000000, NULL
);
555 tmp
-= SECS_1601_TO_1980
;
556 if (tmp
> 0xffffffff) return FALSE
;
557 *Seconds
= (DWORD
)tmp
;
561 /******************************************************************************
562 * RtlSecondsSince1970ToTime [NTDLL.@]
564 * Convert a count of seconds since 1970 to a time.
567 * Seconds [I] Time to convert.
568 * Time [O] Destination for the converted time.
573 void WINAPI
RtlSecondsSince1970ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
575 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
576 Time
->u
.LowPart
= (DWORD
)secs
;
577 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
580 /******************************************************************************
581 * RtlSecondsSince1980ToTime [NTDLL.@]
583 * Convert a count of seconds since 1980 to a time.
586 * Seconds [I] Time to convert.
587 * Time [O] Destination for the converted time.
592 void WINAPI
RtlSecondsSince1980ToTime( DWORD Seconds
, LARGE_INTEGER
*Time
)
594 ULONGLONG secs
= Seconds
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1980
;
595 Time
->u
.LowPart
= (DWORD
)secs
;
596 Time
->u
.HighPart
= (DWORD
)(secs
>> 32);
599 /******************************************************************************
600 * RtlTimeToElapsedTimeFields [NTDLL.@]
602 * Convert a time to a count of elapsed seconds.
605 * Time [I] Time to convert.
606 * TimeFields [O] Destination for the converted time.
611 void WINAPI
RtlTimeToElapsedTimeFields( const LARGE_INTEGER
*Time
, PTIME_FIELDS TimeFields
)
616 time
= RtlExtendedLargeIntegerDivide( Time
->QuadPart
, TICKSPERSEC
, &rem
);
617 TimeFields
->Milliseconds
= rem
/ TICKSPERMSEC
;
619 /* time is now in seconds */
620 TimeFields
->Year
= 0;
621 TimeFields
->Month
= 0;
622 TimeFields
->Day
= RtlExtendedLargeIntegerDivide( time
, SECSPERDAY
, &rem
);
624 /* rem is now the remaining seconds in the last day */
625 TimeFields
->Second
= rem
% 60;
627 TimeFields
->Minute
= rem
% 60;
628 TimeFields
->Hour
= rem
/ 60;
631 /***********************************************************************
632 * NtQuerySystemTime [NTDLL.@]
633 * ZwQuerySystemTime [NTDLL.@]
635 * Get the current system time.
638 * Time [O] Destination for the current system time.
641 * Success: STATUS_SUCCESS.
642 * Failure: An NTSTATUS error code indicating the problem.
644 NTSTATUS WINAPI
NtQuerySystemTime( PLARGE_INTEGER Time
)
648 gettimeofday( &now
, 0 );
649 Time
->QuadPart
= now
.tv_sec
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
650 Time
->QuadPart
+= now
.tv_usec
* 10;
651 return STATUS_SUCCESS
;
654 /***********************************************************************
655 * TIME_GetBias [internal]
657 * Helper function calculates delta local time from UTC.
660 * utc [I] The current utc time.
661 * pdaylight [I] Local daylight.
664 * The bias for the current timezone.
666 static int TIME_GetBias(time_t utc
, int *pdaylight
)
668 struct tm
*ptm
= localtime(&utc
);
669 *pdaylight
= ptm
->tm_isdst
; /* daylight for local timezone */
671 ptm
->tm_isdst
= *pdaylight
; /* use local daylight, not that of Greenwich */
672 return (int)(utc
-mktime(ptm
));
675 /***********************************************************************
676 * TIME_GetTZAsStr [internal]
678 * Helper function that returns the given timezone as a string.
681 * utc [I] The current utc time.
682 * bias [I] The bias of the current timezone.
689 * This could be done with a hash table instead of merely iterating through a
690 * table, however with the small amount of entries (60 or so) I didn't think
693 static const WCHAR
* TIME_GetTZAsStr (time_t utc
, int bias
, int dst
)
696 struct tm
*ptm
= localtime(&utc
);
699 if (!strftime (psTZName
, 7, "%Z", ptm
))
702 for (i
=0; i
<(sizeof(TZ_INFO
) / sizeof(struct tagTZ_INFO
)); i
++)
704 if ( strcmp(TZ_INFO
[i
].psTZFromUnix
, psTZName
) == 0 &&
705 TZ_INFO
[i
].bias
== bias
&&
706 TZ_INFO
[i
].dst
== dst
708 return TZ_INFO
[i
].psTZWindows
;
714 /***********************************************************************
715 * RtlQueryTimeZoneInformation [NTDLL.@]
717 * Get information about the current timezone.
720 * tzinfo [O] Destination for the retrieved timezone info.
723 * Success: STATUS_SUCCESS.
724 * Failure: An NTSTATUS error code indicating the problem.
726 NTSTATUS WINAPI
RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo
)
732 memset(tzinfo
, 0, sizeof(TIME_ZONE_INFORMATION
));
735 bias
= TIME_GetBias(gmt
, &daylight
);
737 tzinfo
->Bias
= -bias
/ 60;
738 tzinfo
->StandardBias
= 0;
739 tzinfo
->DaylightBias
= -60;
740 psTZ
= TIME_GetTZAsStr (gmt
, (-bias
/60), daylight
);
741 if (psTZ
) strcpyW( tzinfo
->StandardName
, psTZ
);
742 return STATUS_SUCCESS
;
745 /***********************************************************************
746 * RtlSetTimeZoneInformation [NTDLL.@]
748 * Set the current time zone information.
751 * tzinfo [I] Timezone information to set.
754 * Success: STATUS_SUCCESS.
755 * Failure: An NTSTATUS error code indicating the problem.
758 * Uses the obsolete unix timezone structure and tz_dsttime member.
760 NTSTATUS WINAPI
RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION
*tzinfo
)
764 tz
.tz_minuteswest
= tzinfo
->Bias
;
766 tz
.tz_dsttime
= DST_NONE
;
770 if(!settimeofday(NULL
, &tz
))
771 return STATUS_SUCCESS
;
772 return STATUS_PRIVILEGE_NOT_HELD
;
775 /***********************************************************************
776 * NtSetSystemTime [NTDLL.@]
777 * ZwSetSystemTime [NTDLL.@]
779 * Set the system time.
782 * NewTime [I] The time to set.
783 * OldTime [O] Optional destination for the previous system time.
786 * Success: STATUS_SUCCESS.
787 * Failure: An NTSTATUS error code indicating the problem.
789 NTSTATUS WINAPI
NtSetSystemTime(const LARGE_INTEGER
*NewTime
, LARGE_INTEGER
*OldTime
)
799 /* Return the old time if necessary */
801 NtQuerySystemTime(OldTime
);
803 RtlTimeToTimeFields(NewTime
, &tf
);
805 /* call gettimeofday to get the current timezone */
806 gettimeofday(&tv
, &tz
);
808 /* get delta local time from utc */
809 bias
= TIME_GetBias(oldsec
, &dst
);
811 /* get the number of seconds */
812 t
.tm_sec
= tf
.Second
;
813 t
.tm_min
= tf
.Minute
;
816 t
.tm_mon
= tf
.Month
- 1;
817 t
.tm_year
= tf
.Year
- 1900;
820 /* correct for timezone and daylight */
823 /* set the new time */
825 tv
.tv_usec
= tf
.Milliseconds
* 1000;
827 /* error and sanity check*/
828 if(sec
== (time_t)-1 || abs((int)(sec
-oldsec
)) > SETTIME_MAX_ADJUST
) {
831 #ifdef HAVE_SETTIMEOFDAY
832 err
= settimeofday(&tv
, NULL
); /* 0 is OK, -1 is error */
834 return STATUS_SUCCESS
;
840 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
841 tf
.Year
, tf
.Month
, tf
.Day
, tf
.Hour
, tf
.Minute
, tf
.Second
,
843 err
== -1 ? "No Permission"
844 : sec
== (time_t)-1 ? "" : "is too large." );
847 return STATUS_INVALID_PARAMETER
;
849 return STATUS_PRIVILEGE_NOT_HELD
;
851 return STATUS_NOT_IMPLEMENTED
;