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