| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use fmt; |
| 5 |
|
| 6 |
// Represents a span of time in the Gregorian chronology; a set of nominal |
| 7 |
// periods of time. Used for chronological arithmetic. |
| 8 |
export type span = struct { |
| 9 |
years: i64, |
| 10 |
months: i64, |
| 11 |
weeks: i64, |
| 12 |
days: i64, |
| 13 |
hours: i64, |
| 14 |
minutes: i64, |
| 15 |
seconds: i64, |
| 16 |
nanoseconds: i64, |
| 17 |
}; |
| 18 |
|
| 19 |
// Returns true if two [[span]]s are numerically equal, false otherwise. |
| 20 |
export fn equalspan(x: span, y: span) bool = { |
| 21 |
return ( |
| 22 |
x.years == y.years |
| 23 |
&& x.months == y.months |
| 24 |
&& x.weeks == y.weeks |
| 25 |
&& x.days == y.days |
| 26 |
&& x.hours == y.hours |
| 27 |
&& x.minutes == y.minutes |
| 28 |
&& x.seconds == y.seconds |
| 29 |
&& x.nanoseconds == y.nanoseconds |
| 30 |
); |
| 31 |
}; |
| 32 |
|
| 33 |
// Returns the sum [[span]] of a set of spans. |
| 34 |
export fn sum(spans: span...) span = { |
| 35 |
let x = span { ... }; |
| 36 |
for (let y &.. spans) { |
| 37 |
x.years += y.years; |
| 38 |
x.months += y.months; |
| 39 |
x.weeks += y.weeks; |
| 40 |
x.days += y.days; |
| 41 |
x.hours += y.hours; |
| 42 |
x.minutes += y.minutes; |
| 43 |
x.seconds += y.seconds; |
| 44 |
x.nanoseconds += y.nanoseconds; |
| 45 |
}; |
| 46 |
return x; |
| 47 |
}; |
| 48 |
|
| 49 |
// Returns a [[span]] with its fields negated. |
| 50 |
export fn neg(x: span) span = span { |
| 51 |
years = -x.years, |
| 52 |
months = -x.months, |
| 53 |
weeks = -x.weeks, |
| 54 |
days = -x.days, |
| 55 |
hours = -x.hours, |
| 56 |
minutes = -x.minutes, |
| 57 |
seconds = -x.seconds, |
| 58 |
nanoseconds = -x.nanoseconds, |
| 59 |
}; |
| 60 |
|
| 61 |
// Returns a [[span]] with its fields made absolute and positive. |
| 62 |
export fn abs(x: span) span = span { |
| 63 |
years = if (x.years < 0) -x.years else x.years, |
| 64 |
months = if (x.months < 0) -x.months else x.months, |
| 65 |
weeks = if (x.weeks < 0) -x.weeks else x.weeks, |
| 66 |
days = if (x.days < 0) -x.days else x.days, |
| 67 |
hours = if (x.hours < 0) -x.hours else x.hours, |
| 68 |
minutes = if (x.minutes < 0) -x.minutes else x.minutes, |
| 69 |
seconds = if (x.seconds < 0) -x.seconds else x.seconds, |
| 70 |
nanoseconds = if (x.nanoseconds < 0) -x.nanoseconds else x.nanoseconds, |
| 71 |
}; |
| 72 |
|
| 73 |
// Formats a [[span]] and writes it into an internal static buffer. The returned |
| 74 |
// string is borrowed from this buffer. The following layout is used: |
| 75 |
// |
| 76 |
// "?Y ?M ?W ?D ?h ?m ?s ?n" |
| 77 |
// |
| 78 |
// where each '?' is replaced with the appopriate span field values in order of |
| 79 |
// decreasing significance, and the inbetween spaces are replaced with the given |
| 80 |
// sep argument. |
| 81 |
export fn formatspan(s: span, sep: rune = ' ') str = { |
| 82 |
static let buf: [128]u8 = [0...]; |
| 83 |
return fmt::bsprintf(buf, "{}Y{}{}M{}{}W{}{}D{}{}h{}{}m{}{}s{}{}n", |
| 84 |
s.years, sep, |
| 85 |
s.months, sep, |
| 86 |
s.weeks, sep, |
| 87 |
s.days, sep, |
| 88 |
s.hours, sep, |
| 89 |
s.minutes, sep, |
| 90 |
s.seconds, sep, |
| 91 |
s.nanoseconds, |
| 92 |
)!; |
| 93 |
}; |