update for HEAD-2003091401
[reactos.git] / ntoskrnl / rtl / time.c
1 /* $Id$
2  *
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)
8  * UPDATE HISTORY:
9  *                  Created 22/05/98
10  *   08/03/98  RJJ  Implemented these functions
11  */
12
13 /* INCLUDES *****************************************************************/
14
15 #include <ddk/ntddk.h>
16
17 #define NDEBUG
18 #include <internal/debug.h>
19
20 #define TICKSPERMIN        600000000
21 #define TICKSPERSEC        10000000
22 #define TICKSPERMSEC       10000
23 #define SECSPERDAY         86400
24 #define SECSPERHOUR        3600
25 #define SECSPERMIN         60
26 #define MINSPERHOUR        60
27 #define HOURSPERDAY        24
28 #define EPOCHWEEKDAY       1
29 #define DAYSPERWEEK        7
30 #define EPOCHYEAR          1601
31 #define DAYSPERNORMALYEAR  365
32 #define DAYSPERLEAPYEAR    366
33 #define MONSPERYEAR        12
34
35 #define TICKSTO1970         0x019db1ded53e8000LL
36 #define TICKSTO1980         0x01a8e79fe1d58000LL
37
38
39 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
40 static const int MonthLengths[2][MONSPERYEAR] =
41 {
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 }
44 };
45
46 static __inline int IsLeapYear(int Year)
47 {
48   return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
49 }
50
51 static __inline void NormalizeTimeFields(CSHORT *FieldToNormalize,
52                                          CSHORT *CarryField,
53                                          int Modulus)
54 {
55   *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
56   *CarryField = (CSHORT) (*CarryField + 1);
57 }
58
59 /* FUNCTIONS *****************************************************************/
60
61 /*
62  * @unimplemented
63  */
64 VOID
65 STDCALL
66 RtlTimeToTimeFields(
67         PLARGE_INTEGER liTime,
68         PTIME_FIELDS TimeFields)
69 {
70   const int *Months;
71   int LeapSecondCorrections, SecondsInDay, CurYear;
72   int LeapYear, CurMonth, GMTOffset;
73   long int Days;
74   long long int Time = (long long int)liTime->QuadPart;
75
76     /* Extract millisecond from time and convert time into seconds */
77   TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
78   Time = Time / TICKSPERSEC;
79
80     /* FIXME: Compute the number of leap second corrections here */
81   LeapSecondCorrections = 0;
82
83     /* FIXME: get the GMT offset here */
84   GMTOffset = 0;
85
86     /* Split the time into days and seconds within the day */
87   Days = Time / SECSPERDAY;
88   SecondsInDay = Time % SECSPERDAY;
89
90     /* Adjust the values for GMT and leap seconds */
91   SecondsInDay += (GMTOffset - LeapSecondCorrections);
92   while (SecondsInDay < 0) 
93     {
94       SecondsInDay += SECSPERDAY;
95       Days--;
96     }
97   while (SecondsInDay >= SECSPERDAY) 
98     {
99       SecondsInDay -= SECSPERDAY;
100       Days++;
101     }
102
103     /* compute time of day */
104   TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
105   SecondsInDay = SecondsInDay % SECSPERHOUR;
106   TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
107   TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
108
109     /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
110
111     /* compute day of week */
112   TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
113
114     /* compute year */
115   CurYear = EPOCHYEAR;
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;
120   CurYear++;
121   Days -= EPOCHYEAR - 1 - (EPOCHYEAR -1) / 4 + (EPOCHYEAR -1) / 100 - (EPOCHYEAR - 1) / 400;
122     /* FIXME: handle calendar modifications */
123   while (1)
124     {
125       LeapYear = IsLeapYear(CurYear);
126       if (Days < (long) YearLengths[LeapYear])
127         {
128           break;
129         }
130       CurYear++;
131       Days = Days - (long) YearLengths[LeapYear];
132     }
133   TimeFields->Year = (CSHORT) CurYear;
134
135     /* Compute month of year */
136   LeapYear = IsLeapYear(CurYear);
137   Months = MonthLengths[LeapYear];
138   for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
139     Days = Days - (long) Months[CurMonth];
140   TimeFields->Month = (CSHORT) (CurMonth + 1);
141   TimeFields->Day = (CSHORT) (Days + 1);
142 }
143
144 /*
145  * @unimplemented
146  */
147 BOOLEAN
148 STDCALL
149 RtlTimeFieldsToTime(
150         PTIME_FIELDS tfTimeFields,
151         PLARGE_INTEGER Time)
152 {
153   int CurMonth;
154   long long int rcTime;
155   TIME_FIELDS TimeFields = *tfTimeFields;
156   const int *Months;
157
158   rcTime = 0;
159   
160   /* Normalize the month value, because we don't know the length for month -1, 0, 13, 14, ... */  
161   if (TimeFields.Month < 1 || TimeFields.Month > 12)
162   {
163      TimeFields.Year += (TimeFields.Month - 1) / MONSPERYEAR;
164      TimeFields.Month = ((TimeFields.Month - 1) % MONSPERYEAR) + 1;
165      if (TimeFields.Month < 1)
166      {
167         TimeFields.Year--;
168         TimeFields.Month += MONSPERYEAR;
169      }
170   }
171     /* FIXME: handle calendar corrections here */
172
173   rcTime += (TimeFields.Year - EPOCHYEAR) * DAYSPERNORMALYEAR;
174   /* Adjust leap years */
175   rcTime += (TimeFields.Year - 1)/ 4 - (TimeFields.Year - 1) / 100 + (TimeFields.Year - 1) / 400;
176   rcTime -= EPOCHYEAR / 4 - EPOCHYEAR / 100 + EPOCHYEAR / 400;
177
178     /* FIXME: handle calendar corrections here */
179   Months = MonthLengths[IsLeapYear(TimeFields.Year)];
180   for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
181     {
182       rcTime += Months[CurMonth - 1];
183     }
184   rcTime += TimeFields.Day - 1;
185   rcTime *= SECSPERDAY;
186   rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN +
187             TimeFields.Second;
188   rcTime *= TICKSPERSEC;
189   rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
190
191     /* FIXME: handle UTC bias here */
192 //  rcTime += UTCBias * TICKSPERMIN;
193
194   *Time = *(LARGE_INTEGER *)&rcTime;
195
196   return TRUE;
197 }
198
199
200 /*
201  * @implemented
202  */
203 VOID
204 STDCALL
205 RtlSecondsSince1970ToTime(
206         ULONG SecondsSince1970,
207         PLARGE_INTEGER Time)
208 {
209    LONGLONG llTime;
210
211    llTime = (SecondsSince1970 * TICKSPERSEC) + TICKSTO1970;
212
213    *Time = *(LARGE_INTEGER *)&llTime;
214 }
215
216
217 /*
218  * @implemented
219  */
220 VOID
221 STDCALL
222 RtlSecondsSince1980ToTime(
223         ULONG SecondsSince1980,
224         PLARGE_INTEGER Time)
225 {
226    LONGLONG llTime;
227
228    llTime = (SecondsSince1980 * TICKSPERSEC) + TICKSTO1980;
229
230    *Time = *(LARGE_INTEGER *)&llTime;
231 }
232
233
234 /*
235  * @implemented
236  */
237 BOOLEAN
238 STDCALL
239 RtlTimeToSecondsSince1970(
240         PLARGE_INTEGER Time,
241         PULONG SecondsSince1970)
242 {
243    LARGE_INTEGER liTime;
244
245    liTime.QuadPart = Time->QuadPart - TICKSTO1970;
246    liTime.QuadPart = liTime.QuadPart / TICKSPERSEC;
247
248    if (liTime.u.HighPart != 0)
249       return FALSE;
250
251    *SecondsSince1970 = liTime.u.LowPart;
252
253    return TRUE;
254 }
255
256
257 /*
258  * @implemented
259  */
260 BOOLEAN
261 STDCALL
262 RtlTimeToSecondsSince1980(
263         PLARGE_INTEGER Time,
264         PULONG SecondsSince1980)
265 {
266    LARGE_INTEGER liTime;
267
268    liTime.QuadPart = Time->QuadPart - TICKSTO1980;
269    liTime.QuadPart = liTime.QuadPart / TICKSPERSEC;
270
271    if (liTime.u.HighPart != 0)
272       return FALSE;
273
274    *SecondsSince1980 = liTime.u.LowPart;
275
276    return TRUE;
277 }
278
279 /* EOF */