1 /*
2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 *
16 */
17 #include "config.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/time.h> /* gettimeofday */
24 #include <dirent.h>
25 #include <unistd.h>
26 #include <time.h>
27
28 #include <windows.h>
29
30 // #define TRACEN(u) u;
31 #define TRACEN(u) /* */
32
33 typedef LONG NTSTATUS;
34 #define STATUS_SUCCESS 0x00000000
35
36 #define TICKSPERSEC 10000000
37 #define TICKSPERMSEC 10000
38 #define SECSPERDAY 86400
39 #define SECSPERHOUR 3600
40 #define SECSPERMIN 60
41 #define MINSPERHOUR 60
42 #define HOURSPERDAY 24
43 #define EPOCHWEEKDAY 1 /* Jan 1, 1601 was Monday */
44 #define DAYSPERWEEK 7
45 #define EPOCHYEAR 1601
46 #define DAYSPERNORMALYEAR 365
47 #define DAYSPERLEAPYEAR 366
48 #define MONSPERYEAR 12
49 #define DAYSPERQUADRICENTENNIUM (365 * 400 + 97)
50 #define DAYSPERNORMALCENTURY (365 * 100 + 24)
51 #define DAYSPERNORMALQUADRENNIUM (365 * 4 + 1)
52
53 /* 1601 to 1970 is 369 years plus 89 leap days */
54 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
55 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
56 /* 1601 to 1980 is 379 years plus 91 leap days */
57 #define SECS_1601_TO_1980 ((379 * 365 + 91) * (ULONGLONG)SECSPERDAY)
58 #define TICKS_1601_TO_1980 (SECS_1601_TO_1980 * TICKSPERSEC)
59 typedef short CSHORT;
60
TIME_GetBias()61 static LONG TIME_GetBias() {
62 time_t utc = time(NULL);
63 struct tm *ptm = localtime(&utc);
64 int localdaylight = ptm->tm_isdst; /* daylight for local timezone */
65 ptm = gmtime(&utc);
66 ptm->tm_isdst = localdaylight; /* use local daylight, not that of Greenwich */
67 LONG bias = (int)(mktime(ptm)-utc);
68 TRACEN((printf("TIME_GetBias %ld\n",(long)bias)))
69 return bias;
70 }
71
RtlSystemTimeToLocalTime(const LARGE_INTEGER * SystemTime,LARGE_INTEGER * LocalTime)72 static inline void RtlSystemTimeToLocalTime( const LARGE_INTEGER *SystemTime,
73 LARGE_INTEGER *LocalTime ) {
74 LONG bias = TIME_GetBias();
75 LocalTime->QuadPart = SystemTime->QuadPart - bias * (LONGLONG)TICKSPERSEC;
76 }
77
RtlSecondsSince1970ToFileTime(DWORD Seconds,FILETIME * ft)78 void WINAPI RtlSecondsSince1970ToFileTime( DWORD Seconds, FILETIME * ft ) {
79 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
80 ft->dwLowDateTime = (DWORD)secs;
81 ft->dwHighDateTime = (DWORD)(secs >> 32);
82 TRACEN((printf("RtlSecondsSince1970ToFileTime %lx => %lx %lx\n",(long)Seconds,(long)ft->dwHighDateTime,(long)ft->dwLowDateTime)))
83 }
84
85 /*
86 void WINAPI RtlSecondsSince1970ToTime( DWORD Seconds, LARGE_INTEGER *Time )
87 {
88 ULONGLONG secs = Seconds * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
89 // Time->u.LowPart = (DWORD)secs; Time->u.HighPart = (DWORD)(secs >> 32);
90 Time->QuadPart = secs;
91 }
92 */
93
94
DosDateTimeToFileTime(WORD fatdate,WORD fattime,FILETIME * ft)95 BOOL WINAPI DosDateTimeToFileTime( WORD fatdate, WORD fattime, FILETIME * ft) {
96 struct tm newtm;
97
98 TRACEN((printf("DosDateTimeToFileTime\n")))
99 // memset(&newtm,0,sizeof(newtm));
100 newtm.tm_sec = (fattime & 0x1f) * 2;
101 newtm.tm_min = (fattime >> 5) & 0x3f;
102 newtm.tm_hour = (fattime >> 11);
103 newtm.tm_mday = (fatdate & 0x1f);
104 newtm.tm_mon = ((fatdate >> 5) & 0x0f) - 1;
105 newtm.tm_year = (fatdate >> 9) + 80;
106 newtm.tm_isdst = -1;
107
108 time_t time1 = mktime(&newtm);
109 LONG bias = TIME_GetBias();
110 RtlSecondsSince1970ToFileTime( time1 - bias, ft );
111
112 TRACEN((printf("DosDateTimeToFileTime(%ld,%ld) t1=%ld bias=%ld => %lx %lx\n",
113 (long)fatdate,(long)fattime,(long)time1,(long)bias,
114 (long)ft->dwHighDateTime,(long)ft->dwLowDateTime)))
115
116 return TRUE;
117 }
118
RtlTimeToSecondsSince1970(const LARGE_INTEGER * Time,DWORD * Seconds)119 BOOLEAN WINAPI RtlTimeToSecondsSince1970( const LARGE_INTEGER *Time, DWORD *Seconds ) {
120 ULONGLONG tmp = Time->QuadPart;
121 TRACEN((printf("RtlTimeToSecondsSince1970-1 %llx\n",tmp)))
122 tmp /= TICKSPERSEC;
123 tmp -= SECS_1601_TO_1970;
124 TRACEN((printf("RtlTimeToSecondsSince1970-2 %llx\n",tmp)))
125 if (tmp > 0xffffffff) return FALSE;
126 *Seconds = (DWORD)tmp;
127 return TRUE;
128 }
129
FileTimeToDosDateTime(const FILETIME * ft,WORD * fatdate,WORD * fattime)130 BOOL WINAPI FileTimeToDosDateTime( const FILETIME *ft, WORD *fatdate, WORD *fattime ) {
131 LARGE_INTEGER li;
132 ULONG t;
133 time_t unixtime;
134 struct tm* tm;
135 WORD fat_d,fat_t;
136
137 TRACEN((printf("FileTimeToDosDateTime\n")))
138 li.QuadPart = ft->dwHighDateTime;
139 li.QuadPart = (li.QuadPart << 32) | ft->dwLowDateTime;
140 RtlTimeToSecondsSince1970( &li, &t );
141 unixtime = t; /* FIXME unixtime = t - TIME_GetBias(); */
142 tm = gmtime( &unixtime );
143
144 fat_t = (tm->tm_hour << 11) + (tm->tm_min << 5) + (tm->tm_sec / 2);
145 fat_d = ((tm->tm_year - 80) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday;
146 if (fattime)
147 *fattime = fat_t;
148 if (fatdate)
149 *fatdate = fat_d;
150
151 TRACEN((printf("FileTimeToDosDateTime : %lx %lx => %d %d\n",
152 (long)ft->dwHighDateTime,(long)ft->dwLowDateTime,(unsigned)fat_d,(unsigned)fat_t)))
153
154 return TRUE;
155 }
156
FileTimeToLocalFileTime(const FILETIME * utcft,FILETIME * localft)157 BOOL WINAPI FileTimeToLocalFileTime( const FILETIME *utcft, FILETIME * localft ) {
158 LARGE_INTEGER local, utc;
159
160 TRACEN((printf("FileTimeToLocalFileTime\n")))
161 utc.QuadPart = utcft->dwHighDateTime;
162 utc.QuadPart = (utc.QuadPart << 32) | utcft->dwLowDateTime;
163 RtlSystemTimeToLocalTime( &utc, &local );
164 localft->dwLowDateTime = (DWORD)local.QuadPart;
165 localft->dwHighDateTime = (DWORD)(local.QuadPart >> 32);
166
167 return TRUE;
168 }
169
170 typedef struct _TIME_FIELDS {
171 CSHORT Year;
172 CSHORT Month;
173 CSHORT Day;
174 CSHORT Hour;
175 CSHORT Minute;
176 CSHORT Second;
177 CSHORT Milliseconds;
178 CSHORT Weekday;
179 } TIME_FIELDS;
180
181 static const int MonthLengths[2][MONSPERYEAR] =
182 {
183 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
184 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
185 };
186
IsLeapYear(int Year)187 static inline int IsLeapYear(int Year) {
188 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0) ? 1 : 0;
189 }
190
RtlTimeToTimeFields(const LARGE_INTEGER * liTime,TIME_FIELDS * TimeFields)191 static inline VOID WINAPI RtlTimeToTimeFields(
192 const LARGE_INTEGER *liTime,
193 TIME_FIELDS * TimeFields) {
194 int SecondsInDay;
195 long int cleaps, years, yearday, months;
196 long int Days;
197 LONGLONG Time;
198
199 /* Extract millisecond from time and convert time into seconds */
200 TimeFields->Milliseconds =
201 (CSHORT) (( liTime->QuadPart % TICKSPERSEC) / TICKSPERMSEC);
202 Time = liTime->QuadPart / TICKSPERSEC;
203
204 /* The native version of RtlTimeToTimeFields does not take leap seconds
205 * into account */
206
207 /* Split the time into days and seconds within the day */
208 Days = Time / SECSPERDAY;
209 SecondsInDay = Time % SECSPERDAY;
210
211 /* compute time of day */
212 TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR);
213 SecondsInDay = SecondsInDay % SECSPERHOUR;
214 TimeFields->Minute = (CSHORT) (SecondsInDay / SECSPERMIN);
215 TimeFields->Second = (CSHORT) (SecondsInDay % SECSPERMIN);
216
217 /* compute day of week */
218 TimeFields->Weekday = (CSHORT) ((EPOCHWEEKDAY + Days) % DAYSPERWEEK);
219
220 /* compute year, month and day of month. */
221 cleaps=( 3 * ((4 * Days + 1227) / DAYSPERQUADRICENTENNIUM) + 3 ) / 4;
222 Days += 28188 + cleaps;
223 years = (20 * Days - 2442) / (5 * DAYSPERNORMALQUADRENNIUM);
224 yearday = Days - (years * DAYSPERNORMALQUADRENNIUM)/4;
225 months = (64 * yearday) / 1959;
226 /* the result is based on a year starting on March.
227 * To convert take 12 from Januari and Februari and
228 * increase the year by one. */
229 if( months < 14 ) {
230 TimeFields->Month = months - 1;
231 TimeFields->Year = years + 1524;
232 } else {
233 TimeFields->Month = months - 13;
234 TimeFields->Year = years + 1525;
235 }
236 /* calculation of day of month is based on the wonderful
237 * sequence of INT( n * 30.6): it reproduces the
238 * 31-30-31-30-31-31 month lengths exactly for small n's */
239 TimeFields->Day = yearday - (1959 * months) / 64 ;
240 }
241
242
FileTimeToSystemTime(const FILETIME * ft,SYSTEMTIME * syst)243 BOOL WINAPI FileTimeToSystemTime( const FILETIME *ft, SYSTEMTIME * syst ) {
244 TIME_FIELDS tf;
245 LARGE_INTEGER t;
246
247 TRACEN((printf("FileTimeToSystemTime\n")))
248 t.QuadPart = ft->dwHighDateTime;
249 t.QuadPart = (t.QuadPart << 32) | ft->dwLowDateTime;
250 RtlTimeToTimeFields(&t, &tf);
251
252 syst->wYear = tf.Year;
253 syst->wMonth = tf.Month;
254 syst->wDay = tf.Day;
255 syst->wHour = tf.Hour;
256 syst->wMinute = tf.Minute;
257 syst->wSecond = tf.Second;
258 syst->wMilliseconds = tf.Milliseconds;
259 syst->wDayOfWeek = tf.Weekday;
260 return TRUE;
261 }
262
263
RtlLocalTimeToSystemTime(const LARGE_INTEGER * LocalTime,LARGE_INTEGER * SystemTime)264 static inline NTSTATUS WINAPI RtlLocalTimeToSystemTime( const LARGE_INTEGER *LocalTime,
265 LARGE_INTEGER *SystemTime) {
266
267 TRACEN((printf("RtlLocalTimeToSystemTime\n")))
268 LONG bias = TIME_GetBias();
269 SystemTime->QuadPart = LocalTime->QuadPart + bias * (LONGLONG)TICKSPERSEC;
270 return STATUS_SUCCESS;
271 }
272
LocalFileTimeToFileTime(const FILETIME * localft,FILETIME * utcft)273 BOOL WINAPI LocalFileTimeToFileTime( const FILETIME *localft, FILETIME * utcft ) {
274 LARGE_INTEGER local, utc;
275
276 TRACEN((printf("LocalFileTimeToFileTime\n")))
277 local.QuadPart = localft->dwHighDateTime;
278 local.QuadPart = (local.QuadPart << 32) | localft->dwLowDateTime;
279 RtlLocalTimeToSystemTime( &local, &utc );
280 utcft->dwLowDateTime = (DWORD)utc.QuadPart;
281 utcft->dwHighDateTime = (DWORD)(utc.QuadPart >> 32);
282
283 return TRUE;
284 }
285
286 /*********************************************************************
287 * GetSystemTime (KERNEL32.@)
288 *
289 * Get the current system time.
290 *
291 * RETURNS
292 * Nothing.
293 */
GetSystemTime(SYSTEMTIME * systime)294 VOID WINAPI GetSystemTime(SYSTEMTIME * systime) /* [O] Destination for current time */
295 {
296 FILETIME ft;
297 LARGE_INTEGER t;
298
299 TRACEN((printf("GetSystemTime\n")))
300
301 struct timeval now;
302 gettimeofday( &now, 0 );
303 t.QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
304 t.QuadPart += now.tv_usec * 10;
305
306 ft.dwLowDateTime = (DWORD)(t.QuadPart);
307 ft.dwHighDateTime = (DWORD)(t.QuadPart >> 32);
308 FileTimeToSystemTime(&ft, systime);
309 }
310
311 /******************************************************************************
312 * RtlTimeFieldsToTime [NTDLL.@]
313 *
314 * Convert a TIME_FIELDS structure into a time.
315 *
316 * PARAMS
317 * ftTimeFields [I] TIME_FIELDS structure to convert.
318 * Time [O] Destination for the converted time.
319 *
320 * RETURNS
321 * Success: TRUE.
322 * Failure: FALSE.
323 */
RtlTimeFieldsToTime(TIME_FIELDS * tfTimeFields,LARGE_INTEGER * Time)324 static BOOLEAN WINAPI RtlTimeFieldsToTime(
325 TIME_FIELDS * tfTimeFields,
326 LARGE_INTEGER *Time)
327 {
328 int month, year, cleaps, day;
329
330 TRACEN((printf("RtlTimeFieldsToTime\n")))
331
332 /* FIXME: normalize the TIME_FIELDS structure here */
333 /* No, native just returns 0 (error) if the fields are not */
334 if( tfTimeFields->Milliseconds< 0 || tfTimeFields->Milliseconds > 999 ||
335 tfTimeFields->Second < 0 || tfTimeFields->Second > 59 ||
336 tfTimeFields->Minute < 0 || tfTimeFields->Minute > 59 ||
337 tfTimeFields->Hour < 0 || tfTimeFields->Hour > 23 ||
338 tfTimeFields->Month < 1 || tfTimeFields->Month > 12 ||
339 tfTimeFields->Day < 1 ||
340 tfTimeFields->Day > MonthLengths
341 [ tfTimeFields->Month ==2 || IsLeapYear(tfTimeFields->Year)]
342 [ tfTimeFields->Month - 1] ||
343 tfTimeFields->Year < 1601 )
344 return FALSE;
345
346 /* now calculate a day count from the date
347 * First start counting years from March. This way the leap days
348 * are added at the end of the year, not somewhere in the middle.
349 * Formula's become so much less complicate that way.
350 * To convert: add 12 to the month numbers of Jan and Feb, and
351 * take 1 from the year */
352 if(tfTimeFields->Month < 3) {
353 month = tfTimeFields->Month + 13;
354 year = tfTimeFields->Year - 1;
355 } else {
356 month = tfTimeFields->Month + 1;
357 year = tfTimeFields->Year;
358 }
359 cleaps = (3 * (year / 100) + 3) / 4; /* nr of "century leap years"*/
360 day = (36525 * year) / 100 - cleaps + /* year * dayperyr, corrected */
361 (1959 * month) / 64 + /* months * daypermonth */
362 tfTimeFields->Day - /* day of the month */
363 584817 ; /* zero that on 1601-01-01 */
364 /* done */
365
366 Time->QuadPart = (((((LONGLONG) day * HOURSPERDAY +
367 tfTimeFields->Hour) * MINSPERHOUR +
368 tfTimeFields->Minute) * SECSPERMIN +
369 tfTimeFields->Second ) * 1000 +
370 tfTimeFields->Milliseconds ) * TICKSPERMSEC;
371
372 return TRUE;
373 }
374
375 /*********************************************************************
376 * SystemTimeToFileTime (KERNEL32.@)
377 */
SystemTimeToFileTime(const SYSTEMTIME * syst,FILETIME * ft)378 BOOL WINAPI SystemTimeToFileTime( const SYSTEMTIME *syst, FILETIME * ft ) {
379 TIME_FIELDS tf;
380 LARGE_INTEGER t;
381
382 TRACEN((printf("SystemTimeToFileTime\n")))
383
384 tf.Year = syst->wYear;
385 tf.Month = syst->wMonth;
386 tf.Day = syst->wDay;
387 tf.Hour = syst->wHour;
388 tf.Minute = syst->wMinute;
389 tf.Second = syst->wSecond;
390 tf.Milliseconds = syst->wMilliseconds;
391
392 RtlTimeFieldsToTime(&tf, &t);
393 ft->dwLowDateTime = (DWORD)t.QuadPart;
394 ft->dwHighDateTime = (DWORD)(t.QuadPart>>32);
395 return TRUE;
396 }
397
398