From: Gurusamy Sarathy Date: Tue, 25 Sep 2001 15:19:13 +0000 (+0000) Subject: avoid the use of ftime() (it does a useless, potentially X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eb1a5092cedc56e09684f2bb18507cb7a80dafdb;p=p5sagit%2Fp5-mst-13.2.git avoid the use of ftime() (it does a useless, potentially expensive call to GetTimeZoneInformation()); this potentially also results in three more digits of precision from Time::HiRes::time() p4raw-id: //depot/perl@12199 --- diff --git a/ext/Time/HiRes/HiRes.xs b/ext/Time/HiRes/HiRes.xs index a0349fa..8e5be07 100644 --- a/ext/Time/HiRes/HiRes.xs +++ b/ext/Time/HiRes/HiRes.xs @@ -62,16 +62,32 @@ struct timeval { long tv_usec; } */ -#include +typedef union { + unsigned __int64 ft_i64; + FILETIME ft_val; +} FT_t; + +/* Number of 100 nanosecond units from 1/1/1601 to 1/1/1970 */ +#define EPOCH_BIAS 116444736000000000i64 + +/* NOTE: This does not compute the timezone info (doing so can be expensive, + * and appears to be unsupported even by glibc) */ int -gettimeofday (struct timeval *tp, int nothing) +gettimeofday (struct timeval *tp, void *not_used) { - struct _timeb timebuffer; - _ftime( &timebuffer ); - tp->tv_sec = timebuffer.time; - tp->tv_usec = timebuffer.millitm * 1000; - return 0; + FT_t ft; + + /* this returns time in 100-nanosecond units (i.e. tens of usecs) */ + GetSystemTimeAsFileTime(&ft.ft_val); + + /* seconds since epoch */ + tp->tv_sec = (long)((ft.ft_i64 - EPOCH_BIAS) / 10000000i64); + + /* microseconds remaining */ + tp->tv_usec = (long)((ft.ft_i64 / 10i64) % 1000000i64); + + return 0; } #endif