Lines
100 %
Functions
54.84 %
Branches
//! Local wall-clock text to UTC instants, with the zone given as a function so
//! the repeated and missing hours of a DST change can be tested.
const MS_PER_MINUTE: i64 = 60_000;
const MS_PER_DAY: i64 = 86_400_000;
/// Days since 1970-01-01 of a proleptic Gregorian date.
fn days_from_civil(year: i64, month: i64, day: i64) -> i64 {
let y = if month <= 2 { year - 1 } else { year };
let era = y.div_euclid(400);
let yoe = y.rem_euclid(400);
let mp = (month + 9) % 12;
let doy = (153 * mp + 2) / 5 + day - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
era * 146_097 + doe - 719_468
}
fn civil_from_days(days: i64) -> (i64, i64, i64) {
let z = days + 719_468;
let era = z.div_euclid(146_097);
let doe = z.rem_euclid(146_097);
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe + era * 400 + i64::from(month <= 2);
(year, month, day)
fn number(text: &str, digits: usize) -> Option<i64> {
(text.len() == digits && text.bytes().all(|b| b.is_ascii_digit()))
.then(|| text.parse().ok())
.flatten()
fn valid_day(year: i64, month: i64, day: i64) -> bool {
(1..=12).contains(&month)
&& day >= 1
&& civil_from_days(days_from_civil(year, month, day)) == (year, month, day)
/// Parses `YYYY-MM-DDTHH:MM[:SS]` as a wall-clock time, in ms as if it were UTC.
#[must_use]
pub fn parse_local(text: &str) -> Option<i64> {
let (date, time) = text.split_once('T')?;
let mut d = date.splitn(3, '-');
let (year, month, day) = (
number(d.next()?, 4)?,
number(d.next()?, 2)?,
);
let mut t = time.splitn(3, ':');
let hour = number(t.next()?, 2)?;
let minute = number(t.next()?, 2)?;
let second = t.next().map_or(Some(0), |s| number(s, 2))?;
(valid_day(year, month, day) && hour < 24 && minute < 60 && second < 60).then(|| {
days_from_civil(year, month, day) * MS_PER_DAY + ((hour * 60 + minute) * 60 + second) * 1000
})
/// The wall-clock text `YYYY-MM-DDTHH:MM` of an instant in a zone `offset_minutes` east of UTC.
pub fn format_local(utc_ms: i64, offset_minutes: i32) -> String {
let local = utc_ms + i64::from(offset_minutes) * MS_PER_MINUTE;
let (year, month, day) = civil_from_days(local.div_euclid(MS_PER_DAY));
let minutes = local.rem_euclid(MS_PER_DAY) / MS_PER_MINUTE;
format!(
"{year:04}-{month:02}-{day:02}T{:02}:{:02}",
minutes / 60,
minutes % 60
)
/// `toISOString`-style UTC text of an instant.
pub fn format_utc(utc_ms: i64) -> String {
let (year, month, day) = civil_from_days(utc_ms.div_euclid(MS_PER_DAY));
let in_day = utc_ms.rem_euclid(MS_PER_DAY);
"{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}.{:03}Z",
in_day / 3_600_000,
in_day / MS_PER_MINUTE % 60,
in_day / 1000 % 60,
in_day % 1000
/// The instant of a wall-clock time. `offset_at(utc_ms)` is the zone offset in
/// minutes east of UTC at that instant. A repeated hour resolves to its first
/// occurrence and a missing hour is read with the offset before the change,
/// as `new Date(text)` does.
pub fn local_to_utc(wall_ms: i64, offset_at: impl Fn(i64) -> i32) -> i64 {
let before = offset_at(wall_ms - MS_PER_DAY);
let after = offset_at(wall_ms + MS_PER_DAY);
let candidates = [before, after].map(|o| wall_ms - i64::from(o) * MS_PER_MINUTE);
let consistent = candidates
.iter()
.zip([before, after])
.filter_map(|(utc, o)| (offset_at(*utc) == o).then_some(*utc));
consistent
.min()
.unwrap_or(wall_ms - i64::from(before) * MS_PER_MINUTE)
#[cfg(test)]
#[path = "date_zone_tests.rs"]
mod tests;