Improved -; fixed tests
* added prefix `-` operator for TimeInterval * improved `-` for both TimeInterval and TimeInfo * Fixed a DST test
This commit is contained in:
parent
544a2cfe1a
commit
aa08c32c2b
2 changed files with 17 additions and 20 deletions
|
|
@ -279,16 +279,20 @@ proc `+`*(ti1, ti2: TimeInterval): TimeInterval =
|
||||||
carryO = `div`(ti1.months + ti2.months, 12)
|
carryO = `div`(ti1.months + ti2.months, 12)
|
||||||
result.years = carryO + ti1.years + ti2.years
|
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 =
|
proc `-`*(ti1, ti2: TimeInterval): TimeInterval =
|
||||||
## Subtracts TimeInterval ``ti1`` from ``ti2``.
|
## Subtracts TimeInterval ``ti1`` from ``ti2``.
|
||||||
result = ti1
|
result = ti1 + (-ti2)
|
||||||
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
|
|
||||||
|
|
||||||
proc isLeapYear*(year: int): bool =
|
proc isLeapYear*(year: int): bool =
|
||||||
## returns true if ``year`` is a leap year
|
## 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
|
## **Note:** This has been only briefly tested, it is inaccurate especially
|
||||||
## when you subtract so much that you reach the Julian calendar.
|
## when you subtract so much that you reach the Julian calendar.
|
||||||
let t = toSeconds(toTime(a))
|
let
|
||||||
var intval: TimeInterval
|
t = toSeconds(toTime(a))
|
||||||
intval.milliseconds = - interval.milliseconds
|
secs = toSeconds(a, -interval)
|
||||||
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)
|
|
||||||
if a.timezone == 0:
|
if a.timezone == 0:
|
||||||
result = getGMTime(fromSeconds(t + secs))
|
result = getGMTime(fromSeconds(t + secs))
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -219,7 +219,7 @@ block dstTest:
|
||||||
# January and one in July to maximize the probability to hit one date with DST
|
# 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.
|
# and one without on the local machine. However, this is not guaranteed.
|
||||||
let
|
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")
|
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(1451962800)
|
||||||
doAssert toTime(parsedJan) == fromSeconds(1452394800)
|
|
||||||
doAssert toTime(parsedJul) == fromSeconds(1467342000)
|
doAssert toTime(parsedJul) == fromSeconds(1467342000)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue