Files
pokerfreak/cockpit/src/zeit.rs
T
mmintelandClaude Opus 5.5 f208b07609 Cockpit (Rust/Slint): Stand aus dem Haupt-Checkout gesichert
Uncommittete Arbeit der Session "Windows Cockpit GUI Swift migration"
(local_47bddbc0) aus D:\Projects\pokerfreak\cockpit, 1:1 kopiert (36 Dateien:
Sitzungsleiste, Live, Reiter, Einstellungen, Zeitplan, Turnierplan, Zuschauer,
Chips, Diagramm, Bruecke live/reiter, UI-Slint). Basis main 5d4f939.
cargo test --locked -j 4: 142 gruen. Optik nicht geprueft; nicht in den
Live-Branch gemergt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 22:14:37 +01:00

124 lines
4.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Uhrzeiten, wie die App sie zeigt (Bausteine.swift `Zeit`): Ortszeit, zweistellig.
/// Stunde, Minute, Sekunde in Ortszeit fuer einen Unix-Zeitpunkt.
fn ortszeit(t: f64) -> Option<(u16, u16, u16)> {
ort(t).map(|(_, _, _, h, m, s)| (h, m, s))
}
/// Jahr, Monat, Tag, Stunde, Minute, Sekunde in Ortszeit.
#[cfg(target_os = "windows")]
fn ort(t: f64) -> Option<(u16, u16, u16, u16, u16, u16)> {
use windows_sys::Win32::Foundation::{FILETIME, SYSTEMTIME};
use windows_sys::Win32::System::Time::{FileTimeToSystemTime, SystemTimeToTzSpecificLocalTime};
if !t.is_finite() || t < 0.0 {
return None;
}
// 100-ns-Schritte seit 1601
let ft = (t * 1e7) as u64 + 116_444_736_000_000_000;
let datei = FILETIME { dwLowDateTime: ft as u32, dwHighDateTime: (ft >> 32) as u32 };
unsafe {
let mut utc: SYSTEMTIME = std::mem::zeroed();
let mut ort: SYSTEMTIME = std::mem::zeroed();
if FileTimeToSystemTime(&datei, &mut utc) == 0 {
return None;
}
if SystemTimeToTzSpecificLocalTime(std::ptr::null(), &utc, &mut ort) == 0 {
return None;
}
Some((ort.wYear, ort.wMonth, ort.wDay, ort.wHour, ort.wMinute, ort.wSecond))
}
}
/// Ausserhalb von Windows (Vorschau, Tests): UTC.
#[cfg(not(target_os = "windows"))]
fn ort(t: f64) -> Option<(u16, u16, u16, u16, u16, u16)> {
if !t.is_finite() || t < 0.0 {
return None;
}
let s = (t as u64) % 86_400;
let (j, m, d) = kalender((t as i64).div_euclid(86_400));
Some((j, m, d, (s / 3600) as u16, ((s / 60) % 60) as u16, (s % 60) as u16))
}
/// Tage seit 1970 -> (Jahr, Monat, Tag), gregorianisch (H. Hinnant, civil_from_days).
#[cfg(not(target_os = "windows"))]
fn kalender(tage: i64) -> (u16, u16, u16) {
let z = tage + 719_468;
let era = z.div_euclid(146_097);
let doe = z - era * 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 d = doy - (153 * mp + 2) / 5 + 1;
let m = if mp < 10 { mp + 3 } else { mp - 9 };
let j = yoe + era * 400 + i64::from(m <= 2);
(j as u16, m as u16, d as u16)
}
const MONATE: [&str; 12] = ["Jan.", "Feb.", "März", "Apr.", "Mai", "Juni", "Juli", "Aug.", "Sept.", "Okt.", "Nov.", "Dez."];
/// "Heute 10:09", "Gestern 01:36", sonst "13. Nov. 18:02" (Ueberblick.swift `Tag.relativ`).
pub fn relativ(t: f64, jetzt: f64) -> String {
let (Some(a), Some(h), Some(g)) = (ort(t), ort(jetzt), ort(jetzt - 86_400.0)) else { return "–".into() };
let uhr = format!("{:02}:{:02}", a.3, a.4);
if (a.0, a.1, a.2) == (h.0, h.1, h.2) {
format!("Heute {uhr}")
} else if (a.0, a.1, a.2) == (g.0, g.1, g.2) {
format!("Gestern {uhr}")
} else {
format!("{}. {} {uhr}", a.2, MONATE[(a.1.clamp(1, 12) - 1) as usize])
}
}
/// "1 h 5 min", "12 min", "40 s" (Bausteine.swift `Zeit.dauer`).
pub fn dauer(s: f64) -> String {
let s = s.max(0.0) as i64;
if s >= 3600 {
format!("{} h {} min", s / 3600, (s % 3600) / 60)
} else if s >= 60 {
format!("{} min", s / 60)
} else {
format!("{s} s")
}
}
/// "14:03:27"; ohne Zeit ein Strich.
pub fn uhr(t: Option<f64>) -> String {
match t.and_then(ortszeit) {
Some((h, m, s)) => format!("{h:02}:{m:02}:{s:02}"),
None => "–".into(),
}
}
/// "14:03"; ohne Zeit leer.
pub fn kurz(t: Option<f64>) -> String {
match t.and_then(ortszeit) {
Some((h, m, _)) => format!("{h:02}:{m:02}"),
None => String::new(),
}
}
/// "14 h" fuer die Stundenachse.
pub fn stunde(t: Option<f64>) -> String {
match t.and_then(ortszeit) {
Some((h, _, _)) => format!("{h} h"),
None => String::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn form_und_strich() {
assert_eq!(uhr(None), "–");
assert_eq!(kurz(None), "");
let u = uhr(Some(1_793_293_590.0));
assert_eq!(u.len(), 8);
assert_eq!(&u[2..3], ":");
assert_eq!(&kurz(Some(1_793_293_590.0)), &u[..5]);
assert_eq!(uhr(Some(f64::NAN)), "–");
}
}