diff options
author | Oscar NihlgÄrd <oscarnihlgard@gmail.com> | 2018-04-16 00:01:34 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-04-16 00:01:34 +0200 |
commit | 02d6dd723d2c246717e2a5e6c7765f4535efc074 (patch) | |
tree | ee0783d9fb016a3c192fa664c514816c3cf482c8 /lib/pure | |
parent | ed5b7cbac004551d77db889eca044fdda6f75790 (diff) | |
download | Nim-02d6dd723d2c246717e2a5e6c7765f4535efc074.tar.gz |
Timeinterval optimization (#7608)
* TimeInterval optimization * Fix typo in `$`(Duration)
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/times.nim | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 3b0b9b625..dc216477b 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -330,7 +330,7 @@ proc `$`*(dur: Duration): string = remS.inc 1 const unitStrings: array[FixedTimeUnit, string] = [ - "nanoseconds", "microsecond", "millisecond", "second", "minute", "hour", "day", "week" + "nanosecond", "microsecond", "millisecond", "second", "minute", "hour", "day", "week" ] for unit in countdown(Weeks, Seconds): @@ -1093,27 +1093,52 @@ proc `+`*(dt: DateTime, dur: Duration): DateTime = proc `-`*(dt: DateTime, dur: Duration): DateTime = (dt.toTime - dur).inZone(dt.timezone) -proc `+=`*(time: var Time, interval: TimeInterval) = - ## Modifies `time` by adding `interval`. - time = toTime(time.local + interval) +proc isStaticInterval(interval: TimeInterval): bool = + interval.years == 0 and interval.months == 0 and + interval.days == 0 and interval.weeks == 0 + +proc evaluateStaticInterval(interval: TimeInterval): Duration = + assert interval.isStaticInterval + initDuration(nanoseconds = interval.nanoseconds, + microseconds = interval.microseconds, + milliseconds = interval.milliseconds, + seconds = interval.seconds, + minutes = interval.minutes, + hours = interval.hours) proc `+`*(time: Time, interval: TimeInterval): Time = - ## Adds `interval` to `time` - ## by converting to a ``DateTime`` in the local timezone, - ## adding the interval, and converting back to ``Time``. + ## Adds `interval` to `time`. + ## If `interval` contains any years, months, weeks or days the operation + ## is performed in the local timezone. ## ## ``echo getTime() + 1.day`` - result = toTime(time.local + interval) + if interval.isStaticInterval: + time + evaluateStaticInterval(interval) + else: + toTime(time.local + interval) -proc `-=`*(time: var Time, interval: TimeInterval) = - ## Modifies `time` by subtracting `interval`. - time = toTime(time.local - interval) +proc `+=`*(time: var Time, interval: TimeInterval) = + ## Modifies `time` by adding `interval`. + ## If `interval` contains any years, months, weeks or days the operation + ## is performed in the local timezone. + time = time + interval proc `-`*(time: Time, interval: TimeInterval): Time = ## Subtracts `interval` from Time `time`. + ## If `interval` contains any years, months, weeks or days the operation + ## is performed in the local timezone. ## ## ``echo getTime() - 1.day`` - result = toTime(time.local - interval) + if interval.isStaticInterval: + time - evaluateStaticInterval(interval) + else: + toTime(time.local - interval) + +proc `-=`*(time: var Time, interval: TimeInterval) = + ## Modifies `time` by subtracting `interval`. + ## If `interval` contains any years, months, weeks or days the operation + ## is performed in the local timezone. + time = time - interval proc formatToken(dt: DateTime, token: string, buf: var string) = ## Helper of the format proc to parse individual tokens. |