| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use types; |
| 5 |
|
| 6 |
// Represents a specific instant in time as seconds (and nanoseconds) since an |
| 7 |
// arbitrary epoch. Instants may only be meaningfully compared with other |
| 8 |
// instants sourced from the same [[clock]]. |
| 9 |
export type instant = struct { |
| 10 |
sec: i64, |
| 11 |
nsec: i64, |
| 12 |
}; |
| 13 |
|
| 14 |
// Represents a unique interval of time between two [[instant]]s. |
| 15 |
export type interval = (instant, instant); |
| 16 |
|
| 17 |
// The [[instant]] representing the origin or epoch "zero" value. |
| 18 |
export def INSTANT_ZERO = instant { |
| 19 |
sec = 0, |
| 20 |
nsec = 0, |
| 21 |
}; |
| 22 |
|
| 23 |
// The earliest representable [[instant]]. |
| 24 |
export def INSTANT_MIN = instant { |
| 25 |
sec = types::I64_MIN, |
| 26 |
nsec = 0, |
| 27 |
}; |
| 28 |
|
| 29 |
// The latest representable [[instant]]. |
| 30 |
export def INSTANT_MAX = instant { |
| 31 |
sec = types::I64_MAX, |
| 32 |
nsec = SECOND - 1, |
| 33 |
}; |
| 34 |
|
| 35 |
// Creates a new [[instant]] using second and nanosecond values. |
| 36 |
// Nanosecond values outside the range 0 to 999,999,999 will cause an abort. |
| 37 |
export fn new(sec: i64, nsec: i64 = 0) instant = instant { |
| 38 |
sec = sec, |
| 39 |
nsec = if (nsec < 0 || nsec >= SECOND) abort("Invalid nsec") else nsec, |
| 40 |
}; |
| 41 |
|
| 42 |
// Creates a new [[instant]] using a nanosecond value from an abitrary epoch. |
| 43 |
export fn from_nsec(nsec: i64) instant = { |
| 44 |
return add(INSTANT_ZERO, nsec); |
| 45 |
}; |