aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2005-08-03 16:28:39 +0000
committerNick Mathewson <nickm@torproject.org>2005-08-03 16:28:39 +0000
commit176e9055ccfb26b6f37e90f8531ac35048a1af2a (patch)
tree20f5e1f79930b83fe997a967e37799e6ee34aab1
parentd90841ff5ad77e2f637e4a96cf9f60c35efcffee (diff)
downloadtor-176e9055ccfb26b6f37e90f8531ac35048a1af2a.tar
tor-176e9055ccfb26b6f37e90f8531ac35048a1af2a.tar.gz
Probably broken attempt to improve tor_gettimeofday granularity on windows.
svn:r4683
-rw-r--r--src/common/compat.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 2f7d0399c..316c335e1 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -23,6 +23,7 @@ const char compat_c_id[] = "$Id$";
#ifdef MS_WINDOWS
#include <process.h>
+
#endif
#ifdef HAVE_UNAME
#include <sys/utsname.h>
@@ -819,7 +820,27 @@ spawn_exit()
void
tor_gettimeofday(struct timeval *timeval)
{
-#ifdef HAVE_GETTIMEOFDAY
+#ifdef MS_WINDOWS
+ /* Epoch bias copied from perl: number of units between windows epoch and
+ * unix epoch. */
+#define EPOCH_BIAS U64_LITERAL(116444736000000000)
+#define UNITS_PER_SEC U64_LITERAL(10000000)
+#define USEC_PER_SEC U64_LITERAL(1000000)
+#define UNITS_PER_USEC U64_LITERAL(10)
+ union {
+ uint64_t ft_64;
+ FILETIME ft_ft;
+ } ft;
+ /* number of 100-nsec units since Jan 1, 1601 */
+ GetSystemTimeAsFileTime(&ft.ft_ft);
+ if (ft.ft_64 < EPOCH_BIAS) {
+ log_fn(LOG_ERR, "System time is before 1970; failing.");
+ exit(1);
+ }
+ ft.ft_64 -= EPOCH_BIAS;
+ tv->tv_sec = ft.ft_64 / UNITS_PER_SEC;
+ tv->tv_usec = (ft.ft_64 / UNITS_PER_USEC) % USEC_PER_SEC;
+#elif defined(HAVE_GETTIMEOFDAY)
if (gettimeofday(timeval, NULL)) {
log_fn(LOG_ERR, "gettimeofday failed.");
/* If gettimeofday dies, we have either given a bad timezone (we didn't),