update for HEAD-2003021201
[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 VOID
62 STDCALL
63 RtlTimeToTimeFields(
64         PLARGE_INTEGER liTime,
65         PTIME_FIELDS TimeFields)
66 {
67   const int *Months;
68   int LeapSecondCorrections, SecondsInDay, CurYear;
69   int LeapYear, CurMonth, GMTOffset;
70   long int Days;
71   long long int Time = (long long int)liTime->QuadPart;
72
73     /* Extract millisecond from time and convert time into seconds */
74   TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
75   Time = Time / TICKSPERSEC;
76
77     /* FIXME: Compute the number of leap second corrections here */
78   LeapSecondCorrections = 0;
79
80     /* FIXME: get the GMT offset here */
81   GMTOffset = 0;
82
83     /* Split the time into days and seconds within the day */
84   Days = Time / SECSPERDAY;
85   SecondsInDay = Time % SECSPERDAY;
86
87     /* Adjust the values for GMT and leap seconds */
88   SecondsInDay += (GMTOffset - LeapSecondCorrections);
89   while (SecondsInDay < 0) 
90     {
91       SecondsInDay += SECSPERDAY;
92       Days--;
93     }
94   while (SecondsInDay >= SECSPERDAY) 
95     {
96       SecondsInDay -= SECSPERDAY;
97       Days++;
98     }
99
100     /* compute time of day */
101   TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
102   SecondsInDay = SecondsInDay % SECSPERHOUR;
103   TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
104   TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
105
106     /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
107
108     /* compute day of week */
109   TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
110
111     /* compute year */
112   CurYear = EPOCHYEAR;
113   CurYear += Days / DAYSPERLEAPYEAR;
114   Days -= (CurYear - EPOCHYEAR) * DAYSPERLEAPYEAR;
115   CurYear--; /* The next calculation needs CurYear - 1 */
116   Days += CurYear - CurYear / 4 + CurYear / 100 - CurYear / 400;
117   CurYear++;
118   Days -= EPOCHYEAR - 1 - (EPOCHYEAR -1) / 4 + (EPOCHYEAR -1) / 100 - (EPOCHYEAR - 1) / 400;
119     /* FIXME: handle calendar modifications */
120   while (1)
121     {
122       LeapYear = IsLeapYear(CurYear);
123       if (Days < (long) YearLengths[LeapYear])
124         {
125           break;
126         }
127       CurYear++;
128       Days = Days - (long) YearLengths[LeapYear];
129     }
130   TimeFields->Year = (CSHORT) CurYear;
131
132     /* Compute month of year */
133   LeapYear = IsLeapYear(CurYear);
134   Months = MonthLengths[LeapYear];
135   for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
136     Days = Days - (long) Months[CurMonth];
137   TimeFields->Month = (CSHORT) (CurMonth + 1);
138   TimeFields->Day = (CSHORT) (Days + 1);
139 }
140
141 BOOLEAN
142 STDCALL
143 RtlTimeFieldsToTime(
144         PTIME_FIELDS tfTimeFields,
145         PLARGE_INTEGER Time)
146 {
147   int CurMonth;
148   long long int rcTime;
149   TIME_FIELDS TimeFields = *tfTimeFields;
150   const int *Months;
151
152   rcTime = 0;
153   
154   /* Normalize the month value, because we don't know the length for month -1, 0, 13, 14, ... */  
155   if (TimeFields.Month < 1 || TimeFields.Month > 12)
156   {
157      TimeFields.Year += (TimeFields.Month - 1) / MONSPERYEAR;
158      TimeFields.Month = ((TimeFields.Month - 1) % MONSPERYEAR) + 1;
159      if (TimeFields.Month < 1)
160      {
161         TimeFields.Year--;
162         TimeFields.Month += MONSPERYEAR;
163      }
164   }
165     /* FIXME: handle calendar corrections here */
166
167   rcTime += (TimeFields.Year - EPOCHYEAR) * DAYSPERNORMALYEAR;
168   /* Adjust leap years */
169   rcTime += (TimeFields.Year - 1)/ 4 - (TimeFields.Year - 1) / 100 + (TimeFields.Year - 1) / 400;
170   rcTime -= EPOCHYEAR / 4 - EPOCHYEAR / 100 + EPOCHYEAR / 400;
171
172     /* FIXME: handle calendar corrections here */
173   Months = MonthLengths[IsLeapYear(TimeFields.Year)];
174   for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
175     {
176       rcTime += Months[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 }
192
193
194 VOID
195 STDCALL
196 RtlSecondsSince1970ToTime(
197         ULONG SecondsSince1970,
198         PLARGE_INTEGER Time)
199 {
200    LONGLONG llTime;
201
202    llTime = (SecondsSince1970 * TICKSPERSEC) + TICKSTO1970;
203
204    *Time = *(LARGE_INTEGER *)&llTime;
205 }
206
207
208 VOID
209 STDCALL
210 RtlSecondsSince1980ToTime(
211         ULONG SecondsSince1980,
212         PLARGE_INTEGER Time)
213 {
214    LONGLONG llTime;
215
216    llTime = (SecondsSince1980 * TICKSPERSEC) + TICKSTO1980;
217
218    *Time = *(LARGE_INTEGER *)&llTime;
219 }
220
221
222 BOOLEAN
223 STDCALL
224 RtlTimeToSecondsSince1970(
225         PLARGE_INTEGER Time,
226         PULONG SecondsSince1970)
227 {
228    LARGE_INTEGER liTime;
229
230    liTime.QuadPart = Time->QuadPart - TICKSTO1970;
231    liTime.QuadPart = liTime.QuadPart / TICKSPERSEC;
232
233    if (liTime.u.HighPart != 0)
234       return FALSE;
235
236    *SecondsSince1970 = liTime.u.LowPart;
237
238    return TRUE;
239 }
240
241
242 BOOLEAN
243 STDCALL
244 RtlTimeToSecondsSince1980(
245         PLARGE_INTEGER Time,
246         PULONG SecondsSince1980)
247 {
248    LARGE_INTEGER liTime;
249
250    liTime.QuadPart = Time->QuadPart - TICKSTO1980;
251    liTime.QuadPart = liTime.QuadPart / TICKSPERSEC;
252
253    if (liTime.u.HighPart != 0)
254       return FALSE;
255
256    *SecondsSince1980 = liTime.u.LowPart;
257
258    return TRUE;
259 }
260
261 /* EOF */