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 __inline
void NormalizeTimeFields(CSHORT
*FieldToNormalize
,
59 *FieldToNormalize
= (CSHORT
) (*FieldToNormalize
- Modulus
);
60 *CarryField
= (CSHORT
) (*CarryField
+ 1);
63 /* FUNCTIONS *****************************************************************/
69 RtlCutoverTimeToSystemTime(IN PTIME_FIELDS CutoverTimeFields
,
70 OUT PLARGE_INTEGER SystemTime
,
71 IN PLARGE_INTEGER CurrentTime
,
72 IN BOOLEAN ThisYearsCutoverOnly
)
74 TIME_FIELDS AdjustedTimeFields
;
75 TIME_FIELDS CurrentTimeFields
;
76 TIME_FIELDS CutoverSystemTimeFields
;
77 LARGE_INTEGER CutoverSystemTime
;
80 BOOLEAN NextYearsCutover
= FALSE
;
82 /* Check fixed cutover time */
83 if (CutoverTimeFields
->Year
!= 0)
85 if (!RtlTimeFieldsToTime(CutoverTimeFields
, SystemTime
))
88 if (SystemTime
->QuadPart
< CurrentTime
->QuadPart
)
95 * Compute recurring cutover time
98 /* Day must be between 1(first) and 5(last) */
99 if (CutoverTimeFields
->Day
== 0 || CutoverTimeFields
->Day
> 5)
102 RtlTimeToTimeFields(CurrentTime
, &CurrentTimeFields
);
106 /* Compute the cutover time of the first day of the current month */
107 AdjustedTimeFields
.Year
= CurrentTimeFields
.Year
;
108 if (NextYearsCutover
== TRUE
)
109 AdjustedTimeFields
.Year
++;
111 AdjustedTimeFields
.Month
= CutoverTimeFields
->Month
;
112 AdjustedTimeFields
.Day
= 1;
113 AdjustedTimeFields
.Hour
= CutoverTimeFields
->Hour
;
114 AdjustedTimeFields
.Minute
= CutoverTimeFields
->Minute
;
115 AdjustedTimeFields
.Second
= CutoverTimeFields
->Second
;
116 AdjustedTimeFields
.Milliseconds
= CutoverTimeFields
->Milliseconds
;
118 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
121 RtlTimeToTimeFields(&CutoverSystemTime
, &CutoverSystemTimeFields
);
123 /* Adjust day to first matching weekday */
124 if (CutoverSystemTimeFields
.Weekday
!= CutoverTimeFields
->Weekday
)
126 if (CutoverSystemTimeFields
.Weekday
< CutoverTimeFields
->Weekday
)
127 Days
= CutoverTimeFields
->Weekday
- CutoverSystemTimeFields
.Weekday
;
129 Days
= DAYSPERWEEK
- (CutoverSystemTimeFields
.Weekday
- CutoverTimeFields
->Weekday
);
131 AdjustedTimeFields
.Day
+= Days
;
134 /* Adjust the number of weeks */
135 if (CutoverTimeFields
->Day
> 1)
137 Days
= DAYSPERWEEK
* (CutoverTimeFields
->Day
- 1);
138 MonthLength
= MonthLengths
[IsLeapYear(AdjustedTimeFields
.Year
)][AdjustedTimeFields
.Month
- 1];
139 if ((AdjustedTimeFields
.Day
+ Days
) > MonthLength
)
142 AdjustedTimeFields
.Day
+= Days
;
145 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
148 if (ThisYearsCutoverOnly
== TRUE
||
149 NextYearsCutover
== TRUE
||
150 CutoverSystemTime
.QuadPart
>= CurrentTime
->QuadPart
)
155 NextYearsCutover
= TRUE
;
158 SystemTime
->QuadPart
= CutoverSystemTime
.QuadPart
;
170 IN PTIME_FIELDS TimeFields
,
171 OUT PLARGE_INTEGER Time
)
175 TIME_FIELDS IntTimeFields
;
178 memcpy(&IntTimeFields
,
180 sizeof(TIME_FIELDS
));
182 /* Normalize the TIME_FIELDS structure here */
183 while (IntTimeFields
.Second
>= SECSPERMIN
)
185 NormalizeTimeFields(&IntTimeFields
.Second
,
186 &IntTimeFields
.Minute
,
189 while (IntTimeFields
.Minute
>= MINSPERHOUR
)
191 NormalizeTimeFields(&IntTimeFields
.Minute
,
195 while (IntTimeFields
.Hour
>= HOURSPERDAY
)
197 NormalizeTimeFields(&IntTimeFields
.Hour
,
201 while (IntTimeFields
.Day
>
202 MonthLengths
[IsLeapYear(IntTimeFields
.Year
)][IntTimeFields
.Month
- 1])
204 NormalizeTimeFields(&IntTimeFields
.Day
,
205 &IntTimeFields
.Month
,
208 while (IntTimeFields
.Month
> MONSPERYEAR
)
210 NormalizeTimeFields(&IntTimeFields
.Month
,
215 /* Compute the time */
216 for (CurYear
= EPOCHYEAR
; CurYear
< IntTimeFields
.Year
; CurYear
++)
218 Time
->QuadPart
+= YearLengths
[IsLeapYear(CurYear
)];
220 for (CurMonth
= 1; CurMonth
< IntTimeFields
.Month
; CurMonth
++)
222 Time
->QuadPart
+= MonthLengths
[IsLeapYear(CurYear
)][CurMonth
- 1];
224 Time
->QuadPart
+= IntTimeFields
.Day
- 1;
225 Time
->QuadPart
*= SECSPERDAY
;
226 Time
->QuadPart
+= IntTimeFields
.Hour
* SECSPERHOUR
+ IntTimeFields
.Minute
* SECSPERMIN
+
227 IntTimeFields
.Second
;
228 Time
->QuadPart
*= TICKSPERSEC
;
229 Time
->QuadPart
+= IntTimeFields
.Milliseconds
* TICKSPERMSEC
;
240 RtlTimeToElapsedTimeFields(IN PLARGE_INTEGER Time
,
241 OUT PTIME_FIELDS TimeFields
)
243 ULONGLONG ElapsedSeconds
;
245 ULONG SecondsInMinute
;
247 /* Extract millisecond from time */
248 TimeFields
->Milliseconds
= (CSHORT
)((Time
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
250 /* Compute elapsed seconds */
251 ElapsedSeconds
= (ULONGLONG
)Time
->QuadPart
/ TICKSPERSEC
;
253 /* Compute seconds within the day */
254 SecondsInDay
= ElapsedSeconds
% SECSPERDAY
;
256 /* Compute elapsed minutes within the day */
257 SecondsInMinute
= SecondsInDay
% SECSPERHOUR
;
259 /* Compute elapsed time of day */
260 TimeFields
->Hour
= (CSHORT
)(SecondsInDay
/ SECSPERHOUR
);
261 TimeFields
->Minute
= (CSHORT
)(SecondsInMinute
/ SECSPERMIN
);
262 TimeFields
->Second
= (CSHORT
)(SecondsInMinute
% SECSPERMIN
);
264 /* Compute elapsed days */
265 TimeFields
->Day
= (CSHORT
)(ElapsedSeconds
/ SECSPERDAY
);
267 /* The elapsed number of months and days cannot be calculated */
268 TimeFields
->Month
= 0;
269 TimeFields
->Year
= 0;
279 IN PLARGE_INTEGER Time
,
280 OUT PTIME_FIELDS TimeFields
)
283 int SecondsInDay
, CurYear
;
284 int LeapYear
, CurMonth
;
286 LONGLONG IntTime
= (LONGLONG
)Time
->QuadPart
;
288 /* Extract millisecond from time and convert time into seconds */
289 TimeFields
->Milliseconds
= (CSHORT
) ((IntTime
% TICKSPERSEC
) / TICKSPERMSEC
);
290 IntTime
= IntTime
/ TICKSPERSEC
;
292 /* Split the time into days and seconds within the day */
293 Days
= IntTime
/ SECSPERDAY
;
294 SecondsInDay
= IntTime
% SECSPERDAY
;
296 /* Adjust the values for days and seconds in day */
297 while (SecondsInDay
< 0)
299 SecondsInDay
+= SECSPERDAY
;
302 while (SecondsInDay
>= SECSPERDAY
)
304 SecondsInDay
-= SECSPERDAY
;
308 /* compute time of day */
309 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
310 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
311 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
312 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
314 /* compute day of week */
315 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
319 CurYear
+= Days
/ DAYSPERLEAPYEAR
;
320 Days
-= (CurYear
- EPOCHYEAR
) * DAYSPERLEAPYEAR
;
321 CurYear
--; /* The next calculation needs CurYear - 1 */
322 Days
+= CurYear
- CurYear
/ 4 + CurYear
/ 100 - CurYear
/ 400;
324 Days
-= EPOCHYEAR
- 1 - (EPOCHYEAR
-1) / 4 + (EPOCHYEAR
-1) / 100 - (EPOCHYEAR
- 1) / 400;
327 LeapYear
= IsLeapYear(CurYear
);
328 if (Days
< (long) YearLengths
[LeapYear
])
333 Days
= Days
- (long) YearLengths
[LeapYear
];
335 TimeFields
->Year
= (CSHORT
) CurYear
;
337 /* Compute month of year */
338 LeapYear
= IsLeapYear(CurYear
);
339 Months
= MonthLengths
[LeapYear
];
340 for (CurMonth
= 0; Days
>= (long) Months
[CurMonth
]; CurMonth
++)
341 Days
= Days
- (long) Months
[CurMonth
];
342 TimeFields
->Month
= (CSHORT
) (CurMonth
+ 1);
343 TimeFields
->Day
= (CSHORT
) (Days
+ 1);
352 RtlTimeToSecondsSince1970(
353 IN PLARGE_INTEGER Time
,
354 OUT PULONG SecondsSince1970
)
356 LARGE_INTEGER IntTime
;
358 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1970
;
359 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
361 if (IntTime
.u
.HighPart
!= 0)
364 *SecondsSince1970
= IntTime
.u
.LowPart
;
375 RtlTimeToSecondsSince1980(
376 IN PLARGE_INTEGER Time
,
377 OUT PULONG SecondsSince1980
)
379 LARGE_INTEGER IntTime
;
381 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1980
;
382 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
384 if (IntTime
.u
.HighPart
!= 0)
387 *SecondsSince1980
= IntTime
.u
.LowPart
;
398 RtlLocalTimeToSystemTime(IN PLARGE_INTEGER LocalTime
,
399 OUT PLARGE_INTEGER SystemTime
)
401 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
404 Status
= ZwQuerySystemInformation(SystemTimeOfDayInformation
,
406 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
408 if (!NT_SUCCESS(Status
))
411 SystemTime
->QuadPart
= LocalTime
->QuadPart
+
412 TimeInformation
.TimeZoneBias
.QuadPart
;
414 return STATUS_SUCCESS
;
423 RtlSystemTimeToLocalTime(IN PLARGE_INTEGER SystemTime
,
424 OUT PLARGE_INTEGER LocalTime
)
426 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
429 Status
= ZwQuerySystemInformation(SystemTimeOfDayInformation
,
431 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
433 if (!NT_SUCCESS(Status
))
436 LocalTime
->QuadPart
= SystemTime
->QuadPart
-
437 TimeInformation
.TimeZoneBias
.QuadPart
;
439 return STATUS_SUCCESS
;
447 RtlSecondsSince1970ToTime(
448 IN ULONG SecondsSince1970
,
449 OUT PLARGE_INTEGER Time
)
451 Time
->QuadPart
= ((LONGLONG
)SecondsSince1970
* TICKSPERSEC
) + TICKSTO1970
;
459 RtlSecondsSince1980ToTime(
460 IN ULONG SecondsSince1980
,
461 OUT PLARGE_INTEGER Time
)
463 Time
->QuadPart
= ((LONGLONG
)SecondsSince1980
* TICKSPERSEC
) + TICKSTO1980
;