use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use std::time::Duration;
use waft_embedder::{Platform, SignedDuration};
use web_time::{SystemTime, UNIX_EPOCH};
use crate::App;
#[derive(Clone, Copy, Debug)]
pub struct DateTime {
microseconds_since_epoch: i64,
is_utc: bool,
}
const MICROSECONDS_PER_MILLISECOND: i64 = 1000;
const MICROSECONDS_PER_SECOND: i64 = 1_000_000;
const MICROSECONDS_PER_MINUTE: i64 = 60 * MICROSECONDS_PER_SECOND;
const MICROSECONDS_PER_HOUR: i64 = 60 * MICROSECONDS_PER_MINUTE;
const MICROSECONDS_PER_DAY: i64 = 24 * MICROSECONDS_PER_HOUR;
const SECONDS_PER_DAY: i64 = 24 * 60 * 60;
const MAX_MILLISECONDS_SINCE_EPOCH: i64 = 8_640_000_000_000_000;
impl DateTime {
pub const MONDAY: i32 = 1;
pub const TUESDAY: i32 = 2;
pub const WEDNESDAY: i32 = 3;
pub const THURSDAY: i32 = 4;
pub const FRIDAY: i32 = 5;
pub const SATURDAY: i32 = 6;
pub const SUNDAY: i32 = 7;
pub const DAYS_PER_WEEK: i32 = 7;
pub const JANUARY: i32 = 1;
pub const FEBRUARY: i32 = 2;
pub const MARCH: i32 = 3;
pub const APRIL: i32 = 4;
pub const MAY: i32 = 5;
pub const JUNE: i32 = 6;
pub const JULY: i32 = 7;
pub const AUGUST: i32 = 8;
pub const SEPTEMBER: i32 = 9;
pub const OCTOBER: i32 = 10;
pub const NOVEMBER: i32 = 11;
pub const DECEMBER: i32 = 12;
pub const MONTHS_PER_YEAR: i32 = 12;
pub fn new(app: &App, year: i32, month: i32, day: i32) -> DateTime {
DateTime::new_with_time(app, year, month, day, 0, 0, 0, 0, 0)
}
#[expect(
clippy::too_many_arguments,
reason = "Dart's positional constructor; each field may overflow into the next"
)]
pub fn new_with_time(
app: &App,
year: i32,
month: i32,
day: i32,
hour: i32,
minute: i32,
second: i32,
millisecond: i32,
microsecond: i32,
) -> DateTime {
let fields = [
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond,
];
DateTime::from_fields(fields, Some(&*app.platform()))
}
pub fn utc(year: i32, month: i32, day: i32) -> DateTime {
DateTime::utc_with_time(year, month, day, 0, 0, 0, 0, 0)
}
#[expect(
clippy::too_many_arguments,
reason = "Dart's positional constructor; each field may overflow into the next"
)]
pub fn utc_with_time(
year: i32,
month: i32,
day: i32,
hour: i32,
minute: i32,
second: i32,
millisecond: i32,
microsecond: i32,
) -> DateTime {
let fields = [
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond,
];
DateTime::from_fields(fields, None)
}
pub fn now() -> DateTime {
DateTime::from_value(current_microseconds(), false)
}
pub fn timestamp() -> DateTime {
DateTime::from_value(current_microseconds(), true)
}
pub fn from_milliseconds_since_epoch(milliseconds_since_epoch: i64, is_utc: bool) -> DateTime {
assert!(
milliseconds_since_epoch.abs() <= MAX_MILLISECONDS_SINCE_EPOCH,
"DateTime is outside valid range: {milliseconds_since_epoch}"
);
DateTime::from_value(
milliseconds_since_epoch * MICROSECONDS_PER_MILLISECOND,
is_utc,
)
}
pub fn from_microseconds_since_epoch(microseconds_since_epoch: i64, is_utc: bool) -> DateTime {
DateTime::from_value(microseconds_since_epoch, is_utc)
}
fn from_value(microseconds_since_epoch: i64, is_utc: bool) -> DateTime {
assert!(
microseconds_since_epoch.abs()
<= MAX_MILLISECONDS_SINCE_EPOCH * MICROSECONDS_PER_MILLISECOND,
"DateTime is outside valid range: {microseconds_since_epoch}"
);
DateTime {
microseconds_since_epoch,
is_utc,
}
}
fn from_fields(fields: [i32; 8], zone: Option<&dyn Platform>) -> DateTime {
let value = broken_down_date_to_value(fields, zone)
.unwrap_or_else(|| panic!("DateTime is outside valid range: {fields:?}"));
DateTime::from_value(value, zone.is_none())
}
pub fn is_utc(&self) -> bool {
self.is_utc
}
pub fn milliseconds_since_epoch(&self) -> i64 {
self.microseconds_since_epoch
.div_euclid(MICROSECONDS_PER_MILLISECOND)
}
pub fn microseconds_since_epoch(&self) -> i64 {
self.microseconds_since_epoch
}
pub fn year(&self, app: &App) -> i32 {
self.civil(app).0
}
pub fn month(&self, app: &App) -> i32 {
self.civil(app).1
}
pub fn day(&self, app: &App) -> i32 {
self.civil(app).2
}
pub fn hour(&self, app: &App) -> i32 {
self.time_of_day_part(app, MICROSECONDS_PER_HOUR, 24)
}
pub fn minute(&self, app: &App) -> i32 {
self.time_of_day_part(app, MICROSECONDS_PER_MINUTE, 60)
}
pub fn second(&self, app: &App) -> i32 {
self.time_of_day_part(app, MICROSECONDS_PER_SECOND, 60)
}
pub fn millisecond(&self, app: &App) -> i32 {
let _ = app;
let part = self
.microseconds_since_epoch
.div_euclid(MICROSECONDS_PER_MILLISECOND)
.rem_euclid(1000);
i32::try_from(part).expect("below a thousand")
}
pub fn microsecond(&self, app: &App) -> i32 {
let _ = app;
let part = self
.microseconds_since_epoch
.rem_euclid(MICROSECONDS_PER_MILLISECOND);
i32::try_from(part).expect("below a thousand")
}
pub fn weekday(&self, app: &App) -> i32 {
let days = self.days_since_epoch(app);
i32::try_from((days + 3).rem_euclid(7)).expect("a weekday") + 1
}
pub fn time_zone_name(&self, app: &App) -> String {
if self.is_utc {
return "UTC".to_string();
}
app.platform()
.time_zone_name(equivalent_seconds(self.microseconds_since_epoch))
}
pub fn time_zone_offset(&self, app: &App) -> SignedDuration {
SignedDuration::from_microseconds(self.offset_microseconds(app))
}
pub fn to_local(&self) -> DateTime {
if !self.is_utc {
return *self;
}
DateTime::from_value(self.microseconds_since_epoch, false)
}
pub fn to_utc(&self) -> DateTime {
if self.is_utc {
return *self;
}
DateTime::from_value(self.microseconds_since_epoch, true)
}
pub fn add(&self, duration: Duration) -> DateTime {
self.moved_by(duration_microseconds(duration))
}
pub fn subtract(&self, duration: Duration) -> DateTime {
self.moved_by(-duration_microseconds(duration))
}
pub fn is_before(&self, other: &DateTime) -> bool {
self.microseconds_since_epoch < other.microseconds_since_epoch
}
pub fn is_after(&self, other: &DateTime) -> bool {
self.microseconds_since_epoch > other.microseconds_since_epoch
}
pub fn is_at_same_moment_as(&self, other: &DateTime) -> bool {
self.microseconds_since_epoch == other.microseconds_since_epoch
}
pub fn compare_to(&self, other: &DateTime) -> Ordering {
self.microseconds_since_epoch
.cmp(&other.microseconds_since_epoch)
}
pub fn to_iso8601_string(&self, app: &App) -> String {
self.format(app, 'T')
}
pub fn to_string(&self, app: &App) -> String {
self.format(app, ' ')
}
fn format(&self, app: &App, separator: char) -> String {
let wall = self.wall_microseconds(app);
let (year, month, day) = civil_from_days(wall.div_euclid(MICROSECONDS_PER_DAY));
let part = |unit: i64, modulus: i64| wall.div_euclid(unit).rem_euclid(modulus);
let y = if (-9999..=9999).contains(&year) {
four_digits(year)
} else {
six_digits(year)
};
let us = match part(1, MICROSECONDS_PER_MILLISECOND) {
0 => String::new(),
microsecond => format!("{microsecond:03}"),
};
let z = if self.is_utc { "Z" } else { "" };
format!(
"{y}-{month:02}-{day:02}{separator}{:02}:{:02}:{:02}.{:03}{us}{z}",
part(MICROSECONDS_PER_HOUR, 24),
part(MICROSECONDS_PER_MINUTE, 60),
part(MICROSECONDS_PER_SECOND, 60),
part(MICROSECONDS_PER_MILLISECOND, 1000),
)
}
fn moved_by(&self, microseconds: i64) -> DateTime {
DateTime::from_value(self.microseconds_since_epoch + microseconds, self.is_utc)
}
fn offset_microseconds(&self, app: &App) -> i64 {
if self.is_utc {
return 0;
}
local_offset_microseconds(&*app.platform(), self.microseconds_since_epoch)
}
fn wall_microseconds(&self, app: &App) -> i64 {
self.microseconds_since_epoch + self.offset_microseconds(app)
}
fn days_since_epoch(&self, app: &App) -> i64 {
self.wall_microseconds(app).div_euclid(MICROSECONDS_PER_DAY)
}
fn civil(&self, app: &App) -> (i32, i32, i32) {
civil_from_days(self.days_since_epoch(app))
}
fn time_of_day_part(&self, app: &App, unit: i64, modulus: i64) -> i32 {
let part = self
.wall_microseconds(app)
.div_euclid(unit)
.rem_euclid(modulus);
i32::try_from(part).expect("below the modulus")
}
}
impl PartialEq for DateTime {
fn eq(&self, other: &DateTime) -> bool {
self.microseconds_since_epoch == other.microseconds_since_epoch
&& self.is_utc == other.is_utc
}
}
impl Eq for DateTime {}
impl Hash for DateTime {
fn hash<H: Hasher>(&self, state: &mut H) {
self.microseconds_since_epoch.hash(state);
self.is_utc.hash(state);
}
}
impl PartialOrd for DateTime {
fn partial_cmp(&self, other: &DateTime) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for DateTime {
fn cmp(&self, other: &DateTime) -> Ordering {
self.compare_to(other).then(self.is_utc.cmp(&other.is_utc))
}
}
fn current_microseconds() -> i64 {
let since_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("the clock is past 1970");
i64::try_from(since_epoch.as_micros()).expect("within range")
}
fn local_offset_microseconds(zone: &dyn Platform, microseconds_since_epoch: i64) -> i64 {
zone.time_zone_offset(equivalent_seconds(microseconds_since_epoch))
.in_microseconds()
}
fn broken_down_date_to_value(fields: [i32; 8], zone: Option<&dyn Platform>) -> Option<i64> {
let [
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond,
] = fields;
let mut microseconds_since_epoch = days_from_civil(year, month, day) * MICROSECONDS_PER_DAY
+ i64::from(hour) * MICROSECONDS_PER_HOUR
+ i64::from(minute) * MICROSECONDS_PER_MINUTE
+ i64::from(second) * MICROSECONDS_PER_SECOND
+ i64::from(millisecond) * MICROSECONDS_PER_MILLISECOND
+ i64::from(microsecond);
let max = MAX_MILLISECONDS_SINCE_EPOCH * MICROSECONDS_PER_MILLISECOND;
if let Some(zone) = zone {
if microseconds_since_epoch.abs() > max + MICROSECONDS_PER_DAY {
return None;
}
microseconds_since_epoch -= to_local_time_offset(zone, microseconds_since_epoch);
}
(microseconds_since_epoch.abs() <= max).then_some(microseconds_since_epoch)
}
fn to_local_time_offset(zone: &dyn Platform, microseconds_since_epoch: i64) -> i64 {
let mut offset = local_offset_microseconds(zone, microseconds_since_epoch);
if offset != 0 {
let offset2 = local_offset_microseconds(zone, microseconds_since_epoch - offset);
if offset2 != offset {
let offset3 = local_offset_microseconds(zone, microseconds_since_epoch - offset2);
return offset2.min(offset3);
}
offset = offset2;
}
let offset4 = local_offset_microseconds(
zone,
microseconds_since_epoch - offset - 2 * MICROSECONDS_PER_HOUR,
);
if offset4 > offset {
if offset4 == offset + 2 * MICROSECONDS_PER_HOUR {
return offset4;
}
let offset5 = local_offset_microseconds(zone, microseconds_since_epoch - offset4);
if offset5 == offset4 {
return offset4;
}
}
offset
}
fn equivalent_seconds(microseconds_since_epoch: i64) -> i64 {
const CUT_OFF_SECONDS: i64 = 0x7FFF_FFFF;
let mut seconds = microseconds_since_epoch.div_euclid(MICROSECONDS_PER_SECOND);
if seconds.abs() > CUT_OFF_SECONDS {
let (year, _, _) = civil_from_days(seconds.div_euclid(SECONDS_PER_DAY));
let days = day_from_year(year);
let equivalent_days = day_from_year(equivalent_year(year));
seconds += (equivalent_days - days) * SECONDS_PER_DAY;
}
seconds
}
fn day_from_year(year: i32) -> i64 {
days_from_civil(year, 1, 1)
}
fn is_leap_year(year: i32) -> bool {
year % 4 == 0 && (year % 16 == 0 || year % 100 != 0)
}
fn equivalent_year(year: i32) -> i32 {
let week_day = (day_from_year(year) + 4).rem_euclid(7);
let week_day = i32::try_from(week_day).expect("a weekday");
let recent_year = if is_leap_year(year) { 1956 } else { 1967 } + week_day * 12;
2008 + (recent_year - 2008).rem_euclid(28)
}
fn duration_microseconds(duration: Duration) -> i64 {
i64::try_from(duration.as_micros()).expect("a duration within the DateTime range")
}
fn four_digits(n: i32) -> String {
let sign = if n < 0 { "-" } else { "" };
format!("{sign}{:04}", n.abs())
}
fn six_digits(n: i32) -> String {
debug_assert!(!(-9999..=9999).contains(&n));
let sign = if n < 0 { "-" } else { "+" };
format!("{sign}{:06}", n.abs())
}
fn days_from_civil(year: i32, month: i32, day: i32) -> i64 {
let year = i64::from(year) + i64::from(month - 1).div_euclid(12);
let month = i64::from(month - 1).rem_euclid(12) + 1;
let year = if month <= 2 { year - 1 } else { year };
let era = year.div_euclid(400);
let year_of_era = year - era * 400;
let month_index = if month > 2 { month - 3 } else { month + 9 };
let day_of_year = (153 * month_index + 2) / 5;
let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
era * 146_097 + day_of_era - 719_468 + i64::from(day - 1)
}
fn civil_from_days(days: i64) -> (i32, i32, i32) {
let days = days + 719_468;
let era = days.div_euclid(146_097);
let day_of_era = days - era * 146_097;
let year_of_era =
(day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146_096) / 365;
let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
let month_index = (5 * day_of_year + 2) / 153;
let day = day_of_year - (153 * month_index + 2) / 5 + 1;
let month = if month_index < 10 {
month_index + 3
} else {
month_index - 9
};
let year = year_of_era + era * 400 + i64::from(month <= 2);
(
i32::try_from(year).expect("a year in range"),
i32::try_from(month).expect("a month"),
i32::try_from(day).expect("a day"),
)
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Instant;
use waft_embedder::{
Dispatcher, InertDispatcher, PlatformRef, TargetPlatform, ViewId, ViewRef,
};
use super::*;
use crate::AppCell;
type Zone = fn(i64) -> (i64, &'static str);
struct ZonePlatform(Cell<Zone>);
impl Platform for ZonePlatform {
fn target_platform(&self) -> TargetPlatform {
TargetPlatform::Android
}
fn request_frame(&self, _view: ViewId) {}
fn now(&self) -> Instant {
Instant::now()
}
fn dispatcher(&self) -> Arc<dyn Dispatcher> {
Arc::new(InertDispatcher)
}
fn views(&self) -> Vec<ViewRef> {
Vec::new()
}
fn view(&self, _id: ViewId) -> Option<ViewRef> {
None
}
fn time_zone_offset(&self, seconds_since_epoch: i64) -> SignedDuration {
SignedDuration::from_seconds((self.0.get())(seconds_since_epoch).0)
}
fn time_zone_name(&self, seconds_since_epoch: i64) -> String {
(self.0.get())(seconds_since_epoch).1.to_string()
}
}
fn app_in(zone: Zone) -> Rc<AppCell> {
AppCell::with_platform(Rc::new(ZonePlatform(Cell::new(zone))))
}
fn utc_app() -> Rc<AppCell> {
AppCell::new()
}
#[test]
fn the_epoch_is_a_thursday_and_the_fields_read_back() {
let cell = utc_app();
let app = cell.borrow();
let epoch = DateTime::from_microseconds_since_epoch(0, true);
assert_eq!(
(epoch.year(&app), epoch.month(&app), epoch.day(&app)),
(1970, DateTime::JANUARY, 1)
);
assert_eq!(epoch.weekday(&app), DateTime::THURSDAY);
let moon = DateTime::utc_with_time(1969, 7, 20, 20, 18, 4, 5, 6);
assert_eq!(
(moon.hour(&app), moon.minute(&app), moon.second(&app)),
(20, 18, 4)
);
assert_eq!((moon.millisecond(&app), moon.microsecond(&app)), (5, 6));
assert_eq!(moon.weekday(&app), DateTime::SUNDAY);
assert!(moon.is_before(&epoch));
assert!(epoch.is_after(&moon));
assert_eq!(moon.compare_to(&epoch), Ordering::Less);
}
#[test]
fn leap_years_and_out_of_range_fields_roll_over_like_dart() {
let cell = utc_app();
let app = cell.borrow();
assert_eq!(DateTime::utc(2024, 2, 29).day(&app), 29);
assert_eq!(DateTime::utc(2024, 2, 30).month(&app), DateTime::MARCH);
assert_eq!(DateTime::utc(1900, 2, 29).month(&app), DateTime::MARCH);
assert_eq!(DateTime::utc(2000, 2, 29).month(&app), DateTime::FEBRUARY);
let thirteenth_month = DateTime::utc(2025, 13, 1);
assert_eq!(
(thirteenth_month.year(&app), thirteenth_month.month(&app)),
(2026, DateTime::JANUARY)
);
let zeroth_day = DateTime::utc(2026, 3, 0);
assert_eq!(
(zeroth_day.month(&app), zeroth_day.day(&app)),
(DateTime::FEBRUARY, 28)
);
let negative_month = DateTime::utc(2026, -1, 1);
assert_eq!(
(negative_month.year(&app), negative_month.month(&app)),
(2025, DateTime::NOVEMBER)
);
assert_eq!(
DateTime::utc_with_time(2026, 1, 1, 25, 0, 0, 0, 0).day(&app),
2
);
}
#[test]
fn add_and_subtract_move_across_month_boundaries() {
let cell = utc_app();
let app = cell.borrow();
let fifty_days = Duration::from_secs(50 * 24 * 60 * 60);
let today = DateTime::utc(2022, 1, 1);
assert_eq!(
today.add(fifty_days).to_string(&app),
"2022-02-20 00:00:00.000Z"
);
assert_eq!(
today.subtract(fifty_days).to_string(&app),
"2021-11-12 00:00:00.000Z"
);
assert_eq!(today.milliseconds_since_epoch(), 1_640_995_200_000);
assert_eq!(
DateTime::from_milliseconds_since_epoch(1_640_995_200_000, true),
today
);
}
#[test]
fn formatting_follows_dart() {
let cell = utc_app();
let app = cell.borrow();
let moon = DateTime::utc_with_time(1969, 7, 20, 20, 18, 4, 0, 0);
assert_eq!(moon.to_string(&app), "1969-07-20 20:18:04.000Z");
assert_eq!(moon.to_iso8601_string(&app), "1969-07-20T20:18:04.000Z");
let precise = DateTime::utc_with_time(2026, 9, 5, 1, 2, 3, 4, 5);
assert_eq!(
precise.to_iso8601_string(&app),
"2026-09-05T01:02:03.004005Z"
);
assert_eq!(
DateTime::utc(-44, 3, 15).to_string(&app),
"-0044-03-15 00:00:00.000Z"
);
assert_eq!(
DateTime::utc(12345, 1, 1).to_iso8601_string(&app),
"+012345-01-01T00:00:00.000Z"
);
}
#[test]
fn dates_before_the_epoch_read_back_their_fields() {
let cell = utc_app();
let app = cell.borrow();
let date = DateTime::utc_with_time(1960, 12, 31, 23, 59, 59, 999, 999);
assert_eq!(
(date.year(&app), date.month(&app), date.day(&app)),
(1960, 12, 31)
);
assert_eq!(
(date.hour(&app), date.minute(&app), date.second(&app)),
(23, 59, 59)
);
assert_eq!((date.millisecond(&app), date.microsecond(&app)), (999, 999));
assert_eq!(
date.add(Duration::from_micros(1)),
DateTime::utc(1961, 1, 1)
);
}
const HOUR: i64 = 60 * 60;
const SPRING_FORWARD: i64 = 1_774_746_000;
const FALL_BACK: i64 = 1_792_890_000;
fn central_europe(seconds: i64) -> (i64, &'static str) {
if (SPRING_FORWARD..FALL_BACK).contains(&seconds) {
(2 * HOUR, "CEST")
} else {
(HOUR, "CET")
}
}
fn pacific(_seconds: i64) -> (i64, &'static str) {
(-8 * HOUR, "PST")
}
#[test]
fn a_local_value_reads_its_fields_in_the_zone_and_keeps_the_instant() {
{
let cell = app_in(central_europe);
let app = cell.borrow();
let epoch = DateTime::from_microseconds_since_epoch(0, false);
assert!(!epoch.is_utc());
assert_eq!(
(epoch.year(&app), epoch.month(&app), epoch.day(&app)),
(1970, 1, 1)
);
assert_eq!(epoch.hour(&app), 1);
assert_eq!(epoch.to_string(&app), "1970-01-01 01:00:00.000");
assert_eq!(epoch.to_utc().to_string(&app), "1970-01-01 00:00:00.000Z");
assert_eq!(epoch.time_zone_name(&app), "CET");
assert_eq!(epoch.time_zone_offset(&app), SignedDuration::from_hours(1));
assert_eq!(epoch.to_utc().time_zone_offset(&app), SignedDuration::ZERO);
assert_eq!(epoch.to_utc().time_zone_name(&app), "UTC");
}
}
#[test]
fn a_local_value_reads_the_zone_the_platform_answers_now() {
let platform = Rc::new(ZonePlatform(Cell::new(central_europe)));
let cell = AppCell::with_platform(Rc::clone(&platform) as PlatformRef);
let app = cell.borrow();
let made_in_paris = DateTime::new_with_time(&app, 2026, 1, 1, 12, 0, 0, 0, 0);
let instant = made_in_paris.microseconds_since_epoch();
platform.0.set(pacific);
assert_eq!(made_in_paris.hour(&app), 3);
assert_eq!(
made_in_paris.time_zone_offset(&app),
SignedDuration::from_hours(-8)
);
assert_eq!(made_in_paris.time_zone_name(&app), "PST");
assert_eq!(made_in_paris.to_string(&app), "2026-01-01 03:00:00.000");
assert_eq!(made_in_paris.microseconds_since_epoch(), instant);
assert_eq!(made_in_paris.to_utc().hour(&app), 11);
}
#[test]
fn the_offset_west_of_utc_is_negative() {
{
let cell = app_in(pacific);
let app = cell.borrow();
let date = DateTime::new_with_time(&app, 2021, 11, 9, 10, 0, 0, 0, 0);
assert_eq!(date.time_zone_offset(&app).to_string(), "-8:00:00.000000");
assert_eq!(date.to_utc().hour(&app), 18);
assert_eq!(date.milliseconds_since_epoch(), 1_636_480_800_000);
}
}
#[test]
fn local_and_utc_values_of_one_instant_are_at_the_same_moment_but_not_equal() {
{
let cell = app_in(central_europe);
let app = cell.borrow();
let local = DateTime::new_with_time(&app, 2026, 7, 1, 12, 30, 15, 250, 7);
assert_eq!(
(
local.year(&app),
local.month(&app),
local.day(&app),
local.hour(&app),
local.minute(&app)
),
(2026, 7, 1, 12, 30)
);
assert_eq!(
(
local.second(&app),
local.millisecond(&app),
local.microsecond(&app)
),
(15, 250, 7)
);
let utc = local.to_utc();
assert_eq!(utc.hour(&app), 10);
assert!(utc.is_at_same_moment_as(&local));
assert_ne!(utc, local);
assert_eq!(utc.to_local(), local);
assert_eq!(local.to_local(), local);
}
}
#[test]
fn a_skipped_wall_time_is_read_in_the_zone_before_the_change() {
{
let cell = app_in(central_europe);
let app = cell.borrow();
let skipped = DateTime::new_with_time(&app, 2026, 3, 29, 2, 30, 0, 0, 0);
assert_eq!((skipped.hour(&app), skipped.minute(&app)), (3, 30));
assert_eq!(skipped.time_zone_name(&app), "CEST");
assert_eq!(skipped.to_utc().to_string(&app), "2026-03-29 01:30:00.000Z");
}
}
#[test]
fn a_repeated_wall_time_is_the_earlier_instant() {
{
let cell = app_in(central_europe);
let app = cell.borrow();
let repeated = DateTime::new_with_time(&app, 2026, 10, 25, 2, 30, 0, 0, 0);
assert_eq!(
repeated.to_utc().to_string(&app),
"2026-10-25 00:30:00.000Z"
);
assert_eq!(repeated.time_zone_name(&app), "CEST");
let an_hour_later = repeated.add(Duration::from_secs(60 * 60));
assert_eq!(
(an_hour_later.hour(&app), an_hour_later.minute(&app)),
(2, 30)
);
assert_eq!(an_hour_later.time_zone_name(&app), "CET");
}
}
#[test]
fn a_far_date_asks_the_zone_about_an_equivalent_year() {
fn checked(seconds: i64) -> (i64, &'static str) {
assert!(seconds.abs() <= 0x7FFF_FFFF, "asked about {seconds}");
central_europe_any_year(seconds)
}
{
let cell = app_in(checked);
let app = cell.borrow();
let summer = DateTime::new_with_time(&app, 2500, 7, 1, 12, 0, 0, 0, 0);
assert_eq!(
(summer.year(&app), summer.month(&app), summer.hour(&app)),
(2500, 7, 12)
);
assert_eq!(summer.time_zone_offset(&app), SignedDuration::from_hours(2));
let winter = DateTime::new_with_time(&app, 1800, 1, 1, 12, 0, 0, 0, 0);
assert_eq!(winter.time_zone_offset(&app), SignedDuration::from_hours(1));
assert_eq!(winter.to_utc().hour(&app), 11);
}
}
fn central_europe_any_year(seconds: i64) -> (i64, &'static str) {
let (_, month, _) = civil_from_days(seconds.div_euclid(SECONDS_PER_DAY));
if (4..=9).contains(&month) {
(2 * HOUR, "CEST")
} else {
(HOUR, "CET")
}
}
#[test]
fn equivalent_years_keep_the_calendar() {
let cell = utc_app();
let app = cell.borrow();
for year in [1600, 1800, 1900, 2100, 2400, 2500, 9999] {
let equivalent = equivalent_year(year);
assert!(
(2008..=2035).contains(&equivalent),
"{year} -> {equivalent}"
);
assert_eq!(is_leap_year(year), is_leap_year(equivalent), "{year}");
assert_eq!(
DateTime::utc(year, 1, 1).weekday(&app),
DateTime::utc(equivalent, 1, 1).weekday(&app),
"{year}"
);
}
}
#[test]
fn now_is_local_and_timestamp_is_utc() {
{
let cell = app_in(pacific);
let app = cell.borrow();
let now = DateTime::now();
let timestamp = DateTime::timestamp();
assert!(!now.is_utc());
assert!(timestamp.is_utc());
assert!(
(timestamp.microseconds_since_epoch() - now.microseconds_since_epoch()).abs()
< 60_000_000
);
assert_eq!(now.time_zone_name(&app), "PST");
}
}
#[test]
fn the_order_agrees_with_equality_at_one_instant() {
{
let cell = app_in(central_europe);
let app = cell.borrow();
let local = DateTime::new(&app, 2026, 1, 1);
let utc = local.to_utc();
assert_eq!(local.compare_to(&utc), Ordering::Equal);
assert_ne!(local, utc);
assert_eq!(local.cmp(&utc), Ordering::Less);
assert_eq!(local.cmp(&local.to_local()), Ordering::Equal);
assert!(utc < local.add(Duration::from_micros(1)));
}
}
#[test]
fn add_keeps_whether_the_value_is_local() {
{
let cell = app_in(central_europe);
let app = cell.borrow();
let local = DateTime::new(&app, 2026, 1, 1);
assert!(!local.add(Duration::from_secs(1)).is_utc());
assert!(
DateTime::utc(2026, 1, 1)
.add(Duration::from_secs(1))
.is_utc()
);
}
}
}