Files

85 lines
2.7 KiB
Rust

#![allow(dead_code)]
use liber_library::library::BookInfo;
use liber_library::{Clock, Event, Library, Op};
/// Europe/Prague's UTC offset at a moment (minutes): CEST from the last Sunday of March 01:00 UTC to the last Sunday of
/// October 01:00 UTC, CET otherwise. Written out here because the core has no time-zone database by design.
pub fn prague(utc_ms: i64) -> i32 {
let day = utc_ms.div_euclid(86_400_000);
let (y, _, _) = civil(day);
let last_sunday = |m: u32| {
let last = liber_library::days_from_civil(y, m, 31);
last - ((last + 3).rem_euclid(7) - 6).rem_euclid(7)
};
let start = last_sunday(3) * 86_400_000 + 3_600_000;
let end = last_sunday(10) * 86_400_000 + 3_600_000;
if utc_ms >= start && utc_ms < end { 120 } else { 60 }
}
pub fn civil(z: i64) -> (i64, u32, u32) {
let z = z + 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) as u32;
let m = if mp < 10 { mp + 3 } else { mp - 9 } as u32;
(yoe + era * 400 + i64::from(m <= 2), m, d)
}
/// A library with one book and the given events after it.
pub struct Log {
pub events: Vec<Event>,
clock: Clock,
n: u32,
}
impl Log {
pub fn new() -> Log {
let mut l = Log { events: vec![], clock: Clock::default(), n: 0 };
l.push(Op::BookAdd {
book: "b".into(),
info: BookInfo { title: "Kniha".into(), ..BookInfo::default() },
restore: false,
});
l
}
pub fn push(&mut self, op: Op) {
self.n += 1;
let hlc = self.clock.tick(self.n as u64);
self.events.push(Event { id: format!("e{}", self.n), hlc, device: "d".into(), op });
}
/// A session starting at `utc_ms` in Prague, `pages` turned.
pub fn read_at(&mut self, utc_ms: i64, pages: u32) {
let n = self.n;
self.push(Op::Session {
id: format!("s{n}"),
book: "b".into(),
start: utc_ms as u64,
end: utc_ms as u64 + 600_000,
active_ms: 300_000,
pages,
date: String::new(),
offset_min: Some(prague(utc_ms)),
});
}
/// One page read at 20:00 Prague time on `day`.
pub fn read_day(&mut self, day: liber_habit::Day) {
let utc = day.0 * 86_400_000 + 20 * 3_600_000;
self.read_at(utc - prague(utc) as i64 * 60_000, 1);
}
pub fn lib(&self) -> Library {
Library::from_events(&self.events)
}
}
/// 2026-03-02 (a Monday) as a local day.
pub fn monday() -> liber_habit::Day {
liber_habit::Day::parse("2026-03-02").unwrap()
}