Fixed a few problems with DCX_* flags handling.
[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  * 
9  */
10
11 #include <string.h>
12 #include <time.h>
13 #include <sys/time.h>
14 #include <unistd.h>
15 #include "ntddk.h"
16 #include "debugtools.h"
17
18 DEFAULT_DEBUG_CHANNEL(ntdll);
19
20 #define TICKSPERSEC        10000000
21 #define TICKSPERMSEC       10000
22 #define SECSPERDAY         86400
23 #define SECSPERHOUR        3600
24 #define SECSPERMIN         60
25 #define MINSPERHOUR        60
26 #define HOURSPERDAY        24
27 #define EPOCHWEEKDAY       0
28 #define DAYSPERWEEK        7
29 #define EPOCHYEAR          1601
30 #define DAYSPERNORMALYEAR  365
31 #define DAYSPERLEAPYEAR    366
32 #define MONSPERYEAR        12
33
34 /* 1601 to 1970 is 369 years plus 89 leap days */
35 #define SECS_1601_TO_1970  ((369 * 365 + 89) * 86400ULL)
36 /* 1601 to 1980 is 379 years plus 91 leap days */
37 #define SECS_1601_to_1980  ((379 * 365 + 91) * 86400ULL)
38
39
40 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
41 static const int MonthLengths[2][MONSPERYEAR] =
42 {
43         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
44         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
45 };
46
47 static inline int IsLeapYear(int Year)
48 {
49         return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
50 }
51
52 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
53 {
54         *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
55         *CarryField = (CSHORT) (*CarryField + 1);
56 }
57
58 /******************************************************************************
59  *  RtlTimeToTimeFields         [NTDLL.@]
60  *
61  */
62
63 VOID WINAPI 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;
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         { SecondsInDay += SECSPERDAY;
91           Days--;
92         }
93         while (SecondsInDay >= SECSPERDAY) 
94         { SecondsInDay -= SECSPERDAY;
95           Days++;
96         }
97
98         /* compute time of day */
99         TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
100         SecondsInDay = SecondsInDay % SECSPERHOUR;
101         TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
102         TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
103
104         /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
105
106         /* compute day of week */
107         TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
108
109         /* compute year */
110         CurYear = EPOCHYEAR;
111         /* FIXME: handle calendar modifications */
112         while (1)
113         { LeapYear = IsLeapYear(CurYear);
114           if (Days < (long) YearLengths[LeapYear])
115           { break;
116           }
117           CurYear++;
118           Days = Days - (long) YearLengths[LeapYear];
119         }
120         TimeFields->Year = (CSHORT) CurYear;
121
122         /* Compute month of year */
123         Months = MonthLengths[LeapYear];
124         for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
125           Days = Days - (long) Months[CurMonth];
126         TimeFields->Month = (CSHORT) (CurMonth + 1);
127         TimeFields->Day = (CSHORT) (Days + 1);
128 }
129 /******************************************************************************
130  *  RtlTimeFieldsToTime         [NTDLL.@]
131  *
132  */
133 BOOLEAN WINAPI RtlTimeFieldsToTime(
134         PTIME_FIELDS tfTimeFields,
135         PLARGE_INTEGER Time)
136 {
137         int CurYear, CurMonth;
138         long long int rcTime;
139         TIME_FIELDS TimeFields = *tfTimeFields;
140
141         rcTime = 0;
142
143         /* FIXME: normalize the TIME_FIELDS structure here */
144         while (TimeFields.Second >= SECSPERMIN)
145         { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
146         }
147         while (TimeFields.Minute >= MINSPERHOUR)
148         { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
149         }
150         while (TimeFields.Hour >= HOURSPERDAY)
151         { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
152         }
153         while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
154         { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
155         }
156         while (TimeFields.Month > MONSPERYEAR)
157         { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
158         }
159
160         /* FIXME: handle calendar corrections here */
161         for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
162         { rcTime += YearLengths[IsLeapYear(CurYear)];
163         }
164         for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
165         { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
166         }
167         rcTime += TimeFields.Day - 1;
168         rcTime *= SECSPERDAY;
169         rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
170         rcTime *= TICKSPERSEC;
171         rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
172         *Time = *(LARGE_INTEGER *)&rcTime;
173
174         return TRUE;
175 }
176 /************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
177
178 /******************************************************************************
179  *  RtlSystemTimeToLocalTime    [NTDLL.@]
180  */
181 VOID WINAPI RtlSystemTimeToLocalTime(
182         IN  PLARGE_INTEGER SystemTime,
183         OUT PLARGE_INTEGER LocalTime)
184 {
185         FIXME("(%p, %p),stub!\n",SystemTime,LocalTime);
186
187         memcpy (LocalTime, SystemTime, sizeof (PLARGE_INTEGER));
188 }
189
190 /******************************************************************************
191  *  RtlTimeToSecondsSince1970           [NTDLL.@]
192  */
193 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const FILETIME *time, LPDWORD res )
194 {
195     ULONGLONG tmp = RtlLargeIntegerDivide( ((LARGE_INTEGER *)time)->QuadPart, 10000000LL, NULL );
196     tmp -= SECS_1601_TO_1970;
197     if (tmp > 0xffffffff) return FALSE;
198     *res = (DWORD)tmp;
199     return TRUE;
200 }
201
202 /******************************************************************************
203  *  RtlTimeToSecondsSince1980           [NTDLL.@]
204  */
205 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const FILETIME *time, LPDWORD res )
206 {
207     ULONGLONG tmp = RtlLargeIntegerDivide( ((LARGE_INTEGER *)time)->QuadPart, 10000000LL, NULL );
208     tmp -= SECS_1601_to_1980;
209     if (tmp > 0xffffffff) return FALSE;
210     *res = (DWORD)tmp;
211     return TRUE;
212 }
213
214 /******************************************************************************
215  *  RtlSecondsSince1970ToTime           [NTDLL.@]
216  */
217 void WINAPI RtlSecondsSince1970ToTime( DWORD time, FILETIME *res )
218 {
219     LONGLONG secs = time + SECS_1601_TO_1970;
220     ((LARGE_INTEGER *)res)->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 );
221 }
222
223 /******************************************************************************
224  *  RtlSecondsSince1980ToTime           [NTDLL.@]
225  */
226 void WINAPI RtlSecondsSince1980ToTime( DWORD time, FILETIME *res )
227 {
228     LONGLONG secs = time + SECS_1601_to_1980;
229     ((LARGE_INTEGER *)res)->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 );
230 }
231
232 /******************************************************************************
233  * RtlTimeToElapsedTimeFields [NTDLL.@]
234  * FIXME: prototype guessed
235  */
236 VOID WINAPI RtlTimeToElapsedTimeFields(
237         PLARGE_INTEGER liTime,
238         PTIME_FIELDS TimeFields)
239 {
240         FIXME("(%p,%p): stub\n",liTime,TimeFields);
241 }
242
243 /***********************************************************************
244  *      NtQuerySystemTime   (NTDLL.@)
245  *      ZwQuerySystemTime   (NTDLL.@)
246  */
247 void WINAPI NtQuerySystemTime( LARGE_INTEGER *time )
248 {
249     LONGLONG secs;
250     struct timeval now;
251
252     gettimeofday( &now, 0 );
253     secs = now.tv_sec + SECS_1601_TO_1970;
254     time->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 ) + now.tv_usec * 10;
255 }