From 96e9babe10af96c6561da220c224b1e94cbfd508 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 14 Apr 2010 06:55:19 +0200 Subject: win32: add gettimeofday() --- win32/mingw.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/win32/mingw.c b/win32/mingw.c index 3611872ec..020e9c420 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -15,3 +15,43 @@ int mkstemp(char *template) return -1; return open(filename, O_RDWR | O_CREAT, 0600); } + +/* + * This is like mktime, but without normalization of tm_wday and tm_yday. + */ +static time_t tm_to_time_t(const struct tm *tm) +{ + static const int mdays[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 + }; + int year = tm->tm_year - 70; + int month = tm->tm_mon; + int day = tm->tm_mday; + + if (year < 0 || year > 129) /* algo only works for 1970-2099 */ + return -1; + if (month < 0 || month > 11) /* array bounds */ + return -1; + if (month < 2 || (year + 2) % 4) + day--; + return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + + tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; +} + +int gettimeofday(struct timeval *tv, void *tz) +{ + SYSTEMTIME st; + struct tm tm; + GetSystemTime(&st); + tm.tm_year = st.wYear-1900; + tm.tm_mon = st.wMonth-1; + tm.tm_mday = st.wDay; + tm.tm_hour = st.wHour; + tm.tm_min = st.wMinute; + tm.tm_sec = st.wSecond; + tv->tv_sec = tm_to_time_t(&tm); + if (tv->tv_sec < 0) + return -1; + tv->tv_usec = st.wMilliseconds*1000; + return 0; +} -- cgit v1.2.3-55-g6feb