source: trunk/src/NTDLL/time.c@ 10367

Last change on this file since 10367 was 9986, checked in by sandervl, 22 years ago

PF: NTDLL update for GCC 3.2.1 + resync with Wine

File size: 24.6 KB
Line 
1/*
2 * Nt time functions.
3 *
4 * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and
5 * adapted to wine with special permissions of the author
6 * Rex Jolliff (rex@lvcablemodem.com)
7 *
8 * Copyright 1999 Juergen Schmied
9 *
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.
14 *
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.
19 *
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
23 */
24#define WINE_LARGE_INTEGER 1
25#include "config.h"
26#include "wine/port.h"
27
28#include <stdlib.h>
29#include <string.h>
30#include <time.h>
31#include <sys/time.h>
32#include <unistd.h>
33
34#define NONAMELESSUNION
35#define NONAMELESSSTRUCT
36#include "winternl.h"
37#include "wine/unicode.h"
38#include "wine/debug.h"
39WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40
41#define SETTIME_MAX_ADJUST 120
42
43/* This structure is used to store strings that represent all of the time zones
44 * in the world. (This is used to help GetTimeZoneInformation)
45 */
46struct tagTZ_INFO
47{
48 const char *psTZFromUnix;
49 WCHAR psTZWindows[32];
50 int bias;
51 int dst;
52};
53
54static const struct tagTZ_INFO TZ_INFO[] =
55{
56 {"MHT",
57 {'D','a','t','e','l','i','n','e',' ','S','t','a','n','d','a','r','d',' ',
58 'T','i','m','e','\0'}, -720, 0},
59 {"SST",
60 {'S','a','m','o','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
61 'e','\0'}, 660, 0},
62 {"HST",
63 {'H','a','w','a','i','i','a','n',' ','S','t','a','n','d','a','r','d',' ',
64 'T','i','m','e','\0'}, 600, 0},
65 {"AKDT",
66 {'A','l','a','s','k','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
67 'i','m','e','\0'}, 480, 1},
68 {"PDT",
69 {'P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r','d',' ','T',
70 'i','m','e','\0'}, 420, 1},
71 {"MST",
72 {'U','S',' ','M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a',
73 'r','d',' ','T','i','m','e','\0'}, 420, 0},
74 {"MDT",
75 {'M','o','u','n','t','a','i','n',' ','S','t','a','n','d','a','r','d',' ',
76 'T','i','m','e','\0'}, 360, 1},
77 {"CST",
78 {'C','e','n','t','r','a','l',' ','A','m','e','r','i','c','a',' ','S','t',
79 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, 360, 0},
80 {"CDT",
81 {'C','e','n','t','r','a','l',' ','S','t','a','n','d','a','r','d',' ','T',
82 'i','m','e','\0'}, 300, 1},
83 {"COT",
84 {'S','A',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d','a','r',
85 'd',' ','T','i','m','e','\0'}, 300, 0},
86 {"EDT",
87 {'E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r','d',' ','T',
88 'i','m','e','\0'}, 240, 1},
89 {"EST",
90 {'U','S',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
91 'd',' ','T','i','m','e','\0'}, 300, 0},
92 {"ADT",
93 {'A','t','l','a','n','t','i','c',' ','S','t','a','n','d','a','r','d',' ',
94 'T','i','m','e','\0'}, 180, 1},
95 {"VET",
96 {'S','A',' ','W','e','s','t','e','r','n',' ','S','t','a','n','d','a','r',
97 'd',' ','T','i','m','e','\0'}, 240, 0},
98 {"CLT",
99 {'P','a','c','i','f','i','c',' ','S','A',' ','S','t','a','n','d','a','r',
100 'd',' ','T','i','m','e','\0'}, 240, 0},
101 {"NDT",
102 {'N','e','w','f','o','u','n','d','l','a','n','d',' ','S','t','a','n','d',
103 'a','r','d',' ','T','i','m','e','\0'}, 150, 1},
104 {"BRT",
105 {'E','.',' ','S','o','u','t','h',' ','A','m','e','r','i','c','a',' ','S',
106 't','a','n','d','a','r','d',' ','T','i','m','e','\0'}, 180, 0},
107 {"ART",
108 {'S','A',' ','E','a','s','t','e','r','n',' ','S','t','a','n','d','a','r',
109 'd',' ','T','i','m','e','\0'}, 180, 0},
110 {"WGST",
111 {'G','r','e','e','n','l','a','n','d',' ','S','t','a','n','d','a','r','d',
112 ' ','T','i','m','e','\0'}, 120, 1},
113 {"GST",
114 {'M','i','d','-','A','t','l','a','n','t','i','c',' ','S','t','a','n','d',
115 'a','r','d',' ','T','i','m','e','\0'}, 120, 0},
116 {"AZOST",
117 {'A','z','o','r','e','s',' ','S','t','a','n','d','a','r','d',' ','T','i',
118 'm','e','\0'}, 0, 1},
119 {"CVT",
120 {'C','a','p','e',' ','V','e','r','d','e',' ','S','t','a','n','d','a','r',
121 'd',' ','T','i','m','e','\0'}, 60, 0},
122 {"WET",
123 {'G','r','e','e','n','w','i','c','h',' ','S','t','a','n','d','a','r','d',
124 ' ','T','i','m','e','\0'}, 0, 0},
125 {"BST",
126 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
127 -60, 1},
128 {"GMT",
129 {'G','M','T',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'},
130 0, 0},
131 {"CEST",
132 {'C','e','n','t','r','a','l',' ','E','u','r','o','p','e',' ','S','t','a',
133 'n','d','a','r','d',' ','T','i','m','e','\0'}, -120, 1},
134 {"WAT",
135 {'W','.',' ','C','e','n','t','r','a','l',' ','A','f','r','i','c','a',' ',
136 'S','t','a','n','d','a','r','d',' ','T','i','m','e','\0'}, -60, 0},
137 {"EEST",
138 {'E','.',' ','E','u','r','o','p','e',' ','S','t','a','n','d','a','r','d',
139 ' ','T','i','m','e','\0'}, -180, 1},
140 {"EET",
141 {'E','g','y','p','t',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
142 'e','\0'}, -120, 0},
143 {"CAT",
144 {'S','o','u','t','h',' ','A','f','r','i','c','a',' ','S','t','a','n','d',
145 'a','r','d',' ','T','i','m','e','\0'}, -120, 0},
146 {"IST",
147 {'I','s','r','a','e','l',' ','S','t','a','n','d','a','r','d',' ','T','i',
148 'm','e','\0'}, -120, 0},
149 {"ADT",
150 {'A','r','a','b','i','c',' ','S','t','a','n','d','a','r','d',' ','T','i',
151 'm','e','\0'}, -240, 1},
152 {"AST",
153 {'A','r','a','b',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
154 '\0'}, -180, 0},
155 {"MSD",
156 {'R','u','s','s','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
157 'i','m','e','\0'}, -240, 1},
158 {"EAT",
159 {'E','.',' ','A','f','r','i','c','a',' ','S','t','a','n','d','a','r','d',
160 ' ','T','i','m','e','\0'}, -180, 0},
161 {"IRST",
162 {'I','r','a','n',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
163 '\0'}, -270, 1},
164 {"GST",
165 {'A','r','a','b','i','a','n',' ','S','t','a','n','d','a','r','d',' ','T',
166 'i','m','e','\0'}, -240, 0},
167 {"AZST",
168 {'C','a','u','c','a','s','u','s',' ','S','t','a','n','d','a','r','d',' ',
169 'T','i','m','e','\0'}, -300, 1},
170 {"AFT",
171 {'A','f','g','h','a','n','i','s','t','a','n',' ','S','t','a','n','d','a',
172 'r','d',' ','T','i','m','e','\0'}, -270, 0},
173 {"YEKST",
174 {'E','k','a','t','e','r','i','n','b','u','r','g',' ','S','t','a','n','d',
175 'a','r','d',' ','T','i','m','e','\0'}, -360, 1},
176 {"PKT",
177 {'W','e','s','t',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',
178 ' ','T','i','m','e','\0'}, -300, 0},
179 {"IST",
180 {'I','n','d','i','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
181 'e','\0'}, -330, 0},
182 {"NPT",
183 {'N','e','p','a','l',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
184 'e','\0'}, -345, 0},
185 {"ALMST",
186 {'N','.',' ','C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t',
187 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -420, 1},
188 {"BDT",
189 {'C','e','n','t','r','a','l',' ','A','s','i','a',' ','S','t','a','n','d',
190 'a','r','d',' ','T','i','m','e','\0'}, -360, 0},
191 {"LKT",
192 {'S','r','i',' ','L','a','n','k','a',' ','S','t','a','n','d','a','r','d',
193 ' ','T','i','m','e','\0'}, -360, 0},
194 {"MMT",
195 {'M','y','a','n','m','a','r',' ','S','t','a','n','d','a','r','d',' ','T',
196 'i','m','e','\0'}, -390, 0},
197 {"ICT",
198 {'S','E',' ','A','s','i','a',' ','S','t','a','n','d','a','r','d',' ','T',
199 'i','m','e','\0'}, -420, 0},
200 {"KRAST",
201 {'N','o','r','t','h',' ','A','s','i','a',' ','S','t','a','n','d','a','r',
202 'd',' ','T','i','m','e','\0'}, -480, 1},
203 {"CST",
204 {'C','h','i','n','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
205 'e','\0'}, -480, 0},
206 {"IRKST",
207 {'N','o','r','t','h',' ','A','s','i','a',' ','E','a','s','t',' ','S','t',
208 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -540, 1},
209 {"SGT",
210 {'M','a','l','a','y',' ','P','e','n','i','n','s','u','l','a',' ','S','t',
211 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -480, 0},
212 {"WST",
213 {'W','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
214 'a','r','d',' ','T','i','m','e','\0'}, -480, 0},
215 {"JST",
216 {'T','o','k','y','o',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
217 'e','\0'}, -540, 0},
218 {"KST",
219 {'K','o','r','e','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
220 'e','\0'}, -540, 0},
221 {"YAKST",
222 {'Y','a','k','u','t','s','k',' ','S','t','a','n','d','a','r','d',' ','T',
223 'i','m','e','\0'}, -600, 1},
224 {"CST",
225 {'C','e','n','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a',
226 'n','d','a','r','d',' ','T','i','m','e','\0'}, -570, 0},
227 {"EST",
228 {'E','.',' ','A','u','s','t','r','a','l','i','a',' ','S','t','a','n','d',
229 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
230 {"GST",
231 {'W','e','s','t',' ','P','a','c','i','f','i','c',' ','S','t','a','n','d',
232 'a','r','d',' ','T','i','m','e','\0'}, -600, 0},
233 {"VLAST",
234 {'V','l','a','d','i','v','o','s','t','o','k',' ','S','t','a','n','d','a',
235 'r','d',' ','T','i','m','e','\0'}, -660, 1},
236 {"MAGST",
237 {'C','e','n','t','r','a','l',' ','P','a','c','i','f','i','c',' ','S','t',
238 'a','n','d','a','r','d',' ','T','i','m','e','\0'}, -720, 1},
239 {"NZST",
240 {'N','e','w',' ','Z','e','a','l','a','n','d',' ','S','t','a','n','d','a',
241 'r','d',' ','T','i','m','e','\0'}, -720, 0},
242 {"FJT",
243 {'F','i','j','i',' ','S','t','a','n','d','a','r','d',' ','T','i','m','e',
244 '\0'}, -720, 0},
245 {"TOT",
246 {'T','o','n','g','a',' ','S','t','a','n','d','a','r','d',' ','T','i','m',
247 'e','\0'}, -780, 0}
248};
249
250/*********** start of code by Rex Jolliff (rex@lvcablemodem.com) **************/
251
252#define TICKSPERSEC 10000000
253#define TICKSPERMSEC 10000
254#define SECSPERDAY 86400
255#define SECSPERHOUR 3600
256#define SECSPERMIN 60
257#define MINSPERHOUR 60
258#define HOURSPERDAY 24
259#define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
260#define DAYSPERWEEK 7
261#define EPOCHYEAR 1601
262#define DAYSPERNORMALYEAR 365
263#define DAYSPERLEAPYEAR 366
264#define MONSPERYEAR 12
265
266/* 1601 to 1970 is 369 years plus 89 leap days */
267#define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)86400)
268/* 1601 to 1980 is 379 years plus 91 leap days */
269#define SECS_1601_to_1980 ((379 * 365 + 91) * (ULONGLONG)86400)
270
271
272static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
273static const int MonthLengths[2][MONSPERYEAR] =
274{
275 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
276 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
277};
278
279static inline int IsLeapYear(int Year)
280{
281 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
282}
283
284static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
285{
286 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
287 *CarryField = (CSHORT) (*CarryField + 1);
288}
289
290/******************************************************************************
291 * RtlTimeToTimeFields [NTDLL.@]
292 *
293 * Parses Time into a TimeFields structure.
294 *
295 * PARAMS:
296 * liTime [I]: Time to convert to timefields.
297 * TimeFields [O]: Pointer to TIME_FIELDS structure to hold parsed info.
298 *
299 * RETURNS:
300 * Nothing.
301 */
302VOID WINAPI RtlTimeToTimeFields(
303 const LARGE_INTEGER *liTime,
304 PTIME_FIELDS TimeFields)
305{
306 const int *Months;
307 int SecondsInDay, CurYear;
308 int LeapYear, CurMonth;
309 long int Days;
310 LONGLONG Time = liTime->QuadPart;
311
312 /* Extract millisecond from time and convert time into seconds */
313 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
314 Time = Time / TICKSPERSEC;
315
316 /* The native version of RtlTimeToTimeFields does not take leap seconds
317 * into account */
318
319 /* Split the time into days and seconds within the day */
320 Days = Time / SECSPERDAY;
321 SecondsInDay = Time % SECSPERDAY;
322
323 /* compute time of day */
324 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
325 SecondsInDay = SecondsInDay % SECSPERHOUR;
326 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
327 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
328
329 /* compute day of week */
330 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
331
332 /* compute year */
333 CurYear = EPOCHYEAR;
334 /* FIXME: handle calendar modifications */
335 while (1)
336 { LeapYear = IsLeapYear(CurYear);
337 if (Days < (long) YearLengths[LeapYear])
338 { break;
339 }
340 CurYear++;
341 Days = Days - (long) YearLengths[LeapYear];
342 }
343 TimeFields->Year = (CSHORT) CurYear;
344
345 /* Compute month of year */
346 Months = MonthLengths[LeapYear];
347 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
348 Days = Days - (long) Months[CurMonth];
349 TimeFields->Month = (CSHORT) (CurMonth + 1);
350 TimeFields->Day = (CSHORT) (Days + 1);
351}
352
353/******************************************************************************
354 * RtlTimeFieldsToTime [NTDLL.@]
355 *
356 * Converts a TIME_FIELDS structure to time.
357 *
358 * PARAMS:
359 * ftTimeFields [I]: Time fields structure to convert.
360 * Time [O]: Converted time.
361 *
362 * RETURNS:
363 * TRUE: Successfull
364 * FALSE: Failure.
365 */
366BOOLEAN WINAPI RtlTimeFieldsToTime(
367 PTIME_FIELDS tfTimeFields,
368 PLARGE_INTEGER Time)
369{
370 int CurYear, CurMonth;
371 LONGLONG rcTime;
372 TIME_FIELDS TimeFields = *tfTimeFields;
373
374 rcTime = 0;
375
376 /* FIXME: normalize the TIME_FIELDS structure here */
377 while (TimeFields.Second >= SECSPERMIN)
378 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
379 }
380 while (TimeFields.Minute >= MINSPERHOUR)
381 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
382 }
383 while (TimeFields.Hour >= HOURSPERDAY)
384 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
385 }
386 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
387 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
388 }
389 while (TimeFields.Month > MONSPERYEAR)
390 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
391 }
392
393 /* FIXME: handle calendar corrections here */
394 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
395 { rcTime += YearLengths[IsLeapYear(CurYear)];
396 }
397 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
398 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
399 }
400 rcTime += TimeFields.Day - 1;
401 rcTime *= SECSPERDAY;
402 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
403 rcTime *= TICKSPERSEC;
404 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
405 Time->QuadPart = rcTime;
406
407 return TRUE;
408}
409/************ end of code by Rex Jolliff (rex@lvcablemodem.com) ***************/
410
411/******************************************************************************
412 * RtlLocalTimeToSystemTime [NTDLL.@]
413 *
414 * Converts local time to system time.
415 *
416 * PARAMS:
417 * LocalTime [I]: Localtime to convert.
418 * SystemTime [O]: SystemTime of the supplied localtime.
419 *
420 * RETURNS:
421 * Status.
422 */
423NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
424 PLARGE_INTEGER SystemTime)
425{
426 TIME_ZONE_INFORMATION tzinfo;
427
428 TRACE("(%p, %p)\n", LocalTime, SystemTime);
429 RtlQueryTimeZoneInformation(&tzinfo);
430 SystemTime->QuadPart = LocalTime->QuadPart + tzinfo.Bias * 60 * (LONGLONG)10000000;
431 return STATUS_SUCCESS;
432}
433
434/******************************************************************************
435 * RtlSystemTimeToLocalTime [NTDLL.@]
436 *
437 * Converts system Time to local time.
438 *
439 * PARAMS:
440 * SystemTime [I]: System time to convert.
441 * LocalTime [O]: Local time of the supplied system time.
442 *
443 * RETURNS:
444 * Nothing.
445 */
446void WINAPI RtlSystemTimeToLocalTime( PLARGE_INTEGER SystemTime,
447 PLARGE_INTEGER LocalTime )
448{
449 TIME_ZONE_INFORMATION tzinfo;
450
451 TRACE("(%p, %p)\n", SystemTime, LocalTime);
452
453 RtlQueryTimeZoneInformation(&tzinfo);
454 LocalTime->QuadPart = SystemTime->QuadPart - tzinfo.Bias * 60 * (LONGLONG)10000000;
455 return STATUS_SUCCESS;
456}
457
458/******************************************************************************
459 * RtlTimeToSecondsSince1970 [NTDLL.@]
460 *
461 * Converts Time to seconds since 1970.
462 *
463 * PARAMS:
464 * time [I]: Time to convert.
465 * res [O]: Pointer to a LONG to recieve the seconds since 1970.
466 *
467 * RETURNS:
468 * TRUE: Successfull.
469 * FALSE: Failure.
470 */
471BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *time, PULONG res )
472{
473 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
474 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
475 tmp -= SECS_1601_TO_1970;
476 if (tmp > 0xffffffff) return FALSE;
477 *res = (DWORD)tmp;
478 return TRUE;
479}
480
481/******************************************************************************
482 * RtlTimeToSecondsSince1980 [NTDLL.@]
483 *
484 * Converts Time to seconds since 1980.
485 *
486 * PARAMS:
487 * time [I]: Time to convert.
488 * res [O]: Pointer to a integer to recieve the time since 1980.
489 *
490 * RETURNS:
491 * TRUE: Successfull
492 * FALSE: Failure.
493 */
494BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *time, LPDWORD res )
495{
496 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
497 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
498 tmp -= SECS_1601_to_1980;
499 if (tmp > 0xffffffff) return FALSE;
500 *res = (DWORD)tmp;
501 return TRUE;
502}
503
504/******************************************************************************
505 * RtlSecondsSince1970ToTime [NTDLL.@]
506 *
507 * Converts seconds since 1970 to time.
508 *
509 * PARAMS:
510 * time [I]: Seconds since 1970 to convert.
511 * res [O]: Seconds since 1970 in Time.
512 *
513 * RETURNS:
514 * Nothing.
515 */
516void WINAPI RtlSecondsSince1970ToTime( DWORD time, LARGE_INTEGER *res )
517{
518 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
519 res->s.LowPart = (DWORD)secs;
520 res->s.HighPart = (DWORD)(secs >> 32);
521}
522
523/******************************************************************************
524 * RtlSecondsSince1980ToTime [NTDLL.@]
525 *
526 * Converts seconds since 1980 to time.
527 *
528 * PARAMS:
529 * time [I]: Seconds since 1980 to convert.
530 * res [O]: Seconds since 1980 in Time.
531 *
532 * RETURNS:
533 * Nothing.
534 */
535void WINAPI RtlSecondsSince1980ToTime( DWORD time, LARGE_INTEGER *res )
536{
537 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
538 res->s.LowPart = (DWORD)secs;
539 res->s.HighPart = (DWORD)(secs >> 32);
540}
541
542/******************************************************************************
543 * RtlTimeToElapsedTimeFields [NTDLL.@]
544 *
545 * RETURNS:
546 * Nothing.
547 */
548void WINAPI RtlTimeToElapsedTimeFields( LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
549{
550 LONGLONG time;
551 UINT rem;
552
553 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
554 TimeFields->Milliseconds = rem / TICKSPERMSEC;
555
556 /* time is now in seconds */
557 TimeFields->Year = 0;
558 TimeFields->Month = 0;
559 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
560
561 /* rem is now the remaining seconds in the last day */
562 TimeFields->Second = rem % 60;
563 rem /= 60;
564 TimeFields->Minute = rem % 60;
565 TimeFields->Hour = rem / 60;
566}
567
568/***********************************************************************
569 * NtQuerySystemTime [NTDLL.@]
570 * ZwQuerySystemTime [NTDLL.@]
571 *
572 * Gets the current system time.
573 *
574 * PARAMS:
575 * time [O]: The current system time.
576 *
577 * RETURNS:
578 * Status.
579 */
580NTSTATUS WINAPI NtQuerySystemTime( LARGE_INTEGER *time )
581{
582 struct timeval now;
583
584 gettimeofday( &now, 0 );
585 time->QuadPart = RtlExtendedIntegerMultiply( now.tv_sec+SECS_1601_TO_1970, 10000000 ) + now.tv_usec * 10;
586 return STATUS_SUCCESS;
587}
588
589/***********************************************************************
590 * TIME_GetBias [internal]
591 *
592 * Helper function calculates delta local time from UTC.
593 *
594 * PARAMS:
595 * utc [I]: The current utc time.
596 * pdaylight [I]: Local daylight.
597 *
598 * RETURNS:
599 * The bias for the current timezone.
600 */
601static int TIME_GetBias(time_t utc, int *pdaylight)
602{
603 struct tm *ptm = localtime(&utc);
604 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
605 ptm = gmtime(&utc);
606 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
607 return (int)(utc-mktime(ptm));
608}
609
610/***********************************************************************
611 * TIME_GetTZAsStr [internal]
612 *
613 * Helper function that returns the given timezone as a string.
614 *
615 * PARAMS:
616 * utc [I]: The current utc time.
617 * bias [I]: The bias of the current timezone.
618 * dst [I]: ??
619 *
620 * RETURNS:
621 * Timezone name.
622 *
623 * NOTES:
624 * This could be done with a hash table instead of merely iterating through a
625 * table, however with the small amount of entries (60 or so) I didn't think
626 * it was worth it.
627 */
628static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
629{
630 char psTZName[7];
631 struct tm *ptm = localtime(&utc);
632 int i;
633
634 if (!strftime (psTZName, 7, "%Z", ptm))
635 return (NULL);
636
637 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
638 {
639 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
640 TZ_INFO[i].bias == bias &&
641 TZ_INFO[i].dst == dst
642 )
643 return TZ_INFO[i].psTZWindows;
644 }
645
646 return (NULL);
647}
648
649/***********************************************************************
650 * RtlQueryTimeZoneInformation [NTDLL.@]
651 *
652 * Returns the timezone.
653 *
654 * PARAMS:
655 * tzinfo [O]: Retrieves the timezone info.
656 *
657 * RETURNS:
658 * Status.
659 */
660NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
661{
662 time_t gmt;
663 int bias, daylight;
664 const WCHAR *psTZ;
665
666 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
667
668 gmt = time(NULL);
669 bias = TIME_GetBias(gmt, &daylight);
670
671 tzinfo->Bias = -bias / 60;
672 tzinfo->StandardBias = 0;
673 tzinfo->DaylightBias = -60;
674 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
675 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
676 return STATUS_SUCCESS;
677}
678
679/***********************************************************************
680 * RtlSetTimeZoneInformation [NTDLL.@]
681 *
682 * Sets the current time zone.
683 *
684 * PARAMS:
685 * tzinfo [I]: Timezone information used to set timezone.
686 *
687 * RETURNS:
688 * Status.
689 *
690 * BUGS:
691 * Uses the obsolete unix timezone structure and tz_dsttime member.
692 */
693NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
694{
695 struct timezone tz;
696
697 tz.tz_minuteswest = tzinfo->Bias;
698#ifdef DST_NONE
699 tz.tz_dsttime = DST_NONE;
700#else
701 tz.tz_dsttime = 0;
702#endif
703#if 0
704 if(!settimeofday(NULL, &tz))
705 return STATUS_SUCCESS;
706#endif
707 return STATUS_PRIVILEGE_NOT_HELD;
708}
709
710/***********************************************************************
711 * NtSetSystemTime [NTDLL.@]
712 * ZwSetSystemTime [NTDLL.@]
713 *
714 * Sets the system time.
715 *
716 * PARAM:
717 * NewTime [I]: The time to set the system time to.
718 * OldTime [O]: Optional (ie. can be NULL). Old Time.
719 *
720 * RETURNS:
721 * Status.
722 */
723NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
724{
725 TIME_FIELDS tf;
726 struct timeval tv;
727 struct timezone tz;
728 struct tm t;
729 time_t sec, oldsec;
730 int dst, bias;
731 int err;
732
733 /* Return the old time if necessary */
734 if(OldTime)
735 NtQuerySystemTime(OldTime);
736
737 RtlTimeToTimeFields(NewTime, &tf);
738
739 /* call gettimeofday to get the current timezone */
740 gettimeofday(&tv, &tz);
741 oldsec = tv.tv_sec;
742 /* get delta local time from utc */
743 bias = TIME_GetBias(oldsec, &dst);
744
745 /* get the number of seconds */
746 t.tm_sec = tf.Second;
747 t.tm_min = tf.Minute;
748 t.tm_hour = tf.Hour;
749 t.tm_mday = tf.Day;
750 t.tm_mon = tf.Month - 1;
751 t.tm_year = tf.Year - 1900;
752 t.tm_isdst = dst;
753 sec = mktime (&t);
754 /* correct for timezone and daylight */
755 sec += bias;
756
757 /* set the new time */
758 tv.tv_sec = sec;
759 tv.tv_usec = tf.Milliseconds * 1000;
760
761 /* error and sanity check*/
762 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
763 err = 2;
764 } else {
765#ifdef HAVE_SETTIMEOFDAY
766 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
767 if(err == 0)
768 return STATUS_SUCCESS;
769#else
770 err = 1;
771#endif
772 }
773
774 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
775 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
776 sec-oldsec, err == -1 ? "No Permission" :
777 sec == (time_t)-1 ? "" : "is too large." );
778
779 if(err == 2)
780 return STATUS_INVALID_PARAMETER;
781 else if(err == -1)
782 return STATUS_PRIVILEGE_NOT_HELD;
783 else
784 return STATUS_NOT_IMPLEMENTED;
785}
Note: See TracBrowser for help on using the repository browser.