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
28 #ifdef HAVE_SYS_TIMES_H
29 # include <sys/times.h>
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
38 static const int MonthLengths
[2][12] =
40 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
41 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
44 static inline int IsLeapYear(int Year
)
46 return Year
% 4 == 0 && (Year
% 100 != 0 || Year
% 400 == 0);
49 #define SECSPERDAY 86400
50 /* 1601 to 1970 is 369 years plus 89 leap days */
51 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
52 #define TICKSPERSEC 10000000
53 #define TICKSPERMSEC 10000
54 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
56 /* native uses a single static buffer for localtime/gmtime/mktime */
57 static struct MSVCRT_tm tm
;
59 /**********************************************************************
62 MSVCRT_time_t
MSVCRT_mktime(struct MSVCRT_tm
*t
)
70 st
.wSecond
= t
->tm_sec
;
71 st
.wMinute
= t
->tm_min
;
72 st
.wHour
= t
->tm_hour
;
74 st
.wMonth
= t
->tm_mon
+ 1;
75 st
.wYear
= t
->tm_year
+ 1900;
77 SystemTimeToFileTime(&st
, &lft
);
78 LocalFileTimeToFileTime(&lft
, &uft
);
80 time
= ((ULONGLONG
)uft
.dwHighDateTime
<< 32) | uft
.dwLowDateTime
;
81 secs
= time
/ TICKSPERSEC
- SECS_1601_TO_1970
;
82 /* compute tm_wday, tm_yday and renormalize the other fields of the
84 if( MSVCRT_localtime( &secs
)) *t
= tm
;
89 /*********************************************************************
90 * localtime (MSVCRT.@)
92 struct MSVCRT_tm
* MSVCRT_localtime(const MSVCRT_time_t
* secs
)
99 TIME_ZONE_INFORMATION tzinfo
;
101 ULONGLONG time
= *secs
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
103 ft
.dwHighDateTime
= (UINT
)(time
>> 32);
104 ft
.dwLowDateTime
= (UINT
)time
;
106 FileTimeToLocalFileTime(&ft
, &lft
);
107 FileTimeToSystemTime(&lft
, &st
);
109 if (st
.wYear
< 1970) return NULL
;
111 tm
.tm_sec
= st
.wSecond
;
112 tm
.tm_min
= st
.wMinute
;
113 tm
.tm_hour
= st
.wHour
;
114 tm
.tm_mday
= st
.wDay
;
115 tm
.tm_year
= st
.wYear
- 1900;
116 tm
.tm_mon
= st
.wMonth
- 1;
117 tm
.tm_wday
= st
.wDayOfWeek
;
119 for (i
= tm
.tm_yday
= 0; i
< st
.wMonth
- 1; i
++) {
120 tm
.tm_yday
+= MonthLengths
[IsLeapYear(st
.wYear
)][i
];
123 tm
.tm_yday
+= st
.wDay
- 1;
125 tzid
= GetTimeZoneInformation(&tzinfo
);
127 if (tzid
== TIME_ZONE_ID_UNKNOWN
|| tzid
== TIME_ZONE_ID_INVALID
) {
130 tm
.tm_isdst
= (tzid
== TIME_ZONE_ID_DAYLIGHT
?1:0);
136 struct MSVCRT_tm
* MSVCRT_gmtime(const MSVCRT_time_t
* secs
)
143 ULONGLONG time
= *secs
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
145 ft
.dwHighDateTime
= (UINT
)(time
>> 32);
146 ft
.dwLowDateTime
= (UINT
)time
;
148 FileTimeToSystemTime(&ft
, &st
);
150 if (st
.wYear
< 1970) return NULL
;
152 tm
.tm_sec
= st
.wSecond
;
153 tm
.tm_min
= st
.wMinute
;
154 tm
.tm_hour
= st
.wHour
;
155 tm
.tm_mday
= st
.wDay
;
156 tm
.tm_year
= st
.wYear
- 1900;
157 tm
.tm_mon
= st
.wMonth
- 1;
158 tm
.tm_wday
= st
.wDayOfWeek
;
159 for (i
= tm
.tm_yday
= 0; i
< st
.wMonth
- 1; i
++) {
160 tm
.tm_yday
+= MonthLengths
[IsLeapYear(st
.wYear
)][i
];
163 tm
.tm_yday
+= st
.wDay
- 1;
169 /**********************************************************************
170 * _strdate (MSVCRT.@)
172 char* _strdate(char* date
)
174 LPCSTR format
= "MM'/'dd'/'yy";
176 GetDateFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
181 /*********************************************************************
182 * _strtime (MSVCRT.@)
184 char* _strtime(char* date
)
186 LPCSTR format
= "HH':'mm':'ss";
188 GetTimeFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
193 /*********************************************************************
196 MSVCRT_clock_t
MSVCRT_clock(void)
198 FILETIME ftc
, fte
, ftk
, ftu
;
199 ULONGLONG utime
, ktime
;
201 MSVCRT_clock_t clock
;
203 GetProcessTimes(GetCurrentProcess(), &ftc
, &fte
, &ftk
, &ftu
);
205 ktime
= ((ULONGLONG
)ftk
.dwHighDateTime
<< 32) | ftk
.dwLowDateTime
;
206 utime
= ((ULONGLONG
)ftu
.dwHighDateTime
<< 32) | ftu
.dwLowDateTime
;
208 clock
= (utime
+ ktime
) / (TICKSPERSEC
/ MSVCRT_CLOCKS_PER_SEC
);
213 /*********************************************************************
214 * difftime (MSVCRT.@)
216 double MSVCRT_difftime(MSVCRT_time_t time1
, MSVCRT_time_t time2
)
218 return (double)(time1
- time2
);
221 /*********************************************************************
224 void _ftime(struct MSVCRT__timeb
*buf
)
226 TIME_ZONE_INFORMATION tzinfo
;
230 DWORD tzid
= GetTimeZoneInformation(&tzinfo
);
231 GetSystemTimeAsFileTime(&ft
);
233 time
= ((ULONGLONG
)ft
.dwHighDateTime
<< 32) | ft
.dwLowDateTime
;
235 buf
->time
= time
/ TICKSPERSEC
- SECS_1601_TO_1970
;
236 buf
->millitm
= (time
% TICKSPERSEC
) / TICKSPERMSEC
;
237 buf
->timezone
= tzinfo
.Bias
+
238 ( tzid
== TIME_ZONE_ID_STANDARD
? tzinfo
.StandardBias
:
239 ( tzid
== TIME_ZONE_ID_DAYLIGHT
? tzinfo
.DaylightBias
: 0 ));
240 buf
->dstflag
= (tzid
== TIME_ZONE_ID_DAYLIGHT
?1:0);
243 /*********************************************************************
246 MSVCRT_time_t
MSVCRT_time(MSVCRT_time_t
* buf
)
248 MSVCRT_time_t curtime
;
249 struct MSVCRT__timeb tb
;
254 return buf
? *buf
= curtime
: curtime
;
257 /*********************************************************************
258 * _daylight (MSVCRT.@)
260 int MSVCRT___daylight
= 1; /* FIXME: assume daylight */
262 /*********************************************************************
263 * __p_daylight (MSVCRT.@)
265 void *MSVCRT___p__daylight(void)
267 return &MSVCRT___daylight
;