3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: kernel/rtl/time.c
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>
18 #include <internal/debug.h>
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
35 #define TICKSTO1970 0x019db1ded53e8000
36 #define TICKSTO1980 0x01a8e79fe1d58000
39 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
40 static const int MonthLengths[2][MONSPERYEAR] =
42 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
43 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
46 static __inline int IsLeapYear(int Year)
48 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
51 static __inline void NormalizeTimeFields(CSHORT *FieldToNormalize,
55 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
56 *CarryField = (CSHORT) (*CarryField + 1);
59 /* FUNCTIONS *****************************************************************/
65 PLARGE_INTEGER liTime,
66 PTIME_FIELDS TimeFields
70 int LeapSecondCorrections, SecondsInDay, CurYear;
71 int LeapYear, CurMonth, GMTOffset;
73 long long int Time = (long long int)liTime->QuadPart;
75 /* Extract millisecond from time and convert time into seconds */
76 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
77 Time = Time / TICKSPERSEC;
79 /* FIXME: Compute the number of leap second corrections here */
80 LeapSecondCorrections = 0;
82 /* FIXME: get the GMT offset here */
85 /* Split the time into days and seconds within the day */
86 Days = Time / SECSPERDAY;
87 SecondsInDay = Time % SECSPERDAY;
89 /* Adjust the values for GMT and leap seconds */
90 SecondsInDay += (GMTOffset - LeapSecondCorrections);
91 while (SecondsInDay < 0)
93 SecondsInDay += SECSPERDAY;
96 while (SecondsInDay >= SECSPERDAY)
98 SecondsInDay -= SECSPERDAY;
102 /* compute time of day */
103 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
104 SecondsInDay = SecondsInDay % SECSPERHOUR;
105 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
106 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
108 /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
110 /* compute day of week */
111 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
115 /* FIXME: handle calendar modifications */
116 CurYear += Days / DAYSPERLEAPYEAR;
117 Days -= (CurYear - EPOCHYEAR) * DAYSPERLEAPYEAR;
118 CurYear--; /* The next calculation needs CurYear - 1 */
119 Days += CurYear - CurYear / 4 + CurYear / 100 - CurYear / 400;
121 Days -= EPOCHYEAR - 1 - (EPOCHYEAR -1) / 4 + (EPOCHYEAR -1) / 100 - (EPOCHYEAR - 1) / 400;
124 LeapYear = IsLeapYear(CurYear);
125 if (Days < (long) YearLengths[LeapYear])
130 Days = Days - (long) YearLengths[LeapYear];
132 TimeFields->Year = (CSHORT) CurYear;
134 /* Compute month of year */
135 LeapYear = IsLeapYear(CurYear);
136 Months = MonthLengths[LeapYear];
137 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
138 Days = Days - (long) Months[CurMonth];
139 TimeFields->Month = (CSHORT) (CurMonth + 1);
140 TimeFields->Day = (CSHORT) (Days + 1);
146 RtlTimeFieldsToTime (
147 PTIME_FIELDS tfTimeFields,
152 long long int rcTime;
153 TIME_FIELDS TimeFields = *tfTimeFields;
158 /* Normalize the month value, because we don't know the length for month -1, 0, 13, 14, ... */
159 if (TimeFields.Month < 1 || TimeFields.Month > 12)
161 TimeFields.Year += (TimeFields.Month - 1) / MONSPERYEAR;
162 TimeFields.Month = ((TimeFields.Month - 1) % MONSPERYEAR) + 1;
163 if (TimeFields.Month < 1)
166 TimeFields.Month += MONSPERYEAR;
169 /* FIXME: handle calendar corrections here */
171 rcTime += (TimeFields.Year - EPOCHYEAR) * DAYSPERNORMALYEAR;
172 /* Adjust leap years */
173 rcTime += (TimeFields.Year - 1)/ 4 - (TimeFields.Year - 1) / 100 + (TimeFields.Year - 1) / 400;
174 rcTime -= EPOCHYEAR / 4 - EPOCHYEAR / 100 + EPOCHYEAR / 400;
176 /* FIXME: handle calendar corrections here */
177 Months = MonthLengths[IsLeapYear(TimeFields.Year)];
178 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
180 rcTime += Months[CurMonth - 1];
182 rcTime += TimeFields.Day - 1;
183 rcTime *= SECSPERDAY;
184 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN +
186 rcTime *= TICKSPERSEC;
187 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
189 /* FIXME: handle UTC bias here */
190 // rcTime += UTCBias * TICKSPERMIN;
192 *Time = *(LARGE_INTEGER *)&rcTime;
200 RtlSecondsSince1970ToTime (
201 ULONG SecondsSince1970,
207 llTime = (SecondsSince1970 * TICKSPERSEC) + TICKSTO1970;
209 *Time = *(LARGE_INTEGER *)&llTime;
216 RtlSecondsSince1980ToTime (
217 ULONG SecondsSince1980,
223 llTime = (SecondsSince1980 * TICKSPERSEC) + TICKSTO1980;
225 *Time = *(LARGE_INTEGER *)&llTime;
228 #endif /* LIBCAPTIVE */
232 RtlTimeToSecondsSince1970 (
234 PULONG SecondsSince1970
237 LARGE_INTEGER liTime;
239 liTime.QuadPart = Time->QuadPart - TICKSTO1970;
240 liTime.QuadPart = liTime.QuadPart / TICKSPERSEC;
242 if (liTime.u.HighPart != 0)
245 *SecondsSince1970 = liTime.u.LowPart;
254 RtlTimeToSecondsSince1980 (
256 PULONG SecondsSince1980
259 LARGE_INTEGER liTime;
261 liTime.QuadPart = Time->QuadPart - TICKSTO1980;
262 liTime.QuadPart = liTime.QuadPart / TICKSPERSEC;
264 if (liTime.u.HighPart != 0)
267 *SecondsSince1980 = liTime.u.LowPart;
272 #endif /* LIBCAPTIVE */