2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
5 * PURPOSE: Conversion between Time and TimeFields
6 * PROGRAMMER: Rex Jolliff (rex@lvcablemodem.com)
9 /* INCLUDES *****************************************************************/
16 #define TICKSPERMIN 600000000
17 #define TICKSPERSEC 10000000
18 #define TICKSPERMSEC 10000
19 #define SECSPERDAY 86400
20 #define SECSPERHOUR 3600
22 #define MINSPERHOUR 60
23 #define HOURSPERDAY 24
24 #define EPOCHWEEKDAY 1
26 #define EPOCHYEAR 1601
27 #define DAYSPERNORMALYEAR 365
28 #define DAYSPERLEAPYEAR 366
29 #define MONSPERYEAR 12
32 #define TICKSTO1970 0x019db1ded53e8000LL
33 #define TICKSTO1980 0x01a8e79fe1d58000LL
35 #define TICKSTO1970 0x019db1ded53e8000i64
36 #define TICKSTO1980 0x01a8e79fe1d58000i64
40 static const int YearLengths
[2] =
42 DAYSPERNORMALYEAR
, DAYSPERLEAPYEAR
44 static const int MonthLengths
[2][MONSPERYEAR
] =
46 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
47 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
50 static __inline
int IsLeapYear(int Year
)
52 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
55 static int DaysSinceEpoch(int Year
)
58 Year
--; /* Don't include a leap day from the current year */
59 Days
= Year
* DAYSPERNORMALYEAR
+ Year
/ 4 - Year
/ 100 + Year
/ 400;
60 Days
-= (EPOCHYEAR
- 1) * DAYSPERNORMALYEAR
+ (EPOCHYEAR
- 1) / 4 - (EPOCHYEAR
- 1) / 100 + (EPOCHYEAR
- 1) / 400;
64 static __inline
void NormalizeTimeFields(CSHORT
*FieldToNormalize
,
68 *FieldToNormalize
= (CSHORT
) (*FieldToNormalize
- Modulus
);
69 *CarryField
= (CSHORT
) (*CarryField
+ 1);
72 /* FUNCTIONS *****************************************************************/
78 RtlCutoverTimeToSystemTime(IN PTIME_FIELDS CutoverTimeFields
,
79 OUT PLARGE_INTEGER SystemTime
,
80 IN PLARGE_INTEGER CurrentTime
,
81 IN BOOLEAN ThisYearsCutoverOnly
)
83 TIME_FIELDS AdjustedTimeFields
;
84 TIME_FIELDS CurrentTimeFields
;
85 TIME_FIELDS CutoverSystemTimeFields
;
86 LARGE_INTEGER CutoverSystemTime
;
89 BOOLEAN NextYearsCutover
= FALSE
;
91 /* Check fixed cutover time */
92 if (CutoverTimeFields
->Year
!= 0)
94 if (!RtlTimeFieldsToTime(CutoverTimeFields
, SystemTime
))
97 if (SystemTime
->QuadPart
< CurrentTime
->QuadPart
)
104 * Compute recurring cutover time
107 /* Day must be between 1(first) and 5(last) */
108 if (CutoverTimeFields
->Day
== 0 || CutoverTimeFields
->Day
> 5)
111 RtlTimeToTimeFields(CurrentTime
, &CurrentTimeFields
);
115 /* Compute the cutover time of the first day of the current month */
116 AdjustedTimeFields
.Year
= CurrentTimeFields
.Year
;
117 if (NextYearsCutover
== TRUE
)
118 AdjustedTimeFields
.Year
++;
120 AdjustedTimeFields
.Month
= CutoverTimeFields
->Month
;
121 AdjustedTimeFields
.Day
= 1;
122 AdjustedTimeFields
.Hour
= CutoverTimeFields
->Hour
;
123 AdjustedTimeFields
.Minute
= CutoverTimeFields
->Minute
;
124 AdjustedTimeFields
.Second
= CutoverTimeFields
->Second
;
125 AdjustedTimeFields
.Milliseconds
= CutoverTimeFields
->Milliseconds
;
127 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
130 RtlTimeToTimeFields(&CutoverSystemTime
, &CutoverSystemTimeFields
);
132 /* Adjust day to first matching weekday */
133 if (CutoverSystemTimeFields
.Weekday
!= CutoverTimeFields
->Weekday
)
135 if (CutoverSystemTimeFields
.Weekday
< CutoverTimeFields
->Weekday
)
136 Days
= CutoverTimeFields
->Weekday
- CutoverSystemTimeFields
.Weekday
;
138 Days
= DAYSPERWEEK
- (CutoverSystemTimeFields
.Weekday
- CutoverTimeFields
->Weekday
);
140 AdjustedTimeFields
.Day
+= Days
;
143 /* Adjust the number of weeks */
144 if (CutoverTimeFields
->Day
> 1)
146 Days
= DAYSPERWEEK
* (CutoverTimeFields
->Day
- 1);
147 MonthLength
= MonthLengths
[IsLeapYear(AdjustedTimeFields
.Year
)][AdjustedTimeFields
.Month
- 1];
148 if ((AdjustedTimeFields
.Day
+ Days
) > MonthLength
)
151 AdjustedTimeFields
.Day
+= Days
;
154 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
157 if (ThisYearsCutoverOnly
== TRUE
||
158 NextYearsCutover
== TRUE
||
159 CutoverSystemTime
.QuadPart
>= CurrentTime
->QuadPart
)
164 NextYearsCutover
= TRUE
;
167 SystemTime
->QuadPart
= CutoverSystemTime
.QuadPart
;
179 IN PTIME_FIELDS TimeFields
,
180 OUT PLARGE_INTEGER Time
)
183 TIME_FIELDS IntTimeFields
;
185 memcpy(&IntTimeFields
,
187 sizeof(TIME_FIELDS
));
189 if (TimeFields
->Milliseconds
< 0 || TimeFields
->Milliseconds
> 999 ||
190 TimeFields
->Second
< 0 || TimeFields
->Second
> 59 ||
191 TimeFields
->Minute
< 0 || TimeFields
->Minute
> 59 ||
192 TimeFields
->Hour
< 0 || TimeFields
->Hour
> 23 ||
193 TimeFields
->Month
< 1 || TimeFields
->Month
> 12 ||
194 TimeFields
->Day
< 1 ||
195 TimeFields
->Day
> MonthLengths
[TimeFields
->Month
== 2 ||
196 IsLeapYear(TimeFields
->Year
)][TimeFields
->Month
- 1] ||
197 TimeFields
->Year
< 1601)
202 /* Normalize the TIME_FIELDS structure here */
203 while (IntTimeFields
.Second
>= SECSPERMIN
)
205 NormalizeTimeFields(&IntTimeFields
.Second
,
206 &IntTimeFields
.Minute
,
209 while (IntTimeFields
.Minute
>= MINSPERHOUR
)
211 NormalizeTimeFields(&IntTimeFields
.Minute
,
215 while (IntTimeFields
.Hour
>= HOURSPERDAY
)
217 NormalizeTimeFields(&IntTimeFields
.Hour
,
221 while (IntTimeFields
.Day
>
222 MonthLengths
[IsLeapYear(IntTimeFields
.Year
)][IntTimeFields
.Month
- 1])
224 NormalizeTimeFields(&IntTimeFields
.Day
,
225 &IntTimeFields
.Month
,
228 while (IntTimeFields
.Month
> MONSPERYEAR
)
230 NormalizeTimeFields(&IntTimeFields
.Month
,
235 /* Compute the time */
236 Time
->QuadPart
= DaysSinceEpoch(IntTimeFields
.Year
);
237 for (CurMonth
= 1; CurMonth
< IntTimeFields
.Month
; CurMonth
++)
239 Time
->QuadPart
+= MonthLengths
[IsLeapYear(IntTimeFields
.Year
)][CurMonth
- 1];
241 Time
->QuadPart
+= IntTimeFields
.Day
- 1;
242 Time
->QuadPart
*= SECSPERDAY
;
243 Time
->QuadPart
+= IntTimeFields
.Hour
* SECSPERHOUR
+ IntTimeFields
.Minute
* SECSPERMIN
+
244 IntTimeFields
.Second
;
245 Time
->QuadPart
*= TICKSPERSEC
;
246 Time
->QuadPart
+= IntTimeFields
.Milliseconds
* TICKSPERMSEC
;
257 RtlTimeToElapsedTimeFields(IN PLARGE_INTEGER Time
,
258 OUT PTIME_FIELDS TimeFields
)
260 ULONGLONG ElapsedSeconds
;
262 ULONG SecondsInMinute
;
264 /* Extract millisecond from time */
265 TimeFields
->Milliseconds
= (CSHORT
)((Time
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
267 /* Compute elapsed seconds */
268 ElapsedSeconds
= (ULONGLONG
)Time
->QuadPart
/ TICKSPERSEC
;
270 /* Compute seconds within the day */
271 SecondsInDay
= ElapsedSeconds
% SECSPERDAY
;
273 /* Compute elapsed minutes within the day */
274 SecondsInMinute
= SecondsInDay
% SECSPERHOUR
;
276 /* Compute elapsed time of day */
277 TimeFields
->Hour
= (CSHORT
)(SecondsInDay
/ SECSPERHOUR
);
278 TimeFields
->Minute
= (CSHORT
)(SecondsInMinute
/ SECSPERMIN
);
279 TimeFields
->Second
= (CSHORT
)(SecondsInMinute
% SECSPERMIN
);
281 /* Compute elapsed days */
282 TimeFields
->Day
= (CSHORT
)(ElapsedSeconds
/ SECSPERDAY
);
284 /* The elapsed number of months and days cannot be calculated */
285 TimeFields
->Month
= 0;
286 TimeFields
->Year
= 0;
296 IN PLARGE_INTEGER Time
,
297 OUT PTIME_FIELDS TimeFields
)
300 int SecondsInDay
, CurYear
;
301 int LeapYear
, CurMonth
;
303 LONGLONG IntTime
= (LONGLONG
)Time
->QuadPart
;
305 /* Extract millisecond from time and convert time into seconds */
306 TimeFields
->Milliseconds
= (CSHORT
) ((IntTime
% TICKSPERSEC
) / TICKSPERMSEC
);
307 IntTime
= IntTime
/ TICKSPERSEC
;
309 /* Split the time into days and seconds within the day */
310 Days
= IntTime
/ SECSPERDAY
;
311 SecondsInDay
= IntTime
% SECSPERDAY
;
313 /* Adjust the values for days and seconds in day */
314 while (SecondsInDay
< 0)
316 SecondsInDay
+= SECSPERDAY
;
319 while (SecondsInDay
>= SECSPERDAY
)
321 SecondsInDay
-= SECSPERDAY
;
325 /* compute time of day */
326 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
327 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
328 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
329 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
331 /* compute day of week */
332 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
336 CurYear
+= Days
/ DAYSPERLEAPYEAR
;
337 Days
-= DaysSinceEpoch(CurYear
);
340 LeapYear
= IsLeapYear(CurYear
);
341 if (Days
< (long) YearLengths
[LeapYear
])
346 Days
= Days
- (long) YearLengths
[LeapYear
];
348 TimeFields
->Year
= (CSHORT
) CurYear
;
350 /* Compute month of year */
351 LeapYear
= IsLeapYear(CurYear
);
352 Months
= MonthLengths
[LeapYear
];
353 for (CurMonth
= 0; Days
>= (long) Months
[CurMonth
]; CurMonth
++)
354 Days
= Days
- (long) Months
[CurMonth
];
355 TimeFields
->Month
= (CSHORT
) (CurMonth
+ 1);
356 TimeFields
->Day
= (CSHORT
) (Days
+ 1);
365 RtlTimeToSecondsSince1970(
366 IN PLARGE_INTEGER Time
,
367 OUT PULONG SecondsSince1970
)
369 LARGE_INTEGER IntTime
;
371 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1970
;
372 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
374 if (IntTime
.u
.HighPart
!= 0)
377 *SecondsSince1970
= IntTime
.u
.LowPart
;
388 RtlTimeToSecondsSince1980(
389 IN PLARGE_INTEGER Time
,
390 OUT PULONG SecondsSince1980
)
392 LARGE_INTEGER IntTime
;
394 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1980
;
395 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
397 if (IntTime
.u
.HighPart
!= 0)
400 *SecondsSince1980
= IntTime
.u
.LowPart
;
411 RtlLocalTimeToSystemTime(IN PLARGE_INTEGER LocalTime
,
412 OUT PLARGE_INTEGER SystemTime
)
414 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
417 Status
= ZwQuerySystemInformation(SystemTimeOfDayInformation
,
419 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
421 if (!NT_SUCCESS(Status
))
424 SystemTime
->QuadPart
= LocalTime
->QuadPart
+
425 TimeInformation
.TimeZoneBias
.QuadPart
;
427 return STATUS_SUCCESS
;
436 RtlSystemTimeToLocalTime(IN PLARGE_INTEGER SystemTime
,
437 OUT PLARGE_INTEGER LocalTime
)
439 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
442 Status
= ZwQuerySystemInformation(SystemTimeOfDayInformation
,
444 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
446 if (!NT_SUCCESS(Status
))
449 LocalTime
->QuadPart
= SystemTime
->QuadPart
-
450 TimeInformation
.TimeZoneBias
.QuadPart
;
452 return STATUS_SUCCESS
;
460 RtlSecondsSince1970ToTime(
461 IN ULONG SecondsSince1970
,
462 OUT PLARGE_INTEGER Time
)
464 Time
->QuadPart
= ((LONGLONG
)SecondsSince1970
* TICKSPERSEC
) + TICKSTO1970
;
472 RtlSecondsSince1980ToTime(
473 IN ULONG SecondsSince1980
,
474 OUT PLARGE_INTEGER Time
)
476 Time
->QuadPart
= ((LONGLONG
)SecondsSince1980
* TICKSPERSEC
) + TICKSTO1980
;