Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Environment variable 'TZ' affects Unix.gettimeofday #6671
Original bug ID: 6671
When running some experiments with timing functions on Windows, I ran into a problem that occurred only in Cygwin's Bash: gettimeofday provided non-UTC timestamps. After some code inspection, here and there, I found the culprit: the TZ environment variable affects gettimeofday and it shouldn't.
Unix.gettimeofday under Windows (mingw and msvc) uses C's 'mktime' to convert a Windows-specific local time (provided by 'GetLocalTime') to a Unix timestamp. However 'mktime' (here Microsoft's one in msvcrt) is sensible to the TZ environment variable. All would be well if 'GetLocalTime' would also take TZ into account. But obviously it does not. Consequently, a wrong time offset appears if TZ is set and is not the current local time zone of Windows.
A possible patch is provided (more information below).
Steps to reproduce
Assuming you don't live in the GMT timezone, run this program with a setting TZ to "" (for instance):
let () = Printf.printf "time=%.3f\ngtod=%.3f\n" (Unix.time ()) (Unix.gettimeofday ())
It may return something like that
Why does it appears inside a Cygwin shell? TZ as some legitimate uses in Cygwin. And Cygwin defines by default TZ to the current time zone of the system (Europe/Paris for me). In this particular case, it should be good, no? obviously not! Microsoft and Cygwin do not have the same understanding of what is a good TZ value.
One possible solution is to not use 'mktime' (patch provided).
However, as you may have noticed from the get-go, this problem is not so current, likely, because the main use of gettimeofday is to measure time periods and beautifully cancels any inappropriate time offset. And because a simple workaround exists (unsetting any TZ before calling an OCaml program), my patch might not be worth its future cost in maintenance and well possibly debugging. However, if you have other ideas...
TZ and cygwin:
Comment author: mdelahaye
Better patch that let Microsoft do the arithmetic ;)