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 *****************************************************************/
15 #include <ddk/ntddk.h>
16 #include <ntdll/rtl.h>
21 #define TICKSPERMIN 600000000
22 #define TICKSPERSEC 10000000
23 #define TICKSPERMSEC 10000
24 #define SECSPERDAY 86400
25 #define SECSPERHOUR 3600
27 #define MINSPERHOUR 60
28 #define HOURSPERDAY 24
29 #define EPOCHWEEKDAY 1
31 #define EPOCHYEAR 1601
32 #define DAYSPERNORMALYEAR 365
33 #define DAYSPERLEAPYEAR 366
34 #define MONSPERYEAR 12
37 #define TICKSTO1970 0x019db1ded53e8000LL
38 #define TICKSTO1980 0x01a8e79fe1d58000LL
40 #define TICKSTO1970 0x019db1ded53e8000i64
41 #define TICKSTO1980 0x01a8e79fe1d58000i64
45 static const int YearLengths
[2] =
47 DAYSPERNORMALYEAR
, DAYSPERLEAPYEAR
49 static const int MonthLengths
[2][MONSPERYEAR
] =
51 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
52 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
55 static __inline
int IsLeapYear(int Year
)
57 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
60 static __inline
void NormalizeTimeFields(CSHORT
*FieldToNormalize
,
64 *FieldToNormalize
= (CSHORT
) (*FieldToNormalize
- Modulus
);
65 *CarryField
= (CSHORT
) (*CarryField
+ 1);
68 /* FUNCTIONS *****************************************************************/
74 RtlCutoverTimeToSystemTime(IN PTIME_FIELDS CutoverTimeFields
,
75 OUT PLARGE_INTEGER SystemTime
,
76 IN PLARGE_INTEGER CurrentTime
,
79 TIME_FIELDS AdjustedTimeFields
;
80 TIME_FIELDS CurrentTimeFields
;
81 TIME_FIELDS CutoverSystemTimeFields
;
82 LARGE_INTEGER CutoverSystemTime
;
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
);
108 /* Compute the cutover time of the first day of the current month */
109 AdjustedTimeFields
.Year
= CurrentTimeFields
.Year
;
110 AdjustedTimeFields
.Month
= CutoverTimeFields
->Month
;
111 AdjustedTimeFields
.Day
= 1;
112 AdjustedTimeFields
.Hour
= CutoverTimeFields
->Hour
;
113 AdjustedTimeFields
.Minute
= CutoverTimeFields
->Minute
;
114 AdjustedTimeFields
.Second
= CutoverTimeFields
->Second
;
115 AdjustedTimeFields
.Milliseconds
= CutoverTimeFields
->Milliseconds
;
117 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
120 RtlTimeToTimeFields(&CutoverSystemTime
, &CutoverSystemTimeFields
);
122 /* Adjust day to first matching weekday */
123 if (CutoverSystemTimeFields
.Weekday
!= CutoverTimeFields
->Weekday
)
125 if (CutoverSystemTimeFields
.Weekday
< CutoverTimeFields
->Weekday
)
126 Days
= CutoverTimeFields
->Weekday
- CutoverSystemTimeFields
.Weekday
;
128 Days
= DAYSPERWEEK
- (CutoverSystemTimeFields
.Weekday
- CutoverTimeFields
->Weekday
);
130 AdjustedTimeFields
.Day
+= Days
;
133 /* Adjust the number of weeks */
134 if (CutoverTimeFields
->Day
> 1)
136 Days
= DAYSPERWEEK
* (CutoverTimeFields
->Day
- 1);
137 MonthLength
= MonthLengths
[IsLeapYear(AdjustedTimeFields
.Year
)][AdjustedTimeFields
.Month
- 1];
138 if ((AdjustedTimeFields
.Day
+ Days
) > MonthLength
)
141 AdjustedTimeFields
.Day
+= Days
;
144 if (!RtlTimeFieldsToTime(&AdjustedTimeFields
, &CutoverSystemTime
))
147 SystemTime
->QuadPart
= CutoverSystemTime
.QuadPart
;
159 IN PTIME_FIELDS TimeFields
,
160 OUT PLARGE_INTEGER Time
)
164 TIME_FIELDS IntTimeFields
;
167 memcpy(&IntTimeFields
,
169 sizeof(TIME_FIELDS
));
171 /* Normalize the TIME_FIELDS structure here */
172 while (IntTimeFields
.Second
>= SECSPERMIN
)
174 NormalizeTimeFields(&IntTimeFields
.Second
,
175 &IntTimeFields
.Minute
,
178 while (IntTimeFields
.Minute
>= MINSPERHOUR
)
180 NormalizeTimeFields(&IntTimeFields
.Minute
,
184 while (IntTimeFields
.Hour
>= HOURSPERDAY
)
186 NormalizeTimeFields(&IntTimeFields
.Hour
,
190 while (IntTimeFields
.Day
>
191 MonthLengths
[IsLeapYear(IntTimeFields
.Year
)][IntTimeFields
.Month
- 1])
193 NormalizeTimeFields(&IntTimeFields
.Day
,
194 &IntTimeFields
.Month
,
197 while (IntTimeFields
.Month
> MONSPERYEAR
)
199 NormalizeTimeFields(&IntTimeFields
.Month
,
204 /* Compute the time */
205 for (CurYear
= EPOCHYEAR
; CurYear
< IntTimeFields
.Year
; CurYear
++)
207 Time
->QuadPart
+= YearLengths
[IsLeapYear(CurYear
)];
209 for (CurMonth
= 1; CurMonth
< IntTimeFields
.Month
; CurMonth
++)
211 Time
->QuadPart
+= MonthLengths
[IsLeapYear(CurYear
)][CurMonth
- 1];
213 Time
->QuadPart
+= IntTimeFields
.Day
- 1;
214 Time
->QuadPart
*= SECSPERDAY
;
215 Time
->QuadPart
+= IntTimeFields
.Hour
* SECSPERHOUR
+ IntTimeFields
.Minute
* SECSPERMIN
+
216 IntTimeFields
.Second
;
217 Time
->QuadPart
*= TICKSPERSEC
;
218 Time
->QuadPart
+= IntTimeFields
.Milliseconds
* TICKSPERMSEC
;
229 RtlTimeToElapsedTimeFields(IN PLARGE_INTEGER Time
,
230 OUT PTIME_FIELDS TimeFields
)
232 ULONGLONG ElapsedSeconds
;
234 ULONG SecondsInMinute
;
236 /* Extract millisecond from time */
237 TimeFields
->Milliseconds
= (CSHORT
)((Time
->QuadPart
% TICKSPERSEC
) / TICKSPERMSEC
);
239 /* Compute elapsed seconds */
240 ElapsedSeconds
= (ULONGLONG
)Time
->QuadPart
/ TICKSPERSEC
;
242 /* Compute seconds within the day */
243 SecondsInDay
= ElapsedSeconds
% SECSPERDAY
;
245 /* Compute elapsed minutes within the day */
246 SecondsInMinute
= SecondsInDay
% SECSPERHOUR
;
248 /* Compute elapsed time of day */
249 TimeFields
->Hour
= (CSHORT
)(SecondsInDay
/ SECSPERHOUR
);
250 TimeFields
->Minute
= (CSHORT
)(SecondsInMinute
/ SECSPERMIN
);
251 TimeFields
->Second
= (CSHORT
)(SecondsInMinute
% SECSPERMIN
);
253 /* Compute elapsed days */
254 TimeFields
->Day
= (CSHORT
)(ElapsedSeconds
/ SECSPERDAY
);
256 /* The elapsed number of months and days cannot be calculated */
257 TimeFields
->Month
= 0;
258 TimeFields
->Year
= 0;
268 IN PLARGE_INTEGER Time
,
269 OUT PTIME_FIELDS TimeFields
)
272 int SecondsInDay
, CurYear
;
273 int LeapYear
, CurMonth
;
275 LONGLONG IntTime
= (LONGLONG
)Time
->QuadPart
;
277 /* Extract millisecond from time and convert time into seconds */
278 TimeFields
->Milliseconds
= (CSHORT
) ((IntTime
% TICKSPERSEC
) / TICKSPERMSEC
);
279 IntTime
= IntTime
/ TICKSPERSEC
;
281 /* Split the time into days and seconds within the day */
282 Days
= IntTime
/ SECSPERDAY
;
283 SecondsInDay
= IntTime
% SECSPERDAY
;
285 /* Adjust the values for days and seconds in day */
286 while (SecondsInDay
< 0)
288 SecondsInDay
+= SECSPERDAY
;
291 while (SecondsInDay
>= SECSPERDAY
)
293 SecondsInDay
-= SECSPERDAY
;
297 /* compute time of day */
298 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
299 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
300 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
301 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
303 /* compute day of week */
304 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
308 CurYear
+= Days
/ DAYSPERLEAPYEAR
;
309 Days
-= (CurYear
- EPOCHYEAR
) * DAYSPERLEAPYEAR
;
310 CurYear
--; /* The next calculation needs CurYear - 1 */
311 Days
+= CurYear
- CurYear
/ 4 + CurYear
/ 100 - CurYear
/ 400;
313 Days
-= EPOCHYEAR
- 1 - (EPOCHYEAR
-1) / 4 + (EPOCHYEAR
-1) / 100 - (EPOCHYEAR
- 1) / 400;
316 LeapYear
= IsLeapYear(CurYear
);
317 if (Days
< (long) YearLengths
[LeapYear
])
322 Days
= Days
- (long) YearLengths
[LeapYear
];
324 TimeFields
->Year
= (CSHORT
) CurYear
;
326 /* Compute month of year */
327 LeapYear
= IsLeapYear(CurYear
);
328 Months
= MonthLengths
[LeapYear
];
329 for (CurMonth
= 0; Days
>= (long) Months
[CurMonth
]; CurMonth
++)
330 Days
= Days
- (long) Months
[CurMonth
];
331 TimeFields
->Month
= (CSHORT
) (CurMonth
+ 1);
332 TimeFields
->Day
= (CSHORT
) (Days
+ 1);
341 RtlTimeToSecondsSince1970(
342 IN PLARGE_INTEGER Time
,
343 OUT PULONG SecondsSince1970
)
345 LARGE_INTEGER IntTime
;
347 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1970
;
348 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
350 if (IntTime
.u
.HighPart
!= 0)
353 *SecondsSince1970
= IntTime
.u
.LowPart
;
364 RtlTimeToSecondsSince1980(
365 IN PLARGE_INTEGER Time
,
366 OUT PULONG SecondsSince1980
)
368 LARGE_INTEGER IntTime
;
370 IntTime
.QuadPart
= Time
->QuadPart
- TICKSTO1980
;
371 IntTime
.QuadPart
= IntTime
.QuadPart
/ TICKSPERSEC
;
373 if (IntTime
.u
.HighPart
!= 0)
376 *SecondsSince1980
= IntTime
.u
.LowPart
;
387 RtlLocalTimeToSystemTime(IN PLARGE_INTEGER LocalTime
,
388 OUT PLARGE_INTEGER SystemTime
)
390 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
393 Status
= NtQuerySystemInformation(SystemTimeOfDayInformation
,
395 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
397 if (!NT_SUCCESS(Status
))
400 SystemTime
->QuadPart
= LocalTime
->QuadPart
+
401 TimeInformation
.TimeZoneBias
.QuadPart
;
403 return STATUS_SUCCESS
;
412 RtlSystemTimeToLocalTime(IN PLARGE_INTEGER SystemTime
,
413 OUT PLARGE_INTEGER LocalTime
)
415 SYSTEM_TIMEOFDAY_INFORMATION TimeInformation
;
418 Status
= NtQuerySystemInformation(SystemTimeOfDayInformation
,
420 sizeof(SYSTEM_TIMEOFDAY_INFORMATION
),
422 if (!NT_SUCCESS(Status
))
425 LocalTime
->QuadPart
= SystemTime
->QuadPart
-
426 TimeInformation
.TimeZoneBias
.QuadPart
;
428 return STATUS_SUCCESS
;
436 RtlSecondsSince1970ToTime(
437 IN ULONG SecondsSince1970
,
438 OUT PLARGE_INTEGER Time
)
440 Time
->QuadPart
= ((LONGLONG
)SecondsSince1970
* TICKSPERSEC
) + TICKSTO1970
;
448 RtlSecondsSince1980ToTime(
449 IN ULONG SecondsSince1980
,
450 OUT PLARGE_INTEGER Time
)
452 Time
->QuadPart
= ((LONGLONG
)SecondsSince1980
* TICKSPERSEC
) + TICKSTO1980
;