179 lines
6.6 KiB
Plaintext
179 lines
6.6 KiB
Plaintext
#! @file moment.sakura
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
|
#! @copyright Malunal Studios, LLC.
|
|
#! @license MIT
|
|
#!
|
|
#! Contains the two kinds of moment, and waiting: `instant`, a moment of the
|
|
#! steady clock, which measures how long something takes, `timestamp`, a moment
|
|
#! of the wall clock, which says when something happened, and `sleep`.
|
|
#!
|
|
#! Both count nanoseconds in an `i64`, so a timestamp reaches from 1677 to 2262.
|
|
#! A moment moves by a length of time, with `+` and `-`, and one less another of
|
|
#! its kind is the length of time between them. The two kinds do not mix: the
|
|
#! steady clock's start means nothing, and the wall clock may be set back.
|
|
|
|
import system;
|
|
|
|
module std::time;
|
|
|
|
|
|
#! A moment of the steady clock, which never goes back, for measuring how long
|
|
#! something takes.
|
|
#!
|
|
#! It means nothing but against another: `instant::now() - began` is how long
|
|
#! ago `began` was, and `began.elapsed()` says the same.
|
|
pub struct instant : ordered {
|
|
var _ns: i64; #! Nanoseconds since a start the system chose, which never moves.
|
|
}
|
|
|
|
#! The moment it is now, by the steady clock.
|
|
#! @returns The moment.
|
|
pub def instant::now(): instant {
|
|
return instant { _ns = system::clock(clock_kind::steady) };
|
|
}
|
|
|
|
#! Whether two moments are the same, as `==` asks.
|
|
#! @param this The moment.
|
|
#! @param other The one to compare it with.
|
|
#! @returns Whether they are.
|
|
pub def instant::equals(&this, other: &instant): bool {
|
|
return this._ns == other._ns;
|
|
}
|
|
|
|
#! Orders two moments, as `<` and the others ask, the earlier first.
|
|
#! @param this The moment.
|
|
#! @param other The one to compare it with.
|
|
#! @returns A negative number where this one comes first, zero where they are
|
|
#! the same, and a positive number where it comes after.
|
|
pub def instant::compare(&this, other: &instant): i64 {
|
|
return this._ns < other._ns ? -1 : this._ns > other._ns ? 1 : 0;
|
|
}
|
|
|
|
#! A moment a length of time later, as `+` gives it.
|
|
#! @param this The moment.
|
|
#! @param length How much later, or earlier where it is negative.
|
|
#! @returns The later moment.
|
|
pub def instant::add(&this, length: &duration): instant {
|
|
return instant { _ns = sum_of(this._ns, length._ns) };
|
|
}
|
|
|
|
#! A moment a length of time earlier, as `-` gives it.
|
|
#! @param this The moment.
|
|
#! @param length How much earlier.
|
|
#! @returns The earlier moment.
|
|
pub def instant::sub(&this, length: &duration): instant {
|
|
return instant { _ns = difference_of(this._ns, length._ns) };
|
|
}
|
|
|
|
#! How long a moment is after another, as `-` between two gives it.
|
|
#! @param this The moment.
|
|
#! @param earlier The one it is measured from.
|
|
#! @returns The length of time between them, which is negative where the other
|
|
#! is the later.
|
|
pub def instant::sub(&this, earlier: &instant): duration {
|
|
return duration { _ns = difference_of(this._ns, earlier._ns) };
|
|
}
|
|
|
|
#! How long ago a moment was.
|
|
#! @param this The moment.
|
|
#! @returns The length of time from it to now.
|
|
pub def instant::elapsed(&this): duration {
|
|
return instant::now() - this;
|
|
}
|
|
|
|
#! A moment of the wall clock: when something happened, in nanoseconds since
|
|
#! 1970 began in UTC, whatever zone it is read in.
|
|
#!
|
|
#! `at` gives its date and time of day in a zone, and `rfc3339` writes it as
|
|
#! text. The wall clock may be set forward or back, so how long something takes
|
|
#! is measured with an `instant`.
|
|
pub struct timestamp : ordered {
|
|
var _ns: i64; #! Nanoseconds since 1970 began in UTC.
|
|
}
|
|
|
|
#! The moment it is now, by the wall clock.
|
|
#! @returns The moment.
|
|
pub def timestamp::now(): timestamp {
|
|
return timestamp { _ns = system::clock(clock_kind::wall) };
|
|
}
|
|
|
|
#! The moment a number of seconds after 1970 began in UTC, as Unix counts time.
|
|
#! @param seconds How many seconds, which is negative for a moment before.
|
|
#! @returns The moment.
|
|
pub def timestamp::of_unix(seconds: i64): timestamp {
|
|
return timestamp { _ns = product_of(seconds, 1000000000) };
|
|
}
|
|
|
|
#! The moment a file's bytes last changed.
|
|
#! @param info What is known of the file, as `io::info` gives it.
|
|
#! @returns The moment.
|
|
pub def timestamp::of_file(info: &file_info): timestamp {
|
|
return timestamp { _ns = info.modified };
|
|
}
|
|
|
|
#! How many whole seconds after 1970 began in UTC the moment is, as Unix counts
|
|
#! time, which is negative for a moment before.
|
|
pub val timestamp::unix: i64 {
|
|
get => floored(this._ns, 1000000000);
|
|
}
|
|
|
|
#! Whether two moments are the same, as `==` asks.
|
|
#! @param this The moment.
|
|
#! @param other The one to compare it with.
|
|
#! @returns Whether they are.
|
|
pub def timestamp::equals(&this, other: ×tamp): bool {
|
|
return this._ns == other._ns;
|
|
}
|
|
|
|
#! Orders two moments, as `<` and the others ask, the earlier first.
|
|
#! @param this The moment.
|
|
#! @param other The one to compare it with.
|
|
#! @returns A negative number where this one comes first, zero where they are
|
|
#! the same, and a positive number where it comes after.
|
|
pub def timestamp::compare(&this, other: ×tamp): i64 {
|
|
return this._ns < other._ns ? -1 : this._ns > other._ns ? 1 : 0;
|
|
}
|
|
|
|
#! A moment a length of time later, as `+` gives it.
|
|
#! @param this The moment.
|
|
#! @param length How much later, or earlier where it is negative.
|
|
#! @returns The later moment.
|
|
pub def timestamp::add(&this, length: &duration): timestamp {
|
|
return timestamp { _ns = sum_of(this._ns, length._ns) };
|
|
}
|
|
|
|
#! A moment a length of time earlier, as `-` gives it.
|
|
#! @param this The moment.
|
|
#! @param length How much earlier.
|
|
#! @returns The earlier moment.
|
|
pub def timestamp::sub(&this, length: &duration): timestamp {
|
|
return timestamp { _ns = difference_of(this._ns, length._ns) };
|
|
}
|
|
|
|
#! How long a moment is after another, as `-` between two gives it.
|
|
#! @param this The moment.
|
|
#! @param earlier The one it is measured from.
|
|
#! @returns The length of time between them, which is negative where the other
|
|
#! is the later.
|
|
pub def timestamp::sub(&this, earlier: ×tamp): duration {
|
|
return duration { _ns = difference_of(this._ns, earlier._ns) };
|
|
}
|
|
|
|
#! Waits for a length of time, which is no time at all where it is not above
|
|
#! zero.
|
|
#! @param length How long to wait.
|
|
#! @remarks A signal the program handles ends the wait once its handlers have
|
|
#! run, as it ends every wait, so a program asked to stop is not held
|
|
#! for the rest of it. Any other signal only interrupts the wait, which
|
|
#! goes on for what is left.
|
|
pub def sleep(length: duration): void {
|
|
_ = system::sleep(length._ns);
|
|
}
|
|
|
|
# A count divided toward negative infinity, so that what is left over is never
|
|
# negative: a moment before 1970 is in the second that began before it.
|
|
def floored(count: i64, by: i64): i64 {
|
|
val whole := count / by;
|
|
return count % by < 0 ? whole - 1 : whole;
|
|
}
|