Debian Codegraph @

time/chrono/arithmetic.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use time;
5
6 // Compares two [[moment]]s. Returns -1 if a precedes b, 0 if a and b are
7 // simultaneous, or +1 if b precedes a.
8 //
9 // [[tscmismatch]] is returned if the [[timescale]]s of the two moments are
10 // different.
11 export fn compare(a: *moment, b: *moment) (i8 | tscmismatch) = {
12 check_timescales(a.tsc, b.tsc)?;
13 return time::compare(to_instant(*a), to_instant(*b));
14 };
15
16 // Returns the [[time::duration]] between two [[moment]]s, from a to b.
17 //
18 // [[tscmismatch]] is returned if the [[timescale]]s of the two moments are
19 // different.
20 export fn diff(a: *moment, b: *moment) (time::duration | tscmismatch) = {
21 check_timescales(a.tsc, b.tsc)?;
22 return time::diff(to_instant(*a), to_instant(*b));
23 };
24
25 // Adds a [[time::duration]] to a [[moment]] with [[time::add]].
26 export fn add(m: *moment, x: time::duration) moment = {
27 return new(m.tsc, time::add(to_instant(*m), x));
28 };