Fixed some alignment issues (based on a patch by Gregg Mattinson).
[wine] / dlls / ntdll / time.c
1 /*
2  * Conversion between Time and TimeFields
3  *
4  * RtlTimeToTimeFields, RtlTimeFieldsToTime and defines are taken from ReactOS and 
5  * adapted to wine with special permissions of the author  
6  * Rex Jolliff (rex@lvcablemodem.com)
7  * 
8  * Copyright 1999 Juergen Schmied
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include <string.h>
26 #include <time.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29 #include "ntddk.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
33
34 #define TICKSPERSEC        10000000
35 #define TICKSPERMSEC       10000
36 #define SECSPERDAY         86400
37 #define SECSPERHOUR        3600
38 #define SECSPERMIN         60
39 #define MINSPERHOUR        60
40 #define HOURSPERDAY        24
41 #define EPOCHWEEKDAY       0
42 #define DAYSPERWEEK        7
43 #define EPOCHYEAR          1601
44 #define DAYSPERNORMALYEAR  365
45 #define DAYSPERLEAPYEAR    366
46 #define MONSPERYEAR        12
47
48 /* 1601 to 1970 is 369 years plus 89 leap days */
49 #define SECS_1601_TO_1970  ((369 * 365 + 89) * 86400ULL)
50 /* 1601 to 1980 is 379 years plus 91 leap days */
51 #define SECS_1601_to_1980  ((379 * 365 + 91) * 86400ULL)
52
53
54 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
55 static const int MonthLengths[2][MONSPERYEAR] =
56 {
57         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
58         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
59 };
60
61 static inline int IsLeapYear(int Year)
62 {
63         return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
64 }
65
66 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
67 {
68         *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
69         *CarryField = (CSHORT) (*CarryField + 1);
70 }
71
72 /******************************************************************************
73  *  RtlTimeToTimeFields         [NTDLL.@]
74  *
75  */
76
77 VOID WINAPI RtlTimeToTimeFields(
78         PLARGE_INTEGER liTime,
79         PTIME_FIELDS TimeFields)
80 {
81         const int *Months;
82         int LeapSecondCorrections, SecondsInDay, CurYear;
83         int LeapYear, CurMonth, GMTOffset;
84         long int Days;
85         long long int Time = *(long long int *)&liTime;
86
87         /* Extract millisecond from time and convert time into seconds */
88         TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
89         Time = Time / TICKSPERSEC;
90
91         /* FIXME: Compute the number of leap second corrections here */
92         LeapSecondCorrections = 0;
93
94         /* FIXME: get the GMT offset here */
95         GMTOffset = 0;
96
97         /* Split the time into days and seconds within the day */
98         Days = Time / SECSPERDAY;
99         SecondsInDay = Time % SECSPERDAY;
100
101         /* Adjust the values for GMT and leap seconds */
102         SecondsInDay += (GMTOffset - LeapSecondCorrections);
103         while (SecondsInDay < 0) 
104         { SecondsInDay += SECSPERDAY;
105           Days--;
106         }
107         while (SecondsInDay >= SECSPERDAY) 
108         { SecondsInDay -= SECSPERDAY;
109           Days++;
110         }
111
112         /* compute time of day */
113         TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
114         SecondsInDay = SecondsInDay % SECSPERHOUR;
115         TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
116         TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
117
118         /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
119
120         /* compute day of week */
121         TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
122
123         /* compute year */
124         CurYear = EPOCHYEAR;
125         /* FIXME: handle calendar modifications */
126         while (1)
127         { LeapYear = IsLeapYear(CurYear);
128           if (Days < (long) YearLengths[LeapYear])
129           { break;
130           }
131           CurYear++;
132           Days = Days - (long) YearLengths[LeapYear];
133         }
134         TimeFields->Year = (CSHORT) CurYear;
135
136         /* Compute month of year */
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  *  RtlTimeFieldsToTime         [NTDLL.@]
145  *
146  */
147 BOOLEAN WINAPI RtlTimeFieldsToTime(
148         PTIME_FIELDS tfTimeFields,
149         PLARGE_INTEGER Time)
150 {
151         int CurYear, CurMonth;
152         long long int rcTime;
153         TIME_FIELDS TimeFields = *tfTimeFields;
154
155         rcTime = 0;
156
157         /* FIXME: normalize the TIME_FIELDS structure here */
158         while (TimeFields.Second >= SECSPERMIN)
159         { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
160         }
161         while (TimeFields.Minute >= MINSPERHOUR)
162         { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
163         }
164         while (TimeFields.Hour >= HOURSPERDAY)
165         { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
166         }
167         while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
168         { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
169         }
170         while (TimeFields.Month > MONSPERYEAR)
171         { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
172         }
173
174         /* FIXME: handle calendar corrections here */
175         for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
176         { rcTime += YearLengths[IsLeapYear(CurYear)];
177         }
178         for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
179         { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
180         }
181         rcTime += TimeFields.Day - 1;
182         rcTime *= SECSPERDAY;
183         rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
184         rcTime *= TICKSPERSEC;
185         rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
186         *Time = *(LARGE_INTEGER *)&rcTime;
187
188         return TRUE;
189 }
190 /************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
191
192 /******************************************************************************
193  *  RtlSystemTimeToLocalTime    [NTDLL.@]
194  */
195 VOID WINAPI RtlSystemTimeToLocalTime(
196         IN  PLARGE_INTEGER SystemTime,
197         OUT PLARGE_INTEGER LocalTime)
198 {
199         FIXME("(%p, %p),stub!\n",SystemTime,LocalTime);
200
201         memcpy (LocalTime, SystemTime, sizeof (PLARGE_INTEGER));
202 }
203
204 /******************************************************************************
205  *  RtlTimeToSecondsSince1970           [NTDLL.@]
206  */
207 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const FILETIME *time, LPDWORD res )
208 {
209     ULONGLONG tmp = ((ULONGLONG)time->dwHighDateTime << 32) | time->dwLowDateTime;
210     tmp = RtlLargeIntegerDivide( tmp, 10000000LL, NULL );
211     tmp -= SECS_1601_TO_1970;
212     if (tmp > 0xffffffff) return FALSE;
213     *res = (DWORD)tmp;
214     return TRUE;
215 }
216
217 /******************************************************************************
218  *  RtlTimeToSecondsSince1980           [NTDLL.@]
219  */
220 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const FILETIME *time, LPDWORD res )
221 {
222     ULONGLONG tmp = ((ULONGLONG)time->dwHighDateTime << 32) | time->dwLowDateTime;
223     tmp = RtlLargeIntegerDivide( tmp, 10000000LL, NULL );
224     tmp -= SECS_1601_to_1980;
225     if (tmp > 0xffffffff) return FALSE;
226     *res = (DWORD)tmp;
227     return TRUE;
228 }
229
230 /******************************************************************************
231  *  RtlSecondsSince1970ToTime           [NTDLL.@]
232  */
233 void WINAPI RtlSecondsSince1970ToTime( DWORD time, FILETIME *res )
234 {
235     ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
236     res->dwLowDateTime  = (DWORD)secs;
237     res->dwHighDateTime = (DWORD)(secs >> 32);
238 }
239
240 /******************************************************************************
241  *  RtlSecondsSince1980ToTime           [NTDLL.@]
242  */
243 void WINAPI RtlSecondsSince1980ToTime( DWORD time, FILETIME *res )
244 {
245     ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
246     res->dwLowDateTime  = (DWORD)secs;
247     res->dwHighDateTime = (DWORD)(secs >> 32);
248 }
249
250 /******************************************************************************
251  * RtlTimeToElapsedTimeFields [NTDLL.@]
252  * FIXME: prototype guessed
253  */
254 VOID WINAPI RtlTimeToElapsedTimeFields(
255         PLARGE_INTEGER liTime,
256         PTIME_FIELDS TimeFields)
257 {
258         FIXME("(%p,%p): stub\n",liTime,TimeFields);
259 }
260
261 /***********************************************************************
262  *      NtQuerySystemTime   (NTDLL.@)
263  *      ZwQuerySystemTime   (NTDLL.@)
264  */
265 void WINAPI NtQuerySystemTime( LARGE_INTEGER *time )
266 {
267     LONGLONG secs;
268     struct timeval now;
269
270     gettimeofday( &now, 0 );
271     secs = now.tv_sec + SECS_1601_TO_1970;
272     time->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 ) + now.tv_usec * 10;
273 }