msvcrt: Stub implementation for wcsftime.
[wine/wine64.git] / dlls / msvcrt / time.c
blobb2b67f1ecea5e7ab2bf6de01d856f4e3bcc5398c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
27 #include <time.h>
28 #ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
30 #endif
31 #include <limits.h>
33 #include "msvcrt.h"
34 #include "winbase.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 static const int MonthLengths[2][12] =
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 }
45 static inline int IsLeapYear(int Year)
47 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
50 static inline void msvcrt_tm_to_unix( struct tm *dest, const struct MSVCRT_tm *src )
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;
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)
71 /**********************************************************************
72 * mktime (MSVCRT.@)
74 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
76 MSVCRT_time_t secs;
77 FILETIME lft, uft;
78 ULONGLONG time;
79 struct MSVCRT_tm ts, *ptm;
80 int cleaps, day;
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;
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;
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;
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 */
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;
127 lft.dwHighDateTime = (DWORD) (time >> 32);
128 lft.dwLowDateTime = (DWORD) time;
130 LocalFileTimeToFileTime(&lft, &uft);
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;
141 return secs;
144 /*********************************************************************
145 * localtime (MSVCRT.@)
147 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
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;
156 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
158 ft.dwHighDateTime = (UINT)(time >> 32);
159 ft.dwLowDateTime = (UINT)time;
161 FileTimeToLocalFileTime(&ft, &lft);
162 FileTimeToSystemTime(&lft, &st);
164 if (st.wYear < 1970) return NULL;
166 data->time_buffer.tm_sec = st.wSecond;
167 data->time_buffer.tm_min = st.wMinute;
168 data->time_buffer.tm_hour = st.wHour;
169 data->time_buffer.tm_mday = st.wDay;
170 data->time_buffer.tm_year = st.wYear - 1900;
171 data->time_buffer.tm_mon = st.wMonth - 1;
172 data->time_buffer.tm_wday = st.wDayOfWeek;
174 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
175 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
178 data->time_buffer.tm_yday += st.wDay - 1;
180 tzid = GetTimeZoneInformation(&tzinfo);
182 if (tzid == TIME_ZONE_ID_INVALID)
183 data->time_buffer.tm_isdst = -1;
184 else
185 data->time_buffer.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
187 return &data->time_buffer;
190 /*********************************************************************
191 * gmtime (MSVCRT.@)
193 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
195 thread_data_t * const data = msvcrt_get_thread_data();
196 int i;
197 FILETIME ft;
198 SYSTEMTIME st;
200 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
202 ft.dwHighDateTime = (UINT)(time >> 32);
203 ft.dwLowDateTime = (UINT)time;
205 FileTimeToSystemTime(&ft, &st);
207 if (st.wYear < 1970) return NULL;
209 data->time_buffer.tm_sec = st.wSecond;
210 data->time_buffer.tm_min = st.wMinute;
211 data->time_buffer.tm_hour = st.wHour;
212 data->time_buffer.tm_mday = st.wDay;
213 data->time_buffer.tm_year = st.wYear - 1900;
214 data->time_buffer.tm_mon = st.wMonth - 1;
215 data->time_buffer.tm_wday = st.wDayOfWeek;
216 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
217 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
220 data->time_buffer.tm_yday += st.wDay - 1;
221 data->time_buffer.tm_isdst = 0;
223 return &data->time_buffer;
226 /**********************************************************************
227 * _strdate (MSVCRT.@)
229 char* _strdate(char* date)
231 LPCSTR format = "MM'/'dd'/'yy";
233 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
235 return date;
238 /**********************************************************************
239 * _wstrdate (MSVCRT.@)
241 MSVCRT_wchar_t* _wstrdate(MSVCRT_wchar_t* date)
243 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
245 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)date, 9);
247 return date;
250 /*********************************************************************
251 * _strtime (MSVCRT.@)
253 char* _strtime(char* time)
255 LPCSTR format = "HH':'mm':'ss";
257 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
259 return time;
262 /*********************************************************************
263 * _wstrtime (MSVCRT.@)
265 MSVCRT_wchar_t* _wstrtime(MSVCRT_wchar_t* time)
267 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
269 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, (LPWSTR)time, 9);
271 return time;
274 /*********************************************************************
275 * clock (MSVCRT.@)
277 MSVCRT_clock_t MSVCRT_clock(void)
279 FILETIME ftc, fte, ftk, ftu;
280 ULONGLONG utime, ktime;
282 MSVCRT_clock_t clock;
284 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
286 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
287 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
289 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
291 return clock;
294 /*********************************************************************
295 * difftime (MSVCRT.@)
297 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
299 return (double)(time1 - time2);
302 /*********************************************************************
303 * _ftime (MSVCRT.@)
305 void _ftime(struct MSVCRT__timeb *buf)
307 TIME_ZONE_INFORMATION tzinfo;
308 FILETIME ft;
309 ULONGLONG time;
311 DWORD tzid = GetTimeZoneInformation(&tzinfo);
312 GetSystemTimeAsFileTime(&ft);
314 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
316 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
317 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
318 buf->timezone = tzinfo.Bias +
319 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
320 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
321 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
324 /*********************************************************************
325 * time (MSVCRT.@)
327 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
329 MSVCRT_time_t curtime;
330 struct MSVCRT__timeb tb;
332 _ftime(&tb);
334 curtime = tb.time;
335 return buf ? *buf = curtime : curtime;
338 /*********************************************************************
339 * _daylight (MSVCRT.@)
341 int MSVCRT___daylight = 0;
343 /*********************************************************************
344 * __p_daylight (MSVCRT.@)
346 int *MSVCRT___p__daylight(void)
348 return &MSVCRT___daylight;
351 /*********************************************************************
352 * _dstbias (MSVCRT.@)
354 int MSVCRT__dstbias = 0;
356 /*********************************************************************
357 * __p_dstbias (MSVCRT.@)
359 int *__p__dstbias(void)
361 return &MSVCRT__dstbias;
364 /*********************************************************************
365 * _timezone (MSVCRT.@)
367 long MSVCRT___timezone = 0;
369 /*********************************************************************
370 * __p_timezone (MSVCRT.@)
372 long *MSVCRT___p__timezone(void)
374 return &MSVCRT___timezone;
377 /*********************************************************************
378 * _tzname (MSVCRT.@)
379 * NOTES
380 * Some apps (notably Mozilla) insist on writing to these, so the buffer
381 * must be large enough. The size is picked based on observation of
382 * Windows XP.
384 static char tzname_std[64] = "";
385 static char tzname_dst[64] = "";
386 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
388 /*********************************************************************
389 * __p_tzname (MSVCRT.@)
391 char **__p__tzname(void)
393 return MSVCRT__tzname;
396 /*********************************************************************
397 * _tzset (MSVCRT.@)
399 void MSVCRT__tzset(void)
401 tzset();
402 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
403 MSVCRT___daylight = daylight;
404 MSVCRT___timezone = timezone;
405 #else
407 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
408 time_t t;
409 struct tm *tmp;
410 long zone_january, zone_july;
412 t = (time((time_t *)0) / seconds_in_year) * seconds_in_year;
413 tmp = localtime(&t);
414 zone_january = -tmp->tm_gmtoff;
415 t += seconds_in_year / 2;
416 tmp = localtime(&t);
417 zone_july = -tmp->tm_gmtoff;
418 MSVCRT___daylight = (zone_january != zone_july);
419 MSVCRT___timezone = max(zone_january, zone_july);
421 #endif
422 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
423 tzname_std[sizeof(tzname_std) - 1] = '\0';
424 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
425 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
428 /*********************************************************************
429 * strftime (MSVCRT.@)
431 MSVCRT_size_t MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
432 const struct MSVCRT_tm *mstm )
434 struct tm tm;
436 msvcrt_tm_to_unix( &tm, mstm );
437 return strftime( str, max, format, &tm );
440 /*********************************************************************
441 * wcsftime (MSVCRT.@)
443 MSVCRT_size_t MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
444 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
446 FIXME( "%p, %d, %s, %p - stub\n", str, max, debugstr_w(format), mstm );
448 *str = '\0';
449 return 0;
452 /*********************************************************************
453 * asctime (MSVCRT.@)
455 char *MSVCRT_asctime(const struct MSVCRT_tm *mstm)
457 thread_data_t *data = msvcrt_get_thread_data();
458 struct tm tm;
460 msvcrt_tm_to_unix( &tm, mstm );
462 if (!data->asctime_buffer)
463 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
465 /* FIXME: may want to map from Unix codepage to CP_ACP */
466 #ifdef HAVE_ASCTIME_R
467 asctime_r( &tm, data->asctime_buffer );
468 #else
469 strcpy( data->asctime_buffer, asctime(&tm) );
470 #endif
471 return data->asctime_buffer;
474 /*********************************************************************
475 * _wasctime (MSVCRT.@)
477 MSVCRT_wchar_t *MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
479 thread_data_t *data = msvcrt_get_thread_data();
480 struct tm tm;
481 char buffer[30];
483 msvcrt_tm_to_unix( &tm, mstm );
485 if (!data->wasctime_buffer)
486 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
487 #ifdef HAVE_ASCTIME_R
488 asctime_r( &tm, buffer );
489 #else
490 strcpy( buffer, asctime(&tm) );
491 #endif
492 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
493 return data->wasctime_buffer;
496 /*********************************************************************
497 * ctime (MSVCRT.@)
499 char *MSVCRT_ctime(const MSVCRT_time_t *time)
501 return MSVCRT_asctime( MSVCRT_localtime(time) );
504 /*********************************************************************
505 * _wctime (MSVCRT.@)
507 MSVCRT_wchar_t *MSVCRT__wctime(const MSVCRT_time_t *time)
509 return MSVCRT__wasctime( MSVCRT_localtime(time) );