[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] Probably broken attempt to improve tor_gettimeofday granula...
Update of /home/or/cvsroot/tor/src/common
In directory moria:/tmp/cvs-serv4829/src/common
Modified Files:
compat.c
Log Message:
Probably broken attempt to improve tor_gettimeofday granularity on windows.
Index: compat.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/compat.c,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -d -r1.61 -r1.62
--- compat.c 22 Jul 2005 21:12:10 -0000 1.61
+++ compat.c 3 Aug 2005 16:28:39 -0000 1.62
@@ -23,6 +23,7 @@
#ifdef MS_WINDOWS
#include <process.h>
+
#endif
#ifdef HAVE_UNAME
#include <sys/utsname.h>
@@ -819,7 +820,27 @@
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),