M2a: server workspace, kvitt-domain, skeleton routes, OpenAPI match, Dockerfile
Cargo workspace (kvitt-domain, kvitt-server, kvitt-import), Rust 1.99.1 pinned. kvitt-domain passes every Rust vector and adds next_occurrence_date (D-015). kvitt-server: config, SQLite pool (WAL, §8.9 pragmas), migration 0001 identical to the contract schema, error type for all 25 codes, auth extractor and protocol layer, all 33 routes (E-1/E-2 implemented, the rest 501), utoipa document equal to contracts/openapi.yaml, seams for M3/M4/M9, serve/migrate/healthcheck. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
//! Time: the fixed RFC 3339 form (SPEC conventions), the `Europe/Stockholm` constant (SPEC §10.4) and an injectable
|
||||
//! clock so tests control "now".
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use chrono::{DateTime, NaiveDate, SecondsFormat, Utc};
|
||||
use chrono_tz::Tz;
|
||||
|
||||
/// The household's time zone; a constant in code, not configuration (SPEC §10.4).
|
||||
pub const STOCKHOLM: Tz = chrono_tz::Europe::Stockholm;
|
||||
|
||||
/// Formats an instant in the fixed form `2026-10-17T06:00:00.000Z` (millisecond precision, literal `Z`).
|
||||
pub fn format_ts(instant: DateTime<Utc>) -> String {
|
||||
instant.to_rfc3339_opts(SecondsFormat::Millis, true)
|
||||
}
|
||||
|
||||
/// Parses a timestamp in exactly the fixed form; anything else (other offsets, other precision) is `None`.
|
||||
pub fn parse_ts(text: &str) -> Option<DateTime<Utc>> {
|
||||
let instant = DateTime::parse_from_rfc3339(text).ok()?.with_timezone(&Utc);
|
||||
(format_ts(instant) == text).then_some(instant)
|
||||
}
|
||||
|
||||
/// Parses a calendar date `YYYY-MM-DD` (exactly that form).
|
||||
pub fn parse_date(text: &str) -> Option<NaiveDate> {
|
||||
let date = NaiveDate::parse_from_str(text, "%Y-%m-%d").ok()?;
|
||||
(date.format("%Y-%m-%d").to_string() == text).then_some(date)
|
||||
}
|
||||
|
||||
/// Today's calendar date in Europe/Stockholm.
|
||||
pub fn stockholm_today(now: DateTime<Utc>) -> NaiveDate {
|
||||
now.with_timezone(&STOCKHOLM).date_naive()
|
||||
}
|
||||
|
||||
/// Source of "now". The server uses [`SystemClock`]; tests use [`FixedClock`].
|
||||
pub trait Clock: Send + Sync + 'static {
|
||||
fn now(&self) -> DateTime<Utc>;
|
||||
|
||||
/// `now` in the fixed RFC 3339 form.
|
||||
fn now_ts(&self) -> String {
|
||||
format_ts(self.now())
|
||||
}
|
||||
}
|
||||
|
||||
/// The real clock.
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct SystemClock;
|
||||
|
||||
impl Clock for SystemClock {
|
||||
fn now(&self) -> DateTime<Utc> {
|
||||
Utc::now()
|
||||
}
|
||||
}
|
||||
|
||||
/// A clock that returns a set instant until changed.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FixedClock(Arc<Mutex<DateTime<Utc>>>);
|
||||
|
||||
impl FixedClock {
|
||||
pub fn new(instant: DateTime<Utc>) -> Self {
|
||||
Self(Arc::new(Mutex::new(instant)))
|
||||
}
|
||||
|
||||
/// Sets the instant every clone of this clock returns from now on.
|
||||
pub fn set(&self, instant: DateTime<Utc>) {
|
||||
match self.0.lock() {
|
||||
Ok(mut guard) => *guard = instant,
|
||||
Err(poisoned) => *poisoned.into_inner() = instant,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clock for FixedClock {
|
||||
fn now(&self) -> DateTime<Utc> {
|
||||
match self.0.lock() {
|
||||
Ok(guard) => *guard,
|
||||
Err(poisoned) => *poisoned.into_inner(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn fixed_form_round_trip() {
|
||||
let t = parse_ts("2026-10-17T06:00:00.000Z").unwrap();
|
||||
assert_eq!(format_ts(t), "2026-10-17T06:00:00.000Z");
|
||||
assert!(parse_ts("2026-10-17T06:00:00Z").is_none());
|
||||
assert!(parse_ts("2026-10-17T08:00:00.000+02:00").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dates() {
|
||||
assert!(parse_date("2026-02-29").is_none());
|
||||
assert!(parse_date("2026-2-01").is_none());
|
||||
assert_eq!(parse_date("2028-02-29").unwrap().to_string(), "2028-02-29");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stockholm_day_boundary() {
|
||||
// 23:30 UTC on 17 October is 01:30 on 18 October in Stockholm (CEST, UTC+2).
|
||||
let t = parse_ts("2026-10-17T23:30:00.000Z").unwrap();
|
||||
assert_eq!(stockholm_today(t).to_string(), "2026-10-18");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixed_clock_is_shared_by_clones() {
|
||||
let clock = FixedClock::new(parse_ts("2026-10-17T06:00:00.000Z").unwrap());
|
||||
let other = clock.clone();
|
||||
clock.set(parse_ts("2026-10-18T06:00:00.000Z").unwrap());
|
||||
assert_eq!(other.now_ts(), "2026-10-18T06:00:00.000Z");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user