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

Last change on this file since 9684 was 9684, checked in by sandervl, 23 years ago

PF: Changes for building dll with GCC

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
430 RtlQueryTimeZoneInformation(&tzinfo);
431 SystemTime->QuadPart = LocalTime->QuadPart + tzinfo.Bias * 60 * (LONGLONG)10000000;
432 return STATUS_SUCCESS;
433}
434
435/******************************************************************************
436 * RtlSystemTimeToLocalTime [NTDLL.@]
437 *
438 * Converts system Time to local time.
439 *
440 * PARAMS:
441 * SystemTime [I]: System time to convert.
442 * LocalTime [O]: Local time of the supplied system time.
443 *
444 * RETURNS:
445 * Nothing.
446 */
447void WINAPI RtlSystemTimeToLocalTime( PLARGE_INTEGER SystemTime,
448 PLARGE_INTEGER LocalTime )
449{
450 TIME_ZONE_INFORMATION tzinfo;
451
452 TRACE("(%p, %p)\n", SystemTime, LocalTime);
453
454 RtlQueryTimeZoneInformation(&tzinfo);
455 LocalTime->QuadPart = SystemTime->QuadPart - tzinfo.Bias * 60 * (LONGLONG)10000000;
456 return STATUS_SUCCESS;
457}
458
459/******************************************************************************
460 * RtlTimeToSecondsSince1970 [NTDLL.@]
461 *
462 * Converts Time to seconds since 1970.
463 *
464 * PARAMS:
465 * time [I]: Time to convert.
466 * res [O]: Pointer to a LONG to recieve the seconds since 1970.
467 *
468 * RETURNS:
469 * TRUE: Successfull.
470 * FALSE: Failure.
471 */
472BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *time, PULONG res )
473{
474 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
475 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
476 tmp -= SECS_1601_TO_1970;
477 if (tmp > 0xffffffff) return FALSE;
478 *res = (DWORD)tmp;
479 return TRUE;
480}
481
482/******************************************************************************
483 * RtlTimeToSecondsSince1980 [NTDLL.@]
484 *
485 * Converts Time to seconds since 1980.
486 *
487 * PARAMS:
488 * time [I]: Time to convert.
489 * res [O]: Pointer to a integer to recieve the time since 1980.
490 *
491 * RETURNS:
492 * TRUE: Successfull
493 * FALSE: Failure.
494 */
495BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *time, LPDWORD res )
496{
497 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
498 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
499 tmp -= SECS_1601_to_1980;
500 if (tmp > 0xffffffff) return FALSE;
501 *res = (DWORD)tmp;
502 return TRUE;
503}
504
505/******************************************************************************
506 * RtlSecondsSince1970ToTime [NTDLL.@]
507 *
508 * Converts seconds since 1970 to time.
509 *
510 * PARAMS:
511 * time [I]: Seconds since 1970 to convert.
512 * res [O]: Seconds since 1970 in Time.
513 *
514 * RETURNS:
515 * Nothing.
516 */
517void WINAPI RtlSecondsSince1970ToTime( DWORD time, LARGE_INTEGER *res )
518{
519 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
520 res->s.LowPart = (DWORD)secs;
521 res->s.HighPart = (DWORD)(secs >> 32);
522}
523
524/******************************************************************************
525 * RtlSecondsSince1980ToTime [NTDLL.@]
526 *
527 * Converts seconds since 1980 to time.
528 *
529 * PARAMS:
530 * time [I]: Seconds since 1980 to convert.
531 * res [O]: Seconds since 1980 in Time.
532 *
533 * RETURNS:
534 * Nothing.
535 */
536void WINAPI RtlSecondsSince1980ToTime( DWORD time, LARGE_INTEGER *res )
537{
538 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
539 res->s.LowPart = (DWORD)secs;
540 res->s.HighPart = (DWORD)(secs >> 32);
541}
542
543/******************************************************************************
544 * RtlTimeToElapsedTimeFields [NTDLL.@]
545 *
546 * RETURNS:
547 * Nothing.
548 */
549void WINAPI RtlTimeToElapsedTimeFields( LARGE_INTEGER *Time, PTIME_FIELDS TimeFields )
550{
551 LONGLONG time;
552 UINT rem;
553
554 time = RtlExtendedLargeIntegerDivide( Time->QuadPart, TICKSPERSEC, &rem );
555 TimeFields->Milliseconds = rem / TICKSPERMSEC;
556
557 /* time is now in seconds */
558 TimeFields->Year = 0;
559 TimeFields->Month = 0;
560 TimeFields->Day = RtlExtendedLargeIntegerDivide( time, SECSPERDAY, &rem );
561
562 /* rem is now the remaining seconds in the last day */
563 TimeFields->Second = rem % 60;
564 rem /= 60;
565 TimeFields->Minute = rem % 60;
566 TimeFields->Hour = rem / 60;
567}
568
569/***********************************************************************
570 * NtQuerySystemTime [NTDLL.@]
571 * ZwQuerySystemTime [NTDLL.@]
572 *
573 * Gets the current system time.
574 *
575 * PARAMS:
576 * time [O]: The current system time.
577 *
578 * RETURNS:
579 * Status.
580 */
581NTSTATUS WINAPI NtQuerySystemTime( LARGE_INTEGER *time )
582{
583 struct timeval now;
584
585 gettimeofday( &now, 0 );
586 time->QuadPart = RtlExtendedIntegerMultiply( now.tv_sec+SECS_1601_TO_1970, 10000000 ) + now.tv_usec * 10;
587 return STATUS_SUCCESS;
588}
589
590/***********************************************************************
591 * TIME_GetBias [internal]
592 *
593 * Helper function calculates delta local time from UTC.
594 *
595 * PARAMS:
596 * utc [I]: The current utc time.
597 * pdaylight [I]: Local daylight.
598 *
599 * RETURNS:
600 * The bias for the current timezone.
601 */
602static int TIME_GetBias(time_t utc, int *pdaylight)
603{
604 struct tm *ptm = localtime(&utc);
605 *pdaylight = ptm->tm_isdst; /* daylight for local timezone */
606 ptm = gmtime(&utc);
607 ptm->tm_isdst = *pdaylight; /* use local daylight, not that of Greenwich */
608 return (int)(utc-mktime(ptm));
609}
610
611/***********************************************************************
612 * TIME_GetTZAsStr [internal]
613 *
614 * Helper function that returns the given timezone as a string.
615 *
616 * PARAMS:
617 * utc [I]: The current utc time.
618 * bias [I]: The bias of the current timezone.
619 * dst [I]: ??
620 *
621 * RETURNS:
622 * Timezone name.
623 *
624 * NOTES:
625 * This could be done with a hash table instead of merely iterating through a
626 * table, however with the small amount of entries (60 or so) I didn't think
627 * it was worth it.
628 */
629static const WCHAR* TIME_GetTZAsStr (time_t utc, int bias, int dst)
630{
631 char psTZName[7];
632 struct tm *ptm = localtime(&utc);
633 int i;
634
635 if (!strftime (psTZName, 7, "%Z", ptm))
636 return (NULL);
637
638 for (i=0; i<(sizeof(TZ_INFO) / sizeof(struct tagTZ_INFO)); i++)
639 {
640 if ( strcmp(TZ_INFO[i].psTZFromUnix, psTZName) == 0 &&
641 TZ_INFO[i].bias == bias &&
642 TZ_INFO[i].dst == dst
643 )
644 return TZ_INFO[i].psTZWindows;
645 }
646
647 return (NULL);
648}
649
650/***********************************************************************
651 * RtlQueryTimeZoneInformation [NTDLL.@]
652 *
653 * Returns the timezone.
654 *
655 * PARAMS:
656 * tzinfo [O]: Retrieves the timezone info.
657 *
658 * RETURNS:
659 * Status.
660 */
661NTSTATUS WINAPI RtlQueryTimeZoneInformation(LPTIME_ZONE_INFORMATION tzinfo)
662{
663 time_t gmt;
664 int bias, daylight;
665 const WCHAR *psTZ;
666
667 memset(tzinfo, 0, sizeof(TIME_ZONE_INFORMATION));
668
669 gmt = time(NULL);
670 bias = TIME_GetBias(gmt, &daylight);
671
672 tzinfo->Bias = -bias / 60;
673 tzinfo->StandardBias = 0;
674 tzinfo->DaylightBias = -60;
675 psTZ = TIME_GetTZAsStr (gmt, (-bias/60), daylight);
676 if (psTZ) strcpyW( tzinfo->StandardName, psTZ );
677 return STATUS_SUCCESS;
678}
679
680/***********************************************************************
681 * RtlSetTimeZoneInformation [NTDLL.@]
682 *
683 * Sets the current time zone.
684 *
685 * PARAMS:
686 * tzinfo [I]: Timezone information used to set timezone.
687 *
688 * RETURNS:
689 * Status.
690 *
691 * BUGS:
692 * Uses the obsolete unix timezone structure and tz_dsttime member.
693 */
694NTSTATUS WINAPI RtlSetTimeZoneInformation( const TIME_ZONE_INFORMATION *tzinfo )
695{
696 struct timezone tz;
697
698 tz.tz_minuteswest = tzinfo->Bias;
699#ifdef DST_NONE
700 tz.tz_dsttime = DST_NONE;
701#else
702 tz.tz_dsttime = 0;
703#endif
704#if 0
705 if(!settimeofday(NULL, &tz))
706 return STATUS_SUCCESS;
707#endif
708 return STATUS_PRIVILEGE_NOT_HELD;
709}
710
711/***********************************************************************
712 * NtSetSystemTime [NTDLL.@]
713 * ZwSetSystemTime [NTDLL.@]
714 *
715 * Sets the system time.
716 *
717 * PARAM:
718 * NewTime [I]: The time to set the system time to.
719 * OldTime [O]: Optional (ie. can be NULL). Old Time.
720 *
721 * RETURNS:
722 * Status.
723 */
724NTSTATUS WINAPI NtSetSystemTime(const LARGE_INTEGER *NewTime, LARGE_INTEGER *OldTime)
725{
726 TIME_FIELDS tf;
727 struct timeval tv;
728 struct timezone tz;
729 struct tm t;
730 time_t sec, oldsec;
731 int dst, bias;
732 int err;
733
734 /* Return the old time if necessary */
735 if(OldTime)
736 NtQuerySystemTime(OldTime);
737
738 RtlTimeToTimeFields(NewTime, &tf);
739
740 /* call gettimeofday to get the current timezone */
741 gettimeofday(&tv, &tz);
742 oldsec = tv.tv_sec;
743 /* get delta local time from utc */
744 bias = TIME_GetBias(oldsec, &dst);
745
746 /* get the number of seconds */
747 t.tm_sec = tf.Second;
748 t.tm_min = tf.Minute;
749 t.tm_hour = tf.Hour;
750 t.tm_mday = tf.Day;
751 t.tm_mon = tf.Month - 1;
752 t.tm_year = tf.Year - 1900;
753 t.tm_isdst = dst;
754 sec = mktime (&t);
755 /* correct for timezone and daylight */
756 sec += bias;
757
758 /* set the new time */
759 tv.tv_sec = sec;
760 tv.tv_usec = tf.Milliseconds * 1000;
761
762 /* error and sanity check*/
763 if(sec == (time_t)-1 || abs((int)(sec-oldsec)) > SETTIME_MAX_ADJUST) {
764 err = 2;
765 } else {
766#ifdef HAVE_SETTIMEOFDAY
767 err = settimeofday(&tv, NULL); /* 0 is OK, -1 is error */
768 if(err == 0)
769 return STATUS_SUCCESS;
770#else
771 err = 1;
772#endif
773 }
774
775 ERR("Cannot set time to %d/%d/%d %d:%d:%d Time adjustment %ld %s\n",
776 tf.Year, tf.Month, tf.Day, tf.Hour, tf.Minute, tf.Second,
777 sec-oldsec, err == -1 ? "No Permission" :
778 sec == (time_t)-1 ? "" : "is too large." );
779
780 if(err == 2)
781 return STATUS_INVALID_PARAMETER;
782 else if(err == -1)
783 return STATUS_PRIVILEGE_NOT_HELD;
784 else
785 return STATUS_NOT_IMPLEMENTED;
786}
Note: See TracBrowser for help on using the repository browser.