summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFelix Krause <contact@flyx.org>2016-11-11 17:52:53 +0100
committerFelix Krause <contact@flyx.org>2016-11-14 18:28:55 +0100
commitaa08c32c2b6a32da1aa1f98234512f37330a6691 (patch)
treefb9b78010f767ed719e2ac52987356f3b95d1e75
parent544a2cfe1a9c4805568f6dd781cd85fa4537cb73 (diff)
downloadNim-aa08c32c2b6a32da1aa1f98234512f37330a6691.tar.gz
Improved `-`; fixed tests
 * added prefix `-` operator for TimeInterval
 * improved `-` for both TimeInterval and TimeInfo
 * Fixed a DST test
-rw-r--r--lib/pure/times.nim33
-rw-r--r--tests/stdlib/ttime.nim4
2 files changed, 17 insertions, 20 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim
index cfc39bc55..b6b9fad5b 100644
--- a/lib/pure/times.nim
+++ b/lib/pure/times.nim
@@ -279,16 +279,20 @@ proc `+`*(ti1, ti2: TimeInterval): TimeInterval =
   carryO = `div`(ti1.months + ti2.months, 12)
   result.years = carryO + ti1.years + ti2.years
 
+proc `-`*(ti: TimeInterval): TimeInterval =
+  result = TimeInterval(
+    milliseconds: -ti.milliseconds,
+    seconds: -ti.seconds,
+    minutes: -ti.minutes,
+    hours: -ti.hours,
+    days: -ti.days,
+    months: -ti.months,
+    years: -ti.years
+  )
+
 proc `-`*(ti1, ti2: TimeInterval): TimeInterval =
   ## Subtracts TimeInterval ``ti1`` from ``ti2``.
-  result = ti1
-  result.milliseconds -= ti2.milliseconds
-  result.seconds -= ti2.seconds
-  result.minutes -= ti2.minutes
-  result.hours -= ti2.hours
-  result.days -= ti2.days
-  result.months -= ti2.months
-  result.years -= ti2.years
+  result = ti1 + (-ti2)
 
 proc isLeapYear*(year: int): bool =
   ## returns true if ``year`` is a leap year
@@ -364,16 +368,9 @@ proc `-`*(a: TimeInfo, interval: TimeInterval): TimeInfo =
   ##
   ## **Note:** This has been only briefly tested, it is inaccurate especially
   ## when you subtract so much that you reach the Julian calendar.
-  let t = toSeconds(toTime(a))
-  var intval: TimeInterval
-  intval.milliseconds = - interval.milliseconds
-  intval.seconds = - interval.seconds
-  intval.minutes = - interval.minutes
-  intval.hours = - interval.hours
-  intval.days = - interval.days
-  intval.months = - interval.months
-  intval.years = - interval.years
-  let secs = toSeconds(a, intval)
+  let
+    t = toSeconds(toTime(a))
+    secs = toSeconds(a, -interval)
   if a.timezone == 0:
     result = getGMTime(fromSeconds(t + secs))
   else:
diff --git a/tests/stdlib/ttime.nim b/tests/stdlib/ttime.nim
index 6d3d7c93a..39106f1e0 100644
--- a/tests/stdlib/ttime.nim
+++ b/tests/stdlib/ttime.nim
@@ -219,7 +219,7 @@ block dstTest:
   # January and one in July to maximize the probability to hit one date with DST
   # and one without on the local machine. However, this is not guaranteed.
   let
+    parsedJan = parse("2016-01-05 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
     parsedJul = parse("2016-07-01 04:00:00+01:00", "yyyy-MM-dd HH:mm:sszzz")
-    parsedJan = parse("2016-01-05 04:00:00+01:00", "yyyy-MM-ss HH:mm:sszzz")
-  doAssert toTime(parsedJan) == fromSeconds(1452394800)
+  doAssert toTime(parsedJan) == fromSeconds(1451962800)
   doAssert toTime(parsedJul) == fromSeconds(1467342000)