Merge pull request #5102 from nigredo-tori/fix-4690

Fix 4690 (WIP)
This commit is contained in:
Andreas Rumpf 2016-12-06 15:02:57 +01:00 • committed by GitHub
commit e53e7d9688

View file

@ -436,6 +436,11 @@ when not defined(JS):
TimeInfoPtr = ptr StructTM TimeInfoPtr = ptr StructTM
Clock {.importc: "clock_t".} = distinct int Clock {.importc: "clock_t".} = distinct int
when not defined(windows):
# This is not ANSI C, but common enough
proc timegm(t: StructTM): Time {.
importc: "timegm", header: "<time.h>", tags: [].}
proc localtime(timer: ptr Time): TimeInfoPtr {. proc localtime(timer: ptr Time): TimeInfoPtr {.
importc: "localtime", header: "<time.h>", tags: [].} importc: "localtime", header: "<time.h>", tags: [].}
proc gmtime(timer: ptr Time): TimeInfoPtr {. proc gmtime(timer: ptr Time): TimeInfoPtr {.
@ -515,20 +520,22 @@ when not defined(JS):
# the conversion is not expensive # the conversion is not expensive
proc timeInfoToTime(timeInfo: TimeInfo): Time = proc timeInfoToTime(timeInfo: TimeInfo): Time =
var cTimeInfo = timeInfo # for C++ we have to make a copy, toTime(timeInfo)
# because the header of mktime is broken in my version of libc
result = mktime(timeInfoToTM(cTimeInfo))
# mktime is defined to interpret the input as local time. As timeInfoToTM
# does ignore the timezone, we need to adjust this here.
result = Time(TimeImpl(result) - getTimezone() + timeInfo.timezone)
proc toTime(timeInfo: TimeInfo): Time = proc toTime(timeInfo: TimeInfo): Time =
var cTimeInfo = timeInfo # for C++ we have to make a copy, var cTimeInfo = timeInfo # for C++ we have to make a copy
# because the header of mktime is broken in my version of libc # because the header of mktime is broken in my version of libc
when defined(windows):
# On Windows `mktime` is broken enough to make this work.
result = mktime(timeInfoToTM(cTimeInfo)) result = mktime(timeInfoToTM(cTimeInfo))
# mktime is defined to interpret the input as local time. As timeInfoToTM # mktime is defined to interpret the input as local time. As timeInfoToTM
# does ignore the timezone, we need to adjust this here. # does ignore the timezone, we need to adjust this here.
result = Time(TimeImpl(result) - getTimezone() + timeInfo.timezone) result = Time(TimeImpl(result) - getTimezone() + timeInfo.timezone)
else:
result = timegm(timeInfoToTM(cTimeInfo))
# As timeInfoToTM does ignore the timezone, we need to adjust this here.
result = Time(TimeImpl(result) + timeInfo.timezone)
const const
epochDiff = 116444736000000000'i64 epochDiff = 116444736000000000'i64