Fixed buffer overflow.
[wine/multimedia.git] / dlls / msvcrt / time.c
blob42ff2be9dc6bf61d9ba8f48a30faedd285eb3caa
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
32 #include "msvcrt.h"
33 #include "msvcrt/sys/timeb.h"
34 #include "msvcrt/time.h"
36 #include "winbase.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
42 static const int MonthLengths[2][12] =
44 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
45 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
48 static inline int IsLeapYear(int Year)
50 return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
53 #define SECSPERDAY 86400
54 /* 1601 to 1970 is 369 years plus 89 leap days */
55 #define SECS_1601_TO_1970 ((369 * 365 + 89) * (ULONGLONG)SECSPERDAY)
56 #define TICKSPERSEC 10000000
57 #define TICKSPERMSEC 10000
58 #define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
60 /* native uses a single static buffer for localtime/gmtime/mktime */
61 static struct MSVCRT_tm tm;
63 /**********************************************************************
64 * mktime (MSVCRT.@)
66 MSVCRT_time_t MSVCRT_mktime(struct MSVCRT_tm *t)
68 MSVCRT_time_t secs;
70 SYSTEMTIME st;
71 FILETIME lft, uft;
72 ULONGLONG time;
74 st.wMilliseconds = 0;
75 st.wSecond = t->tm_sec;
76 st.wMinute = t->tm_min;
77 st.wHour = t->tm_hour;
78 st.wDay = t->tm_mday;
79 st.wMonth = t->tm_mon + 1;
80 st.wYear = t->tm_year + 1900;
82 SystemTimeToFileTime(&st, &lft);
83 LocalFileTimeToFileTime(&lft, &uft);
85 time = ((ULONGLONG)uft.dwHighDateTime << 32) | uft.dwLowDateTime;
86 secs = time / TICKSPERSEC - SECS_1601_TO_1970;
88 return secs;
91 /*********************************************************************
92 * localtime (MSVCRT.@)
94 struct MSVCRT_tm* MSVCRT_localtime(const MSVCRT_time_t* secs)
96 int i;
98 FILETIME ft, lft;
99 SYSTEMTIME st;
100 DWORD tzid;
101 TIME_ZONE_INFORMATION tzinfo;
103 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
105 ft.dwHighDateTime = (UINT)(time >> 32);
106 ft.dwLowDateTime = (UINT)time;
108 FileTimeToLocalFileTime(&ft, &lft);
109 FileTimeToSystemTime(&lft, &st);
111 if (st.wYear < 1970) return NULL;
113 tm.tm_sec = st.wSecond;
114 tm.tm_min = st.wMinute;
115 tm.tm_hour = st.wHour;
116 tm.tm_mday = st.wDay;
117 tm.tm_year = st.wYear - 1900;
118 tm.tm_mon = st.wMonth - 1;
119 tm.tm_wday = st.wDayOfWeek;
121 for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
122 tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
125 tm.tm_yday += st.wDay - 1;
127 tzid = GetTimeZoneInformation(&tzinfo);
129 if (tzid == TIME_ZONE_ID_UNKNOWN) {
130 tm.tm_isdst = -1;
131 } else {
132 tm.tm_isdst = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
135 return &tm;
138 struct MSVCRT_tm* MSVCRT_gmtime(const MSVCRT_time_t* secs)
140 int i;
142 FILETIME ft;
143 SYSTEMTIME st;
145 ULONGLONG time = *secs * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
147 ft.dwHighDateTime = (UINT)(time >> 32);
148 ft.dwLowDateTime = (UINT)time;
150 FileTimeToSystemTime(&ft, &st);
152 if (st.wYear < 1970) return NULL;
154 tm.tm_sec = st.wSecond;
155 tm.tm_min = st.wMinute;
156 tm.tm_hour = st.wHour;
157 tm.tm_mday = st.wDay;
158 tm.tm_year = st.wYear - 1900;
159 tm.tm_mon = st.wMonth - 1;
160 tm.tm_wday = st.wDayOfWeek;
161 for (i = tm.tm_yday = 0; i < st.wMonth - 1; i++) {
162 tm.tm_yday += MonthLengths[IsLeapYear(st.wYear)][i];
165 tm.tm_yday += st.wDay - 1;
166 tm.tm_isdst = 0;
168 return &tm;
171 /**********************************************************************
172 * _strdate (MSVCRT.@)
174 char* _strdate(char* date)
176 LPCSTR format = "MM'/'dd'/'yy";
178 GetDateFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
180 return date;
183 /*********************************************************************
184 * _strtime (MSVCRT.@)
186 char* _strtime(char* date)
188 LPCSTR format = "HH':'mm':'ss";
190 GetTimeFormatA(LOCALE_NEUTRAL, 0, NULL, format, date, 9);
192 return date;
195 /*********************************************************************
196 * clock (MSVCRT.@)
198 MSVCRT_clock_t MSVCRT_clock(void)
200 FILETIME ftc, fte, ftk, ftu;
201 ULONGLONG utime, ktime;
203 MSVCRT_clock_t clock;
205 GetProcessTimes(GetCurrentProcess(), &ftc, &fte, &ftk, &ftu);
207 ktime = ((ULONGLONG)ftk.dwHighDateTime << 32) | ftk.dwLowDateTime;
208 utime = ((ULONGLONG)ftu.dwHighDateTime << 32) | ftu.dwLowDateTime;
210 clock = ((utime + ktime) / TICKSPERSEC) * CLOCKS_PER_SEC;
212 return clock;
215 /*********************************************************************
216 * difftime (MSVCRT.@)
218 double MSVCRT_difftime(MSVCRT_time_t time1, MSVCRT_time_t time2)
220 return (double)(time1 - time2);
223 /*********************************************************************
224 * time (MSVCRT.@)
226 MSVCRT_time_t MSVCRT_time(MSVCRT_time_t* buf)
228 MSVCRT_time_t curtime;
229 struct _timeb tb;
231 _ftime(&tb);
233 curtime = tb.time;
234 return buf ? *buf = curtime : curtime;
237 /*********************************************************************
238 * _ftime (MSVCRT.@)
240 void _ftime(struct _timeb *buf)
242 TIME_ZONE_INFORMATION tzinfo;
243 FILETIME ft;
244 ULONGLONG time;
246 DWORD tzid = GetTimeZoneInformation(&tzinfo);
247 GetSystemTimeAsFileTime(&ft);
249 time = ((ULONGLONG)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
251 buf->time = time / TICKSPERSEC - SECS_1601_TO_1970;
252 buf->millitm = (time % TICKSPERSEC) / TICKSPERMSEC;
253 buf->timezone = tzinfo.Bias;
254 buf->dstflag = (tzid == TIME_ZONE_ID_DAYLIGHT?1:0);
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;