Files
std/time/calendar.sakura
T

240 lines
8.6 KiB
Plaintext
Raw Normal View History

2026-10-26 09:42:00 -05:00
#! @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);
}