aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2010-04-14 06:55:19 +0200
committerNguyễn Thái Ngọc Duy <pclouds@gmail.com>2010-09-10 18:39:43 +1000
commit96e9babe10af96c6561da220c224b1e94cbfd508 (patch)
tree51a2c6ab13aad4b715fed1f501eeb2c6b26f5ed4
parentb6a1a2ff65a5755d801654f7a41b4854082c96e1 (diff)
downloadbusybox-w32-96e9babe10af96c6561da220c224b1e94cbfd508.tar.gz
busybox-w32-96e9babe10af96c6561da220c224b1e94cbfd508.tar.bz2
busybox-w32-96e9babe10af96c6561da220c224b1e94cbfd508.zip
win32: add gettimeofday()
-rw-r--r--win32/mingw.c40
1 files changed, 40 insertions, 0 deletions
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)
15 return -1; 15 return -1;
16 return open(filename, O_RDWR | O_CREAT, 0600); 16 return open(filename, O_RDWR | O_CREAT, 0600);
17} 17}
18
19/*
20 * This is like mktime, but without normalization of tm_wday and tm_yday.
21 */
22static time_t tm_to_time_t(const struct tm *tm)
23{
24 static const int mdays[] = {
25 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
26 };
27 int year = tm->tm_year - 70;
28 int month = tm->tm_mon;
29 int day = tm->tm_mday;
30
31 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
32 return -1;
33 if (month < 0 || month > 11) /* array bounds */
34 return -1;
35 if (month < 2 || (year + 2) % 4)
36 day--;
37 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
38 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
39}
40
41int gettimeofday(struct timeval *tv, void *tz)
42{
43 SYSTEMTIME st;
44 struct tm tm;
45 GetSystemTime(&st);
46 tm.tm_year = st.wYear-1900;
47 tm.tm_mon = st.wMonth-1;
48 tm.tm_mday = st.wDay;
49 tm.tm_hour = st.wHour;
50 tm.tm_min = st.wMinute;
51 tm.tm_sec = st.wSecond;
52 tv->tv_sec = tm_to_time_t(&tm);
53 if (tv->tv_sec < 0)
54 return -1;
55 tv->tv_usec = st.wMilliseconds*1000;
56 return 0;
57}