Keep track of per-column information inside the listview.
[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 "config.h"
26 #include "wine/port.h"
27
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"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40
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
54
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)
59
60
61 static const int YearLengths[2] = {DAYSPERNORMALYEAR, DAYSPERLEAPYEAR};
62 static const int MonthLengths[2][MONSPERYEAR] =
63 {
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 }
66 };
67
68 static inline int IsLeapYear(int Year)
69 {
70         return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
71 }
72
73 static inline void NormalizeTimeFields(CSHORT *FieldToNormalize, CSHORT *CarryField,int Modulus)
74 {
75         *FieldToNormalize = (CSHORT) (*FieldToNormalize - Modulus);
76         *CarryField = (CSHORT) (*CarryField + 1);
77 }
78
79 /******************************************************************************
80  *  RtlTimeToTimeFields         [NTDLL.@]
81  *
82  */
83
84 VOID WINAPI RtlTimeToTimeFields(
85         PLARGE_INTEGER liTime,
86         PTIME_FIELDS TimeFields)
87 {
88         const int *Months;
89         int LeapSecondCorrections, SecondsInDay, CurYear;
90         int LeapYear, CurMonth, GMTOffset;
91         long int Days;
92         LONGLONG Time = *(LONGLONG *)&liTime;
93
94         /* Extract millisecond from time and convert time into seconds */
95         TimeFields->Milliseconds = (CSHORT) ((Time % TICKSPERSEC) / TICKSPERMSEC);
96         Time = Time / TICKSPERSEC;
97
98         /* FIXME: Compute the number of leap second corrections here */
99         LeapSecondCorrections = 0;
100
101         /* FIXME: get the GMT offset here */
102         GMTOffset = 0;
103
104         /* Split the time into days and seconds within the day */
105         Days = Time / SECSPERDAY;
106         SecondsInDay = Time % SECSPERDAY;
107
108         /* Adjust the values for GMT and leap seconds */
109         SecondsInDay += (GMTOffset - LeapSecondCorrections);
110         while (SecondsInDay < 0)
111         { SecondsInDay += SECSPERDAY;
112           Days--;
113         }
114         while (SecondsInDay >= SECSPERDAY)
115         { SecondsInDay -= SECSPERDAY;
116           Days++;
117         }
118
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);
124
125         /* FIXME: handle the possibility that we are on a leap second (i.e. Second = 60) */
126
127         /* compute day of week */
128         TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
129
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;
137           }
138           CurYear++;
139           Days = Days - (long) YearLengths[LeapYear];
140         }
141         TimeFields->Year = (CSHORT) CurYear;
142
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);
149 }
150 /******************************************************************************
151  *  RtlTimeFieldsToTime         [NTDLL.@]
152  *
153  */
154 BOOLEAN WINAPI RtlTimeFieldsToTime(
155         PTIME_FIELDS tfTimeFields,
156         PLARGE_INTEGER Time)
157 {
158         int CurYear, CurMonth;
159         LONGLONG rcTime;
160         TIME_FIELDS TimeFields = *tfTimeFields;
161
162         rcTime = 0;
163
164         /* FIXME: normalize the TIME_FIELDS structure here */
165         while (TimeFields.Second >= SECSPERMIN)
166         { NormalizeTimeFields(&TimeFields.Second, &TimeFields.Minute, SECSPERMIN);
167         }
168         while (TimeFields.Minute >= MINSPERHOUR)
169         { NormalizeTimeFields(&TimeFields.Minute, &TimeFields.Hour, MINSPERHOUR);
170         }
171         while (TimeFields.Hour >= HOURSPERDAY)
172         { NormalizeTimeFields(&TimeFields.Hour, &TimeFields.Day, HOURSPERDAY);
173         }
174         while (TimeFields.Day > MonthLengths[IsLeapYear(TimeFields.Year)][TimeFields.Month - 1])
175         { NormalizeTimeFields(&TimeFields.Day, &TimeFields.Month, SECSPERMIN);
176         }
177         while (TimeFields.Month > MONSPERYEAR)
178         { NormalizeTimeFields(&TimeFields.Month, &TimeFields.Year, MONSPERYEAR);
179         }
180
181         /* FIXME: handle calendar corrections here */
182         for (CurYear = EPOCHYEAR; CurYear < TimeFields.Year; CurYear++)
183         { rcTime += YearLengths[IsLeapYear(CurYear)];
184         }
185         for (CurMonth = 1; CurMonth < TimeFields.Month; CurMonth++)
186         { rcTime += MonthLengths[IsLeapYear(CurYear)][CurMonth - 1];
187         }
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;
194
195         return TRUE;
196 }
197 /************* end of code by Rex Jolliff (rex@lvcablemodem.com) *******************/
198
199 /******************************************************************************
200  *  RtlSystemTimeToLocalTime    [NTDLL.@]
201  */
202 VOID WINAPI RtlSystemTimeToLocalTime(
203         IN  PLARGE_INTEGER SystemTime,
204         OUT PLARGE_INTEGER LocalTime)
205 {
206         FIXME("(%p, %p),stub!\n",SystemTime,LocalTime);
207
208         memcpy (LocalTime, SystemTime, sizeof (PLARGE_INTEGER));
209 }
210
211 /******************************************************************************
212  *  RtlTimeToSecondsSince1970           [NTDLL.@]
213  */
214 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *time, PULONG res )
215 {
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;
222 }
223
224 /******************************************************************************
225  *  RtlTimeToSecondsSince1980           [NTDLL.@]
226  */
227 BOOLEAN WINAPI RtlTimeToSecondsSince1980( const LARGE_INTEGER *time, LPDWORD res )
228 {
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;
235 }
236
237 /******************************************************************************
238  *  RtlSecondsSince1970ToTime           [NTDLL.@]
239  */
240 void WINAPI RtlSecondsSince1970ToTime( DWORD time, LARGE_INTEGER *res )
241 {
242     ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_TO_1970, 10000000 );
243     res->s.LowPart  = (DWORD)secs;
244     res->s.HighPart = (DWORD)(secs >> 32);
245 }
246
247 /******************************************************************************
248  *  RtlSecondsSince1980ToTime           [NTDLL.@]
249  */
250 void WINAPI RtlSecondsSince1980ToTime( DWORD time, LARGE_INTEGER *res )
251 {
252     ULONGLONG secs = RtlExtendedIntegerMultiply( time + SECS_1601_to_1980, 10000000 );
253     res->s.LowPart  = (DWORD)secs;
254     res->s.HighPart = (DWORD)(secs >> 32);
255 }
256
257 /******************************************************************************
258  * RtlTimeToElapsedTimeFields [NTDLL.@]
259  * FIXME: prototype guessed
260  */
261 VOID WINAPI RtlTimeToElapsedTimeFields(
262         PLARGE_INTEGER liTime,
263         PTIME_FIELDS TimeFields)
264 {
265         FIXME("(%p,%p): stub\n",liTime,TimeFields);
266 }
267
268 /***********************************************************************
269  *      NtQuerySystemTime   (NTDLL.@)
270  *      ZwQuerySystemTime   (NTDLL.@)
271  */
272 NTSTATUS WINAPI NtQuerySystemTime( PLARGE_INTEGER time )
273 {
274     ULONGLONG secs;
275     struct timeval now;
276
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;
282 }