crypt32: Rename a function to reflect its behavior better, and return whether it...
[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 "winnls.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
40
41 static const int MonthLengths[2][12] =
42 {
43     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
44     { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
45 };
46
47 static inline int IsLeapYear(int Year)
48 {
49     return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
50 }
51
52 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
53 {
54     memset( dest, 0, sizeof(*dest) );
55     dest->tm_sec   = src->tm_sec;
56     dest->tm_min   = src->tm_min;
57     dest->tm_hour  = src->tm_hour;
58     dest->tm_mday  = src->tm_mday;
59     dest->tm_mon   = src->tm_mon;
60     dest->tm_year  = src->tm_year;
61     dest->tm_wday  = src->tm_wday;
62     dest->tm_yday  = src->tm_yday;
63     dest->tm_isdst = src->tm_isdst;
64 }
65
66 static inline void unix_tm_to_msvcrt( struct MSVCRT_tm *dest, const struct tm *src )
67 {
68     memset( dest, 0, sizeof(*dest) );
69     dest->tm_sec   = src->tm_sec;
70     dest->tm_min   = src->tm_min;
71     dest->tm_hour  = src->tm_hour;
72     dest->tm_mday  = src->tm_mday;
73     dest->tm_mon   = src->tm_mon;
74     dest->tm_year  = src->tm_year;
75     dest->tm_wday  = src->tm_wday;
76     dest->tm_yday  = src->tm_yday;
77     dest->tm_isdst = src->tm_isdst;
78 }
79
80 #define SECSPERDAY        86400
81 /* 1601 to 1970 is 369 years plus 89 leap days */
82 #define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
83 #define TICKSPERSEC       10000000
84 #define TICKSPERMSEC      10000
85 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
86
87 /**********************************************************************
88  *              _mktime64 (MSVCRT.@)
89  */
90 MSVCRT___time64_t CDECL MSVCRT__mktime64(struct MSVCRT_tm *mstm)
91 {
92     time_t secs;
93     struct tm tm;
94
95     msvcrt_tm_to_unix( &tm, mstm );
96     secs = mktime( &tm );
97     unix_tm_to_msvcrt( mstm, &tm );
98
99     return secs < 0 ? -1 : secs;
100 }
101
102 /**********************************************************************
103  *              _mktime32 (MSVCRT.@)
104  */
105 MSVCRT___time32_t CDECL MSVCRT__mktime32(struct MSVCRT_tm *mstm)
106 {
107     return MSVCRT__mktime64( mstm );
108 }
109
110 /**********************************************************************
111  *              mktime (MSVCRT.@)
112  */
113 #ifdef _WIN64
114 MSVCRT___time64_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
115 {
116     return MSVCRT__mktime64( mstm );
117 }
118 #else
119 MSVCRT___time32_t CDECL MSVCRT_mktime(struct MSVCRT_tm *mstm)
120 {
121     return MSVCRT__mktime32( mstm );
122 }
123 #endif
124
125 /*********************************************************************
126  *      _localtime64 (MSVCRT.@)
127  */
128 struct MSVCRT_tm* CDECL MSVCRT__localtime64(const MSVCRT___time64_t* secs)
129 {
130     struct tm tm;
131     thread_data_t *data;
132     time_t seconds = *secs;
133
134     if (seconds < 0) return NULL;
135
136     if (!localtime_r( &seconds, &tm )) return NULL;
137
138     data = msvcrt_get_thread_data();
139     unix_tm_to_msvcrt( &data->time_buffer, &tm );
140
141     return &data->time_buffer;
142 }
143
144 /*********************************************************************
145  *      _localtime32 (MSVCRT.@)
146  */
147 struct MSVCRT_tm* CDECL MSVCRT__localtime32(const MSVCRT___time32_t* secs)
148 {
149     MSVCRT___time64_t secs64 = *secs;
150     return MSVCRT__localtime64( &secs64 );
151 }
152
153 /*********************************************************************
154  *      localtime (MSVCRT.@)
155  */
156 #ifdef _WIN64
157 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time64_t* secs)
158 {
159     return MSVCRT__localtime64( secs );
160 }
161 #else
162 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT___time32_t* secs)
163 {
164     return MSVCRT__localtime32( secs );
165 }
166 #endif
167
168 /*********************************************************************
169  *      _gmtime64 (MSVCRT.@)
170  */
171 struct MSVCRT_tm* CDECL MSVCRT__gmtime64(const MSVCRT___time64_t* secs)
172 {
173   thread_data_t * const data = msvcrt_get_thread_data();
174   int i;
175   FILETIME ft;
176   SYSTEMTIME st;
177
178   ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
179
180   ft.dwHighDateTime = (UINT)(time >> 32);
181   ft.dwLowDateTime  = (UINT)time;
182
183   FileTimeToSystemTime(&ft, &st);
184
185   if (st.wYear < 1970) return NULL;
186
187   data->time_buffer.tm_sec  = st.wSecond;
188   data->time_buffer.tm_min  = st.wMinute;
189   data->time_buffer.tm_hour = st.wHour;
190   data->time_buffer.tm_mday = st.wDay;
191   data->time_buffer.tm_year = st.wYear - 1900;
192   data->time_buffer.tm_mon  = st.wMonth - 1;
193   data->time_buffer.tm_wday = st.wDayOfWeek;
194   for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
195     data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
196   }
197
198   data->time_buffer.tm_yday += st.wDay - 1;
199   data->time_buffer.tm_isdst = 0;
200
201   return &data->time_buffer;
202 }
203
204 /*********************************************************************
205  *      _gmtime32 (MSVCRT.@)
206  */
207 struct MSVCRT_tm* CDECL MSVCRT__gmtime32(const MSVCRT___time32_t* secs)
208 {
209     MSVCRT___time64_t secs64 = *secs;
210     return MSVCRT__gmtime64( &secs64 );
211 }
212
213 /*********************************************************************
214  *      gmtime (MSVCRT.@)
215  */
216 #ifdef _WIN64
217 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time64_t* secs)
218 {
219     return MSVCRT__gmtime64( secs );
220 }
221 #else
222 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT___time32_t* secs)
223 {
224     return MSVCRT__gmtime32( secs );
225 }
226 #endif
227
228 /**********************************************************************
229  *              _strdate (MSVCRT.@)
230  */
231 char* CDECL _strdate(char* date)
232 {
233   static const char 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* CDECL _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, date, 9);
248
249   return date;
250 }
251
252 /*********************************************************************
253  *              _strtime (MSVCRT.@)
254  */
255 char* CDECL _strtime(char* time)
256 {
257   static const char 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* CDECL _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, time, 9);
272
273   return time;
274 }
275
276 /*********************************************************************
277  *              clock (MSVCRT.@)
278  */
279 MSVCRT_clock_t CDECL 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  *              _difftime64 (MSVCRT.@)
298  */
299 double CDECL MSVCRT__difftime64(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
300 {
301   return (double)(time1 - time2);
302 }
303
304 /*********************************************************************
305  *              _difftime32 (MSVCRT.@)
306  */
307 double CDECL MSVCRT__difftime32(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
308 {
309   return (double)(time1 - time2);
310 }
311
312 /*********************************************************************
313  *              difftime (MSVCRT.@)
314  */
315 #ifdef _WIN64
316 double CDECL MSVCRT_difftime(MSVCRT___time64_t time1, MSVCRT___time64_t time2)
317 {
318     return MSVCRT__difftime64( time1, time2 );
319 }
320 #else
321 double CDECL MSVCRT_difftime(MSVCRT___time32_t time1, MSVCRT___time32_t time2)
322 {
323     return MSVCRT__difftime32( time1, time2 );
324 }
325 #endif
326
327 /*********************************************************************
328  *              _ftime64 (MSVCRT.@)
329  */
330 void CDECL MSVCRT__ftime64(struct MSVCRT___timeb64 *buf)
331 {
332   TIME_ZONE_INFORMATION tzinfo;
333   FILETIME ft;
334   ULONGLONG time;
335
336   DWORD tzid = GetTimeZoneInformation(&tzinfo);
337   GetSystemTimeAsFileTime(&ft);
338
339   time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
340
341   buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
342   buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
343   buf->timezone = tzinfo.Bias +
344       ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
345       ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
346   buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
347 }
348
349 /*********************************************************************
350  *              _ftime32 (MSVCRT.@)
351  */
352 void CDECL MSVCRT__ftime32(struct MSVCRT___timeb32 *buf)
353 {
354     struct MSVCRT___timeb64 buf64;
355
356     MSVCRT__ftime64( &buf64 );
357     buf->time     = buf64.time;
358     buf->millitm  = buf64.millitm;
359     buf->timezone = buf64.timezone;
360     buf->dstflag  = buf64.dstflag;
361 }
362
363 /*********************************************************************
364  *              _ftime (MSVCRT.@)
365  */
366 #ifdef _WIN64
367 void CDECL MSVCRT__ftime(struct MSVCRT___timeb64 *buf)
368 {
369     return MSVCRT__ftime64( buf );
370 }
371 #else
372 void CDECL MSVCRT__ftime(struct MSVCRT___timeb32 *buf)
373 {
374     return MSVCRT__ftime32( buf );
375 }
376 #endif
377
378 /*********************************************************************
379  *              _time64 (MSVCRT.@)
380  */
381 MSVCRT___time64_t CDECL MSVCRT__time64(MSVCRT___time64_t *buf)
382 {
383     MSVCRT___time64_t curtime;
384     struct MSVCRT___timeb64 tb;
385
386     MSVCRT__ftime64(&tb);
387
388     curtime = tb.time;
389     return buf ? *buf = curtime : curtime;
390 }
391
392 /*********************************************************************
393  *              _time32 (MSVCRT.@)
394  */
395 MSVCRT___time32_t CDECL MSVCRT__time32(MSVCRT___time32_t *buf)
396 {
397     MSVCRT___time32_t curtime;
398     struct MSVCRT___timeb64 tb;
399
400     MSVCRT__ftime64(&tb);
401
402     curtime = tb.time;
403     return buf ? *buf = curtime : curtime;
404 }
405
406 /*********************************************************************
407  *              time (MSVCRT.@)
408  */
409 #ifdef _WIN64
410 MSVCRT___time64_t CDECL MSVCRT_time(MSVCRT___time64_t* buf)
411 {
412     return MSVCRT__time64( buf );
413 }
414 #else
415 MSVCRT___time32_t CDECL MSVCRT_time(MSVCRT___time32_t* buf)
416 {
417     return MSVCRT__time32( buf );
418 }
419 #endif
420
421 /*********************************************************************
422  *              _daylight (MSVCRT.@)
423  */
424 int MSVCRT___daylight = 0;
425
426 /*********************************************************************
427  *              __p_daylight (MSVCRT.@)
428  */
429 int * CDECL MSVCRT___p__daylight(void)
430 {
431         return &MSVCRT___daylight;
432 }
433
434 /*********************************************************************
435  *              _dstbias (MSVCRT.@)
436  */
437 int MSVCRT__dstbias = 0;
438
439 /*********************************************************************
440  *              __p_dstbias (MSVCRT.@)
441  */
442 int * CDECL __p__dstbias(void)
443 {
444     return &MSVCRT__dstbias;
445 }
446
447 /*********************************************************************
448  *              _timezone (MSVCRT.@)
449  */
450 MSVCRT_long MSVCRT___timezone = 0;
451
452 /*********************************************************************
453  *              __p_timezone (MSVCRT.@)
454  */
455 MSVCRT_long * CDECL MSVCRT___p__timezone(void)
456 {
457         return &MSVCRT___timezone;
458 }
459
460 /*********************************************************************
461  *              _tzname (MSVCRT.@)
462  * NOTES
463  *  Some apps (notably Mozilla) insist on writing to these, so the buffer
464  *  must be large enough.  The size is picked based on observation of
465  *  Windows XP.
466  */
467 static char tzname_std[64] = "";
468 static char tzname_dst[64] = "";
469 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
470
471 /*********************************************************************
472  *              __p_tzname (MSVCRT.@)
473  */
474 char ** CDECL __p__tzname(void)
475 {
476         return MSVCRT__tzname;
477 }
478
479 /*********************************************************************
480  *              _tzset (MSVCRT.@)
481  */
482 void CDECL MSVCRT__tzset(void)
483 {
484     tzset();
485 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
486     MSVCRT___daylight = daylight;
487     MSVCRT___timezone = timezone;
488 #else
489     {
490         static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
491         time_t t;
492         struct tm *tmp;
493         int zone_january, zone_july;
494
495         t = (time(NULL) / seconds_in_year) * seconds_in_year;
496         tmp = localtime(&t);
497         zone_january = -tmp->tm_gmtoff;
498         t += seconds_in_year / 2;
499         tmp = localtime(&t);
500         zone_july = -tmp->tm_gmtoff;
501         MSVCRT___daylight = (zone_january != zone_july);
502         MSVCRT___timezone = max(zone_january, zone_july);
503     }
504 #endif
505     lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
506     tzname_std[sizeof(tzname_std) - 1] = '\0';
507     lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
508     tzname_dst[sizeof(tzname_dst) - 1] = '\0';
509 }
510
511 /*********************************************************************
512  *              strftime (MSVCRT.@)
513  */
514 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
515                                      const struct MSVCRT_tm *mstm )
516 {
517     struct tm tm;
518
519     msvcrt_tm_to_unix( &tm, mstm );
520     return strftime( str, max, format, &tm );
521 }
522
523 /*********************************************************************
524  *              wcsftime (MSVCRT.@)
525  */
526 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
527                                      const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
528 {
529     char *s, *fmt;
530     MSVCRT_size_t len;
531
532     TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
533
534     len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
535     if (!(fmt = MSVCRT_malloc( len ))) return 0;
536     WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
537
538     if ((s = MSVCRT_malloc( max*4 )))
539     {
540         struct tm tm;
541         msvcrt_tm_to_unix( &tm, mstm );
542         if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
543         len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
544         if (len) len--;
545         MSVCRT_free( s );
546     }
547     else len = 0;
548
549     MSVCRT_free( fmt );
550     return len;
551 }
552
553 /*********************************************************************
554  *              asctime (MSVCRT.@)
555  */
556 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
557 {
558     thread_data_t *data = msvcrt_get_thread_data();
559     struct tm tm;
560
561     msvcrt_tm_to_unix( &tm, mstm );
562
563     if (!data->asctime_buffer)
564         data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
565
566     /* FIXME: may want to map from Unix codepage to CP_ACP */
567 #ifdef HAVE_ASCTIME_R
568     asctime_r( &tm, data->asctime_buffer );
569 #else
570     strcpy( data->asctime_buffer, asctime(&tm) );
571 #endif
572     return data->asctime_buffer;
573 }
574
575 /*********************************************************************
576  *              _wasctime (MSVCRT.@)
577  */
578 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
579 {
580     thread_data_t *data = msvcrt_get_thread_data();
581     struct tm tm;
582     char buffer[30];
583
584     msvcrt_tm_to_unix( &tm, mstm );
585
586     if (!data->wasctime_buffer)
587         data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
588 #ifdef HAVE_ASCTIME_R
589     asctime_r( &tm, buffer );
590 #else
591     strcpy( buffer, asctime(&tm) );
592 #endif
593     MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
594     return data->wasctime_buffer;
595 }
596
597 /*********************************************************************
598  *              _ctime64 (MSVCRT.@)
599  */
600 char * CDECL MSVCRT__ctime64(const MSVCRT___time64_t *time)
601 {
602     struct MSVCRT_tm *t;
603     t = MSVCRT__localtime64( time );
604     if (!t) return NULL;
605     return MSVCRT_asctime( t );
606 }
607
608 /*********************************************************************
609  *              _ctime32 (MSVCRT.@)
610  */
611 char * CDECL MSVCRT__ctime32(const MSVCRT___time32_t *time)
612 {
613     struct MSVCRT_tm *t;
614     t = MSVCRT__localtime32( time );
615     if (!t) return NULL;
616     return MSVCRT_asctime( t );
617 }
618
619 /*********************************************************************
620  *              ctime (MSVCRT.@)
621  */
622 #ifdef _WIN64
623 char * CDECL MSVCRT_ctime(const MSVCRT___time64_t *time)
624 {
625     return MSVCRT__ctime64( time );
626 }
627 #else
628 char * CDECL MSVCRT_ctime(const MSVCRT___time32_t *time)
629 {
630     return MSVCRT__ctime32( time );
631 }
632 #endif
633
634 /*********************************************************************
635  *              _wctime64 (MSVCRT.@)
636  */
637 MSVCRT_wchar_t * CDECL MSVCRT__wctime64(const MSVCRT___time64_t *time)
638 {
639     return MSVCRT__wasctime( MSVCRT__localtime64(time) );
640 }
641
642 /*********************************************************************
643  *              _wctime32 (MSVCRT.@)
644  */
645 MSVCRT_wchar_t * CDECL MSVCRT__wctime32(const MSVCRT___time32_t *time)
646 {
647     return MSVCRT__wasctime( MSVCRT__localtime32(time) );
648 }
649
650 /*********************************************************************
651  *              _wctime (MSVCRT.@)
652  */
653 #ifdef _WIN64
654 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time64_t *time)
655 {
656     return MSVCRT__wctime64( time );
657 }
658 #else
659 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT___time32_t *time)
660 {
661     return MSVCRT__wctime32( time );
662 }
663 #endif