wininet: Use a blank password if none is provided in FTP_Connect.
[wine] / dlls / msvcrt / time.c
1 /*
2  * msvcrt.dll date/time functions
3  *
4  * Copyright 1996,1998 Marcus Meissner
5  * Copyright 1996 Jukka Iivonen
6  * Copyright 1997,2000 Uwe Bonnes
7  * Copyright 2000 Jon Griffiths
8  * Copyright 2004 Hans Leidekker
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26
27 #define _POSIX_PTHREAD_SEMANTICS /* switch to a 2 arg style asctime_r on Solaris */
28 #include <time.h>
29 #ifdef HAVE_SYS_TIMES_H
30 # include <sys/times.h>
31 #endif
32 #include <limits.h>
33
34 #include "msvcrt.h"
35 #include "winbase.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39
40 static const int MonthLengths[2][12] =
41 {
42     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
43     { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
44 };
45
46 static inline int IsLeapYear(int Year)
47 {
48     return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
49 }
50
51 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
52 {
53     memset( dest, 0, sizeof(*dest) );
54     dest->tm_sec   = src->tm_sec;
55     dest->tm_min   = src->tm_min;
56     dest->tm_hour  = src->tm_hour;
57     dest->tm_mday  = src->tm_mday;
58     dest->tm_mon   = src->tm_mon;
59     dest->tm_year  = src->tm_year;
60     dest->tm_wday  = src->tm_wday;
61     dest->tm_yday  = src->tm_yday;
62     dest->tm_isdst = src->tm_isdst;
63 }
64
65 #define SECSPERDAY        86400
66 /* 1601 to 1970 is 369 years plus 89 leap days */
67 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
68 #define TICKSPERSEC       10000000
69 #define TICKSPERMSEC      10000
70 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
71
72 /**********************************************************************
73  *              mktime (MSVCRT.@)
74  */
75 MSVCRT_time_t CDECL MSVCRT_mktime(struct MSVCRT_tm *t)
76 {
77     MSVCRT_time_t secs;
78     FILETIME lft, uft;
79     ULONGLONG time;
80     struct MSVCRT_tm ts, *ptm;
81     int cleaps, day;
82
83     ts=*t;
84     /* to prevent arithmetic overflows put constraints on some fields */
85     /* whether the effective date falls in the 1970-2038 time period */
86     /* will be tested later */
87     /* BTW, I have no idea what limits native msvcrt has. */
88     if ( ts.tm_year < 0 || ts.tm_year > 140  ||
89             ts.tm_mon < -840 || ts.tm_mon > 840 ||
90             ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
91             ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
92             ts.tm_min < -29000000 || ts.tm_min > 29000000 )
93         return -1;
94            
95     /* normalize the tm month fields */
96     if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
97     if( ts.tm_mon < 0) {
98         int dy = (11 - ts.tm_mon) / 12;
99         ts.tm_year -= dy;
100         ts.tm_mon += dy * 12;
101     }
102     /* now calculate a day count from the date
103      * First start counting years from March. This way the leap days
104      * are added at the end of the year, not somewhere in the middle.
105      * Formula's become so much less complicate that way.
106      * To convert: add 12 to the month numbers of Jan and Feb, and 
107      * take 1 from the year */
108     if(ts.tm_mon < 2) {
109         ts.tm_mon += 14;
110         ts.tm_year += 1899;
111     } else {
112         ts.tm_mon += 2;
113         ts.tm_year += 1900;
114     }
115     cleaps = (3 * (ts.tm_year / 100) + 3) / 4;   /* nr of "century leap years"*/
116     day =  (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
117              (1959 * ts.tm_mon) / 64 +    /* months * daypermonth */
118              ts.tm_mday -                 /* day of the month */
119              584817 ;                     /* zero that on 1601-01-01 */
120     /* done */
121
122     /* convert to 100 ns ticks */
123     time = ((((ULONGLONG) day * 24 +
124             ts.tm_hour) * 60 +
125             ts.tm_min) * 60 +
126             ts.tm_sec ) * TICKSPERSEC;
127     
128     lft.dwHighDateTime = (DWORD) (time >> 32);
129     lft.dwLowDateTime = (DWORD) time;
130
131     LocalFileTimeToFileTime(&lft, &uft);
132
133     time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
134     time /= TICKSPERSEC;
135     if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
136         return -1;
137     secs = time - SECS_1601_TO_1970;
138     /* compute tm_wday, tm_yday and renormalize the other fields of the
139      * tm structure */
140     if ((ptm = MSVCRT_localtime( &secs ))) *t = *ptm;
141
142     return secs; 
143 }
144
145 /*********************************************************************
146  *      localtime (MSVCRT.@)
147  */
148 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT_time_t* secs)
149 {
150   thread_data_t * const data = msvcrt_get_thread_data();
151   int i;
152   FILETIME ft, lft;
153   SYSTEMTIME st;
154   DWORD tzid;
155   TIME_ZONE_INFORMATION tzinfo;
156   ULONGLONG time;
157
158   /* time < 0 means a date before midnight of January 1, 1970 */
159   if (*secs < 0) return NULL;
160
161   time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
162
163   ft.dwHighDateTime = (UINT)(time >> 32);
164   ft.dwLowDateTime  = (UINT)time;
165
166   FileTimeToLocalFileTime(&ft, &lft);
167   FileTimeToSystemTime(&lft, &st);
168
169   data->time_buffer.tm_sec  = st.wSecond;
170   data->time_buffer.tm_min  = st.wMinute;
171   data->time_buffer.tm_hour = st.wHour;
172   data->time_buffer.tm_mday = st.wDay;
173   data->time_buffer.tm_year = st.wYear - 1900;
174   data->time_buffer.tm_mon  = st.wMonth  - 1;
175   data->time_buffer.tm_wday = st.wDayOfWeek;
176
177   for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
178     data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
179   }
180
181   data->time_buffer.tm_yday += st.wDay - 1;
182  
183   tzid = GetTimeZoneInformation(&tzinfo);
184
185   if (tzid == TIME_ZONE_ID_INVALID)
186     data->time_buffer.tm_isdst = -1;
187   else 
188     data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
189
190   return &data->time_buffer;
191 }
192
193 /*********************************************************************
194  *      gmtime (MSVCRT.@)
195  */
196 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT_time_t* secs)
197 {
198   thread_data_t * const data = msvcrt_get_thread_data();
199   int i;
200   FILETIME ft;
201   SYSTEMTIME st;
202
203   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
204
205   ft.dwHighDateTime = (UINT)(time >> 32);
206   ft.dwLowDateTime  = (UINT)time;
207
208   FileTimeToSystemTime(&ft, &st);
209
210   if (st.wYear < 1970) return NULL;
211
212   data->time_buffer.tm_sec  = st.wSecond;
213   data->time_buffer.tm_min  = st.wMinute;
214   data->time_buffer.tm_hour = st.wHour;
215   data->time_buffer.tm_mday = st.wDay;
216   data->time_buffer.tm_year = st.wYear - 1900;
217   data->time_buffer.tm_mon  = st.wMonth - 1;
218   data->time_buffer.tm_wday = st.wDayOfWeek;
219   for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
220     data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
221   }
222
223   data->time_buffer.tm_yday += st.wDay - 1;
224   data->time_buffer.tm_isdst = 0;
225
226   return &data->time_buffer;
227 }
228
229 /**********************************************************************
230  *              _strdate (MSVCRT.@)
231  */
232 char* CDECL _strdate(char* date)
233 {
234   LPCSTR format = "MM'/'dd'/'yy";
235
236   GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
237
238   return date;
239 }
240
241 /**********************************************************************
242  *              _wstrdate (MSVCRT.@)
243  */
244 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
245 {
246   static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
247
248   GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
249
250   return date;
251 }
252
253 /*********************************************************************
254  *              _strtime (MSVCRT.@)
255  */
256 char* CDECL _strtime(char* time)
257 {
258   LPCSTR format = "HH':'mm':'ss";
259
260   GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9); 
261
262   return time;
263 }
264
265 /*********************************************************************
266  *              _wstrtime (MSVCRT.@)
267  */
268 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
269 {
270   static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
271
272   GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
273
274   return time;
275 }
276
277 /*********************************************************************
278  *              clock (MSVCRT.@)
279  */
280 MSVCRT_clock_t CDECL MSVCRT_clock(void)
281 {
282   FILETIME ftc, fte, ftk, ftu;
283   ULONGLONG utime, ktime;
284  
285   MSVCRT_clock_t clock;
286
287   GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
288
289   ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
290   utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
291
292   clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
293
294   return clock;
295 }
296
297 /*********************************************************************
298  *              difftime (MSVCRT.@)
299  */
300 double CDECL MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
301 {
302   return (double)(time1 - time2);
303 }
304
305 /*********************************************************************
306  *              _ftime (MSVCRT.@)
307  */
308 void CDECL _ftime(struct MSVCRT__timeb *buf)
309 {
310   TIME_ZONE_INFORMATION tzinfo;
311   FILETIME ft;
312   ULONGLONG time;
313
314   DWORD tzid = GetTimeZoneInformation(&tzinfo);
315   GetSystemTimeAsFileTime(&ft);
316
317   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
318
319   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
320   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
321   buf->timezone = tzinfo.Bias +
322       ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
323       ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
324   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
325 }
326
327 /*********************************************************************
328  *              time (MSVCRT.@)
329  */
330 MSVCRT_time_t CDECL MSVCRT_time(MSVCRT_time_t* buf)
331 {
332   MSVCRT_time_t curtime;
333   struct MSVCRT__timeb tb;
334
335   _ftime(&tb);
336
337   curtime = tb.time;
338   return buf ? *buf = curtime : curtime;
339 }
340
341 /*********************************************************************
342  *              _daylight (MSVCRT.@)
343  */
344 int MSVCRT___daylight = 0;
345
346 /*********************************************************************
347  *              __p_daylight (MSVCRT.@)
348  */
349 int * CDECL MSVCRT___p__daylight(void)
350 {
351         return &MSVCRT___daylight;
352 }
353
354 /*********************************************************************
355  *              _dstbias (MSVCRT.@)
356  */
357 int MSVCRT__dstbias = 0;
358
359 /*********************************************************************
360  *              __p_dstbias (MSVCRT.@)
361  */
362 int * CDECL __p__dstbias(void)
363 {
364     return &MSVCRT__dstbias;
365 }
366
367 /*********************************************************************
368  *              _timezone (MSVCRT.@)
369  */
370 long MSVCRT___timezone = 0;
371
372 /*********************************************************************
373  *              __p_timezone (MSVCRT.@)
374  */
375 long * CDECL MSVCRT___p__timezone(void)
376 {
377         return &MSVCRT___timezone;
378 }
379
380 /*********************************************************************
381  *              _tzname (MSVCRT.@)
382  * NOTES
383  *  Some apps (notably Mozilla) insist on writing to these, so the buffer
384  *  must be large enough.  The size is picked based on observation of
385  *  Windows XP.
386  */
387 static char tzname_std[64] = "";
388 static char tzname_dst[64] = "";
389 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
390
391 /*********************************************************************
392  *              __p_tzname (MSVCRT.@)
393  */
394 char ** CDECL __p__tzname(void)
395 {
396         return MSVCRT__tzname;
397 }
398
399 /*********************************************************************
400  *              _tzset (MSVCRT.@)
401  */
402 void CDECL MSVCRT__tzset(void)
403 {
404     tzset();
405 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
406     MSVCRT___daylight = daylight;
407     MSVCRT___timezone = timezone;
408 #else
409     {
410         static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
411         time_t t;
412         struct tm *tmp;
413         long zone_january, zone_july;
414
415         t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
416         tmp = localtime(&t);
417         zone_january = -tmp->tm_gmtoff;
418         t += seconds_in_year / 2;
419         tmp = localtime(&t);
420         zone_july = -tmp->tm_gmtoff;
421         MSVCRT___daylight = (zone_january != zone_july);
422         MSVCRT___timezone = max(zone_january, zone_july);
423     }
424 #endif
425     lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
426     tzname_std[sizeof(tzname_std) - 1] = '\0';
427     lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
428     tzname_dst[sizeof(tzname_dst) - 1] = '\0';
429 }
430
431 /*********************************************************************
432  *              strftime (MSVCRT.@)
433  */
434 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
435                                      const struct MSVCRT_tm *mstm )
436 {
437     struct tm tm;
438
439     msvcrt_tm_to_unix( &tm, mstm );
440     return strftime( str, max, format, &tm );
441 }
442
443 /*********************************************************************
444  *              wcsftime (MSVCRT.@)
445  */
446 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
447                                      const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
448 {
449     char *s, *fmt;
450     MSVCRT_size_t len;
451
452     TRACE("%p %d %s %p\n", str, max, debugstr_w(format), mstm );
453
454     len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
455     if (!(fmt = MSVCRT_malloc( len ))) return 0;
456     WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
457
458     if ((s = MSVCRT_malloc( max*4 )))
459     {
460         struct tm tm;
461         msvcrt_tm_to_unix( &tm, mstm );
462         if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
463         len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
464         if (len) len--;
465         MSVCRT_free( s );
466     }
467     else len = 0;
468
469     MSVCRT_free( fmt );
470     return len;
471 }
472
473 /*********************************************************************
474  *              asctime (MSVCRT.@)
475  */
476 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
477 {
478     thread_data_t *data = msvcrt_get_thread_data();
479     struct tm tm;
480
481     msvcrt_tm_to_unix( &tm, mstm );
482
483     if (!data->asctime_buffer)
484         data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
485
486     /* FIXME: may want to map from Unix codepage to CP_ACP */
487 #ifdef HAVE_ASCTIME_R
488     asctime_r( &tm, data->asctime_buffer );
489 #else
490     strcpy( data->asctime_buffer, asctime(&tm) );
491 #endif
492     return data->asctime_buffer;
493 }
494
495 /*********************************************************************
496  *              _wasctime (MSVCRT.@)
497  */
498 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
499 {
500     thread_data_t *data = msvcrt_get_thread_data();
501     struct tm tm;
502     char buffer[30];
503
504     msvcrt_tm_to_unix( &tm, mstm );
505
506     if (!data->wasctime_buffer)
507         data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
508 #ifdef HAVE_ASCTIME_R
509     asctime_r( &tm, buffer );
510 #else
511     strcpy( buffer, asctime(&tm) );
512 #endif
513     MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
514     return data->wasctime_buffer;
515 }
516
517 /*********************************************************************
518  *              ctime (MSVCRT.@)
519  */
520 char * CDECL MSVCRT_ctime(const MSVCRT_time_t *time)
521 {
522     return MSVCRT_asctime( MSVCRT_localtime(time) );
523 }
524
525 /*********************************************************************
526  *              _wctime (MSVCRT.@)
527  */
528 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT_time_t *time)
529 {
530     return MSVCRT__wasctime( MSVCRT_localtime(time) );
531 }