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