Environment: OS Win10, timezone: UTC +7
When I change local timezone to UTC, this code works fine.
Found out that the issue doesn't appear on macOS. Seems like it's Windows-only.
Can you give the output of
I would run it myself, but I don't have any functioning Windows environment currently.
@flxy on my machine
This works correctly:
Output (+01:00; wil not work on +07:00, try 3600 * 7 there):
3600 * 7
The problem is that on Windows, mktime returns -1 if the given date interpreted as local time lies before January 1, 1970. On OSX, it returns the correct negative amount of seconds. So this seems to be a Windows limitation, and the only thing that can be done here is to check the result of mktime for -1 on Windows and if true, raise some exception.
Yes, works correctly starting with 3600 * 7 - 1 for UTC +7.
Checked the output of mktime, it returns -1 indeed. And if I understand correctly, since mktime accepts local time, TimeInfo is being converted to local time before calling mktime and there is where the problem lies.
So, if add, like, 1 day to TimeInfo before calling toTime and substract 1 day after that, everyting will work fine. Something like that should do the trick:
3600 * 7 - 1
proc toTimeSafe*(timeInfo: TimeInfo): Time =
if timeInfo.year == 1970 and timeInfo.month == mJan and timeInfo.monthDay == 1:
var safeTimeInfo = timeInfo
safeTimeInfo.monthDay += 1
return safeTimeInfo.toTime() - 1.days
Found function _mkgmtime on Windows that accepts time in GMT rather than local time. Using this instead of mktime may also help. And for Linux there is timegm which do the same.
Fix TimeInfo to Time conversion. Fixes #5065.