Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-10 09:44:01 -06:00
commit 4aa98abfe5
1625 files changed
+43592

No files matched your search

+245
View File
@@ -0,0 +1,245 @@
#! @file duration.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `duration`, a length of time, the functions that make one of a count
#! of each unit, and its arithmetic, which its operators are.
#!
#! A duration is a count of nanoseconds in an `i64`, which reaches 292 years
#! either way. Durations add, subtract, and negate as one type, so `duration` is
#! `additive`, and they scale by a count: `seconds(2) * 3`, `total / 4`. One
#! divided by another is how many times it holds it, a number. Arithmetic that
#! leaves what an `i64` holds stops the program, as indexing past an end does.
module std::time;
# The most and the least an `i64` holds.
const most: i64 = 0x7FFFFFFFFFFFFFFF;
const least: i64 = -0x7FFFFFFFFFFFFFFF - 1;
# What a time outside the range says where it stops the program.
const outside: strview = "The time is within what an `i64` of nanoseconds holds, which reaches 292 years either way.";
#! A length of time, to the nanosecond, which may be negative.
#!
#! It is made of a count of a unit, as `seconds(5)` or `milliseconds(250)`, or
#! by subtracting one moment from another, and its operators are its
#! arithmetic: `+`, `-`, and `-x` with another, `*` and `/` by a count, and `/`
#! by another for the number of times it holds it.
pub struct duration : additive, ordered {
var _ns: i64; #! How many nanoseconds it is.
}
# The sum of two counts of nanoseconds, stopping the program where an `i64`
# cannot hold it.
def sum_of(a: i64, b: i64): i64 {
expects((b >= 0 && a <= most - b) || (b < 0 && a >= least - b), outside);
return a + b;
}
# One count of nanoseconds less another, stopping the program where an `i64`
# cannot hold it.
def difference_of(a: i64, b: i64): i64 {
expects((b <= 0 && a <= most + b) || (b > 0 && a >= least + b), outside);
return a - b;
}
# A count of nanoseconds a number of times over, stopping the program where an
# `i64` cannot hold it.
def product_of(a: i64, by: i64): i64 {
if a == 0 || by == 0 {
return 0;
}
# The least has no positive, so it is checked before anything divides by -1.
expects(!(a == least && by == -1) && !(by == least && a == -1) && (a * by) / by == a, outside);
return a * by;
}
#! A length of time of a number of nanoseconds.
#! @param count How many.
#! @returns The length of time.
pub def nanoseconds(count: i64): duration {
return duration { _ns = count };
}
#! A length of time of a number of microseconds.
#! @param count How many.
#! @returns The length of time.
pub def microseconds(count: i64): duration {
return duration { _ns = product_of(count, 1000) };
}
#! A length of time of a number of milliseconds.
#! @param count How many.
#! @returns The length of time.
pub def milliseconds(count: i64): duration {
return duration { _ns = product_of(count, 1000000) };
}
#! A length of time of a number of seconds.
#! @param count How many.
#! @returns The length of time.
pub def seconds(count: i64): duration {
return duration { _ns = product_of(count, 1000000000) };
}
#! A length of time of a number of minutes.
#! @param count How many.
#! @returns The length of time.
pub def minutes(count: i64): duration {
return duration { _ns = product_of(count, 60000000000) };
}
#! A length of time of a number of hours.
#! @param count How many.
#! @returns The length of time.
pub def hours(count: i64): duration {
return duration { _ns = product_of(count, 3600000000000) };
}
#! Whether two lengths of time are the same, as `==` asks.
#! @param this The length of time.
#! @param other The one to compare it with.
#! @returns Whether they are.
pub def duration::equals(&this, other: &duration): bool {
return this._ns == other._ns;
}
#! Orders two lengths of time, as `<` and the others ask, the shorter first, and
#! a negative one before any that is not.
#! @param this The length of time.
#! @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 duration::compare(&this, other: &duration): i64 {
return this._ns < other._ns ? -1 : this._ns > other._ns ? 1 : 0;
}
#! Two lengths of time together, as `+` gives them.
#! @param this The length of time.
#! @param other The one to add.
#! @returns Their sum.
pub def duration::add(&this, other: &duration): duration {
return duration { _ns = sum_of(this._ns, other._ns) };
}
#! A length of time less another, as `-` gives it.
#! @param this The length of time.
#! @param other The one to take away.
#! @returns Their difference, which is negative where the other is longer.
pub def duration::sub(&this, other: &duration): duration {
return duration { _ns = difference_of(this._ns, other._ns) };
}
#! A length of time the other way, as `-x` gives it.
#! @param this The length of time.
#! @returns As long, and negative where this one is not.
pub def duration::negate(&this): duration {
return duration { _ns = difference_of(0, this._ns) };
}
#! A length of time a number of times over, as `*` gives it.
#! @param this The length of time.
#! @param count How many times.
#! @returns That many of it.
pub def duration::mul(&this, count: &i64): duration {
return duration { _ns = product_of(this._ns, count) };
}
#! A length of time shared out into a number of equal parts, as `/` gives it.
#! @param this The length of time.
#! @param count How many parts, which is not zero.
#! @returns One part, to the nanosecond, rounded toward zero.
pub def duration::div(&this, count: &i64): duration {
expects(count != 0, "A length of time is shared out into a number of parts that is not zero.");
expects(!(this._ns == least && count == -1), outside);
return duration { _ns = this._ns / count };
}
#! How many times a length of time holds another, as `/` between two gives it.
#! @param this The length of time.
#! @param other The one it is measured in.
#! @returns The number of times, which need not be whole.
pub def duration::div(&this, other: &duration): f64 {
return this._ns as f64 / other._ns as f64;
}
#! How many nanoseconds the length of time is.
pub val duration::in_nanoseconds: i64 {
get => this._ns;
}
#! How many seconds the length of time is, with what is left of one as a
#! fraction.
pub val duration::in_seconds: f64 {
get => this._ns as f64 / 1000000000.0;
}
#! Writes a length of time as a person reads one: under a minute, in the
#! largest unit it fills, to two places, as "1.5s", "250ms", or "2.31s", and
#! from a minute on, in its two largest units that are not zero, as "2m3s" or
#! "2h3m".
#! @param this The length of time.
#! @returns The text.
pub def duration::describe(&this): string {
var made := string::new(nothing {});
if this._ns == 0 {
made.append("0s");
return made;
}
# The least has no positive, so its size is worked out without negating it.
var size: u64 = this._ns as u64;
if this._ns < 0 {
made.push('-');
size = (-(this._ns + 1)) as u64 + 1;
}
val second: u64 = 1000000000;
if size >= 60 * second {
val whole := size / second;
var parts: u64[3];
parts[0] = whole / 3600;
parts[1] = whole / 60 % 60;
parts[2] = whole % 60;
val units: strview = "hms";
var written := 0;
for i in 0u..3u {
continue if parts[i] == 0 || written == 2;
val count := $"{parts[i]}";
made.append(count.span);
made.push(units[i]);
written++;
}
return made;
}
var unit: u64 = 1;
var named: strview = "ns";
if size >= second {
unit = second;
named = "s";
} else if size >= 1000000 {
unit = 1000000;
named = "ms";
} else if size >= 1000 {
unit = 1000;
named = "us";
}
val whole := $"{size / unit}";
made.append(whole.span);
val hundredths := size % unit * 100 / unit;
if hundredths != 0 {
made.push('.');
made.push('0' + (hundredths / 10) as u8);
if hundredths % 10 != 0 {
made.push('0' + (hundredths % 10) as u8);
}
}
made.append(named);
return made;
}