Fixed bug in RtlTimeToTimeFields().
[reactos.git] / reactos / ntoskrnl / rtl / time.c
1 /*
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)
7 * UPDATE HISTORY:
8 * Created 22/05/98
9 * 08/03/98 RJJ Implemented these functions
10 */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15
16 #define NDEBUG
17 #include <internal/debug.h>
18
19 #define TICKSPERMIN 600000000
20 #define TICKSPERSEC 10000000
21 #define TICKSPERMSEC 10000
22 #define SECSPERDAY 86400
23 #define SECSPERHOUR 3600
24 #define SECSPERMIN 60
25 #define MINSPERHOUR 60
26 #define HOURSPERDAY 24
27 #define EPOCHWEEKDAY 0
28 #define DAYSPERWEEK 7
29 #define EPOCHYEAR 1601
30 #define DAYSPERNORMALYEAR 365
31 #define DAYSPERLEAPYEAR 366
32 #define MONSPERYEAR 12
33
34 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
35 static const int MonthLengths[2][MONSPERYEAR] =
36 {
37 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
38 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
39 };
40
41 static __inline int IsLeapYear(int Year)
42 {
43 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
44 }
45
46 static __inline void NormalizeTimeFields(CSHORT *FieldToNormalize,
47 CSHORT *CarryField,
48 int Modulus)
49 {
50 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
51 *CarryField = (CSHORT) (*CarryField + 1);
52 }
53
54 /* FUNCTIONS *****************************************************************/
55
56 VOID RtlTimeToTimeFields(PLARGE_INTEGER liTime,
57 PTIME_FIELDS TimeFields)
58 {
59 const int *Months;
60 int LeapSecondCorrections, SecondsInDay, CurYear;
61 int LeapYear, CurMonth, GMTOffset;
62 long int Days;
63 long long int Time = (long long int)liTime->QuadPart;
64
65 /* Extract millisecond from time and convert time into seconds */
66 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
67 Time = Time / TICKSPERSEC;
68
69 /* FIXME: Compute the number of leap second corrections here */
70 LeapSecondCorrections = 0;
71
72 /* FIXME: get the GMT offset here */
73 GMTOffset = 0;
74
75 /* Split the time into days and seconds within the day */
76 Days = Time / SECSPERDAY;
77 SecondsInDay = Time % SECSPERDAY;
78
79 /* Adjust the values for GMT and leap seconds */
80 SecondsInDay += (GMTOffset - LeapSecondCorrections);
81 while (SecondsInDay < 0)
82 {
83 SecondsInDay += SECSPERDAY;
84 Days--;
85 }
86 while (SecondsInDay >= SECSPERDAY)
87 {
88 SecondsInDay -= SECSPERDAY;
89 Days++;
90 }
91
92 /* compute time of day */
93 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
94 SecondsInDay = SecondsInDay % SECSPERHOUR;
95 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
96 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
97
98 /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
99
100 /* compute day of week */
101 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
102
103 /* compute year */
104 CurYear = EPOCHYEAR;
105 /* FIXME: handle calendar modifications */
106 while (1)
107 {
108 LeapYear = IsLeapYear(CurYear);
109 if (Days < (long) YearLengths[LeapYear])
110 {
111 break;
112 }
113 CurYear++;
114 Days = Days - (long) YearLengths[LeapYear];
115 }
116 TimeFields->Year = (CSHORT) CurYear;
117
118 /* Compute month of year */
119 Months = MonthLengths[LeapYear];
120 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
121 Days = Days - (long) Months[CurMonth];
122 TimeFields->Month = (CSHORT) (CurMonth + 1);
123 TimeFields->Day = (CSHORT) (Days + 1);
124 }
125
126 BOOLEAN RtlTimeFieldsToTime(PTIME_FIELDS tfTimeFields,
127 PLARGE_INTEGER Time)
128 {
129 int CurYear, CurMonth, MonthLength;
130 long long int rcTime;
131 TIME_FIELDS TimeFields = *tfTimeFields;
132
133 rcTime = 0;
134
135 /* FIXME: normalize the TIME_FIELDS structure here */
136 while (TimeFields.Second >= SECSPERMIN)
137 {
138 NormalizeTimeFields(&TimeFields.Second,
139 &TimeFields.Minute,
140 SECSPERMIN);
141 }
142 while (TimeFields.Minute >= MINSPERHOUR)
143 {
144 NormalizeTimeFields(&TimeFields.Minute,
145 &TimeFields.Hour,
146 MINSPERHOUR);
147 }
148 while (TimeFields.Hour >= HOURSPERDAY)
149 {
150 NormalizeTimeFields(&TimeFields.Hour,
151 &TimeFields.Day,
152 HOURSPERDAY);
153 }
154 MonthLength =
155 MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1];
156 while (TimeFields.Day > MonthLength)
157 {
158 NormalizeTimeFields(&TimeFields.Day,
159 &TimeFields.Month,
160 MonthLength);
161 }
162 while (TimeFields.Month > MONSPERYEAR)
163 {
164 NormalizeTimeFields(&TimeFields.Month,
165 &TimeFields.Year,
166 MONSPERYEAR);
167 }
168
169 /* FIXME: handle calendar corrections here */
170 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
171 {
172 rcTime += YearLengths[IsLeapYear(CurYear)];
173 }
174 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
175 {
176 rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
177 }
178 rcTime += TimeFields.Day - 1;
179 rcTime *= SECSPERDAY;
180 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN +
181 TimeFields.Second;
182 rcTime *= TICKSPERSEC;
183 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
184
185 /* FIXME: handle UTC bias here */
186 // rcTime += UTCBias * TICKSPERMIN;
187
188 *Time = *(LARGE_INTEGER *)&rcTime;
189
190 return TRUE;
191 }