1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
7 * \file tor_gettimeofday.c
8 * \brief Implementat gettimeofday() for windows, and other platforms without
13 #include "lib/err/torerr.h"
14 #include "lib/wallclock/tor_gettimeofday.h"
15 #include "lib/cc/torint.h"
20 #ifdef HAVE_SYS_TIME_H
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
32 #ifndef HAVE_GETTIMEOFDAY
34 #include <sys/timeb.h>
38 /** Set *timeval to the current time of day. On error, log and terminate.
39 * (Same as gettimeofday(timeval,NULL), but never returns -1.)
42 tor_gettimeofday
, (struct timeval
*timeval
))
45 /* Epoch bias copied from perl: number of units between windows epoch and
47 #define EPOCH_BIAS UINT64_C(116444736000000000)
48 #define UNITS_PER_SEC UINT64_C(10000000)
49 #define USEC_PER_SEC UINT64_C(1000000)
50 #define UNITS_PER_USEC UINT64_C(10)
55 /* number of 100-nsec units since Jan 1, 1601 */
56 GetSystemTimeAsFileTime(&ft
.ft_ft
);
57 if (ft
.ft_64
< EPOCH_BIAS
) {
59 raw_assert_unreached_msg("System time is before 1970; failing.");
62 ft
.ft_64
-= EPOCH_BIAS
;
63 timeval
->tv_sec
= (unsigned) (ft
.ft_64
/ UNITS_PER_SEC
);
64 timeval
->tv_usec
= (unsigned) ((ft
.ft_64
/ UNITS_PER_USEC
) % USEC_PER_SEC
);
65 #elif defined(HAVE_GETTIMEOFDAY)
66 if (gettimeofday(timeval
, NULL
)) {
68 /* If gettimeofday dies, we have either given a bad timezone (we didn't),
70 raw_assert_unreached_msg("gettimeofday failed");
73 #elif defined(HAVE_FTIME)
76 timeval
->tv_sec
= tb
.time
;
77 timeval
->tv_usec
= tb
.millitm
* 1000;
79 #error "No way to get time."
80 #endif /* defined(_WIN32) || ... */