hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 35f7eea5187e3d65408df39ad3bba3fed7f271dd
parent edce6d8480b64c9e158414a769a7785b8b97468a
Author: Byron Torres <b@torresjrjr.com>
Date:   Wed, 20 Apr 2022 11:26:45 +0100

time: fix add()

Signed-off-by: Byron Torres <b@torresjrjr.com>

Diffstat:
Mtime/arithm.ha | 17++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/time/arithm.ha b/time/arithm.ha @@ -5,9 +5,20 @@ // Adds a [[duration]] to an [[instant]], returning an instant further in the // future (given a positive duration), or further in the past (given a negative // duration). -export fn add(a: instant, d: duration) instant = instant { - sec = a.sec + d / SECOND, - nsec = a.nsec + d, +export fn add(a: instant, d: duration) instant = { + if (d == 0) { + return a; + } else if (d > 0) { + return instant { + sec = a.sec + (a.nsec + d) / SECOND, + nsec = (a.nsec + d) % SECOND, + }; + } else { + return instant { + sec = a.sec + (a.nsec + d - SECOND + NANOSECOND) / SECOND, + nsec = (a.nsec + (d % SECOND) + SECOND) % SECOND, + }; + }; }; // Returns the [[duration]] from [[instant]] "a" to [[instant]] "b".