[other] Minor optimization in times

This commit is contained in:
Oscar Nihlgård 2019-07-11 11:19:34 +02:00
commit 974362c98f

View file

@ -511,17 +511,20 @@ template convert(dur: Duration, unit: static[FixedTimeUnit]): int64 =
# The correction is required due to how durations are normalized. # The correction is required due to how durations are normalized.
# For example,` initDuration(nanoseconds = -1)` is stored as # For example,` initDuration(nanoseconds = -1)` is stored as
# { seconds = -1, nanoseconds = 999999999 }. # { seconds = -1, nanoseconds = 999999999 }.
let correction = dur.seconds < 0 and dur.nanosecond > 0 when unit == Nanoseconds:
when unit >= Seconds: dur.seconds * 1_000_000_000 + dur.nanosecond
convert(Seconds, unit, dur.seconds + ord(correction))
else: else:
if correction: let correction = dur.seconds < 0 and dur.nanosecond > 0
convert(Seconds, unit, dur.seconds + 1) - when unit >= Seconds:
convert(Nanoseconds, unit, convert(Seconds, unit, dur.seconds + ord(correction))
convert(Seconds, Nanoseconds, 1) - dur.nanosecond)
else: else:
convert(Seconds, unit, dur.seconds) + if correction:
convert(Nanoseconds, unit, dur.nanosecond) 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 = proc inWeeks*(dur: Duration): int64 =
## Convert the duration to the number of whole weeks. ## Convert the duration to the number of whole weeks.