summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-12-06 15:02:57 +0100
committerGitHub <noreply@github.com>2016-12-06 15:02:57 +0100
commite53e7d9688547893e7de67f1dbc830bef5cac2df (patch)
tree3b04e0a6276d80c62b41cd1c8924eb816c9381f0 /lib/pure
parent14b9eaee06894f5bf00548117984381d37f16ec7 (diff)
parentdc8ee72eb6069c181621a3dc4ad97f8cced9db4b (diff)
downloadNim-e53e7d9688547893e7de67f1dbc830bef5cac2df.tar.gz
Merge pull request #5102 from nigredo-tori/fix-4690
Fix 4690 (WIP)
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/times.nim29
1 files changed, 18 insertions, 11 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index 1767a37be..cf4e7dde6 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -436,6 +436,11 @@ when not defined(JS):
     TimeInfoPtr = ptr StructTM
     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 {.
     importc: "localtime", header: "<time.h>", tags: [].}
   proc gmtime(timer: ptr Time): TimeInfoPtr {.
@@ -515,20 +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 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)
+    toTime(timeInfo)
 
   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
-    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)
+
+    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