Files
std/time/text.sakura
T

225 lines
8.2 KiB
Plaintext
Raw Permalink Normal View History

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