File: instant.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (45 lines) | stat: -rw-r--r-- 1,271 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use types;

// Represents a specific instant in time as seconds (and nanoseconds) since an
// arbitrary epoch. Instants may only be meaningfully compared with other
// instants sourced from the same [[clock]].
export type instant = struct {
	sec: i64,
	nsec: i64,
};

// Represents a unique interval of time between two [[instant]]s.
export type interval = (instant, instant);

// The [[instant]] representing the origin or epoch "zero" value.
export def INSTANT_ZERO = instant {
	sec = 0,
	nsec = 0,
};

// The earliest representable [[instant]].
export def INSTANT_MIN = instant {
	sec = types::I64_MIN,
	nsec = 0,
};

// The latest representable [[instant]].
export def INSTANT_MAX = instant {
	sec = types::I64_MAX,
	nsec = SECOND - 1,
};

// Creates a new [[instant]] using second and nanosecond values.
// Nanosecond values outside the range 0 to 999,999,999 will cause an abort.
export fn new(sec: i64, nsec: i64 = 0) instant = instant {
	sec = sec,
	nsec = if (nsec < 0 || nsec >= SECOND) abort("Invalid nsec") else nsec,
};

// Creates a new [[instant]] using a nanosecond value from an abitrary epoch.
export fn from_nsec(nsec: i64) instant = {
	return add(INSTANT_ZERO, nsec);
};