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
)
71 st
.wSecond
= t
->tm_sec
;
72 st
.wMinute
= t
->tm_min
;
73 st
.wHour
= t
->tm_hour
;
75 st
.wMonth
= t
->tm_mon
+ 1;
76 st
.wYear
= t
->tm_year
+ 1900;
78 SystemTimeToFileTime(&st
, &lft
);
79 LocalFileTimeToFileTime(&lft
, &uft
);
81 time
= ((ULONGLONG
)uft
.dwHighDateTime
<< 32) | uft
.dwLowDateTime
;
82 secs
= time
/ TICKSPERSEC
- SECS_1601_TO_1970
;
87 /*********************************************************************
88 * localtime (MSVCRT.@)
90 struct MSVCRT_tm
* MSVCRT_localtime(const MSVCRT_time_t
* secs
)
97 TIME_ZONE_INFORMATION tzinfo
;
99 ULONGLONG time
= *secs
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
101 ft
.dwHighDateTime
= (UINT
)(time
>> 32);
102 ft
.dwLowDateTime
= (UINT
)time
;
104 FileTimeToLocalFileTime(&ft
, &lft
);
105 FileTimeToSystemTime(&lft
, &st
);
107 if (st
.wYear
< 1970) return NULL
;
109 tm
.tm_sec
= st
.wSecond
;
110 tm
.tm_min
= st
.wMinute
;
111 tm
.tm_hour
= st
.wHour
;
112 tm
.tm_mday
= st
.wDay
;
113 tm
.tm_year
= st
.wYear
- 1900;
114 tm
.tm_mon
= st
.wMonth
- 1;
115 tm
.tm_wday
= st
.wDayOfWeek
;
117 for (i
= tm
.tm_yday
= 0; i
< st
.wMonth
- 1; i
++) {
118 tm
.tm_yday
+= MonthLengths
[IsLeapYear(st
.wYear
)][i
];
121 tm
.tm_yday
+= st
.wDay
- 1;
123 tzid
= GetTimeZoneInformation(&tzinfo
);
125 if (tzid
== TIME_ZONE_ID_UNKNOWN
) {
128 tm
.tm_isdst
= (tzid
== TIME_ZONE_ID_DAYLIGHT
?1:0);
134 struct MSVCRT_tm
* MSVCRT_gmtime(const MSVCRT_time_t
* secs
)
141 ULONGLONG time
= *secs
* (ULONGLONG
)TICKSPERSEC
+ TICKS_1601_TO_1970
;
143 ft
.dwHighDateTime
= (UINT
)(time
>> 32);
144 ft
.dwLowDateTime
= (UINT
)time
;
146 FileTimeToSystemTime(&ft
, &st
);
148 if (st
.wYear
< 1970) return NULL
;
150 tm
.tm_sec
= st
.wSecond
;
151 tm
.tm_min
= st
.wMinute
;
152 tm
.tm_hour
= st
.wHour
;
153 tm
.tm_mday
= st
.wDay
;
154 tm
.tm_year
= st
.wYear
- 1900;
155 tm
.tm_mon
= st
.wMonth
- 1;
156 tm
.tm_wday
= st
.wDayOfWeek
;
157 for (i
= tm
.tm_yday
= 0; i
< st
.wMonth
- 1; i
++) {
158 tm
.tm_yday
+= MonthLengths
[IsLeapYear(st
.wYear
)][i
];
161 tm
.tm_yday
+= st
.wDay
- 1;
167 /**********************************************************************
168 * _strdate (MSVCRT.@)
170 char* _strdate(char* date
)
172 LPCSTR format
= "MM'/'dd'/'yy";
174 GetDateFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
179 /*********************************************************************
180 * _strtime (MSVCRT.@)
182 char* _strtime(char* date
)
184 LPCSTR format
= "HH':'mm':'ss";
186 GetTimeFormatA(LOCALE_NEUTRAL
, 0, NULL
, format
, date
, 9);
191 /*********************************************************************
194 MSVCRT_clock_t
MSVCRT_clock(void)
196 FILETIME ftc
, fte
, ftk
, ftu
;
197 ULONGLONG utime
, ktime
;
199 MSVCRT_clock_t clock
;
201 GetProcessTimes(GetCurrentProcess(), &ftc
, &fte
, &ftk
, &ftu
);
203 ktime
= ((ULONGLONG
)ftk
.dwHighDateTime
<< 32) | ftk
.dwLowDateTime
;
204 utime
= ((ULONGLONG
)ftu
.dwHighDateTime
<< 32) | ftu
.dwLowDateTime
;
206 clock
= (utime
+ ktime
) / (TICKSPERSEC
/ MSVCRT_CLOCKS_PER_SEC
);
211 /*********************************************************************
212 * difftime (MSVCRT.@)
214 double MSVCRT_difftime(MSVCRT_time_t time1
, MSVCRT_time_t time2
)
216 return (double)(time1
- time2
);
219 /*********************************************************************
222 void _ftime(struct MSVCRT__timeb
*buf
)
224 TIME_ZONE_INFORMATION tzinfo
;
228 DWORD tzid
= GetTimeZoneInformation(&tzinfo
);
229 GetSystemTimeAsFileTime(&ft
);
231 time
= ((ULONGLONG
)ft
.dwHighDateTime
<< 32) | ft
.dwLowDateTime
;
233 buf
->time
= time
/ TICKSPERSEC
- SECS_1601_TO_1970
;
234 buf
->millitm
= (time
% TICKSPERSEC
) / TICKSPERMSEC
;
235 buf
->timezone
= tzinfo
.Bias
;
236 buf
->dstflag
= (tzid
== TIME_ZONE_ID_DAYLIGHT
?1:0);
239 /*********************************************************************
242 MSVCRT_time_t
MSVCRT_time(MSVCRT_time_t
* buf
)
244 MSVCRT_time_t curtime
;
245 struct MSVCRT__timeb tb
;
250 return buf
? *buf
= curtime
: curtime
;
253 /*********************************************************************
254 * _daylight (MSVCRT.@)
256 int MSVCRT___daylight
= 1; /* FIXME: assume daylight */
258 /*********************************************************************
259 * __p_daylight (MSVCRT.@)
261 void *MSVCRT___p__daylight(void)
263 return &MSVCRT___daylight
;