2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: kernel/rtl/time.c
5 * PURPOSE: Conversion between Time and TimeFields
6 * PROGRAMMER: Rex Jolliff (rex@lvcablemodem.com)
9 * 08/03/98 RJJ Implemented these functions
12 /* INCLUDES *****************************************************************/
14 #include <ddk/ntddk.h>
16 #include <internal/debug.h>
18 #define TICKSPERSEC 10000000
19 #define TICKSPERMSEC 10000
20 #define SECSPERDAY 86400
21 #define SECSPERHOUR 3600
23 #define MINSPERHOUR 60
24 #define HOURSPERDAY 24
25 #define EPOCHWEEKDAY 0
27 #define EPOCHYEAR 1601
28 #define DAYSPERNORMALYEAR 365
29 #define DAYSPERLEAPYEAR 366
30 #define MONSPERYEAR 12
32 static const int YearLengths
[2] = {DAYSPERNORMALYEAR
, DAYSPERLEAPYEAR
};
33 static const int MonthLengths
[2][MONSPERYEAR
] =
35 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
36 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
39 static __inline
int IsLeapYear(int Year
)
41 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0) ? 1 : 0;
44 static __inline
void NormalizeTimeFields(CSHORT
*FieldToNormalize
,
48 *FieldToNormalize
= (CSHORT
) (*FieldToNormalize
- Modulus
);
49 *CarryField
= (CSHORT
) (*CarryField
+ 1);
52 /* FUNCTIONS *****************************************************************/
54 VOID
RtlTimeToTimeFields(PLARGE_INTEGER liTime
,
55 PTIME_FIELDS TimeFields
)
58 int LeapSecondCorrections
, SecondsInDay
, CurYear
;
59 int LeapYear
, CurMonth
, GMTOffset
;
61 long long int Time
= (long long int)liTime
->QuadPart
;
63 /* Extract millisecond from time and convert time into seconds */
64 TimeFields
->Milliseconds
= (CSHORT
) ((Time
% TICKSPERSEC
) / TICKSPERMSEC
);
65 Time
= Time
/ TICKSPERSEC
;
67 /* FIXME: Compute the number of leap second corrections here */
68 LeapSecondCorrections
= 0;
70 /* FIXME: get the GMT offset here */
73 /* Split the time into days and seconds within the day */
74 Days
= Time
/ SECSPERDAY
;
75 SecondsInDay
= Time
% SECSPERDAY
;
77 /* Adjust the values for GMT and leap seconds */
78 SecondsInDay
+= (GMTOffset
- LeapSecondCorrections
);
79 while (SecondsInDay
< 0)
81 SecondsInDay
+= SECSPERDAY
;
84 while (SecondsInDay
>= SECSPERDAY
)
86 SecondsInDay
-= SECSPERDAY
;
90 /* compute time of day */
91 TimeFields
->Hour
= (CSHORT
) (SecondsInDay
/ SECSPERHOUR
);
92 SecondsInDay
= SecondsInDay
% SECSPERHOUR
;
93 TimeFields
->Minute
= (CSHORT
) (SecondsInDay
/ SECSPERMIN
);
94 TimeFields
->Second
= (CSHORT
) (SecondsInDay
% SECSPERMIN
);
96 /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
98 /* compute day of week */
99 TimeFields
->Weekday
= (CSHORT
) ((EPOCHWEEKDAY
+ Days
) % DAYSPERWEEK
);
103 /* FIXME: handle calendar modifications */
106 LeapYear
= IsLeapYear(CurYear
);
107 if (Days
< (long) YearLengths
[LeapYear
])
112 Days
= Days
- (long) YearLengths
[LeapYear
];
114 TimeFields
->Year
= (CSHORT
) CurYear
;
116 /* Compute month of year */
117 Months
= MonthLengths
[LeapYear
];
118 for (CurMonth
= 0; Days
>= (long) Months
[CurMonth
]; CurMonth
++)
119 Days
= Days
- (long) Months
[CurMonth
];
120 TimeFields
->Month
= (CSHORT
) (CurMonth
+ 1);
121 TimeFields
->Day
= (CSHORT
) (Days
+ 1);
124 BOOLEAN
RtlTimeFieldsToTime(PTIME_FIELDS tfTimeFields
,
127 int CurYear
, CurMonth
;
128 long long int rcTime
;
129 TIME_FIELDS TimeFields
= *tfTimeFields
;
133 /* FIXME: normalize the TIME_FIELDS structure here */
134 while (TimeFields
.Second
>= SECSPERMIN
)
136 NormalizeTimeFields(&TimeFields
.Second
,
140 while (TimeFields
.Minute
>= MINSPERHOUR
)
142 NormalizeTimeFields(&TimeFields
.Minute
,
146 while (TimeFields
.Hour
>= HOURSPERDAY
)
148 NormalizeTimeFields(&TimeFields
.Hour
,
152 while (TimeFields
.Day
>
153 MonthLengths
[IsLeapYear(TimeFields
.Year
)][TimeFields
.Month
- 1])
155 NormalizeTimeFields(&TimeFields
.Day
,
159 while (TimeFields
.Month
> MONSPERYEAR
)
161 NormalizeTimeFields(&TimeFields
.Month
,
166 /* FIXME: handle calendar corrections here */
167 for (CurYear
= EPOCHYEAR
; CurYear
< TimeFields
.Year
; CurYear
++)
169 rcTime
+= YearLengths
[IsLeapYear(CurYear
)];
171 for (CurMonth
= 1; CurMonth
< TimeFields
.Month
; CurMonth
++)
173 rcTime
+= MonthLengths
[IsLeapYear(CurYear
)][CurMonth
- 1];
175 rcTime
+= TimeFields
.Day
- 1;
176 rcTime
*= SECSPERDAY
;
177 rcTime
+= TimeFields
.Hour
* SECSPERHOUR
+ TimeFields
.Minute
* SECSPERMIN
+
179 rcTime
*= TICKSPERSEC
;
180 rcTime
+= TimeFields
.Milliseconds
* TICKSPERMSEC
;
181 *Time
= *(LARGE_INTEGER
*)&rcTime
;