summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorOscar Nihlgård <oscarnihlgard@gmail.com>2019-07-11 11:19:34 +0200
committerOscar Nihlgård <oscarnihlgard@gmail.com>2019-07-12 10:15:12 +0200
commit974362c98f6adc4f3dd3be3c5ba0d684f2cfa489 (patch)
tree92b8ebbe9b161313d6f8d3e9cb43a09ae6de6f27 /lib
parentfd24a8923ebc51592d2210637304ff2c0a2b8ecd (diff)
downloadNim-974362c98f6adc4f3dd3be3c5ba0d684f2cfa489.tar.gz
[other] Minor optimization in times
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/times.nim21
1 files changed, 12 insertions, 9 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index 9d0ce8677..9b1ac8d3b 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -511,17 +511,20 @@ template convert(dur: Duration, unit: static[FixedTimeUnit]): int64 =
   # The correction is required due to how durations are normalized.
   # For example,` initDuration(nanoseconds = -1)` is stored as
   # { seconds = -1, nanoseconds = 999999999 }.
-  let correction = dur.seconds < 0 and dur.nanosecond > 0
-  when unit >= Seconds:
-    convert(Seconds, unit, dur.seconds + ord(correction))
+  when unit == Nanoseconds:
+    dur.seconds * 1_000_000_000 + dur.nanosecond
   else:
-    if correction:
-      convert(Seconds, unit, dur.seconds + 1) -
-        convert(Nanoseconds, unit,
-          convert(Seconds, Nanoseconds, 1) - dur.nanosecond)
+    let correction = dur.seconds < 0 and dur.nanosecond > 0
+    when unit >= Seconds:
+      convert(Seconds, unit, dur.seconds + ord(correction))
     else:
-      convert(Seconds, unit, dur.seconds) +
-        convert(Nanoseconds, unit, dur.nanosecond)
+      if correction:
+        convert(Seconds, unit, dur.seconds + 1) -
+          convert(Nanoseconds, unit,
+            convert(Seconds, Nanoseconds, 1) - dur.nanosecond)
+      else:
+        convert(Seconds, unit, dur.seconds) +
+          convert(Nanoseconds, unit, dur.nanosecond)
 
 proc inWeeks*(dur: Duration): int64 =
   ## Convert the duration to the number of whole weeks.