widl: Output correct alignments in type format strings.
[wine/wine64.git] / dlls / msvcrt / time.c
blob5da2f9c18430f7856638fa70f1779a8ae0ff96cf
1 /*
2 * msvcrt.dll date/time functions
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
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.
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.
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
25 #include "config.h"
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>
34 #include "msvcrt.h"
35 #include "winbase.h"
36 #include "winnls.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
41 static const int MonthLengths[2][12] =
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 }
47 static inline int IsLeapYear(int Year)
49 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
52 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
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;
66 #define SECSPERDAY 86400
67 /* 1601 to 1970 is 369 years plus 89 leap days */
68 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
69 #define TICKSPERSEC 10000000
70 #define TICKSPERMSEC 10000
71 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
73 /**********************************************************************
74 * mktime (MSVCRT.@)
76 MSVCRT_time_t CDECL MSVCRT_mktime(struct MSVCRT_tm *t)
78 MSVCRT_time_t secs;
79 FILETIME lft, uft;
80 ULONGLONG time;
81 struct MSVCRT_tm ts, *ptm;
82 int cleaps, day;
84 ts=*t;
85 /* to prevent arithmetic overflows put constraints on some fields */
86 /* whether the effective date falls in the 1970-2038 time period */
87 /* will be tested later */
88 /* BTW, I have no idea what limits native msvcrt has. */
89 if ( ts.tm_year < 0 || ts.tm_year > 140 ||
90 ts.tm_mon < -840 || ts.tm_mon > 840 ||
91 ts.tm_mday < -20160 || ts.tm_mday > 20160 ||
92 ts.tm_hour < -484000 || ts.tm_hour > 484000 ||
93 ts.tm_min < -29000000 || ts.tm_min > 29000000 )
94 return -1;
96 /* normalize the tm month fields */
97 if( ts.tm_mon > 11) { ts.tm_year += ts.tm_mon / 12; ts.tm_mon %= 12; }
98 if( ts.tm_mon < 0) {
99 int dy = (11 - ts.tm_mon) / 12;
100 ts.tm_year -= dy;
101 ts.tm_mon += dy * 12;
103 /* now calculate a day count from the date
104 * First start counting years from March. This way the leap days
105 * are added at the end of the year, not somewhere in the middle.
106 * Formula's become so much less complicate that way.
107 * To convert: add 12 to the month numbers of Jan and Feb, and
108 * take 1 from the year */
109 if(ts.tm_mon < 2) {
110 ts.tm_mon += 14;
111 ts.tm_year += 1899;
112 } else {
113 ts.tm_mon += 2;
114 ts.tm_year += 1900;
116 cleaps = (3 * (ts.tm_year / 100) + 3) / 4; /* nr of "century leap years"*/
117 day = (36525 * ts.tm_year) / 100 - cleaps + /* year * dayperyr, corrected*/
118 (1959 * ts.tm_mon) / 64 + /* months * daypermonth */
119 ts.tm_mday - /* day of the month */
120 584817 ; /* zero that on 1601-01-01 */
121 /* done */
123 /* convert to 100 ns ticks */
124 time = ((((ULONGLONG) day * 24 +
125 ts.tm_hour) * 60 +
126 ts.tm_min) * 60 +
127 ts.tm_sec ) * TICKSPERSEC;
129 lft.dwHighDateTime = (DWORD) (time >> 32);
130 lft.dwLowDateTime = (DWORD) time;
132 LocalFileTimeToFileTime(&lft, &uft);
134 time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
135 time /= TICKSPERSEC;
136 if( time < SECS_1601_TO_1970 || time > (SECS_1601_TO_1970 + INT_MAX))
137 return -1;
138 secs = time - SECS_1601_TO_1970;
139 /* compute tm_wday, tm_yday and renormalize the other fields of the
140 * tm structure */
141 if ((ptm = MSVCRT_localtime( &secs ))) *t = *ptm;
143 return secs;
146 /*********************************************************************
147 * localtime (MSVCRT.@)
149 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT_time_t* secs)
151 thread_data_t * const data = msvcrt_get_thread_data();
152 int i;
153 FILETIME ft, lft;
154 SYSTEMTIME st;
155 DWORD tzid;
156 TIME_ZONE_INFORMATION tzinfo;
157 ULONGLONG time;
159 /* time < 0 means a date before midnight of January 1, 1970 */
160 if (*secs < 0) return NULL;
162 time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
164 ft.dwHighDateTime = (UINT)(time >> 32);
165 ft.dwLowDateTime = (UINT)time;
167 FileTimeToLocalFileTime(&ft, &lft);
168 FileTimeToSystemTime(&lft, &st);
170 data->time_buffer.tm_sec = st.wSecond;
171 data->time_buffer.tm_min = st.wMinute;
172 data->time_buffer.tm_hour = st.wHour;
173 data->time_buffer.tm_mday = st.wDay;
174 data->time_buffer.tm_year = st.wYear - 1900;
175 data->time_buffer.tm_mon = st.wMonth - 1;
176 data->time_buffer.tm_wday = st.wDayOfWeek;
178 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
179 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
182 data->time_buffer.tm_yday += st.wDay - 1;
184 tzid = GetTimeZoneInformation(&tzinfo);
186 if (tzid == TIME_ZONE_ID_INVALID)
187 data->time_buffer.tm_isdst = -1;
188 else
189 data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
191 return &data->time_buffer;
194 /*********************************************************************
195 * gmtime (MSVCRT.@)
197 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT_time_t* secs)
199 thread_data_t * const data = msvcrt_get_thread_data();
200 int i;
201 FILETIME ft;
202 SYSTEMTIME st;
204 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
206 ft.dwHighDateTime = (UINT)(time >> 32);
207 ft.dwLowDateTime = (UINT)time;
209 FileTimeToSystemTime(&ft, &st);
211 if (st.wYear < 1970) return NULL;
213 data->time_buffer.tm_sec = st.wSecond;
214 data->time_buffer.tm_min = st.wMinute;
215 data->time_buffer.tm_hour = st.wHour;
216 data->time_buffer.tm_mday = st.wDay;
217 data->time_buffer.tm_year = st.wYear - 1900;
218 data->time_buffer.tm_mon = st.wMonth - 1;
219 data->time_buffer.tm_wday = st.wDayOfWeek;
220 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
221 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
224 data->time_buffer.tm_yday += st.wDay - 1;
225 data->time_buffer.tm_isdst = 0;
227 return &data->time_buffer;
230 /**********************************************************************
231 * _strdate (MSVCRT.@)
233 char* CDECL _strdate(char* date)
235 LPCSTR format = "MM'/'dd'/'yy";
237 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
239 return date;
242 /**********************************************************************
243 * _wstrdate (MSVCRT.@)
245 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
247 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
249 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
251 return date;
254 /*********************************************************************
255 * _strtime (MSVCRT.@)
257 char* CDECL _strtime(char* time)
259 LPCSTR format = "HH':'mm':'ss";
261 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
263 return time;
266 /*********************************************************************
267 * _wstrtime (MSVCRT.@)
269 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
271 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
273 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
275 return time;
278 /*********************************************************************
279 * clock (MSVCRT.@)
281 MSVCRT_clock_t CDECL MSVCRT_clock(void)
283 FILETIME ftc, fte, ftk, ftu;
284 ULONGLONG utime, ktime;
286 MSVCRT_clock_t clock;
288 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
290 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
291 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
293 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
295 return clock;
298 /*********************************************************************
299 * difftime (MSVCRT.@)
301 double CDECL MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
303 return (double)(time1 - time2);
306 /*********************************************************************
307 * _ftime (MSVCRT.@)
309 void CDECL _ftime(struct MSVCRT__timeb *buf)
311 TIME_ZONE_INFORMATION tzinfo;
312 FILETIME ft;
313 ULONGLONG time;
315 DWORD tzid = GetTimeZoneInformation(&tzinfo);
316 GetSystemTimeAsFileTime(&ft);
318 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
320 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
321 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
322 buf->timezone = tzinfo.Bias +
323 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
324 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
325 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
328 /*********************************************************************
329 * time (MSVCRT.@)
331 MSVCRT_time_t CDECL MSVCRT_time(MSVCRT_time_t* buf)
333 MSVCRT_time_t curtime;
334 struct MSVCRT__timeb tb;
336 _ftime(&tb);
338 curtime = tb.time;
339 return buf ? *buf = curtime : curtime;
342 /*********************************************************************
343 * _daylight (MSVCRT.@)
345 int MSVCRT___daylight = 0;
347 /*********************************************************************
348 * __p_daylight (MSVCRT.@)
350 int * CDECL MSVCRT___p__daylight(void)
352 return &MSVCRT___daylight;
355 /*********************************************************************
356 * _dstbias (MSVCRT.@)
358 int MSVCRT__dstbias = 0;
360 /*********************************************************************
361 * __p_dstbias (MSVCRT.@)
363 int * CDECL __p__dstbias(void)
365 return &MSVCRT__dstbias;
368 /*********************************************************************
369 * _timezone (MSVCRT.@)
371 long MSVCRT___timezone = 0;
373 /*********************************************************************
374 * __p_timezone (MSVCRT.@)
376 long * CDECL MSVCRT___p__timezone(void)
378 return &MSVCRT___timezone;
381 /*********************************************************************
382 * _tzname (MSVCRT.@)
383 * NOTES
384 * Some apps (notably Mozilla) insist on writing to these, so the buffer
385 * must be large enough. The size is picked based on observation of
386 * Windows XP.
388 static char tzname_std[64] = "";
389 static char tzname_dst[64] = "";
390 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
392 /*********************************************************************
393 * __p_tzname (MSVCRT.@)
395 char ** CDECL __p__tzname(void)
397 return MSVCRT__tzname;
400 /*********************************************************************
401 * _tzset (MSVCRT.@)
403 void CDECL MSVCRT__tzset(void)
405 tzset();
406 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
407 MSVCRT___daylight = daylight;
408 MSVCRT___timezone = timezone;
409 #else
411 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
412 time_t t;
413 struct tm *tmp;
414 long zone_january, zone_july;
416 t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
417 tmp = localtime(&t);
418 zone_january = -tmp->tm_gmtoff;
419 t += seconds_in_year / 2;
420 tmp = localtime(&t);
421 zone_july = -tmp->tm_gmtoff;
422 MSVCRT___daylight = (zone_january != zone_july);
423 MSVCRT___timezone = max(zone_january, zone_july);
425 #endif
426 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
427 tzname_std[sizeof(tzname_std) - 1] = '\0';
428 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
429 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
432 /*********************************************************************
433 * strftime (MSVCRT.@)
435 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
436 const struct MSVCRT_tm *mstm )
438 struct tm tm;
440 msvcrt_tm_to_unix( &tm, mstm );
441 return strftime( str, max, format, &tm );
444 /*********************************************************************
445 * wcsftime (MSVCRT.@)
447 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
448 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
450 char *s, *fmt;
451 MSVCRT_size_t len;
453 TRACE("%p %d %s %p\n", str, max, debugstr_w(format), mstm );
455 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
456 if (!(fmt = MSVCRT_malloc( len ))) return 0;
457 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
459 if ((s = MSVCRT_malloc( max*4 )))
461 struct tm tm;
462 msvcrt_tm_to_unix( &tm, mstm );
463 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
464 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
465 if (len) len--;
466 MSVCRT_free( s );
468 else len = 0;
470 MSVCRT_free( fmt );
471 return len;
474 /*********************************************************************
475 * asctime (MSVCRT.@)
477 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
479 thread_data_t *data = msvcrt_get_thread_data();
480 struct tm tm;
482 msvcrt_tm_to_unix( &tm, mstm );
484 if (!data->asctime_buffer)
485 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
487 /* FIXME: may want to map from Unix codepage to CP_ACP */
488 #ifdef HAVE_ASCTIME_R
489 asctime_r( &tm, data->asctime_buffer );
490 #else
491 strcpy( data->asctime_buffer, asctime(&tm) );
492 #endif
493 return data->asctime_buffer;
496 /*********************************************************************
497 * _wasctime (MSVCRT.@)
499 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
501 thread_data_t *data = msvcrt_get_thread_data();
502 struct tm tm;
503 char buffer[30];
505 msvcrt_tm_to_unix( &tm, mstm );
507 if (!data->wasctime_buffer)
508 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
509 #ifdef HAVE_ASCTIME_R
510 asctime_r( &tm, buffer );
511 #else
512 strcpy( buffer, asctime(&tm) );
513 #endif
514 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
515 return data->wasctime_buffer;
518 /*********************************************************************
519 * ctime (MSVCRT.@)
521 char * CDECL MSVCRT_ctime(const MSVCRT_time_t *time)
523 return MSVCRT_asctime( MSVCRT_localtime(time) );
526 /*********************************************************************
527 * _wctime (MSVCRT.@)
529 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT_time_t *time)
531 return MSVCRT__wasctime( MSVCRT_localtime(time) );