Added preferences to dinput drivers (so we can have two joystick
[wine/hacks.git] / dlls / ntdll / time.c
blob1f7ad4e582a8ea5859bc8c2cd417a597f9334577
1 /*
2 * Conversion between Time and TimeFields
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 */
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"
18 DEFAULT_DEBUG_CHANNEL(ntdll);
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
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)
40 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
41 static const int MonthLengths[2][MONSPERYEAR] =
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 }
47 static inline int IsLeapYear(int Year)
49 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
52 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
54 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
55 *CarryField = (CSHORT) (*CarryField + 1);
58 /******************************************************************************
59 * RtlTimeToTimeFields [NTDLL.265]
63 VOID WINAPI RtlTimeToTimeFields(
64 PLARGE_INTEGER liTime,
65 PTIME_FIELDS TimeFields)
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;
73 /* Extract millisecond from time and convert time into seconds */
74 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
75 Time = Time / TICKSPERSEC;
77 /* FIXME: Compute the number of leap second corrections here */
78 LeapSecondCorrections = 0;
80 /* FIXME: get the GMT offset here */
81 GMTOffset = 0;
83 /* Split the time into days and seconds within the day */
84 Days = Time / SECSPERDAY;
85 SecondsInDay = Time % SECSPERDAY;
87 /* Adjust the values for GMT and leap seconds */
88 SecondsInDay += (GMTOffset - LeapSecondCorrections);
89 while (SecondsInDay < 0)
90 { SecondsInDay += SECSPERDAY;
91 Days--;
93 while (SecondsInDay >= SECSPERDAY)
94 { SecondsInDay -= SECSPERDAY;
95 Days++;
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);
104 /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
106 /* compute day of week */
107 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
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;
117 CurYear++;
118 Days = Days - (long) YearLengths[LeapYear];
120 TimeFields->Year = (CSHORT) CurYear;
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);
129 /******************************************************************************
130 * RtlTimeFieldsToTime [NTDLL.265]
133 BOOLEAN WINAPI RtlTimeFieldsToTime(
134 PTIME_FIELDS tfTimeFields,
135 PLARGE_INTEGER Time)
137 int CurYear, CurMonth;
138 long long int rcTime;
139 TIME_FIELDS TimeFields = *tfTimeFields;
141 rcTime = 0;
143 /* FIXME: normalize the TIME_FIELDS structure here */
144 while (TimeFields.Second >= SECSPERMIN)
145 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
147 while (TimeFields.Minute >= MINSPERHOUR)
148 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
150 while (TimeFields.Hour >= HOURSPERDAY)
151 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
153 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
154 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
156 while (TimeFields.Month > MONSPERYEAR)
157 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
160 /* FIXME: handle calendar corrections here */
161 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
162 { rcTime += YearLengths[IsLeapYear(CurYear)];
164 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
165 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
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;
174 return TRUE;
176 /************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
178 /******************************************************************************
179 * RtlSystemTimeToLocalTime [NTDLL]
181 VOID WINAPI RtlSystemTimeToLocalTime(
182 IN PLARGE_INTEGER SystemTime,
183 OUT PLARGE_INTEGER LocalTime)
185 FIXME("(%p, %p),stub!\n",SystemTime,LocalTime);
187 memcpy (LocalTime, SystemTime, sizeof (PLARGE_INTEGER));
190 /******************************************************************************
191 * RtlTimeToSecondsSince1970 [NTDLL]
193 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const FILETIME *time, LPDWORD res )
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;
202 /******************************************************************************
203 * RtlTimeToSecondsSince1980 [NTDLL]
205 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const FILETIME *time, LPDWORD res )
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;
214 /******************************************************************************
215 * RtlSecondsSince1970ToTime [NTDLL]
217 void WINAPI RtlSecondsSince1970ToTime( DWORD time, FILETIME *res )
219 LONGLONG secs = time + SECS_1601_TO_1970;
220 ((LARGE_INTEGER *)res)->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 );
223 /******************************************************************************
224 * RtlSecondsSince1980ToTime [NTDLL]
226 void WINAPI RtlSecondsSince1980ToTime( DWORD time, FILETIME *res )
228 LONGLONG secs = time + SECS_1601_to_1980;
229 ((LARGE_INTEGER *)res)->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 );
232 /******************************************************************************
233 * RtlTimeToElapsedTimeFields [NTDLL.502]
234 * FIXME: prototype guessed
236 VOID WINAPI RtlTimeToElapsedTimeFields(
237 PLARGE_INTEGER liTime,
238 PTIME_FIELDS TimeFields)
240 FIXME("(%p,%p): stub\n",liTime,TimeFields);
243 /***********************************************************************
244 * NtQuerySystemTime (NTDLL.@)
246 void WINAPI NtQuerySystemTime( LARGE_INTEGER *time )
248 LONGLONG secs;
249 struct timeval now;
251 gettimeofday( &now, 0 );
252 secs = now.tv_sec + SECS_1601_TO_1970;
253 time->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 ) + now.tv_usec * 10;