From 50a1267b7d028f18df39523fd90399f0751180a4 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Wed, 30 Nov 2016 20:28:17 +0700 Subject: [PATCH 1/2] Fix TimeInfo to Time conversion. Fixes #5065. --- lib/pure/times.nim | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 1767a37be3..e5b4f383f3 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -436,6 +436,13 @@ when not defined(JS): TimeInfoPtr = ptr StructTM Clock {.importc: "clock_t".} = distinct int + when defined(windows): + proc timegm(t: StructTM): Time {. + importc: "_mkgmtime", header: "", tags: [].} + else: + proc timegm(t: StructTM): Time {. + importc: "timegm", header: "", tags: [].} + proc localtime(timer: ptr Time): TimeInfoPtr {. importc: "localtime", header: "", tags: [].} proc gmtime(timer: ptr Time): TimeInfoPtr {. @@ -516,19 +523,17 @@ when not defined(JS): proc timeInfoToTime(timeInfo: TimeInfo): Time = var cTimeInfo = timeInfo # for C++ we have to make a copy, - # 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) + # because the header of timegm is broken in my version of libc + result = timegm(timeInfoToTM(cTimeInfo)) + # As timeInfoToTM does ignore the timezone, we need to adjust this here. + result = Time(TimeImpl(result) + timeInfo.timezone) proc toTime(timeInfo: TimeInfo): Time = var cTimeInfo = timeInfo # for C++ we have to make a copy, - # 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) + # because the header of timegm is broken in my version of libc + result = timegm(timeInfoToTM(cTimeInfo)) + # As timeInfoToTM does ignore the timezone, we need to adjust this here. + result = Time(TimeImpl(result) + timeInfo.timezone) const epochDiff = 116444736000000000'i64 From dc8ee72eb6069c181621a3dc4ad97f8cced9db4b Mon Sep 17 00:00:00 2001 From: Dmitry Polienko Date: Tue, 6 Dec 2016 14:16:00 +0700 Subject: [PATCH 2/2] Revert @endragor's fix for Windows _mkgmtime is not supported by mingw because of the older msvcrt versions. Fortunately, mktime implementation in Windows is just broken enough to make the initial implementation work... As far as I can tell, this works with both *nix-like platforms and Win, and fixes #4690 --- lib/pure/times.nim | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/pure/times.nim b/lib/pure/times.nim index e5b4f383f3..cf4e7dde64 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -436,10 +436,8 @@ when not defined(JS): TimeInfoPtr = ptr StructTM Clock {.importc: "clock_t".} = distinct int - when defined(windows): - proc timegm(t: StructTM): Time {. - importc: "_mkgmtime", header: "", tags: [].} - else: + when not defined(windows): + # This is not ANSI C, but common enough proc timegm(t: StructTM): Time {. importc: "timegm", header: "", tags: [].} @@ -522,18 +520,22 @@ when not defined(JS): # the conversion is not expensive proc timeInfoToTime(timeInfo: TimeInfo): Time = - var cTimeInfo = timeInfo # for C++ we have to make a copy, - # because the header of timegm is broken in my version of libc - result = timegm(timeInfoToTM(cTimeInfo)) - # As timeInfoToTM does ignore the timezone, we need to adjust this here. - result = Time(TimeImpl(result) + timeInfo.timezone) + toTime(timeInfo) proc toTime(timeInfo: TimeInfo): Time = - var cTimeInfo = timeInfo # for C++ we have to make a copy, - # because the header of timegm is broken in my version of libc - result = timegm(timeInfoToTM(cTimeInfo)) - # As timeInfoToTM does ignore the timezone, we need to adjust this here. - result = Time(TimeImpl(result) + timeInfo.timezone) + var cTimeInfo = timeInfo # for C++ we have to make a copy + # 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)) + # 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) + else: + result = timegm(timeInfoToTM(cTimeInfo)) + # As timeInfoToTM does ignore the timezone, we need to adjust this here. + result = Time(TimeImpl(result) + timeInfo.timezone) const epochDiff = 116444736000000000'i64