3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
6 * PURPOSE: Conversion between Time and TimeFields
7 * PROGRAMMER: Rex Jolliff (rex@lvcablemodem.com)
10 * 08/03/98 RJJ Implemented these functions
13 /* INCLUDES *****************************************************************/
20 #define TICKSPERMIN 600000000
21 #define TICKSPERSEC 10000000
22 #define TICKSPERMSEC 10000
23 #define SECSPERDAY 86400
24 #define SECSPERHOUR 3600
26 #define MINSPERHOUR 60
27 #define HOURSPERDAY 24
28 #define EPOCHWEEKDAY 1
30 #define EPOCHYEAR 1601
31 #define DAYSPERNORMALYEAR 365
32 #define DAYSPERLEAPYEAR 366
33 #define MONSPERYEAR 12
36 #define TICKSTO1970 0x019db1ded53e8000LL
37 #define TICKSTO1980 0x01a8e79fe1d58000LL
39 #define TICKSTO1970 0x019db1ded53e8000i64
40 #define TICKSTO1980 0x01a8e79fe1d58000i64
44 static const int YearLengths
[2] =
46 DAYSPERNORMALYEAR
, DAYSPERLEAPYEAR
48 static const int MonthLengths
[2][MONSPERYEAR
] =
50 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
51 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
54 static __inline
int IsLeapYear(int Year
)
56 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
59 static __inline
void NormalizeTimeFields(CSHORT
*FieldToNormalize
,
63 *FieldToNormalize
= (CSHORT
) (*FieldToNormalize
- Modulus
);
64 *CarryField
= (CSHORT
) (*CarryField
+ 1);
67 /* FUNCTIONS *****************************************************************/
73 RtlCutoverTimeToSystemTime(IN PTIME_FIELDS CutoverTimeFields
,
74 OUT PLARGE_INTEGER SystemTime
,
75 IN PLARGE_INTEGER CurrentTime
,
76 IN BOOLEAN ThisYearsCutoverOnly
)
78 TIME_FIELDS AdjustedTimeFields
;
79 TIME_FIELDS CurrentTimeFields
;
80 TIME_FIELDS CutoverSystemTimeFields
;
81 LARGE_INTEGER CutoverSystemTime
;
84 BOOLEAN NextYearsCutover
= FALSE
;
86 /* Check fixed cutover time */
87 if (CutoverTimeFields
->Year
!= 0)
89 if (!RtlTimeFieldsToTime(CutoverTimeFields
, SystemTime
))
92 if (SystemTime
->QuadPart
< CurrentTime
->QuadPart
)
99 * Compute recurring cutover time
102 /* Day must be between 1(first) and 5(last) */
103 if (CutoverTimeFields
->Day
== 0 || CutoverTimeFields
->Day
> 5)
106 RtlTimeToTimeFields(CurrentTime
, &CurrentTimeFields
);
110 /* Compute the cutover time of the first day of the current month */
111 AdjustedTimeFields
.Year
= CurrentTimeFields
.Year
;
112 if (NextYearsCutover
== TRUE
)
113 AdjustedTimeFields
.Year
++;
115 AdjustedTimeFields
.Month
= CutoverTimeFields
->Month
;
116 AdjustedTimeFields
.Day
= 1;
117 AdjustedTimeFields
.Hour
= CutoverTimeFields
->Hour
;
118 AdjustedTimeFields
.Minute
= CutoverTimeFields
->Minute
;
119 AdjustedTimeFields
.Second
= CutoverTimeFields
->Second
;
120 AdjustedTimeFields
.Milliseconds
= CutoverTimeFields
->Milliseconds
;
122 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
125 RtlTimeToTimeFields(&CutoverSystemTime
, &CutoverSystemTimeFields
);
127 /* Adjust day to first matching weekday */
128 if (CutoverSystemTimeFields
.Weekday
!= CutoverTimeFields
->Weekday
)
130 if (CutoverSystemTimeFields
.Weekday
< CutoverTimeFields
->Weekday
)
131 Days
= CutoverTimeFields
->Weekday
- CutoverSystemTimeFields
.Weekday
;
133 Days
= DAYSPERWEEK
- (CutoverSystemTimeFields
.Weekday
- CutoverTimeFields
->Weekday
);
135 AdjustedTimeFields
.Day
+= Days
;
138 /* Adjust the number of weeks */
139 if (CutoverTimeFields
->Day
> 1)
141 Days
= DAYSPERWEEK
* (CutoverTimeFields
->Day
- 1);
142 MonthLength
= MonthLengths
[IsLeapYear(AdjustedTimeFields
.Year
)][AdjustedTimeFields
.Month
- 1];
143 if ((AdjustedTimeFields
.Day
+ Days
) > MonthLength
)
146 AdjustedTimeFields
.Day
+= Days
;
149 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
152 if (ThisYearsCutoverOnly
== TRUE
||
153 NextYearsCutover
== TRUE
||
154 CutoverSystemTime
.QuadPart
>= CurrentTime
->QuadPart
)
159 NextYearsCutover
= TRUE
;
162 SystemTime
->QuadPart
= CutoverSystemTime
.QuadPart
;
174 IN PTIME_FIELDS TimeFields
,
175 OUT PLARGE_INTEGER Time
)
179 TIME_FIELDS IntTimeFields
;
182 memcpy(&IntTimeFields
,
184 sizeof(TIME_FIELDS
));
186 /* Normalize the TIME_FIELDS structure here */
187 while (IntTimeFields
.Second
>= SECSPERMIN
)
189 NormalizeTimeFields(&IntTimeFields
.Second
,
190 &IntTimeFields
.Minute
,
193 while (IntTimeFields
.Minute
>= MINSPERHOUR
)
195 NormalizeTimeFields(&IntTimeFields
.Minute
,
199 while (IntTimeFields
.Hour
>= HOURSPERDAY
)
201 NormalizeTimeFields(&IntTimeFields
.Hour
,
205 while (IntTimeFields
.Day
>
206 MonthLengths
[IsLeapYear(IntTimeFields
.Year
)][IntTimeFields
.Month
- 1])
208 NormalizeTimeFields(&IntTimeFields
.Day
,
209 &IntTimeFields
.Month
,
212 while (IntTimeFields
.Month
> MONSPERYEAR
)
214 NormalizeTimeFields(&IntTimeFields
.Month
,
219 /* Compute the time */
220 for (CurYear
= EPOCHYEAR
; CurYear
< IntTimeFields
.Year
; CurYear
++)
222 Time
->QuadPart
+= YearLengths
[IsLeapYear(CurYear
)];
224 for (CurMonth
= 1; CurMonth
< IntTimeFields
.Month
; CurMonth
++)
226 Time
->QuadPart
+= MonthLengths
[IsLeapYear(CurYear
)][CurMonth
- 1];
228 Time
->QuadPart
+= IntTimeFields
.Day
- 1;
229 Time
->QuadPart
*= SECSPERDAY
;
230 Time
->QuadPart
+= IntTimeFields
.Hour
* SECSPERHOUR
+ IntTimeFields
.Minute
* SECSPERMIN
+
231 IntTimeFields
.Second
;
232 Time
->QuadPart
*= TICKSPERSEC
;
233 Time
->QuadPart
+= IntTimeFields
.Milliseconds
* TICKSPERMSEC
;
244 RtlTimeToElapsedTimeFields(IN PLARGE_INTEGER Time
,
245 OUT PTIME_FIELDS TimeFields
)
247 ULONGLONG ElapsedSeconds
;
249 ULONG SecondsInMinute
;
251 /* Extract millisecond from time */
252 TimeFields
->Milliseconds
= (CSHORT
)((Time
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
254 /* Compute elapsed seconds */
255 ElapsedSeconds
= (ULONGLONG
)Time
->QuadPart
/ TICKSPERSEC
;
257 /* Compute seconds within the day */
258 SecondsInDay
= ElapsedSeconds
% SECSPERDAY
;
260 /* Compute elapsed minutes within the day */
261 SecondsInMinute
= SecondsInDay
% SECSPERHOUR
;
263 /* Compute elapsed time of day */
264 TimeFields
->Hour
= (CSHORT
)(SecondsInDay
/ SECSPERHOUR
);
265 TimeFields
->Minute
= (CSHORT
)(SecondsInMinute
/ SECSPERMIN
);
266 TimeFields
->Second
= (CSHORT
)(SecondsInMinute
% SECSPERMIN
);
268 /* Compute elapsed days */
269 TimeFields
->Day
= (CSHORT
)(ElapsedSeconds
/ SECSPERDAY
);
271 /* The elapsed number of months and days cannot be calculated */
272 TimeFields
->Month
= 0;
273 TimeFields
->Year
= 0;
283 IN PLARGE_INTEGER Time
,
284 OUT PTIME_FIELDS TimeFields
)
287 int SecondsInDay
, CurYear
;
288 int LeapYear
, CurMonth
;
290 LONGLONG IntTime
= (LONGLONG
)Time
->QuadPart
;
292 /* Extract millisecond from time and convert time into seconds */
293 TimeFields
->Milliseconds
= (CSHORT
) ((IntTime
% TICKSPERSEC
) / TICKSPERMSEC
);
294 IntTime
= IntTime
/ TICKSPERSEC
;
296 /* Split the time into days and seconds within the day */
297 Days
= IntTime
/ SECSPERDAY
;
298 SecondsInDay
= IntTime
% SECSPERDAY
;
300 /* Adjust the values for days and seconds in day */
301 while (SecondsInDay
< 0)
303 SecondsInDay
+= SECSPERDAY
;
306 while (SecondsInDay
>= SECSPERDAY
)
308 SecondsInDay
-= SECSPERDAY
;
312 /* compute time of day */
313 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
314 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
315 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
316 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
318 /* compute day of week */
319 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
323 CurYear
+= Days
/ DAYSPERLEAPYEAR
;
324 Days
-= (CurYear
- EPOCHYEAR
) * DAYSPERLEAPYEAR
;
325 CurYear
--; /* The next calculation needs CurYear - 1 */
326 Days
+= CurYear
- CurYear
/ 4 + CurYear
/ 100 - CurYear
/ 400;
328 Days
-= EPOCHYEAR
- 1 - (EPOCHYEAR
-1) / 4 + (EPOCHYEAR
-1) / 100 - (EPOCHYEAR
- 1) / 400;
331 LeapYear
= IsLeapYear(CurYear
);
332 if (Days
< (long) YearLengths
[LeapYear
])
337 Days
= Days
- (long) YearLengths
[LeapYear
];
339 TimeFields
->Year
= (CSHORT
) CurYear
;
341 /* Compute month of year */
342 LeapYear
= IsLeapYear(CurYear
);
343 Months
= MonthLengths
[LeapYear
];
344 for (CurMonth
= 0; Days
>= (long) Months
[CurMonth
]; CurMonth
++)
345 Days
= Days
- (long) Months
[CurMonth
];
346 TimeFields
->Month
= (CSHORT
) (CurMonth
+ 1);
347 TimeFields
->Day
= (CSHORT
) (Days
+ 1);
356 RtlTimeToSecondsSince1970(
357 IN PLARGE_INTEGER Time
,
358 OUT PULONG SecondsSince1970
)
360 LARGE_INTEGER IntTime
;
362 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1970
;
363 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
365 if (IntTime
.u
.HighPart
!= 0)
368 *SecondsSince1970
= IntTime
.u
.LowPart
;
379 RtlTimeToSecondsSince1980(
380 IN PLARGE_INTEGER Time
,
381 OUT PULONG SecondsSince1980
)
383 LARGE_INTEGER IntTime
;
385 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1980
;
386 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
388 if (IntTime
.u
.HighPart
!= 0)
391 *SecondsSince1980
= IntTime
.u
.LowPart
;
402 RtlLocalTimeToSystemTime(IN PLARGE_INTEGER LocalTime
,
403 OUT PLARGE_INTEGER SystemTime
)
405 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
408 Status
= NtQuerySystemInformation(SystemTimeOfDayInformation
,
410 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
412 if (!NT_SUCCESS(Status
))
415 SystemTime
->QuadPart
= LocalTime
->QuadPart
+
416 TimeInformation
.TimeZoneBias
.QuadPart
;
418 return STATUS_SUCCESS
;
427 RtlSystemTimeToLocalTime(IN PLARGE_INTEGER SystemTime
,
428 OUT PLARGE_INTEGER LocalTime
)
430 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
433 Status
= NtQuerySystemInformation(SystemTimeOfDayInformation
,
435 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
437 if (!NT_SUCCESS(Status
))
440 LocalTime
->QuadPart
= SystemTime
->QuadPart
-
441 TimeInformation
.TimeZoneBias
.QuadPart
;
443 return STATUS_SUCCESS
;
451 RtlSecondsSince1970ToTime(
452 IN ULONG SecondsSince1970
,
453 OUT PLARGE_INTEGER Time
)
455 Time
->QuadPart
= ((LONGLONG
)SecondsSince1970
* TICKSPERSEC
) + TICKSTO1970
;
463 RtlSecondsSince1980ToTime(
464 IN ULONG SecondsSince1980
,
465 OUT PLARGE_INTEGER Time
)
467 Time
->QuadPart
= ((LONGLONG
)SecondsSince1980
* TICKSPERSEC
) + TICKSTO1980
;