Fixed some alignment issues (based on a patch by Gregg Mattinson).
[wine.git] / dlls / ntdll / time.c
blob3dd48a2426de4fd1370687416cd0dbc5edf9fe37
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 * Copyright 1999 Juergen Schmied
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.
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.
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
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"
32 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
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
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)
54 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
55 static const int MonthLengths[2][MONSPERYEAR] =
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 }
61 static inline int IsLeapYear(int Year)
63 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
66 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
68 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
69 *CarryField = (CSHORT) (*CarryField + 1);
72 /******************************************************************************
73 * RtlTimeToTimeFields [NTDLL.@]
77 VOID WINAPI RtlTimeToTimeFields(
78 PLARGE_INTEGER liTime,
79 PTIME_FIELDS TimeFields)
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;
87 /* Extract millisecond from time and convert time into seconds */
88 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
89 Time = Time / TICKSPERSEC;
91 /* FIXME: Compute the number of leap second corrections here */
92 LeapSecondCorrections = 0;
94 /* FIXME: get the GMT offset here */
95 GMTOffset = 0;
97 /* Split the time into days and seconds within the day */
98 Days = Time / SECSPERDAY;
99 SecondsInDay = Time % SECSPERDAY;
101 /* Adjust the values for GMT and leap seconds */
102 SecondsInDay += (GMTOffset - LeapSecondCorrections);
103 while (SecondsInDay < 0)
104 { SecondsInDay += SECSPERDAY;
105 Days--;
107 while (SecondsInDay >= SECSPERDAY)
108 { SecondsInDay -= SECSPERDAY;
109 Days++;
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);
118 /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
120 /* compute day of week */
121 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
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;
131 CurYear++;
132 Days = Days - (long) YearLengths[LeapYear];
134 TimeFields->Year = (CSHORT) CurYear;
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);
143 /******************************************************************************
144 * RtlTimeFieldsToTime [NTDLL.@]
147 BOOLEAN WINAPI RtlTimeFieldsToTime(
148 PTIME_FIELDS tfTimeFields,
149 PLARGE_INTEGER Time)
151 int CurYear, CurMonth;
152 long long int rcTime;
153 TIME_FIELDS TimeFields = *tfTimeFields;
155 rcTime = 0;
157 /* FIXME: normalize the TIME_FIELDS structure here */
158 while (TimeFields.Second >= SECSPERMIN)
159 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
161 while (TimeFields.Minute >= MINSPERHOUR)
162 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
164 while (TimeFields.Hour >= HOURSPERDAY)
165 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
167 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
168 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
170 while (TimeFields.Month > MONSPERYEAR)
171 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
174 /* FIXME: handle calendar corrections here */
175 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
176 { rcTime += YearLengths[IsLeapYear(CurYear)];
178 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
179 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
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;
188 return TRUE;
190 /************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
192 /******************************************************************************
193 * RtlSystemTimeToLocalTime [NTDLL.@]
195 VOID WINAPI RtlSystemTimeToLocalTime(
196 IN PLARGE_INTEGER SystemTime,
197 OUT PLARGE_INTEGER LocalTime)
199 FIXME("(%p, %p),stub!\n",SystemTime,LocalTime);
201 memcpy (LocalTime, SystemTime, sizeof (PLARGE_INTEGER));
204 /******************************************************************************
205 * RtlTimeToSecondsSince1970 [NTDLL.@]
207 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const FILETIME *time, LPDWORD res )
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;
217 /******************************************************************************
218 * RtlTimeToSecondsSince1980 [NTDLL.@]
220 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const FILETIME *time, LPDWORD res )
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;
230 /******************************************************************************
231 * RtlSecondsSince1970ToTime [NTDLL.@]
233 void WINAPI RtlSecondsSince1970ToTime( DWORD time, FILETIME *res )
235 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
236 res->dwLowDateTime = (DWORD)secs;
237 res->dwHighDateTime = (DWORD)(secs >> 32);
240 /******************************************************************************
241 * RtlSecondsSince1980ToTime [NTDLL.@]
243 void WINAPI RtlSecondsSince1980ToTime( DWORD time, FILETIME *res )
245 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
246 res->dwLowDateTime = (DWORD)secs;
247 res->dwHighDateTime = (DWORD)(secs >> 32);
250 /******************************************************************************
251 * RtlTimeToElapsedTimeFields [NTDLL.@]
252 * FIXME: prototype guessed
254 VOID WINAPI RtlTimeToElapsedTimeFields(
255 PLARGE_INTEGER liTime,
256 PTIME_FIELDS TimeFields)
258 FIXME("(%p,%p): stub\n",liTime,TimeFields);
261 /***********************************************************************
262 * NtQuerySystemTime (NTDLL.@)
263 * ZwQuerySystemTime (NTDLL.@)
265 void WINAPI NtQuerySystemTime( LARGE_INTEGER *time )
267 LONGLONG secs;
268 struct timeval now;
270 gettimeofday( &now, 0 );
271 secs = now.tv_sec + SECS_1601_TO_1970;
272 time->QuadPart = RtlExtendedIntegerMultiply( secs, 10000000 ) + now.tv_usec * 10;