summaryrefslogtreecommitdiff
path: root/tests/libtest/testutil.c
diff options
context:
space:
mode:
authorYang Tse <yangsita@gmail.com>2008-05-09 16:31:51 +0000
committerYang Tse <yangsita@gmail.com>2008-05-09 16:31:51 +0000
commit19479ea0217c93fd2973168084d1bb724eb8a34f (patch)
treea8b59cbb90c38f93c414ec5bebee3c60bd5ae887 /tests/libtest/testutil.c
parentd708ef6731445a84a3d0499a4b13fd09a0dd0520 (diff)
downloadgnurl-19479ea0217c93fd2973168084d1bb724eb8a34f.tar.gz
gnurl-19479ea0217c93fd2973168084d1bb724eb8a34f.tar.bz2
gnurl-19479ea0217c93fd2973168084d1bb724eb8a34f.zip
Internal time differences now use monotonic time source if available.
This also implies the removal of the winmm.lib dependency for WIN32.
Diffstat (limited to 'tests/libtest/testutil.c')
-rw-r--r--tests/libtest/testutil.c103
1 files changed, 52 insertions, 51 deletions
diff --git a/tests/libtest/testutil.c b/tests/libtest/testutil.c
index 146660c71..8ed2b2f08 100644
--- a/tests/libtest/testutil.c
+++ b/tests/libtest/testutil.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@@ -25,69 +25,70 @@
#include "testutil.h"
-#ifndef HAVE_GETTIMEOFDAY
+#if defined(WIN32) && !defined(MSDOS)
-#ifdef WIN32
-#include <mmsystem.h>
-
-static int gettimeofday(struct timeval *tp, void *nothing)
+struct timeval tutil_tvnow(void)
{
-#ifdef WITHOUT_MM_LIB
- SYSTEMTIME st;
- time_t tt;
- struct tm tmtm;
- /* mktime converts local to UTC */
- GetLocalTime (&st);
- tmtm.tm_sec = st.wSecond;
- tmtm.tm_min = st.wMinute;
- tmtm.tm_hour = st.wHour;
- tmtm.tm_mday = st.wDay;
- tmtm.tm_mon = st.wMonth - 1;
- tmtm.tm_year = st.wYear - 1900;
- tmtm.tm_isdst = -1;
- tt = mktime (&tmtm);
- tp->tv_sec = tt;
- tp->tv_usec = st.wMilliseconds * 1000;
-#else
- /**
- ** The earlier time calculations using GetLocalTime
- ** had a time resolution of 10ms.The timeGetTime, part
- ** of multimedia apis offer a better time resolution
- ** of 1ms.Need to link against winmm.lib for this
- **/
- unsigned long Ticks = 0;
- unsigned long Sec =0;
- unsigned long Usec = 0;
- Ticks = timeGetTime();
-
- Sec = Ticks/1000;
- Usec = (Ticks - (Sec*1000))*1000;
- tp->tv_sec = Sec;
- tp->tv_usec = Usec;
-#endif /* WITHOUT_MM_LIB */
- (void)nothing;
- return 0;
+ /*
+ ** GetTickCount() is available on _all_ Windows versions from W95 up
+ ** to nowadays. Returns milliseconds elapsed since last system boot,
+ ** increases monotonically and wraps once 49.7 days have elapsed.
+ */
+ struct timeval now;
+ DWORD milliseconds = GetTickCount();
+ now.tv_sec = milliseconds / 1000;
+ now.tv_usec = (milliseconds % 1000) * 1000;
+ return now;
}
-#else /* WIN32 */
-/* non-win32 version of Curl_gettimeofday() */
-static int gettimeofday(struct timeval *tp, void *nothing)
+
+#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
+
+struct timeval tutil_tvnow(void)
{
- (void)nothing; /* we don't support specific time-zones */
- tp->tv_sec = (long)time(NULL);
- tp->tv_usec = 0;
- return 0;
+ /*
+ ** clock_gettime() is granted to be increased monotonically when the
+ ** monotonic clock is queried. Time starting point is unspecified, it
+ ** could be the system start-up time, the Epoch, or something else,
+ ** in any case the time starting point does not change once that the
+ ** system has started up.
+ */
+ struct timeval now;
+ struct timespec tsnow;
+ (void)clock_gettime(CLOCK_MONOTONIC, &tsnow)
+ now.tv_sec = tsnow.tv_sec;
+ now.tv_usec = tsnow.tv_nsec / 1000;
+ return now;
}
-#endif /* WIN32 */
-#endif /* HAVE_GETTIMEOFDAY */
-/* Return the current time in a timeval struct */
+#elif defined(HAVE_GETTIMEOFDAY)
+
struct timeval tutil_tvnow(void)
{
+ /*
+ ** gettimeofday() is not granted to be increased monotonically, due to
+ ** clock drifting and external source time synchronization it can jump
+ ** forward or backward in time.
+ */
struct timeval now;
(void)gettimeofday(&now, NULL);
return now;
}
+#else
+
+struct timeval tutil_tvnow(void)
+{
+ /*
+ ** time() returns the value of time in seconds since the Epoch.
+ */
+ struct timeval now;
+ now.tv_sec = (long)time(NULL);
+ now.tv_usec = 0;
+ return now;
+}
+
+#endif
+
/*
* Make sure that the first argument is the more recent time, as otherwise
* we'll get a weird negative time-diff back...