Skip to main content

icydb_schema/
date.rs

1//! Canonical day-precision date atom.
2//!
3//! This module owns the strict calendar/date representation and its public
4//! wire conversion. Database ordering and storage policy remain runtime-owned.
5
6use crate::{Decimal, NumericValue, TypeParseError};
7use candid::CandidType;
8use serde::{Deserialize, Deserializer, Serialize};
9use std::fmt::{self, Debug, Display};
10use time::{Date as TimeDate, Duration as TimeDuration, Month};
11
12// Invariant:
13// Date is internally represented as days since Unix epoch (`i32`) and is
14// bounded to the proleptic Gregorian calendar range 0000-01-01..=9999-12-31.
15// API/JSON deserialization accepts ISO-8601 text (`YYYY-MM-DD`).
16// Ordering and arithmetic remain numeric and deterministic over day counts.
17
18//
19// Date
20//
21// Represented as days since Unix epoch.
22// API/JSON decode expects ISO-8601 text (`YYYY-MM-DD`).
23//
24
25#[derive(CandidType, Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
26#[repr(transparent)]
27pub struct Date(i32);
28
29impl Date {
30    /// The Unix epoch date, 1970-01-01.
31    pub const EPOCH: Self = Self(0);
32    /// The earliest supported calendar date, 0000-01-01.
33    pub const MIN: Self = Self(-719_528);
34    /// The latest supported calendar date, 9999-12-31.
35    pub const MAX: Self = Self(2_932_896);
36
37    const fn epoch_date() -> TimeDate {
38        // Safe: constant valid date
39        match TimeDate::from_calendar_date(1970, Month::January, 1) {
40            Ok(d) => d,
41            Err(_) => unreachable!(),
42        }
43    }
44
45    /// Build a date from exact calendar parts.
46    ///
47    /// Returns `None` when any component is out of range.
48    #[must_use]
49    pub fn try_new(y: i32, m: u8, d: u8) -> Option<Self> {
50        if !(0..=9_999).contains(&y) {
51            return None;
52        }
53        let month = Month::try_from(m).ok()?;
54        let date = TimeDate::from_calendar_date(y, month, d).ok()?;
55        Self::from_time_date(date)
56    }
57
58    /// Construct from the bounded internal day-count representation.
59    #[must_use]
60    pub const fn try_from_days_since_epoch(days: i32) -> Option<Self> {
61        if days < Self::MIN.0 || days > Self::MAX.0 {
62            None
63        } else {
64            Some(Self(days))
65        }
66    }
67
68    /// Return the internal day-count representation.
69    #[must_use]
70    pub const fn as_days_since_epoch(self) -> i32 {
71        self.0
72    }
73
74    /// Fallible conversion from `i64` day-count representation.
75    #[must_use]
76    pub fn try_from_i64(days: i64) -> Option<Self> {
77        i32::try_from(days)
78            .ok()
79            .and_then(Self::try_from_days_since_epoch)
80    }
81
82    /// Fallible conversion from `u64` day-count representation.
83    #[must_use]
84    pub fn try_from_u64(days: u64) -> Option<Self> {
85        i32::try_from(days)
86            .ok()
87            .and_then(Self::try_from_days_since_epoch)
88    }
89
90    /// Add a signed number of days without leaving the supported calendar.
91    #[must_use]
92    pub fn checked_add_days(self, days: i64) -> Option<Self> {
93        i64::from(self.0)
94            .checked_add(days)
95            .and_then(Self::try_from_i64)
96    }
97
98    /// Subtract a signed number of days without leaving the supported calendar.
99    #[must_use]
100    pub fn checked_sub_days(self, days: i64) -> Option<Self> {
101        i64::from(self.0)
102            .checked_sub(days)
103            .and_then(Self::try_from_i64)
104    }
105
106    /// Return the signed number of days from `earlier` to this date.
107    #[must_use]
108    pub fn days_since(self, earlier: Self) -> i64 {
109        i64::from(self.0) - i64::from(earlier.0)
110    }
111
112    /// Returns the year component (e.g. 2025).
113    #[must_use]
114    pub fn year(self) -> i32 {
115        self.to_time_date().year()
116    }
117
118    /// Returns the month component (1-12).
119    #[must_use]
120    pub fn month(self) -> u8 {
121        self.to_time_date().month().into()
122    }
123
124    /// Returns the day-of-month component (1-31).
125    #[must_use]
126    pub fn day(self) -> u8 {
127        self.to_time_date().day()
128    }
129
130    /// Parse a strict ISO `YYYY-MM-DD` string into a `Date`.
131    #[must_use]
132    pub fn parse(s: &str) -> Option<Self> {
133        let bytes = s.as_bytes();
134        if bytes.len() != 10 || bytes[4] != b'-' || bytes[7] != b'-' {
135            return None;
136        }
137
138        // Phase 1: decode one strict fixed-width `YYYY-MM-DD` payload without
139        // routing through the heavier `time` text parser.
140        let year = parse_ascii_i32(&bytes[0..4])?;
141        let month = parse_ascii_u8(&bytes[5..7])?;
142        let day = parse_ascii_u8(&bytes[8..10])?;
143
144        Self::try_new(year, month, day)
145    }
146
147    fn from_time_date(date: TimeDate) -> Option<Self> {
148        let epoch = Self::epoch_date();
149        let days = (date - epoch).whole_days();
150        Self::try_from_i64(days)
151    }
152
153    // Safe public construction and persisted decoding enforce the bounded Date
154    // invariant before this private calendar conversion is reachable.
155    fn to_time_date(self) -> TimeDate {
156        let epoch = Self::epoch_date();
157        let delta = TimeDuration::days(self.0.into());
158        match epoch.checked_add(delta) {
159            Some(date) => date,
160            None => unreachable!("bounded Date invariant must produce a supported calendar date"),
161        }
162    }
163}
164
165impl Debug for Date {
166    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167        write!(f, "Date({self})")
168    }
169}
170
171impl Display for Date {
172    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173        let d = self.to_time_date();
174        let month: u8 = d.month().into();
175        write!(f, "{:04}-{:02}-{:02}", d.year(), month, d.day())
176    }
177}
178
179impl NumericValue for Date {
180    fn try_to_decimal(&self) -> Option<Decimal> {
181        Decimal::from_i64(i64::from(self.0))
182    }
183
184    fn try_from_decimal(value: Decimal) -> Option<Self> {
185        value.to_i32().and_then(Self::try_from_days_since_epoch)
186    }
187}
188
189impl<'de> Deserialize<'de> for Date {
190    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
191    where
192        D: Deserializer<'de>,
193    {
194        struct DateVisitor;
195
196        impl serde::de::Visitor<'_> for DateVisitor {
197            type Value = Date;
198
199            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
200                formatter.write_str("ISO date text or canonical epoch-day integer")
201            }
202
203            fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
204            where
205                E: serde::de::Error,
206            {
207                Date::try_from_days_since_epoch(value)
208                    .ok_or_else(|| E::custom(TypeParseError::InvalidDate))
209            }
210
211            fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
212            where
213                E: serde::de::Error,
214            {
215                Date::try_from_i64(value).ok_or_else(|| E::custom(TypeParseError::InvalidDate))
216            }
217
218            fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
219            where
220                E: serde::de::Error,
221            {
222                Date::try_from_u64(value).ok_or_else(|| E::custom(TypeParseError::InvalidDate))
223            }
224
225            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
226            where
227                E: serde::de::Error,
228            {
229                Date::parse(value).ok_or_else(|| E::custom(TypeParseError::InvalidDate))
230            }
231        }
232
233        deserializer.deserialize_any(DateVisitor)
234    }
235}
236
237impl Serialize for Date {
238    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
239    where
240        S: serde::Serializer,
241    {
242        serializer.serialize_str(&self.to_string())
243    }
244}
245
246fn parse_ascii_i32(bytes: &[u8]) -> Option<i32> {
247    bytes.iter().try_fold(0_i32, |value, byte| {
248        byte.checked_sub(b'0')
249            .filter(|digit| *digit <= 9)
250            .and_then(|digit| value.checked_mul(10)?.checked_add(i32::from(digit)))
251    })
252}
253
254fn parse_ascii_u8(bytes: &[u8]) -> Option<u8> {
255    bytes.iter().try_fold(0_u8, |value, byte| {
256        byte.checked_sub(b'0')
257            .filter(|digit| *digit <= 9)
258            .and_then(|digit| value.checked_mul(10)?.checked_add(digit))
259    })
260}
261
262//
263// TESTS
264//
265
266#[cfg(test)]
267mod tests {
268    use super::*;
269
270    // Internal semantic/storage representation behavior.
271
272    #[test]
273    fn from_ymd_and_to_naive_date_round_trip() {
274        let date = Date::try_new(2024, 10, 19).expect("valid calendar date should construct");
275        assert_eq!(date.year(), 2024);
276        assert_eq!(date.month(), 10);
277        assert_eq!(date.day(), 19);
278    }
279
280    #[test]
281    fn try_new_rejects_out_of_range_month_and_day() {
282        assert!(Date::try_new(2025, 13, 99).is_none());
283    }
284
285    #[test]
286    fn invalid_date_parse_returns_none() {
287        assert!(Date::parse("2025-13-40").is_none());
288        assert!(Date::try_new(2025, 2, 30).is_none());
289    }
290
291    #[test]
292    fn try_new_rejects_out_of_range_year() {
293        assert!(Date::try_new(-1, 1, 1).is_none());
294        assert!(Date::try_new(10_000, 1, 1).is_none());
295        assert!(Date::try_new(i32::MAX, 1, 1).is_none());
296    }
297
298    #[test]
299    fn overflow_protection_in_try_from_u64() {
300        // i32::MAX + 1 should safely fail
301        let too_large = (i32::MAX as u64) + 1;
302        assert!(Date::try_from_u64(too_large).is_none());
303    }
304
305    #[test]
306    fn ordering_and_equality_follow_internal_day_count() {
307        let d1 = Date::try_new(2020, 1, 1).unwrap();
308        let d2 = Date::try_new(2021, 1, 1).unwrap();
309
310        assert!(d1 < d2);
311        assert!(d1.as_days_since_epoch() < d2.as_days_since_epoch());
312        assert_eq!(d1, d1);
313    }
314
315    #[test]
316    fn internal_day_count_helpers_round_trip() {
317        let days = -365;
318        let date = Date::try_from_days_since_epoch(days).expect("bounded day should construct");
319        assert_eq!(date.as_days_since_epoch(), days);
320    }
321
322    #[test]
323    fn raw_day_construction_rejects_values_outside_calendar_bounds() {
324        assert_eq!(
325            Date::try_from_days_since_epoch(Date::MIN.as_days_since_epoch()),
326            Some(Date::MIN),
327        );
328        assert_eq!(
329            Date::try_from_days_since_epoch(Date::MAX.as_days_since_epoch()),
330            Some(Date::MAX),
331        );
332        assert!(Date::try_from_days_since_epoch(Date::MIN.as_days_since_epoch() - 1).is_none(),);
333        assert!(Date::try_from_days_since_epoch(Date::MAX.as_days_since_epoch() + 1).is_none(),);
334    }
335
336    #[test]
337    fn checked_day_arithmetic_obeys_calendar_bounds() {
338        let leap_day = Date::try_new(2024, 2, 29).expect("leap day should construct");
339        let march_first = Date::try_new(2024, 3, 1).expect("next day should construct");
340
341        assert_eq!(leap_day.checked_add_days(1), Some(march_first));
342        assert_eq!(march_first.checked_sub_days(1), Some(leap_day));
343        assert_eq!(march_first.days_since(leap_day), 1);
344        assert!(Date::MAX.checked_add_days(1).is_none());
345        assert!(Date::MIN.checked_sub_days(1).is_none());
346        assert!(Date::EPOCH.checked_add_days(i64::MAX).is_none());
347        assert!(Date::EPOCH.checked_sub_days(i64::MIN).is_none());
348    }
349
350    #[test]
351    fn display_formats_as_iso_date() {
352        let date = Date::try_new(2025, 10, 19).unwrap();
353        assert_eq!(format!("{date}"), "2025-10-19");
354    }
355
356    #[test]
357    fn parse_stays_iso_strict() {
358        assert_eq!(Date::parse("2025-10-19"), Date::try_new(2025, 10, 19));
359        assert!(Date::parse("10/19/2025").is_none());
360        assert!(Date::parse("2025-10-19T00:00:00Z").is_none());
361    }
362
363    #[test]
364    fn parse_supports_pre_epoch_and_leap_year_cases() {
365        assert_eq!(
366            Date::parse("1900-01-01"),
367            Date::try_new(1900, 1, 1),
368            "expected non-leap-century date to parse",
369        );
370        assert_eq!(
371            Date::parse("1969-12-31"),
372            Date::try_new(1969, 12, 31),
373            "expected pre-epoch date to parse",
374        );
375        assert_eq!(
376            Date::parse("2000-02-29"),
377            Date::try_new(2000, 2, 29),
378            "expected leap-day date to parse",
379        );
380    }
381
382    #[test]
383    fn parse_rejects_invalid_non_leap_day() {
384        assert!(Date::parse("1900-02-29").is_none());
385    }
386
387    #[test]
388    fn calendar_boundaries_format_and_parse_exactly() {
389        assert_eq!(Date::MIN.to_string(), "0000-01-01");
390        assert_eq!(Date::MAX.to_string(), "9999-12-31");
391        assert_eq!(Date::parse(Date::MIN.to_string().as_str()), Some(Date::MIN));
392        assert_eq!(Date::parse(Date::MAX.to_string().as_str()), Some(Date::MAX));
393    }
394
395    #[test]
396    fn candid_decode_rejects_out_of_range_epoch_days() {
397        let min =
398            candid::encode_one(Date::MIN.as_days_since_epoch()).expect("minimum day should encode");
399        let max =
400            candid::encode_one(Date::MAX.as_days_since_epoch()).expect("maximum day should encode");
401        let below = candid::encode_one(Date::MIN.as_days_since_epoch() - 1)
402            .expect("out-of-range day should encode as raw i32");
403        let above = candid::encode_one(Date::MAX.as_days_since_epoch() + 1)
404            .expect("out-of-range day should encode as raw i32");
405
406        assert_eq!(
407            candid::decode_one::<Date>(&min).expect("minimum day should decode"),
408            Date::MIN,
409        );
410        assert_eq!(
411            candid::decode_one::<Date>(&max).expect("maximum day should decode"),
412            Date::MAX,
413        );
414        assert!(candid::decode_one::<Date>(&below).is_err());
415        assert!(candid::decode_one::<Date>(&above).is_err());
416    }
417}