Initial Commit
This commit is contained in:
1625 files changed
+43245
No files matched your search
@@ -0,0 +1,240 @@
|
||||
#! @file calendar.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the calendar: `date`, a day of it, with the months and the days of
|
||||
#! the week, `time_of_day`, and `error`, why a date, a zone, or text could not
|
||||
#! be made into a time.
|
||||
#!
|
||||
#! The calendar is the Gregorian one, for every year, those before it was kept
|
||||
#! too. Days are counted from 1970 by Howard Hinnant's `days_from_civil` and
|
||||
#! `civil_from_days`, which need no tables. Leap seconds are left out, as POSIX
|
||||
#! leaves them out, so every day is 86,400 seconds.
|
||||
|
||||
|
||||
module std::time;
|
||||
|
||||
|
||||
#! Why a date, a zone, or text could not be made into a time.
|
||||
pub enum error: u8 {
|
||||
no_such_date, #! The calendar has no such day, as the 30th of February.
|
||||
no_such_zone, #! The system's database has no zone of the name.
|
||||
unreadable_zone, #! A zone's file is not one, or is cut short.
|
||||
malformed, #! The text is not written as a date or a time is.
|
||||
out_of_range, #! A number is outside what its place takes, as an hour of 25.
|
||||
}
|
||||
|
||||
#! A month of the year, numbered from 1 as dates write them.
|
||||
pub enum month: u8 {
|
||||
january = 1, february, march, april, may, june,
|
||||
july, august, september, october, november, december,
|
||||
}
|
||||
|
||||
#! A day of the week, from Monday, as ISO 8601 numbers them.
|
||||
pub enum weekday: u8 {
|
||||
monday, tuesday, wednesday, thursday, friday, saturday, sunday,
|
||||
}
|
||||
|
||||
#! Whether a year has a 29th of February: one divisible by 4, but for the
|
||||
#! centuries not divisible by 400.
|
||||
#! @param year The year.
|
||||
#! @returns Whether it has.
|
||||
pub def is_leap_year(year: i32): bool {
|
||||
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
|
||||
}
|
||||
|
||||
# How many days a month of a year has.
|
||||
def days_in(year: i32, of: month): u8 {
|
||||
return match of {
|
||||
april, june, september, november => 30,
|
||||
february => is_leap_year(year) ? 29 : 28,
|
||||
else => 31
|
||||
};
|
||||
}
|
||||
|
||||
# The days from the first of 1970 to a day of the calendar, which is negative
|
||||
# for a day before it.
|
||||
def days_from_civil(year: i64, month: i64, day: i64): i64 {
|
||||
val y := month <= 2 ? year - 1 : year;
|
||||
val era := (y >= 0 ? y : y - 399) / 400;
|
||||
val yoe := y - era * 400;
|
||||
val doy := (153 * (month > 2 ? month - 3 : month + 9) + 2) / 5 + day - 1;
|
||||
val doe := yoe * 365 + yoe / 4 - yoe / 100 + doy;
|
||||
return era * 146097 + doe - 719468;
|
||||
}
|
||||
|
||||
#! A day of the calendar.
|
||||
#!
|
||||
#! `date::of` makes one, giving an error for a day the calendar does not have.
|
||||
#! Its fields are read and written as they are, so one written as a literal is
|
||||
#! whatever it says.
|
||||
pub struct date : ordered {
|
||||
pub var year: i32; #! The year, as the calendar numbers it, where 1 BC is 0.
|
||||
pub var month: month; #! The month.
|
||||
pub var day: u8; #! The day of the month, from 1.
|
||||
}
|
||||
|
||||
#! Makes a day of the calendar.
|
||||
#! @param year The year.
|
||||
#! @param month The month.
|
||||
#! @param day The day of the month, from 1.
|
||||
#! @returns The day, or `no_such_date` where the month has no such day.
|
||||
pub def date::of(year: i32, month: month, day: u8): result<date, error> {
|
||||
if day == 0 || day > days_in(year, month) {
|
||||
return error::no_such_date;
|
||||
}
|
||||
return date { year = year, month = month, day = day };
|
||||
}
|
||||
|
||||
# The day a number of days after the first of 1970.
|
||||
def date::of_days(days: i64): date {
|
||||
val z := days + 719468;
|
||||
val era := (z >= 0 ? z : z - 146096) / 146097;
|
||||
val doe := z - era * 146097;
|
||||
val yoe := (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
|
||||
val doy := doe - (365 * yoe + yoe / 4 - yoe / 100);
|
||||
val mp := (5 * doy + 2) / 153;
|
||||
val day := doy - (153 * mp + 2) / 5 + 1;
|
||||
val of := mp < 10 ? mp + 3 : mp - 9;
|
||||
val year := yoe + era * 400 + (of <= 2 ? 1 : 0);
|
||||
expects(year >= -2147483648 && year <= 2147483647, "The year is one an `i32` holds.");
|
||||
return date { year = year as i32, month = of as u8 as month, day = day as u8 };
|
||||
}
|
||||
|
||||
# The days from the first of 1970 to the day, which is negative for one before.
|
||||
def date::days(&this): i64 {
|
||||
return days_from_civil(this.year as i64, this.month as i64, this.day as i64);
|
||||
}
|
||||
|
||||
#! Whether two days are the same, as `==` asks.
|
||||
#! @param this The day.
|
||||
#! @param other The one to compare it with.
|
||||
#! @returns Whether they are.
|
||||
pub def date::equals(&this, other: &date): bool {
|
||||
return this.year == other.year && this.month == other.month && this.day == other.day;
|
||||
}
|
||||
|
||||
#! Orders two days, as `<` and the others ask, the earlier first.
|
||||
#! @param this The day.
|
||||
#! @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 date::compare(&this, other: &date): i64 {
|
||||
if this.year != other.year {
|
||||
return this.year < other.year ? -1 : 1;
|
||||
}
|
||||
if this.month != other.month {
|
||||
return (this.month as u8) < (other.month as u8) ? -1 : 1;
|
||||
}
|
||||
return this.day as i64 - other.day as i64;
|
||||
}
|
||||
|
||||
#! The day of the week the day is.
|
||||
pub val date::weekday: weekday {
|
||||
# The first of 1970 was a Thursday.
|
||||
get {
|
||||
val since := (this.days() + 3) % 7;
|
||||
return (since < 0 ? since + 7 : since) as u8 as weekday;
|
||||
}
|
||||
}
|
||||
|
||||
#! Which day of its year the day is, from 1.
|
||||
pub val date::day_of_year: u16 {
|
||||
get => (this.days() - days_from_civil(this.year as i64, 1, 1) + 1) as u16;
|
||||
}
|
||||
|
||||
#! The day a number of days later.
|
||||
#! @param this The day.
|
||||
#! @param count How many days later, or earlier where it is negative.
|
||||
#! @returns That day.
|
||||
pub def date::plus_days(&this, count: i64): date {
|
||||
return date::of_days(sum_of(this.days(), count));
|
||||
}
|
||||
|
||||
#! The same day of the month a number of months later, or the last day of that
|
||||
#! month where it has no such day, as a month after the 31st of October is the
|
||||
#! 30th of November.
|
||||
#! @param this The day.
|
||||
#! @param count How many months later, or earlier where it is negative.
|
||||
#! @returns That day.
|
||||
pub def date::plus_months(&this, count: i64): date {
|
||||
val months := sum_of(this.year as i64 * 12 + this.month as i64 - 1, count);
|
||||
val year := floored(months, 12);
|
||||
val of := months - year * 12 + 1;
|
||||
expects(year >= -2147483648 && year <= 2147483647, "The year is one an `i32` holds.");
|
||||
val last := days_in(year as i32, of as u8 as month);
|
||||
return date { year = year as i32, month = of as u8 as month, day = this.day < last ? this.day : last };
|
||||
}
|
||||
|
||||
#! How many days the day is after another.
|
||||
#! @param this The day.
|
||||
#! @param earlier The one it is counted from.
|
||||
#! @returns The days between them, which is negative where the other is the
|
||||
#! later.
|
||||
pub def date::days_since(&this, earlier: date): i64 {
|
||||
return this.days() - earlier.days();
|
||||
}
|
||||
|
||||
#! Writes the day as ISO 8601 does, as "2026-10-07".
|
||||
#! @param this The day.
|
||||
#! @returns The text.
|
||||
pub def date::iso(&this): string {
|
||||
var made := string::new(nothing {});
|
||||
put_date(&mut made, this);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! A time of day, to the nanosecond.
|
||||
#!
|
||||
#! Its fields are read and written as they are, so one written as a literal is
|
||||
#! whatever it says, as `time_of_day { hour = 9, minute = 30 }`.
|
||||
pub struct time_of_day : ordered {
|
||||
pub var hour: u8; #! The hour, from 0 to 23.
|
||||
pub var minute: u8; #! The minute, from 0 to 59.
|
||||
pub var second: u8; #! The second, from 0 to 59.
|
||||
pub var nanosecond: u32; #! What is left of a second, in nanoseconds.
|
||||
}
|
||||
|
||||
# How many nanoseconds into its day the time of day is.
|
||||
def time_of_day::since_midnight(&this): i64 {
|
||||
return (this.hour as i64 * 3600 + this.minute as i64 * 60 + this.second as i64) * 1000000000 + this.nanosecond as i64;
|
||||
}
|
||||
|
||||
#! Whether two times of day are the same, as `==` asks.
|
||||
#! @param this The time of day.
|
||||
#! @param other The one to compare it with.
|
||||
#! @returns Whether they are.
|
||||
pub def time_of_day::equals(&this, other: &time_of_day): bool {
|
||||
return this.since_midnight() == other.since_midnight();
|
||||
}
|
||||
|
||||
#! Orders two times of day, as `<` and the others ask, the earlier first.
|
||||
#! @param this The time of day.
|
||||
#! @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 time_of_day::compare(&this, other: &time_of_day): i64 {
|
||||
return this.since_midnight() - other.since_midnight();
|
||||
}
|
||||
|
||||
# Writes a number in at least so many digits, with zeros before it.
|
||||
def put_padded(into: &mut string, value: u64, width: u64): void {
|
||||
val digits := $"{value}";
|
||||
for _ in digits.length..width {
|
||||
into.push('0');
|
||||
}
|
||||
into.append(digits.view());
|
||||
}
|
||||
|
||||
# Writes a day as ISO 8601 does, a year before 0 with its sign.
|
||||
def put_date(into: &mut string, day: &date): void {
|
||||
if day.year < 0 {
|
||||
into.push('-');
|
||||
}
|
||||
put_padded(into, (day.year < 0 ? -(day.year as i64) : day.year as i64) as u64, 4);
|
||||
into.push('-');
|
||||
put_padded(into, day.month as u64, 2);
|
||||
into.push('-');
|
||||
put_padded(into, day.day as u64, 2);
|
||||
}
|
||||
@@ -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.view());
|
||||
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.view());
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
#! @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;
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
#! @file text.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `date_time`, the date and time of day a zone's clocks show at a
|
||||
#! moment, and the text moments and dates are written and read as: RFC 3339,
|
||||
#! as "2026-10-07T07:42:03-05:00", and ISO 8601's dates, as "2026-10-07".
|
||||
|
||||
|
||||
module std::time;
|
||||
|
||||
|
||||
#! A date and a time of day in a zone, with how far ahead of UTC the zone's
|
||||
#! clocks were then.
|
||||
#!
|
||||
#! `timestamp::at` gives one for a moment in a zone, and `parse_rfc3339` reads
|
||||
#! one. Its fields are read and written as they are, and `timestamp` gives the
|
||||
#! moment they say.
|
||||
pub struct date_time : equatable {
|
||||
pub var date: date; #! The date the clocks show.
|
||||
pub var time: time_of_day; #! The time of day they show.
|
||||
pub var offset: duration; #! How far ahead of UTC they are, which is negative to its west.
|
||||
#! What the zone calls its time then, as "CDT", viewing the zone it came from,
|
||||
#! so it is read while that zone is held. One read from text has none.
|
||||
pub var name: strview;
|
||||
}
|
||||
|
||||
#! Whether two say the same date and time of day, the same length of time from
|
||||
#! UTC. What their zones call it is not compared.
|
||||
#! @param this The date and time.
|
||||
#! @param other The one to compare it with.
|
||||
#! @returns Whether they do.
|
||||
pub def date_time::equals(&this, other: &date_time): bool {
|
||||
return this.date == other.date && this.time == other.time && this.offset == other.offset;
|
||||
}
|
||||
|
||||
#! The moment a date and time say.
|
||||
#! @param this The date and time.
|
||||
#! @returns The moment, which stops the program where it is outside what a
|
||||
#! `timestamp` holds, from 1677 to 2262.
|
||||
pub def date_time::timestamp(&this): timestamp {
|
||||
val shown := sum_of(product_of(this.date.days(), 86400000000000), this.time.since_midnight());
|
||||
return timestamp { _ns = difference_of(shown, this.offset._ns) };
|
||||
}
|
||||
|
||||
#! Writes a date and time as RFC 3339 does, as "2026-10-07T07:42:03-05:00":
|
||||
#! its seconds, a fraction only where there is one, and its offset from UTC,
|
||||
#! which is "Z" where it has none.
|
||||
#! @param this The date and time.
|
||||
#! @returns The text.
|
||||
pub def date_time::rfc3339(&this): string {
|
||||
var made := string::new(nothing {});
|
||||
put_date(&mut made, &this.date);
|
||||
made.push('T');
|
||||
put_padded(&mut made, this.time.hour as u64, 2);
|
||||
made.push(':');
|
||||
put_padded(&mut made, this.time.minute as u64, 2);
|
||||
made.push(':');
|
||||
put_padded(&mut made, this.time.second as u64, 2);
|
||||
if this.time.nanosecond != 0 {
|
||||
# Nine places, less the zeros that end them.
|
||||
var places: u64 = 9;
|
||||
var part := this.time.nanosecond as u64;
|
||||
while part % 10 == 0 {
|
||||
part /= 10;
|
||||
places--;
|
||||
}
|
||||
made.push('.');
|
||||
put_padded(&mut made, part, places);
|
||||
}
|
||||
|
||||
val ahead := this.offset._ns / 1000000000;
|
||||
if ahead == 0 {
|
||||
made.push('Z');
|
||||
} else {
|
||||
put_offset(&mut made, ahead, false);
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# The date and time of day clocks so far ahead of UTC show at a moment.
|
||||
def shown_at(moment: i64, ahead: i64, name: strview): date_time {
|
||||
val second := floored(moment, 1000000000);
|
||||
val local := second + ahead;
|
||||
val day := floored(local, 86400);
|
||||
val into := local - day * 86400;
|
||||
return date_time {
|
||||
date = date::of_days(day),
|
||||
time = time_of_day {
|
||||
hour = (into / 3600) as u8,
|
||||
minute = (into / 60 % 60) as u8,
|
||||
second = (into % 60) as u8,
|
||||
nanosecond = (moment - second * 1000000000) as u32
|
||||
},
|
||||
offset = seconds(ahead),
|
||||
name = name
|
||||
};
|
||||
}
|
||||
|
||||
#! The date and time of day a zone's clocks show at a moment.
|
||||
#! @param this The moment.
|
||||
#! @param place The zone.
|
||||
#! @returns The date and time, whose `name` views the zone.
|
||||
pub def timestamp::at(&this, place: &zone): date_time {
|
||||
val way := place.way_at(floored(this._ns, 1000000000));
|
||||
return shown_at(this._ns, way.offset as i64, place.name_of(&way));
|
||||
}
|
||||
|
||||
#! Writes a moment as RFC 3339 does, in UTC, as "2026-10-07T12:42:03.451Z".
|
||||
#! @param this The moment.
|
||||
#! @returns The text.
|
||||
pub def timestamp::rfc3339(&this): string {
|
||||
val shown := shown_at(this._ns, 0, "UTC");
|
||||
return shown.rfc3339();
|
||||
}
|
||||
|
||||
# Reads so many digits, giving the number they write.
|
||||
def digits_at(text: strview, at: &mut u64, count: u64): maybe<u64> {
|
||||
if at + count > text.length {
|
||||
return nothing {};
|
||||
}
|
||||
var made: u64 = 0;
|
||||
for i in 0u..count {
|
||||
val digit: u8 = text[at + i];
|
||||
if digit < '0' || digit > '9' {
|
||||
return nothing {};
|
||||
}
|
||||
made = made * 10 + (digit - '0') as u64;
|
||||
}
|
||||
at += count;
|
||||
return made;
|
||||
}
|
||||
|
||||
# Reads one character, where it is the one asked for.
|
||||
def reads(text: strview, at: &mut u64, wanted: u8): bool {
|
||||
if at >= text.length || text[at] != wanted {
|
||||
return false;
|
||||
}
|
||||
at++;
|
||||
return true;
|
||||
}
|
||||
|
||||
# Reads a date as ISO 8601 writes one, from a place in the text.
|
||||
def date_at(text: strview, at: &mut u64): result<date, error> {
|
||||
val year := digits_at(text, at, 4) else { return error::malformed; };
|
||||
return error::malformed if !reads(text, at, '-');
|
||||
val of := digits_at(text, at, 2) else { return error::malformed; };
|
||||
return error::malformed if !reads(text, at, '-');
|
||||
val day := digits_at(text, at, 2) else { return error::malformed; };
|
||||
return error::out_of_range if of < 1 || of > 12;
|
||||
return date::of(year as i32, of as u8 as month, day as u8);
|
||||
}
|
||||
|
||||
#! Reads a date written as ISO 8601 writes one, as "2026-10-07".
|
||||
#! @param text The text, which is the date and nothing more.
|
||||
#! @returns The date, `malformed` where the text is not written so,
|
||||
#! `out_of_range` where its month is none, or `no_such_date` where the
|
||||
#! month has no such day.
|
||||
pub def parse_date(text: strview): result<date, error> {
|
||||
var at: u64 = 0;
|
||||
val made := date_at(text, &mut at) else (why) { return why; };
|
||||
return error::malformed if at != text.length;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Reads a date and time written as RFC 3339 writes one, as
|
||||
#! "2026-10-07T07:42:03.5-05:00". The `T` and the `Z` may be written small, and
|
||||
#! a space may stand for the `T`, as RFC 3339 and ISO 8601 allow.
|
||||
#! @param text The text, which is the date and time and nothing more.
|
||||
#! @returns The date and time, with the offset the text gives and no name;
|
||||
#! `malformed` where the text is not written so, `out_of_range` where a
|
||||
#! number is outside what its place takes, as an hour of 25 or a second
|
||||
#! of 60, or `no_such_date` where the month has no such day.
|
||||
pub def parse_rfc3339(text: strview): result<date_time, error> {
|
||||
var at: u64 = 0;
|
||||
val day := date_at(text, &mut at) else (why) { return why; };
|
||||
return error::malformed if !reads(text, &mut at, 'T') && !reads(text, &mut at, 't') && !reads(text, &mut at, ' ');
|
||||
|
||||
val hour := digits_at(text, &mut at, 2) else { return error::malformed; };
|
||||
return error::malformed if !reads(text, &mut at, ':');
|
||||
val minute := digits_at(text, &mut at, 2) else { return error::malformed; };
|
||||
return error::malformed if !reads(text, &mut at, ':');
|
||||
val second := digits_at(text, &mut at, 2) else { return error::malformed; };
|
||||
|
||||
# A fraction of any length, read to the nanosecond.
|
||||
var nanosecond: u64 = 0;
|
||||
if reads(text, &mut at, '.') {
|
||||
var places: u64 = 0;
|
||||
while at < text.length && text[at] >= '0' && text[at] <= '9' {
|
||||
if places < 9 {
|
||||
nanosecond = nanosecond * 10 + (text[at] - '0') as u64;
|
||||
}
|
||||
places++;
|
||||
at++;
|
||||
}
|
||||
return error::malformed if places == 0;
|
||||
for _ in places..9 {
|
||||
nanosecond *= 10;
|
||||
}
|
||||
}
|
||||
|
||||
var ahead: i64 = 0;
|
||||
if !reads(text, &mut at, 'Z') && !reads(text, &mut at, 'z') {
|
||||
val west := reads(text, &mut at, '-');
|
||||
return error::malformed if !west && !reads(text, &mut at, '+');
|
||||
val hours := digits_at(text, &mut at, 2) else { return error::malformed; };
|
||||
return error::malformed if !reads(text, &mut at, ':');
|
||||
val minutes := digits_at(text, &mut at, 2) else { return error::malformed; };
|
||||
return error::out_of_range if hours > 23 || minutes > 59;
|
||||
ahead = (hours * 3600 + minutes * 60) as i64;
|
||||
if west {
|
||||
ahead = -ahead;
|
||||
}
|
||||
}
|
||||
return error::malformed if at != text.length;
|
||||
return error::out_of_range if hour > 23 || minute > 59 || second > 59;
|
||||
|
||||
return date_time {
|
||||
date = day,
|
||||
time = time_of_day { hour = hour as u8, minute = minute as u8, second = second as u8, nanosecond = nanosecond as u32 },
|
||||
offset = seconds(ahead),
|
||||
name = ""
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,540 @@
|
||||
#! @file zone.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `zone`, a time zone: how far its clocks are from UTC, and when that
|
||||
#! changed. A zone is UTC, a fixed offset, or one of the system's database,
|
||||
#! which `zone::named` reads, as `zone::local` reads the system's own.
|
||||
#!
|
||||
#! The database is a directory of TZif files, as RFC 8536 describes them: the
|
||||
#! moments a zone's clocks changed, each with the offset kept from then on and
|
||||
#! what the zone calls it, as "CST" and "CDT". A file ends in a rule, as
|
||||
#! `CST6CDT,M3.2.0,M11.1.0`, which says how the clocks change every year after
|
||||
#! the last moment it lists. A zone holds both, so it answers for any moment.
|
||||
|
||||
import std::io;
|
||||
|
||||
module std::time;
|
||||
|
||||
|
||||
# A way a zone keeps its clocks: how far ahead of UTC they are, and where what
|
||||
# it calls that is among the zone's names.
|
||||
struct keeping {
|
||||
var offset: i32; # Seconds ahead of UTC, which is negative to its west.
|
||||
var name: u32; # Where its abbreviation begins among the names.
|
||||
var length: u32; # How many bytes its abbreviation is.
|
||||
}
|
||||
|
||||
# When a zone's clocks change each year, as the rule ending its file says it: a
|
||||
# day of the year, in one of three forms, and a time of that day.
|
||||
struct turning {
|
||||
var form: u8; # 0 for `Mm.w.d`, 1 for `Jn`, and 2 for a day counted from 0.
|
||||
var month: u8; # Of `Mm.w.d`, the month.
|
||||
var week: u8; # Of `Mm.w.d`, which of the month's weeks, 5 being its last.
|
||||
var day: u16; # Of `Mm.w.d`, the day of the week from Sunday, and otherwise the day of the year.
|
||||
var time: i32; # Seconds after midnight, as the clocks read before the change.
|
||||
}
|
||||
|
||||
#! A time zone: how far its clocks are from UTC at any moment, and what it
|
||||
#! calls its time then.
|
||||
#!
|
||||
#! It owns its tables, which are closed with it. `zone::local()` lends the
|
||||
#! system's own zone, which the program keeps once it is read.
|
||||
pub struct zone {
|
||||
var _moments: vector<i64>; #! When its clocks changed, in seconds since 1970, in order.
|
||||
var _after: vector<u8>; #! The way it kept its clocks from each of those on.
|
||||
var _ways: vector<keeping>; #! The ways it has kept its clocks, the first being that before any change.
|
||||
var _names: string; #! What it calls each, one after another.
|
||||
var _ruled: bool; #! Whether a rule says how its clocks change after the last of those.
|
||||
var _summer: bool; #! Whether the rule changes the clocks for part of each year.
|
||||
var _usual: keeping; #! How the rule keeps its clocks most of the year.
|
||||
var _saving: keeping; #! How it keeps them for the rest.
|
||||
var _begins: turning; #! When that begins each year.
|
||||
var _ends: turning; #! When it ends.
|
||||
}
|
||||
|
||||
# A zone with nothing in it yet.
|
||||
def zone::empty(): zone {
|
||||
return zone {
|
||||
_moments = vector::<i64>::new(),
|
||||
_after = vector::<u8>::new(),
|
||||
_ways = vector::<keeping>::new(),
|
||||
_names = string::new(nothing {})
|
||||
};
|
||||
}
|
||||
|
||||
# Adds a way of keeping the clocks, called what the text says.
|
||||
def zone::keep(&mut this, offset: i32, name: strview): keeping {
|
||||
val made := keeping { offset = offset, name = this._names.length as u32, length = name.length as u32 };
|
||||
this._names.append(name);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The zone of UTC, which never changes.
|
||||
#! @returns The zone.
|
||||
pub def zone::utc(): zone {
|
||||
return zone::fixed(nanoseconds(0));
|
||||
}
|
||||
|
||||
#! A zone a fixed length of time ahead of UTC, to the second, which never
|
||||
#! changes. It calls its time "UTC" where it is no time ahead, and otherwise by
|
||||
#! its offset, as "+05:30".
|
||||
#! @param offset How far ahead of UTC its clocks are, which is negative to its
|
||||
#! west.
|
||||
#! @returns The zone.
|
||||
pub def zone::fixed(offset: duration): zone {
|
||||
var made := zone::empty();
|
||||
val ahead := offset._ns / 1000000000;
|
||||
expects(ahead > -2147483648 && ahead < 2147483647, "A zone's offset is one an `i32` of seconds holds.");
|
||||
var name := string::new(nothing {});
|
||||
if ahead == 0 {
|
||||
name.append("UTC");
|
||||
} else {
|
||||
put_offset(&mut name, ahead, true);
|
||||
}
|
||||
val way := made.keep(ahead as i32, name.view());
|
||||
made._ways.append(way);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The zone of a name in the system's database, as "Asia/Tokyo" or
|
||||
#! "America/Chicago", read from `/usr/share/zoneinfo`. Windows keeps no such
|
||||
#! database, so there it is read from the directory `TZDIR` names, where the
|
||||
#! program has that variable, and no name is a zone where it has not.
|
||||
#! @param name The zone's name. One beginning with `/` or holding `..` names
|
||||
#! nothing, so no name reaches outside the database, and neither
|
||||
#! does one beginning with `\` or holding `:` on Windows.
|
||||
#! @returns The zone, `no_such_zone` where the database has no file of the
|
||||
#! name, or `unreadable_zone` where the file is not a zone's.
|
||||
pub def zone::named(name: strview): result<zone, error> {
|
||||
if name.length == 0 || name[0] == '/' {
|
||||
return error::no_such_zone;
|
||||
}
|
||||
for i in 1u..name.length {
|
||||
if name[i] == '.' && name[i - 1] == '.' {
|
||||
return error::no_such_zone;
|
||||
}
|
||||
}
|
||||
when os == windows {
|
||||
if name[0] == '\\' {
|
||||
return error::no_such_zone;
|
||||
}
|
||||
for i in 0u..name.length {
|
||||
if name[i] == ':' {
|
||||
return error::no_such_zone;
|
||||
}
|
||||
}
|
||||
val directory := database() else { return error::no_such_zone; };
|
||||
val text := $"{directory}/{name}";
|
||||
var at := path::parse(text.view()) else (_) { return error::no_such_zone; };
|
||||
return zone::read(&at);
|
||||
}
|
||||
val text := $"/usr/share/zoneinfo/{name}";
|
||||
var at := path::parse(text.view()) else (_) { return error::no_such_zone; };
|
||||
return zone::read(&at);
|
||||
}
|
||||
|
||||
#! The zone a TZif file describes, wherever the file is.
|
||||
#! @param at The file's path.
|
||||
#! @returns The zone, `no_such_zone` where the file cannot be read, or
|
||||
#! `unreadable_zone` where it is not a zone's.
|
||||
pub def zone::read(at: &path): result<zone, error> {
|
||||
var file := io::file::open(at) else (_) { return error::no_such_zone; };
|
||||
val data := io::read_all(&mut file) else (_) { return error::no_such_zone; };
|
||||
return zone::parse(data.view());
|
||||
}
|
||||
|
||||
# The zone the program keeps as the system's own, once it has been read.
|
||||
var local_read: bool = false;
|
||||
var local_zone: zone;
|
||||
|
||||
when os == windows {
|
||||
# The directory of the database of zones, as `TZDIR` names it, which Windows
|
||||
# reads whatever the case of its letters, or nothing where the program has
|
||||
# none.
|
||||
def database(): maybe<strview> {
|
||||
for each in system::environment() {
|
||||
if each.length <= 6 || each[5] != '=' {
|
||||
continue;
|
||||
}
|
||||
var same := true;
|
||||
for i in 0u..5u {
|
||||
same = same && each[i].to_upper == "TZDIR"[i];
|
||||
}
|
||||
if same {
|
||||
return each.substr(6, each.length - 6);
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
}
|
||||
|
||||
#! The zone the system is set to, read from `/etc/localtime` the first time it
|
||||
#! is asked for, and kept for as long as the program runs. Where the system
|
||||
#! says none that can be read, it is UTC.
|
||||
#!
|
||||
#! Windows keeps no such file, so there it is the rule the system keeps its
|
||||
#! clocks by now, as `system::zone_rule` writes it, which says nothing of how
|
||||
#! the zone kept them in years the rule was not yet made.
|
||||
#! @returns The zone, which the program keeps.
|
||||
pub def zone::local(): &zone {
|
||||
if !local_read {
|
||||
local_read = true;
|
||||
local_zone = zone::utc();
|
||||
when os == windows {
|
||||
var rule := string::new(nothing {});
|
||||
if system::zone_rule(&mut rule) {
|
||||
var found := zone::empty();
|
||||
found.rule(rule.view());
|
||||
if found._ruled {
|
||||
local_zone = move(found);
|
||||
}
|
||||
}
|
||||
return &local_zone;
|
||||
}
|
||||
var at := path::parse("/etc/localtime") else (_) { return &local_zone; };
|
||||
var found := zone::read(&at) else (_) { return &local_zone; };
|
||||
local_zone = move(found);
|
||||
}
|
||||
return &local_zone;
|
||||
}
|
||||
|
||||
# A number written in so many bytes, the most significant first.
|
||||
def unsigned_at(data: strview, at: u64, bytes: u64): u64 {
|
||||
var made: u64 = 0;
|
||||
for i in 0u..bytes {
|
||||
made = (made << 8) | data[at + i] as u64;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# A number written in so many bytes, the most significant first, which is
|
||||
# negative where its first bit is set.
|
||||
def signed_at(data: strview, at: u64, bytes: u64): i64 {
|
||||
val raw := unsigned_at(data, at, bytes);
|
||||
if bytes < 8 && (raw >> (bytes * 8 - 1)) != 0 {
|
||||
return raw as i64 - (1 as i64 << (bytes * 8));
|
||||
}
|
||||
return raw as i64;
|
||||
}
|
||||
|
||||
#! The zone the bytes of a TZif file describe, as RFC 8536 lays them out: the
|
||||
#! file's second block, of 64-bit moments, and the rule after it, where it has
|
||||
#! them, and its first otherwise.
|
||||
#! @param data The bytes of the file.
|
||||
#! @returns The zone, or `unreadable_zone` where the bytes are not a zone's, or
|
||||
#! are cut short.
|
||||
pub def zone::parse(data: strview): result<zone, error> {
|
||||
if data.length < 44 || data.substr(0, 4) != "TZif" {
|
||||
return error::unreadable_zone;
|
||||
}
|
||||
|
||||
# A header counts what its block holds. A file of a later version holds a
|
||||
# first block of 32-bit moments, for old readers, and then all of it again in
|
||||
# 64 bits.
|
||||
var head: u64 = 0;
|
||||
var wide: u64 = 4;
|
||||
if data[4] != 0 {
|
||||
val moments := unsigned_at(data, 32, 4);
|
||||
val ways := unsigned_at(data, 36, 4);
|
||||
val names := unsigned_at(data, 40, 4);
|
||||
val leaps := unsigned_at(data, 28, 4);
|
||||
head = 44 + moments * 5 + ways * 6 + names + leaps * 8 + unsigned_at(data, 24, 4) + unsigned_at(data, 20, 4);
|
||||
wide = 8;
|
||||
if data.length < head + 44 || data.substr(head, 4) != "TZif" {
|
||||
return error::unreadable_zone;
|
||||
}
|
||||
}
|
||||
|
||||
val universal := unsigned_at(data, head + 20, 4);
|
||||
val standard := unsigned_at(data, head + 24, 4);
|
||||
val leaps := unsigned_at(data, head + 28, 4);
|
||||
val moments := unsigned_at(data, head + 32, 4);
|
||||
val ways := unsigned_at(data, head + 36, 4);
|
||||
val names := unsigned_at(data, head + 40, 4);
|
||||
val body := head + 44;
|
||||
val size := moments * (wide + 1) + ways * 6 + names + leaps * (wide + 4) + standard + universal;
|
||||
if ways == 0 || ways > 256 || data.length < body + size {
|
||||
return error::unreadable_zone;
|
||||
}
|
||||
|
||||
var made := zone::empty();
|
||||
val kinds := body + moments * wide;
|
||||
val kept := kinds + moments;
|
||||
val text := kept + ways * 6;
|
||||
for i in 0u..moments {
|
||||
made._moments.append(signed_at(data, body + i * wide, wide));
|
||||
if data[kinds + i] as u64 >= ways {
|
||||
return error::unreadable_zone;
|
||||
}
|
||||
made._after.append(data[kinds + i]);
|
||||
}
|
||||
made._names.append(data.substr(text, names));
|
||||
for i in 0u..ways {
|
||||
# Each is its offset, whether it is a summer's, and where its abbreviation
|
||||
# begins, which ends at a zero.
|
||||
val begins := data[kept + i * 6 + 5] as u64;
|
||||
if begins >= names {
|
||||
return error::unreadable_zone;
|
||||
}
|
||||
var length: u64 = 0;
|
||||
while begins + length < names && data[text + begins + length] != 0 {
|
||||
length++;
|
||||
}
|
||||
made._ways.append(keeping {
|
||||
offset = signed_at(data, kept + i * 6, 4) as i32,
|
||||
name = begins as u32,
|
||||
length = length as u32
|
||||
});
|
||||
}
|
||||
|
||||
# The rule is a line of its own after the second block.
|
||||
val after := body + size;
|
||||
if wide == 8 && after < data.length && data[after] == '\n' {
|
||||
var end := after + 1;
|
||||
while end < data.length && data[end] != '\n' {
|
||||
end++;
|
||||
}
|
||||
made.rule(data.substr(after + 1, end - after - 1));
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
# What the rule ending a file says, as `CST6CDT,M3.2.0,M11.1.0`: what the zone
|
||||
# calls its usual time and how far behind UTC that is, and where its clocks
|
||||
# change, what it calls the other time, how far behind that is, and when it
|
||||
# begins and ends each year. A rule that cannot be read is no rule.
|
||||
def zone::rule(&mut this, text: strview): void {
|
||||
var at: u64 = 0;
|
||||
val usual := rule_name(text, &mut at);
|
||||
var behind: i64 = 0;
|
||||
return if usual.length == 0 || !rule_time(text, &mut at, &mut behind);
|
||||
this._usual = this.keep((-behind) as i32, usual);
|
||||
this._ruled = true;
|
||||
return if at >= text.length;
|
||||
|
||||
val saving := rule_name(text, &mut at);
|
||||
return if saving.length == 0;
|
||||
# The other time is an hour ahead of the usual one where the rule gives none.
|
||||
var other := behind - 3600;
|
||||
if at < text.length && text[at] != ',' {
|
||||
return if !rule_time(text, &mut at, &mut other);
|
||||
}
|
||||
var begins := turning {};
|
||||
var ends := turning {};
|
||||
return if !rule_day(text, &mut at, &mut begins) || !rule_day(text, &mut at, &mut ends);
|
||||
this._saving = this.keep((-other) as i32, saving);
|
||||
this._begins = begins;
|
||||
this._ends = ends;
|
||||
this._summer = true;
|
||||
}
|
||||
|
||||
# A name of a rule: letters, or anything between `<` and `>`.
|
||||
def rule_name(text: strview, at: &mut u64): strview {
|
||||
val start: u64 = at;
|
||||
if at < text.length && text[at] == '<' {
|
||||
while at < text.length && text[at] != '>' {
|
||||
at++;
|
||||
}
|
||||
return "" if at >= text.length;
|
||||
at++;
|
||||
return text.substr(start + 1, at - start - 2);
|
||||
}
|
||||
while at < text.length && ((text[at] >= 'a' && text[at] <= 'z') || (text[at] >= 'A' && text[at] <= 'Z')) {
|
||||
at++;
|
||||
}
|
||||
return text.substr(start, at - start);
|
||||
}
|
||||
|
||||
# A time of a rule, as `6`, `-5:30`, or `2:00:00`, in seconds.
|
||||
def rule_time(text: strview, at: &mut u64, into: &mut i64): bool {
|
||||
var negative := false;
|
||||
if at < text.length && (text[at] == '+' || text[at] == '-') {
|
||||
negative = text[at] == '-';
|
||||
at++;
|
||||
}
|
||||
var total: i64 = 0;
|
||||
var scale: i64 = 3600;
|
||||
var parts := 0;
|
||||
while parts < 3 {
|
||||
var value: i64 = 0;
|
||||
val start: u64 = at;
|
||||
while at < text.length && text[at] >= '0' && text[at] <= '9' {
|
||||
value = value * 10 + (text[at] - '0') as i64;
|
||||
at++;
|
||||
}
|
||||
return false if at == start || value > 999;
|
||||
total += value * scale;
|
||||
scale /= 60;
|
||||
parts++;
|
||||
break if at >= text.length || text[at] != ':';
|
||||
at++;
|
||||
}
|
||||
into = negative ? -total : total;
|
||||
return true;
|
||||
}
|
||||
|
||||
# A day of a rule after its comma, as `M3.2.0`, `J60`, or `59`, and the time
|
||||
# after its `/`, which is two in the morning where none is written.
|
||||
def rule_day(text: strview, at: &mut u64, into: &mut turning): bool {
|
||||
return false if at >= text.length || text[at] != ',';
|
||||
at++;
|
||||
return false if at >= text.length;
|
||||
|
||||
var numbers: i64[3];
|
||||
var count: u64 = 1;
|
||||
into.form = 2;
|
||||
if text[at] == 'M' {
|
||||
into.form = 0;
|
||||
count = 3;
|
||||
at++;
|
||||
} else if text[at] == 'J' {
|
||||
into.form = 1;
|
||||
at++;
|
||||
}
|
||||
for i in 0u..count {
|
||||
if i != 0 {
|
||||
return false if at >= text.length || text[at] != '.';
|
||||
at++;
|
||||
}
|
||||
val start: u64 = at;
|
||||
var value: i64 = 0;
|
||||
while at < text.length && text[at] >= '0' && text[at] <= '9' {
|
||||
value = value * 10 + (text[at] - '0') as i64;
|
||||
at++;
|
||||
}
|
||||
return false if at == start || value > 366;
|
||||
numbers[i] = value;
|
||||
}
|
||||
if into.form == 0 {
|
||||
return false if numbers[0] < 1 || numbers[0] > 12 || numbers[1] < 1 || numbers[1] > 5 || numbers[2] > 6;
|
||||
into.month = numbers[0] as u8;
|
||||
into.week = numbers[1] as u8;
|
||||
into.day = numbers[2] as u16;
|
||||
} else {
|
||||
return false if into.form == 1 && (numbers[0] < 1 || numbers[0] > 365);
|
||||
into.day = numbers[0] as u16;
|
||||
}
|
||||
|
||||
var time: i64 = 7200;
|
||||
if at < text.length && text[at] == '/' {
|
||||
at++;
|
||||
return false if !rule_time(text, at, &mut time);
|
||||
}
|
||||
into.time = time as i32;
|
||||
return true;
|
||||
}
|
||||
|
||||
# The second, since 1970, a change of the rule comes in a year, where the
|
||||
# clocks are so far ahead of UTC before it.
|
||||
def turning::second_in(&this, year: i64, ahead: i64): i64 {
|
||||
val first := days_from_civil(year, 1, 1);
|
||||
var day := first + this.day as i64;
|
||||
if this.form == 0 {
|
||||
# The first of the month, the first of the day of the week on or after it,
|
||||
# and then the week asked for, the fifth being the month's last.
|
||||
val begins := days_from_civil(year, this.month as i64, 1);
|
||||
val length := days_in(year as i32, this.month as month) as i64;
|
||||
val falls := ((begins + 4) % 7 + 7) % 7;
|
||||
day = begins + (this.day as i64 - falls + 7) % 7 + (this.week as i64 - 1) * 7;
|
||||
while day >= begins + length {
|
||||
day -= 7;
|
||||
}
|
||||
} else if this.form == 1 {
|
||||
# Counted from 1, with no 29th of February.
|
||||
day = first + this.day as i64 - 1 + (is_leap_year(year as i32) && this.day >= 60 ? 1 : 0);
|
||||
}
|
||||
return day * 86400 + this.time as i64 - ahead;
|
||||
}
|
||||
|
||||
# How the rule keeps the zone's clocks at a second since 1970.
|
||||
def zone::ruled_at(&this, second: i64): keeping {
|
||||
return this._usual if !this._summer;
|
||||
|
||||
val year := date::of_days(floored(second + this._usual.offset as i64, 86400)).year as i64;
|
||||
val begins := this._begins.second_in(year, this._usual.offset as i64);
|
||||
val ends := this._ends.second_in(year, this._saving.offset as i64);
|
||||
# South of the equator the other time runs over the new year.
|
||||
val saving := begins < ends ? second >= begins && second < ends : !(second >= ends && second < begins);
|
||||
return saving ? this._saving : this._usual;
|
||||
}
|
||||
|
||||
# How the zone keeps its clocks at a second since 1970.
|
||||
def zone::way_at(&this, second: i64): keeping {
|
||||
val count := this._moments.length;
|
||||
if count == 0 || second < this._moments[0] {
|
||||
return this._ruled && count == 0 ? this.ruled_at(second) : this._ways[0];
|
||||
}
|
||||
if this._ruled && second >= this._moments[count - 1] {
|
||||
return this.ruled_at(second);
|
||||
}
|
||||
|
||||
# The last change at or before it.
|
||||
var low: u64 = 0;
|
||||
var high: u64 = count;
|
||||
while high - low > 1 {
|
||||
val middle := low + (high - low) / 2;
|
||||
if this._moments[middle] <= second {
|
||||
low = middle;
|
||||
} else {
|
||||
high = middle;
|
||||
}
|
||||
}
|
||||
return this._ways[this._after[low]];
|
||||
}
|
||||
|
||||
# What the zone calls a way it keeps its clocks, viewing the zone's own names.
|
||||
def zone::name_of(&this, way: &keeping): strview {
|
||||
return this._names.view().substr(way.name as u64, way.length as u64);
|
||||
}
|
||||
|
||||
#! How far ahead of UTC the zone's clocks are at a moment.
|
||||
#! @param this The zone.
|
||||
#! @param moment The moment.
|
||||
#! @returns The offset, which is negative to the west of UTC.
|
||||
pub def zone::offset_at(&this, moment: timestamp): duration {
|
||||
return seconds(this.way_at(moment.unix).offset as i64);
|
||||
}
|
||||
|
||||
#! The moment the zone's clocks show a date and a time of day.
|
||||
#!
|
||||
#! Where they go forward, the times they skip are never shown, and one of those
|
||||
#! is taken as the moment that long after the change. Where they go back, the
|
||||
#! times they repeat are shown twice, and one of those is taken at its first.
|
||||
#! @param this The zone.
|
||||
#! @param day The date its clocks show.
|
||||
#! @param time The time of day they show.
|
||||
#! @returns The moment.
|
||||
pub def zone::resolve(&this, day: date, time: time_of_day): timestamp {
|
||||
val shown := sum_of(product_of(day.days(), 86400), time.hour as i64 * 3600 + time.minute as i64 * 60 + time.second as i64);
|
||||
|
||||
# The clocks kept one offset a day before, and one a day after, and change at
|
||||
# most once between. The moment is the time shown less whichever of the two
|
||||
# the clocks kept at it.
|
||||
val before := this.way_at(shown - 86400).offset as i64;
|
||||
val after := this.way_at(shown + 86400).offset as i64;
|
||||
var second := shown - before;
|
||||
if before != after {
|
||||
val early := this.way_at(shown - before).offset as i64 == before;
|
||||
val late := this.way_at(shown - after).offset as i64 == after;
|
||||
if late && (!early || shown - after < shown - before) {
|
||||
second = shown - after;
|
||||
}
|
||||
}
|
||||
return timestamp { _ns = sum_of(product_of(second, 1000000000), time.nanosecond as i64) };
|
||||
}
|
||||
|
||||
# Writes an offset from UTC as `+05:30`, with its seconds where it has any and
|
||||
# they are asked for.
|
||||
def put_offset(into: &mut string, ahead: i64, exact: bool): void {
|
||||
val size := ahead < 0 ? -ahead : ahead;
|
||||
into.push(ahead < 0 ? '-' : '+');
|
||||
put_padded(into, (size / 3600) as u64, 2);
|
||||
into.push(':');
|
||||
put_padded(into, (size / 60 % 60) as u64, 2);
|
||||
if exact && size % 60 != 0 {
|
||||
into.push(':');
|
||||
put_padded(into, (size % 60) as u64, 2);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user