Fixed "conditional expr is always true due to being unsigned < 0"
[wine/dcerpc.git] / dlls / ntdll / time.c
blobecabb32df272f164be0f5d4d5bb753037a09cf2d
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)
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 "config.h"
26 #include "wine/port.h"
28 #include <string.h>
29 #include <time.h>
30 #ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "winternl.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
41 #define TICKSPERSEC 10000000
42 #define TICKSPERMSEC 10000
43 #define SECSPERDAY 86400
44 #define SECSPERHOUR 3600
45 #define SECSPERMIN 60
46 #define MINSPERHOUR 60
47 #define HOURSPERDAY 24
48 #define EPOCHWEEKDAY 0
49 #define DAYSPERWEEK 7
50 #define EPOCHYEAR 1601
51 #define DAYSPERNORMALYEAR 365
52 #define DAYSPERLEAPYEAR 366
53 #define MONSPERYEAR 12
55 /* 1601 to 1970 is 369 years plus 89 leap days */
56 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)86400)
57 /* 1601 to 1980 is 379 years plus 91 leap days */
58 #define SECS_1601_to_1980 ((379 * 365 + 91) * (ULONGLONG)86400)
61 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
62 static const int MonthLengths[2][MONSPERYEAR] =
64 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
65 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
68 static inline int IsLeapYear(int Year)
70 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
73 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
75 *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
76 *CarryField = (CSHORT) (*CarryField + 1);
79 /******************************************************************************
80 * RtlTimeToTimeFields [NTDLL.@]
84 VOID WINAPI RtlTimeToTimeFields(
85 PLARGE_INTEGER liTime,
86 PTIME_FIELDS TimeFields)
88 const int *Months;
89 int LeapSecondCorrections, SecondsInDay, CurYear;
90 int LeapYear, CurMonth, GMTOffset;
91 long int Days;
92 LONGLONG Time = *(LONGLONG *)&liTime;
94 /* Extract millisecond from time and convert time into seconds */
95 TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
96 Time = Time / TICKSPERSEC;
98 /* FIXME: Compute the number of leap second corrections here */
99 LeapSecondCorrections = 0;
101 /* FIXME: get the GMT offset here */
102 GMTOffset = 0;
104 /* Split the time into days and seconds within the day */
105 Days = Time / SECSPERDAY;
106 SecondsInDay = Time % SECSPERDAY;
108 /* Adjust the values for GMT and leap seconds */
109 SecondsInDay += (GMTOffset - LeapSecondCorrections);
110 while (SecondsInDay < 0)
111 { SecondsInDay += SECSPERDAY;
112 Days--;
114 while (SecondsInDay >= SECSPERDAY)
115 { SecondsInDay -= SECSPERDAY;
116 Days++;
119 /* compute time of day */
120 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
121 SecondsInDay = SecondsInDay % SECSPERHOUR;
122 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
123 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
125 /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
127 /* compute day of week */
128 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
130 /* compute year */
131 CurYear = EPOCHYEAR;
132 /* FIXME: handle calendar modifications */
133 while (1)
134 { LeapYear = IsLeapYear(CurYear);
135 if (Days < (long) YearLengths[LeapYear])
136 { break;
138 CurYear++;
139 Days = Days - (long) YearLengths[LeapYear];
141 TimeFields->Year = (CSHORT) CurYear;
143 /* Compute month of year */
144 Months = MonthLengths[LeapYear];
145 for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++)
146 Days = Days - (long) Months[CurMonth];
147 TimeFields->Month = (CSHORT) (CurMonth + 1);
148 TimeFields->Day = (CSHORT) (Days + 1);
150 /******************************************************************************
151 * RtlTimeFieldsToTime [NTDLL.@]
154 BOOLEAN WINAPI RtlTimeFieldsToTime(
155 PTIME_FIELDS tfTimeFields,
156 PLARGE_INTEGER Time)
158 int CurYear, CurMonth;
159 LONGLONG rcTime;
160 TIME_FIELDS TimeFields = *tfTimeFields;
162 rcTime = 0;
164 /* FIXME: normalize the TIME_FIELDS structure here */
165 while (TimeFields.Second >= SECSPERMIN)
166 { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
168 while (TimeFields.Minute >= MINSPERHOUR)
169 { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
171 while (TimeFields.Hour >= HOURSPERDAY)
172 { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
174 while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
175 { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
177 while (TimeFields.Month > MONSPERYEAR)
178 { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
181 /* FIXME: handle calendar corrections here */
182 for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
183 { rcTime += YearLengths[IsLeapYear(CurYear)];
185 for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
186 { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
188 rcTime += TimeFields.Day - 1;
189 rcTime *= SECSPERDAY;
190 rcTime += TimeFields.Hour * SECSPERHOUR + TimeFields.Minute * SECSPERMIN + TimeFields.Second;
191 rcTime *= TICKSPERSEC;
192 rcTime += TimeFields.Milliseconds * TICKSPERMSEC;
193 *Time = *(LARGE_INTEGER *)&rcTime;
195 return TRUE;
197 /************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
199 /******************************************************************************
200 * RtlSystemTimeToLocalTime [NTDLL.@]
202 VOID WINAPI RtlSystemTimeToLocalTime(
203 IN PLARGE_INTEGER SystemTime,
204 OUT PLARGE_INTEGER LocalTime)
206 FIXME("(%p, %p),stub!\n",SystemTime,LocalTime);
208 memcpy (LocalTime, SystemTime, sizeof (PLARGE_INTEGER));
211 /******************************************************************************
212 * RtlTimeToSecondsSince1970 [NTDLL.@]
214 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *time, PULONG res )
216 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
217 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
218 tmp -= SECS_1601_TO_1970;
219 if (tmp > 0xffffffff) return FALSE;
220 *res = (DWORD)tmp;
221 return TRUE;
224 /******************************************************************************
225 * RtlTimeToSecondsSince1980 [NTDLL.@]
227 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *time, LPDWORD res )
229 ULONGLONG tmp = ((ULONGLONG)time->s.HighPart << 32) | time->s.LowPart;
230 tmp = RtlLargeIntegerDivide( tmp, 10000000, NULL );
231 tmp -= SECS_1601_to_1980;
232 if (tmp > 0xffffffff) return FALSE;
233 *res = (DWORD)tmp;
234 return TRUE;
237 /******************************************************************************
238 * RtlSecondsSince1970ToTime [NTDLL.@]
240 void WINAPI RtlSecondsSince1970ToTime( DWORD time, LARGE_INTEGER *res )
242 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
243 res->s.LowPart = (DWORD)secs;
244 res->s.HighPart = (DWORD)(secs >> 32);
247 /******************************************************************************
248 * RtlSecondsSince1980ToTime [NTDLL.@]
250 void WINAPI RtlSecondsSince1980ToTime( DWORD time, LARGE_INTEGER *res )
252 ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
253 res->s.LowPart = (DWORD)secs;
254 res->s.HighPart = (DWORD)(secs >> 32);
257 /******************************************************************************
258 * RtlTimeToElapsedTimeFields [NTDLL.@]
259 * FIXME: prototype guessed
261 VOID WINAPI RtlTimeToElapsedTimeFields(
262 PLARGE_INTEGER liTime,
263 PTIME_FIELDS TimeFields)
265 FIXME("(%p,%p): stub\n",liTime,TimeFields);
268 /***********************************************************************
269 * NtQuerySystemTime (NTDLL.@)
270 * ZwQuerySystemTime (NTDLL.@)
272 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER time )
274 ULONGLONG secs;
275 struct timeval now;
277 gettimeofday( &now, 0 );
278 secs = RtlExtendedIntegerMultiply( now.tv_sec+SECS_1601_TO_1970, 10000000 ) + now.tv_usec * 10;
279 time->s.LowPart = (DWORD)secs;
280 time->s.HighPart = (DWORD)(secs >> 32);
281 return STATUS_SUCCESS;