1use crate::{Decimal, NumericValue, TypeParseError};
7
8use serde::{Deserialize, Deserializer, Serialize};
9use std::fmt::{self, Debug, Display};
10
11#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
25#[repr(transparent)]
26pub struct Date(i32);
27
28impl Date {
29 pub const EPOCH: Self = Self(0);
31 pub const MIN: Self = Self(-719_528);
33 pub const MAX: Self = Self(2_932_896);
35
36 const DAYS_PER_CYCLE: i32 = 146_097;
40 const MARCH_EPOCH_OFFSET: i32 = 719_468;
41
42 #[must_use]
46 pub fn try_new(y: i32, m: u8, d: u8) -> Option<Self> {
47 if !(0..=9_999).contains(&y) {
48 return None;
49 }
50 let month_days = match m {
51 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
52 4 | 6 | 9 | 11 => 30,
53 2 if y % 4 == 0 && (y % 100 != 0 || y % 400 == 0) => 29,
54 2 => 28,
55 _ => return None,
56 };
57 if d == 0 || d > month_days {
58 return None;
59 }
60
61 let year = y - i32::from(m <= 2);
62 let cycle = year.div_euclid(400);
63 let cycle_year = year.rem_euclid(400);
64 let march_month = i32::from(m) + if m > 2 { -3 } else { 9 };
65 let cycle_day = 365 * cycle_year + cycle_year / 4 - cycle_year / 100
66 + (153 * march_month + 2) / 5
67 + i32::from(d)
68 - 1;
69 Self::try_from_days_since_epoch(
70 cycle * Self::DAYS_PER_CYCLE + cycle_day - Self::MARCH_EPOCH_OFFSET,
71 )
72 }
73
74 #[must_use]
76 pub const fn try_from_days_since_epoch(days: i32) -> Option<Self> {
77 if days < Self::MIN.0 || days > Self::MAX.0 {
78 None
79 } else {
80 Some(Self(days))
81 }
82 }
83
84 #[must_use]
86 pub const fn as_days_since_epoch(self) -> i32 {
87 self.0
88 }
89
90 #[must_use]
92 pub fn try_from_i64(days: i64) -> Option<Self> {
93 i32::try_from(days)
94 .ok()
95 .and_then(Self::try_from_days_since_epoch)
96 }
97
98 #[must_use]
100 pub fn try_from_u64(days: u64) -> Option<Self> {
101 i32::try_from(days)
102 .ok()
103 .and_then(Self::try_from_days_since_epoch)
104 }
105
106 #[must_use]
108 pub fn checked_add_days(self, days: i64) -> Option<Self> {
109 i64::from(self.0)
110 .checked_add(days)
111 .and_then(Self::try_from_i64)
112 }
113
114 #[must_use]
116 pub fn checked_sub_days(self, days: i64) -> Option<Self> {
117 i64::from(self.0)
118 .checked_sub(days)
119 .and_then(Self::try_from_i64)
120 }
121
122 #[must_use]
124 pub fn days_since(self, earlier: Self) -> i64 {
125 i64::from(self.0) - i64::from(earlier.0)
126 }
127
128 #[must_use]
130 pub fn year(self) -> i32 {
131 self.calendar_parts().0
132 }
133
134 #[must_use]
136 pub fn month(self) -> u8 {
137 self.calendar_parts().1
138 }
139
140 #[must_use]
142 pub fn day(self) -> u8 {
143 self.calendar_parts().2
144 }
145
146 #[must_use]
148 pub fn parse(s: &str) -> Option<Self> {
149 let bytes = s.as_bytes();
150 if bytes.len() != 10 || bytes[4] != b'-' || bytes[7] != b'-' {
151 return None;
152 }
153
154 let year = parse_ascii_i32(&bytes[0..4])?;
156 let month = parse_ascii_u8(&bytes[5..7])?;
157 let day = parse_ascii_u8(&bytes[8..10])?;
158
159 Self::try_new(year, month, day)
160 }
161
162 #[expect(
165 clippy::cast_possible_truncation,
166 clippy::cast_sign_loss,
167 reason = "bounded Gregorian conversion yields positive u8 month/day components"
168 )]
169 fn calendar_parts(self) -> (i32, u8, u8) {
170 let march_day = self.0 + Self::MARCH_EPOCH_OFFSET;
171 let cycle = march_day.div_euclid(Self::DAYS_PER_CYCLE);
172 let cycle_day = march_day.rem_euclid(Self::DAYS_PER_CYCLE);
173 let cycle_year =
174 (cycle_day - cycle_day / 1_460 + cycle_day / 36_524 - cycle_day / 146_096) / 365;
175 let year = cycle * 400 + cycle_year;
176 let march_ordinal = cycle_day - (365 * cycle_year + cycle_year / 4 - cycle_year / 100);
177 let march_month = (5 * march_ordinal + 2) / 153;
178 let day = march_ordinal - (153 * march_month + 2) / 5 + 1;
179 let month = march_month + if march_month < 10 { 3 } else { -9 };
180
181 (year + i32::from(month <= 2), month as u8, day as u8)
182 }
183}
184
185impl Debug for Date {
186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187 write!(f, "Date({self})")
188 }
189}
190
191impl Display for Date {
192 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
193 let (year, month, day) = self.calendar_parts();
194 write!(f, "{year:04}-{month:02}-{day:02}")
195 }
196}
197
198impl NumericValue for Date {
199 fn try_to_decimal(&self) -> Option<Decimal> {
200 Decimal::from_i64(i64::from(self.0))
201 }
202
203 fn try_from_decimal(value: Decimal) -> Option<Self> {
204 value.to_i32().and_then(Self::try_from_days_since_epoch)
205 }
206}
207
208impl<'de> Deserialize<'de> for Date {
209 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
210 where
211 D: Deserializer<'de>,
212 {
213 if deserializer.is_human_readable() {
214 return Self::parse(&String::deserialize(deserializer)?)
215 .ok_or_else(|| serde::de::Error::custom(TypeParseError::InvalidDate));
216 }
217 Self::try_from_days_since_epoch(i32::deserialize(deserializer)?)
219 .ok_or_else(|| serde::de::Error::custom(TypeParseError::InvalidDate))
220 }
221}
222
223impl Serialize for Date {
224 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
225 where
226 S: serde::Serializer,
227 {
228 if serializer.is_human_readable() {
229 serializer.collect_str(self)
230 } else {
231 serializer.serialize_i32(self.0)
232 }
233 }
234}
235
236fn parse_ascii_i32(bytes: &[u8]) -> Option<i32> {
237 bytes.iter().try_fold(0_i32, |value, byte| {
238 byte.checked_sub(b'0')
239 .filter(|digit| *digit <= 9)
240 .and_then(|digit| value.checked_mul(10)?.checked_add(i32::from(digit)))
241 })
242}
243
244fn parse_ascii_u8(bytes: &[u8]) -> Option<u8> {
245 bytes.iter().try_fold(0_u8, |value, byte| {
246 byte.checked_sub(b'0')
247 .filter(|digit| *digit <= 9)
248 .and_then(|digit| value.checked_mul(10)?.checked_add(digit))
249 })
250}
251
252#[cfg(test)]
257mod tests {
258 use super::*;
259
260 #[test]
261 fn every_supported_calendar_day_matches_the_reference_calendar() {
262 use time::{Date as ReferenceDate, Month};
263
264 let epoch = ReferenceDate::from_calendar_date(1970, Month::January, 1).unwrap();
265 for days in Date::MIN.as_days_since_epoch()..=Date::MAX.as_days_since_epoch() {
266 let reference = ReferenceDate::from_julian_day(epoch.to_julian_day() + days).unwrap();
267 let (year, month, day) = reference.to_calendar_date();
268 let month = u8::from(month);
269 let date = Date::try_from_days_since_epoch(days).unwrap();
270 assert_eq!((date.year(), date.month(), date.day()), (year, month, day));
271 assert_eq!(Date::try_new(year, month, day), Some(date));
272 }
273 }
274
275 #[test]
276 fn calendar_admission_matches_reference_at_month_and_leap_boundaries() {
277 use time::{Date as ReferenceDate, Month};
278
279 let epoch = ReferenceDate::from_calendar_date(1970, Month::January, 1).unwrap();
280 for year in [
281 0, 1, 4, 100, 400, 1582, 1900, 1969, 1970, 2000, 2024, 2100, 2400, 9999,
282 ] {
283 for month in 0..=13 {
284 for day in 0..=32 {
285 let expected = Month::try_from(month)
286 .ok()
287 .and_then(|month| ReferenceDate::from_calendar_date(year, month, day).ok())
288 .map(|date| (date - epoch).whole_days());
289 assert_eq!(
290 Date::try_new(year, month, day)
291 .map(|date| i64::from(date.as_days_since_epoch())),
292 expected,
293 "{year:04}-{month:02}-{day:02}"
294 );
295 }
296 }
297 }
298 }
299
300 #[test]
301 fn serde_calendar_boundaries_and_batches_use_compact_days() {
302 let today = Date::try_new(2026, 9, 12).unwrap();
303 for (date, width) in [(Date::EPOCH, 1), (today, 3), (Date::MIN, 3), (Date::MAX, 4)] {
304 let json = serde_json::to_string(&date).unwrap();
305 assert_eq!(json, format!("\"{date}\""));
306 assert_eq!(serde_json::from_str::<Date>(&json).unwrap(), date);
307
308 let wire = crate::wire::encode_one(date).unwrap();
309 assert_eq!(wire.len(), width);
310 assert_eq!(
311 wire,
312 crate::wire::encode_one(date.as_days_since_epoch()).unwrap()
313 );
314 assert_eq!(crate::wire::decode_one::<Date>(&wire).unwrap(), date);
315
316 let batch = vec![date; 1_000];
317 let encoded = crate::wire::encode_one(&batch).unwrap();
318 assert_eq!(encoded.len(), 2 + 1_000 * width);
319 assert_eq!(
320 crate::wire::decode_one::<Vec<Date>>(&encoded).unwrap(),
321 batch
322 );
323 }
324 }
325
326 #[test]
327 fn binary_serde_rejects_days_outside_calendar_domain() {
328 for day in [
329 i64::MIN,
330 i64::from(Date::MIN.as_days_since_epoch()) - 1,
331 i64::from(Date::MAX.as_days_since_epoch()) + 1,
332 i64::MAX,
333 ] {
334 let encoded = crate::wire::encode_one(day).unwrap();
335 assert!(crate::wire::decode_one::<Date>(&encoded).is_err());
336 }
337 }
338
339 #[test]
342 fn from_ymd_and_to_naive_date_round_trip() {
343 let date = Date::try_new(2024, 10, 19).expect("valid calendar date should construct");
344 assert_eq!(date.year(), 2024);
345 assert_eq!(date.month(), 10);
346 assert_eq!(date.day(), 19);
347 }
348
349 #[test]
350 fn try_new_rejects_out_of_range_month_and_day() {
351 assert!(Date::try_new(2025, 13, 99).is_none());
352 }
353
354 #[test]
355 fn invalid_date_parse_returns_none() {
356 assert!(Date::parse("2025-13-40").is_none());
357 assert!(Date::try_new(2025, 2, 30).is_none());
358 }
359
360 #[test]
361 fn try_new_rejects_out_of_range_year() {
362 assert!(Date::try_new(-1, 1, 1).is_none());
363 assert!(Date::try_new(10_000, 1, 1).is_none());
364 assert!(Date::try_new(i32::MAX, 1, 1).is_none());
365 }
366
367 #[test]
368 fn overflow_protection_in_try_from_u64() {
369 let too_large = (i32::MAX as u64) + 1;
371 assert!(Date::try_from_u64(too_large).is_none());
372 }
373
374 #[test]
375 fn ordering_and_equality_follow_internal_day_count() {
376 let d1 = Date::try_new(2020, 1, 1).unwrap();
377 let d2 = Date::try_new(2021, 1, 1).unwrap();
378
379 assert!(d1 < d2);
380 assert!(d1.as_days_since_epoch() < d2.as_days_since_epoch());
381 assert_eq!(d1, d1);
382 }
383
384 #[test]
385 fn internal_day_count_helpers_round_trip() {
386 let days = -365;
387 let date = Date::try_from_days_since_epoch(days).expect("bounded day should construct");
388 assert_eq!(date.as_days_since_epoch(), days);
389 }
390
391 #[test]
392 fn raw_day_construction_rejects_values_outside_calendar_bounds() {
393 assert_eq!(
394 Date::try_from_days_since_epoch(Date::MIN.as_days_since_epoch()),
395 Some(Date::MIN),
396 );
397 assert_eq!(
398 Date::try_from_days_since_epoch(Date::MAX.as_days_since_epoch()),
399 Some(Date::MAX),
400 );
401 assert!(Date::try_from_days_since_epoch(Date::MIN.as_days_since_epoch() - 1).is_none(),);
402 assert!(Date::try_from_days_since_epoch(Date::MAX.as_days_since_epoch() + 1).is_none(),);
403 }
404
405 #[test]
406 fn checked_day_arithmetic_obeys_calendar_bounds() {
407 let leap_day = Date::try_new(2024, 2, 29).expect("leap day should construct");
408 let march_first = Date::try_new(2024, 3, 1).expect("next day should construct");
409
410 assert_eq!(leap_day.checked_add_days(1), Some(march_first));
411 assert_eq!(march_first.checked_sub_days(1), Some(leap_day));
412 assert_eq!(march_first.days_since(leap_day), 1);
413 assert!(Date::MAX.checked_add_days(1).is_none());
414 assert!(Date::MIN.checked_sub_days(1).is_none());
415 assert!(Date::EPOCH.checked_add_days(i64::MAX).is_none());
416 assert!(Date::EPOCH.checked_sub_days(i64::MIN).is_none());
417 }
418
419 #[test]
420 fn display_formats_as_iso_date() {
421 let date = Date::try_new(2025, 10, 19).unwrap();
422 assert_eq!(format!("{date}"), "2025-10-19");
423 }
424
425 #[test]
426 fn parse_stays_iso_strict() {
427 assert_eq!(Date::parse("2025-10-19"), Date::try_new(2025, 10, 19));
428 assert!(Date::parse("10/19/2025").is_none());
429 assert!(Date::parse("2025-10-19T00:00:00Z").is_none());
430 }
431
432 #[test]
433 fn parse_supports_pre_epoch_and_leap_year_cases() {
434 assert_eq!(
435 Date::parse("1900-01-01"),
436 Date::try_new(1900, 1, 1),
437 "expected non-leap-century date to parse",
438 );
439 assert_eq!(
440 Date::parse("1969-12-31"),
441 Date::try_new(1969, 12, 31),
442 "expected pre-epoch date to parse",
443 );
444 assert_eq!(
445 Date::parse("2000-02-29"),
446 Date::try_new(2000, 2, 29),
447 "expected leap-day date to parse",
448 );
449 }
450
451 #[test]
452 fn parse_rejects_invalid_non_leap_day() {
453 assert!(Date::parse("1900-02-29").is_none());
454 }
455
456 #[test]
457 fn calendar_boundaries_format_and_parse_exactly() {
458 assert_eq!(Date::MIN.to_string(), "0000-01-01");
459 assert_eq!(Date::MAX.to_string(), "9999-12-31");
460 assert_eq!(Date::parse(Date::MIN.to_string().as_str()), Some(Date::MIN));
461 assert_eq!(Date::parse(Date::MAX.to_string().as_str()), Some(Date::MAX));
462 }
463
464 #[test]
465 fn wire_decode_rejects_out_of_range_epoch_days() {
466 let min = crate::wire::encode_one(Date::MIN.as_days_since_epoch())
467 .expect("minimum day should encode");
468 let max = crate::wire::encode_one(Date::MAX.as_days_since_epoch())
469 .expect("maximum day should encode");
470 let below = crate::wire::encode_one(Date::MIN.as_days_since_epoch() - 1)
471 .expect("out-of-range day should encode as raw i32");
472 let above = crate::wire::encode_one(Date::MAX.as_days_since_epoch() + 1)
473 .expect("out-of-range day should encode as raw i32");
474
475 assert_eq!(
476 crate::wire::decode_one::<Date>(&min).expect("minimum day should decode"),
477 Date::MIN,
478 );
479 assert_eq!(
480 crate::wire::decode_one::<Date>(&max).expect("maximum day should decode"),
481 Date::MAX,
482 );
483 assert!(crate::wire::decode_one::<Date>(&below).is_err());
484 assert!(crate::wire::decode_one::<Date>(&above).is_err());
485 }
486}