I'm writing a portable Socket class that supports timeouts for both sending and receiving... To implement these timeouts I'm using select()
.... But, I sometimes need to know how long I was blocked inside select()
which of course on Linux I would implement by calling gettimeofday()
before and after I call select()
and then using timersub()
to calculate the delta...
Given that select()
on Windows accepts struct timeval
for it's timeout, what method should I used to replace gettimeofday() on Windows?
I ended up finding this page:
gettimeofday()
function for Windows (now via the Wayback Machine) which has a handy, dandy implementation ofgettimeofday()
on Windows. It uses theGetSystemTimeAsFileTime()
method to get an accurate clock.Update: Here's an alternative active link from the 'Unix to Windows Porting Dictionary for HPC'
gettimeofday()
(now via the Wayback Machine) that points to the implementation the OP referred to. Note also that there's a typo in the linked implementation:The values shown are missing an extra
0
at the end (they assumed microseconds, not the number of 100-nanosecond intervals). This typo was found via this comment on a Google code project page. The correct values to use are shown below:PostgreSQL's implementation of gettimeofday for Windows: