Newest first, the most recent chosen at each opening; teams by Placement with the Account highlighted, and « Indisponible » where the source says nothing. The list is its own virtualized view; below 900 px it goes behind a button. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
137 lines
4.0 KiB
Rust
137 lines
4.0 KiB
Rust
//! The player's wall clock: a Match's start as the date and time Windows shows, with the
|
|
//! daylight saving time in force on that day, not today's.
|
|
|
|
use arena_tracker_core::Date;
|
|
|
|
/// A day and a time of day on the player's clock.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct LocalTime {
|
|
pub date: Date,
|
|
pub hour: u8,
|
|
pub minute: u8,
|
|
}
|
|
|
|
impl LocalTime {
|
|
/// The UTC day and time of that instant: the clock where Windows cannot tell.
|
|
pub fn utc(epoch_ms: i64) -> LocalTime {
|
|
const MS_PER_MINUTE: i64 = 60_000;
|
|
let minutes = epoch_ms.rem_euclid(86_400_000) / MS_PER_MINUTE;
|
|
LocalTime {
|
|
date: Date::from_ms(epoch_ms),
|
|
hour: (minutes / 60) as u8,
|
|
minute: (minutes % 60) as u8,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// That instant on the player's clock.
|
|
pub fn local_time(epoch_ms: i64) -> LocalTime {
|
|
#[cfg(windows)]
|
|
if let Some(local) = windows::local_time(epoch_ms) {
|
|
return local;
|
|
}
|
|
LocalTime::utc(epoch_ms)
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
mod windows {
|
|
use super::LocalTime;
|
|
use arena_tracker_core::Date;
|
|
|
|
#[repr(C)]
|
|
#[derive(Default)]
|
|
struct FileTime {
|
|
low: u32,
|
|
high: u32,
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Default)]
|
|
struct SystemTime {
|
|
year: u16,
|
|
month: u16,
|
|
day_of_week: u16,
|
|
day: u16,
|
|
hour: u16,
|
|
minute: u16,
|
|
second: u16,
|
|
milliseconds: u16,
|
|
}
|
|
|
|
#[link(name = "kernel32")]
|
|
unsafe extern "system" {
|
|
fn FileTimeToSystemTime(file_time: *const FileTime, system_time: *mut SystemTime) -> i32;
|
|
fn SystemTimeToTzSpecificLocalTime(
|
|
time_zone: *const core::ffi::c_void,
|
|
universal: *const SystemTime,
|
|
local: *mut SystemTime,
|
|
) -> i32;
|
|
}
|
|
|
|
/// Milliseconds from 1601-01-01, where Windows counts from, to 1970-01-01.
|
|
const EPOCH_FROM_1601_MS: i64 = 11_644_473_600_000;
|
|
|
|
/// `None` when Windows refuses the instant.
|
|
pub(super) fn local_time(epoch_ms: i64) -> Option<LocalTime> {
|
|
let ticks = u64::try_from(epoch_ms.checked_add(EPOCH_FROM_1601_MS)?)
|
|
.ok()?
|
|
.checked_mul(10_000)?;
|
|
let file_time = FileTime {
|
|
low: ticks as u32,
|
|
high: (ticks >> 32) as u32,
|
|
};
|
|
let (mut universal, mut local) = (SystemTime::default(), SystemTime::default());
|
|
// SAFETY: both structures are the Win32 layouts, alive for the calls; a null time zone
|
|
// is the one in force.
|
|
let converted = unsafe {
|
|
FileTimeToSystemTime(&file_time, &mut universal) != 0
|
|
&& SystemTimeToTzSpecificLocalTime(core::ptr::null(), &universal, &mut local) != 0
|
|
};
|
|
converted.then(|| LocalTime {
|
|
date: Date {
|
|
year: i32::from(local.year),
|
|
month: local.month as u8,
|
|
day: local.day as u8,
|
|
},
|
|
hour: local.hour as u8,
|
|
minute: local.minute as u8,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::{LocalTime, local_time};
|
|
use arena_tracker_core::Date;
|
|
|
|
#[test]
|
|
fn the_utc_clock_splits_the_instant() {
|
|
// 2026-11-15 17:29:12 UTC.
|
|
let time = LocalTime::utc(1_794_763_752_767);
|
|
assert_eq!(
|
|
time,
|
|
LocalTime {
|
|
date: Date {
|
|
year: 2026,
|
|
month: 11,
|
|
day: 15
|
|
},
|
|
hour: 17,
|
|
minute: 29,
|
|
}
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_local_clock_is_whole_minutes_away_from_utc() {
|
|
// Whatever the machine's time zone: at most 14 hours away, by whole quarter hours.
|
|
let at = 1_794_763_752_767;
|
|
let local = local_time(at);
|
|
let minutes = |t: LocalTime| {
|
|
(Date::start_ms(&t.date) / 60_000) + i64::from(t.hour) * 60 + i64::from(t.minute)
|
|
};
|
|
let shift = minutes(local) - minutes(LocalTime::utc(at));
|
|
assert!(shift.abs() <= 14 * 60 && shift % 15 == 0, "{shift}");
|
|
}
|
|
}
|