Skip to main content

vibe/cosmic/
stardate.rs

1//! Piecewise stardate morphism (OCS §12.1).
2//!
3//! Converts Star Trek stardates to approximate Gregorian years and back.
4//!
5//! Reference: OCS Specification v2.2.0 §12.1.
6
7/// Stardate era (OCS §12.1).
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum StardateEra {
10    /// TOS: 0 ≤ S < 10000, Gregorian(2265.0 + 0.1·S)
11    Tos,
12    /// TNG/DS9/VOY: 41000 ≤ S < 60000, Gregorian(2364.0 + (S-41000)/1000)
13    Tng,
14    /// 32nd Century: S ≥ 860000, Gregorian(3188.0 + (S-860000)/1000)
15    Century32,
16}
17
18/// A parsed stardate with its era.
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub struct Stardate {
21    pub value: f64,
22    pub era: StardateEra,
23}
24
25impl Stardate {
26    /// Create a stardate from a raw value, auto-detecting the era.
27    pub fn new(s: f64) -> Self {
28        let era = if s >= 860_000.0 {
29            StardateEra::Century32
30        } else if s >= 41_000.0 {
31            StardateEra::Tng
32        } else {
33            StardateEra::Tos
34        };
35        Self { value: s, era }
36    }
37
38    /// Convert stardate to approximate Gregorian year (OCS §12.1).
39    pub fn to_gregorian_year(&self) -> f64 {
40        match self.era {
41            StardateEra::Tos => 2265.0 + 0.1 * self.value,
42            StardateEra::Tng => 2364.0 + (self.value - 41_000.0) / 1000.0,
43            StardateEra::Century32 => 3188.0 + (self.value - 860_000.0) / 1000.0,
44        }
45    }
46
47    /// Convert a Gregorian year to the nearest stardate in the given era.
48    pub fn from_gregorian_year(year: f64, era: StardateEra) -> Self {
49        let value = match era {
50            StardateEra::Tos => (year - 2265.0) / 0.1,
51            StardateEra::Tng => (year - 2364.0) * 1000.0 + 41_000.0,
52            StardateEra::Century32 => (year - 3188.0) * 1000.0 + 860_000.0,
53        };
54        Self { value, era }
55    }
56}
57
58impl StardateEra {
59    /// `str` form.
60    pub fn as_str(&self) -> &'static str {
61        match self {
62            Self::Tos => "TOS",
63            Self::Tng => "TNG",
64            Self::Century32 => "32nd Century",
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn tos_stardate_1312() {
75        // TOS stardate 1312.4 → ~2265.0 + 131.24 = 2396.24... wait
76        // 2265.0 + 0.1 * 1312.4 = 2265.0 + 131.24 = 2396.24
77        // Actually TOS stardates are roughly 2265-2269 range
78        // 0.1 * 1312.4 = 131.24 → year 2396.24? That seems wrong.
79        // The spec says Gregorian(2265.0 + 0.1·S), so S=1312.4 → 2265+131.24 = 2396.24
80        // This is the spec formula, even if it doesn't match fan chronologies exactly.
81        let s = Stardate::new(1312.4);
82        assert_eq!(s.era, StardateEra::Tos);
83        let year = s.to_gregorian_year();
84        assert!((year - 2396.24).abs() < 0.01);
85    }
86
87    #[test]
88    fn tng_stardate_47634() {
89        // TNG stardate 47634.44 → 2364.0 + (47634.44-41000)/1000 = 2364 + 6.63444 = 2370.634
90        let s = Stardate::new(47634.44);
91        assert_eq!(s.era, StardateEra::Tng);
92        let year = s.to_gregorian_year();
93        assert!((year - 2370.63444).abs() < 0.001);
94    }
95
96    #[test]
97    fn century32_stardate_865211() {
98        // 32nd century stardate 865211.2 → 3188.0 + (865211.2-860000)/1000 = 3188 + 5.2112 = 3193.211
99        let s = Stardate::new(865211.2);
100        assert_eq!(s.era, StardateEra::Century32);
101        let year = s.to_gregorian_year();
102        assert!((year - 3193.2112).abs() < 0.001);
103    }
104
105    #[test]
106    fn round_trip_tng() {
107        let original = Stardate::new(47634.44);
108        let year = original.to_gregorian_year();
109        let recovered = Stardate::from_gregorian_year(year, StardateEra::Tng);
110        assert!((recovered.value - original.value).abs() < 0.001);
111    }
112
113    #[test]
114    fn round_trip_tos() {
115        let original = Stardate::new(1312.4);
116        let year = original.to_gregorian_year();
117        let recovered = Stardate::from_gregorian_year(year, StardateEra::Tos);
118        assert!((recovered.value - original.value).abs() < 0.001);
119    }
120
121    #[test]
122    fn round_trip_century32() {
123        let original = Stardate::new(865211.2);
124        let year = original.to_gregorian_year();
125        let recovered = Stardate::from_gregorian_year(year, StardateEra::Century32);
126        assert!((recovered.value - original.value).abs() < 0.001);
127    }
128
129    #[test]
130    fn era_detection() {
131        assert_eq!(Stardate::new(500.0).era, StardateEra::Tos);
132        assert_eq!(Stardate::new(50000.0).era, StardateEra::Tng);
133        assert_eq!(Stardate::new(900000.0).era, StardateEra::Century32);
134    }
135}