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 * mktime (MSVCRT.@)
89 */
90 MSVCRT_time_t CDECL MSVCRT_mktime(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 * localtime (MSVCRT.@)
104 */
105 struct MSVCRT_tm* CDECL MSVCRT_localtime(const MSVCRT_time_t* secs)
106 {
107 struct tm tm;
108 thread_data_t *data;
109 time_t seconds = *secs;
110
111 if (seconds < 0) return NULL;
112
113 if (!localtime_r( &seconds, &tm )) return NULL;
114
115 data = msvcrt_get_thread_data();
116 unix_tm_to_msvcrt( &data->time_buffer, &tm );
117
118 return &data->time_buffer;
119 }
120
121 /*********************************************************************
122 * gmtime (MSVCRT.@)
123 */
124 struct MSVCRT_tm* CDECL MSVCRT_gmtime(const MSVCRT_time_t* secs)
125 {
126 thread_data_t * const data = msvcrt_get_thread_data();
127 int i;
128 FILETIME ft;
129 SYSTEMTIME st;
130
131 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
132
133 ft.dwHighDateTime = (UINT)(time >> 32);
134 ft.dwLowDateTime = (UINT)time;
135
136 FileTimeToSystemTime(&ft, &st);
137
138 if (st.wYear < 1970) return NULL;
139
140 data->time_buffer.tm_sec = st.wSecond;
141 data->time_buffer.tm_min = st.wMinute;
142 data->time_buffer.tm_hour = st.wHour;
143 data->time_buffer.tm_mday = st.wDay;
144 data->time_buffer.tm_year = st.wYear - 1900;
145 data->time_buffer.tm_mon = st.wMonth - 1;
146 data->time_buffer.tm_wday = st.wDayOfWeek;
147 for (i = data->time_buffer.tm_yday = 0; i < st.wMonth - 1; i++) {
148 data->time_buffer.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
149 }
150
151 data->time_buffer.tm_yday += st.wDay - 1;
152 data->time_buffer.tm_isdst = 0;
153
154 return &data->time_buffer;
155 }
156
157 /**********************************************************************
158 * _strdate (MSVCRT.@)
159 */
160 char* CDECL _strdate(char* date)
161 {
162 static const char format[] = "MM'/'dd'/'yy";
163
164 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
165
166 return date;
167 }
168
169 /**********************************************************************
170 * _wstrdate (MSVCRT.@)
171 */
172 MSVCRT_wchar_t* CDECL _wstrdate(MSVCRT_wchar_t* date)
173 {
174 static const WCHAR format[] = { 'M','M','\'','/','\'','d','d','\'','/','\'','y','y',0 };
175
176 GetDateFormatW(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
177
178 return date;
179 }
180
181 /*********************************************************************
182 * _strtime (MSVCRT.@)
183 */
184 char* CDECL _strtime(char* time)
185 {
186 static const char format[] = "HH':'mm':'ss";
187
188 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
189
190 return time;
191 }
192
193 /*********************************************************************
194 * _wstrtime (MSVCRT.@)
195 */
196 MSVCRT_wchar_t* CDECL _wstrtime(MSVCRT_wchar_t* time)
197 {
198 static const WCHAR format[] = { 'H','H','\'',':','\'','m','m','\'',':','\'','s','s',0 };
199
200 GetTimeFormatW(LOCALE_NEUTRAL, 0, NULL, format, time, 9);
201
202 return time;
203 }
204
205 /*********************************************************************
206 * clock (MSVCRT.@)
207 */
208 MSVCRT_clock_t CDECL MSVCRT_clock(void)
209 {
210 FILETIME ftc, fte, ftk, ftu;
211 ULONGLONG utime, ktime;
212
213 MSVCRT_clock_t clock;
214
215 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
216
217 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
218 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
219
220 clock = (utime + ktime) / (TICKSPERSEC / MSVCRT_CLOCKS_PER_SEC);
221
222 return clock;
223 }
224
225 /*********************************************************************
226 * difftime (MSVCRT.@)
227 */
228 double CDECL MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
229 {
230 return (double)(time1 - time2);
231 }
232
233 /*********************************************************************
234 * _ftime (MSVCRT.@)
235 */
236 void CDECL MSVCRT__ftime(struct MSVCRT__timeb *buf)
237 {
238 TIME_ZONE_INFORMATION tzinfo;
239 FILETIME ft;
240 ULONGLONG time;
241
242 DWORD tzid = GetTimeZoneInformation(&tzinfo);
243 GetSystemTimeAsFileTime(&ft);
244
245 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
246
247 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
248 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
249 buf->timezone = tzinfo.Bias +
250 ( tzid == TIME_ZONE_ID_STANDARD ? tzinfo.StandardBias :
251 ( tzid == TIME_ZONE_ID_DAYLIGHT ? tzinfo.DaylightBias : 0 ));
252 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
253 }
254
255 /*********************************************************************
256 * time (MSVCRT.@)
257 */
258 MSVCRT_time_t CDECL MSVCRT_time(MSVCRT_time_t* buf)
259 {
260 MSVCRT_time_t curtime;
261 struct MSVCRT__timeb tb;
262
263 MSVCRT__ftime(&tb);
264
265 curtime = tb.time;
266 return buf ? *buf = curtime : curtime;
267 }
268
269 /*********************************************************************
270 * _daylight (MSVCRT.@)
271 */
272 int MSVCRT___daylight = 0;
273
274 /*********************************************************************
275 * __p_daylight (MSVCRT.@)
276 */
277 int * CDECL MSVCRT___p__daylight(void)
278 {
279 return &MSVCRT___daylight;
280 }
281
282 /*********************************************************************
283 * _dstbias (MSVCRT.@)
284 */
285 int MSVCRT__dstbias = 0;
286
287 /*********************************************************************
288 * __p_dstbias (MSVCRT.@)
289 */
290 int * CDECL __p__dstbias(void)
291 {
292 return &MSVCRT__dstbias;
293 }
294
295 /*********************************************************************
296 * _timezone (MSVCRT.@)
297 */
298 long MSVCRT___timezone = 0;
299
300 /*********************************************************************
301 * __p_timezone (MSVCRT.@)
302 */
303 long * CDECL MSVCRT___p__timezone(void)
304 {
305 return &MSVCRT___timezone;
306 }
307
308 /*********************************************************************
309 * _tzname (MSVCRT.@)
310 * NOTES
311 * Some apps (notably Mozilla) insist on writing to these, so the buffer
312 * must be large enough. The size is picked based on observation of
313 * Windows XP.
314 */
315 static char tzname_std[64] = "";
316 static char tzname_dst[64] = "";
317 char *MSVCRT__tzname[2] = { tzname_std, tzname_dst };
318
319 /*********************************************************************
320 * __p_tzname (MSVCRT.@)
321 */
322 char ** CDECL __p__tzname(void)
323 {
324 return MSVCRT__tzname;
325 }
326
327 /*********************************************************************
328 * _tzset (MSVCRT.@)
329 */
330 void CDECL MSVCRT__tzset(void)
331 {
332 tzset();
333 #if defined(HAVE_TIMEZONE) && defined(HAVE_DAYLIGHT)
334 MSVCRT___daylight = daylight;
335 MSVCRT___timezone = timezone;
336 #else
337 {
338 static const time_t seconds_in_year = (365 * 24 + 6) * 3600;
339 time_t t;
340 struct tm *tmp;
341 long zone_january, zone_july;
342
343 t = (time(NULL) / seconds_in_year) * seconds_in_year;
344 tmp = localtime(&t);
345 zone_january = -tmp->tm_gmtoff;
346 t += seconds_in_year / 2;
347 tmp = localtime(&t);
348 zone_july = -tmp->tm_gmtoff;
349 MSVCRT___daylight = (zone_january != zone_july);
350 MSVCRT___timezone = max(zone_january, zone_july);
351 }
352 #endif
353 lstrcpynA(tzname_std, tzname[0], sizeof(tzname_std));
354 tzname_std[sizeof(tzname_std) - 1] = '\0';
355 lstrcpynA(tzname_dst, tzname[1], sizeof(tzname_dst));
356 tzname_dst[sizeof(tzname_dst) - 1] = '\0';
357 }
358
359 /*********************************************************************
360 * strftime (MSVCRT.@)
361 */
362 MSVCRT_size_t CDECL MSVCRT_strftime( char *str, MSVCRT_size_t max, const char *format,
363 const struct MSVCRT_tm *mstm )
364 {
365 struct tm tm;
366
367 msvcrt_tm_to_unix( &tm, mstm );
368 return strftime( str, max, format, &tm );
369 }
370
371 /*********************************************************************
372 * wcsftime (MSVCRT.@)
373 */
374 MSVCRT_size_t CDECL MSVCRT_wcsftime( MSVCRT_wchar_t *str, MSVCRT_size_t max,
375 const MSVCRT_wchar_t *format, const struct MSVCRT_tm *mstm )
376 {
377 char *s, *fmt;
378 MSVCRT_size_t len;
379
380 TRACE("%p %ld %s %p\n", str, max, debugstr_w(format), mstm );
381
382 len = WideCharToMultiByte( CP_UNIXCP, 0, format, -1, NULL, 0, NULL, NULL );
383 if (!(fmt = MSVCRT_malloc( len ))) return 0;
384 WideCharToMultiByte( CP_UNIXCP, 0, format, -1, fmt, len, NULL, NULL );
385
386 if ((s = MSVCRT_malloc( max*4 )))
387 {
388 struct tm tm;
389 msvcrt_tm_to_unix( &tm, mstm );
390 if (!strftime( s, max*4, fmt, &tm )) s[0] = 0;
391 len = MultiByteToWideChar( CP_UNIXCP, 0, s, -1, str, max );
392 if (len) len--;
393 MSVCRT_free( s );
394 }
395 else len = 0;
396
397 MSVCRT_free( fmt );
398 return len;
399 }
400
401 /*********************************************************************
402 * asctime (MSVCRT.@)
403 */
404 char * CDECL MSVCRT_asctime(const struct MSVCRT_tm *mstm)
405 {
406 thread_data_t *data = msvcrt_get_thread_data();
407 struct tm tm;
408
409 msvcrt_tm_to_unix( &tm, mstm );
410
411 if (!data->asctime_buffer)
412 data->asctime_buffer = MSVCRT_malloc( 30 ); /* ought to be enough */
413
414 /* FIXME: may want to map from Unix codepage to CP_ACP */
415 #ifdef HAVE_ASCTIME_R
416 asctime_r( &tm, data->asctime_buffer );
417 #else
418 strcpy( data->asctime_buffer, asctime(&tm) );
419 #endif
420 return data->asctime_buffer;
421 }
422
423 /*********************************************************************
424 * _wasctime (MSVCRT.@)
425 */
426 MSVCRT_wchar_t * CDECL MSVCRT__wasctime(const struct MSVCRT_tm *mstm)
427 {
428 thread_data_t *data = msvcrt_get_thread_data();
429 struct tm tm;
430 char buffer[30];
431
432 msvcrt_tm_to_unix( &tm, mstm );
433
434 if (!data->wasctime_buffer)
435 data->wasctime_buffer = MSVCRT_malloc( 30*sizeof(MSVCRT_wchar_t) ); /* ought to be enough */
436 #ifdef HAVE_ASCTIME_R
437 asctime_r( &tm, buffer );
438 #else
439 strcpy( buffer, asctime(&tm) );
440 #endif
441 MultiByteToWideChar( CP_UNIXCP, 0, buffer, -1, data->wasctime_buffer, 30 );
442 return data->wasctime_buffer;
443 }
444
445 /*********************************************************************
446 * ctime (MSVCRT.@)
447 */
448 char * CDECL MSVCRT_ctime(const MSVCRT_time_t *time)
449 {
450 struct MSVCRT_tm *t;
451 t = MSVCRT_localtime( time );
452 if (!t) return NULL;
453 return MSVCRT_asctime( t );
454 }
455
456 /*********************************************************************
457 * _wctime (MSVCRT.@)
458 */
459 MSVCRT_wchar_t * CDECL MSVCRT__wctime(const MSVCRT_time_t *time)
460 {
461 return MSVCRT__wasctime( MSVCRT_localtime(time) );
462 }
463
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.