1use core::fmt;
2
3#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Date {
6 pub year: i16,
7 pub month: i8,
8 pub day: i8,
9}
10
11impl fmt::Display for Date {
12 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13 write!(f, "{:04}-{:02}-{:02}", self.year, self.month, self.day)
14 }
15}
16
17#[cfg(feature = "jiff")]
18impl From<jiff::civil::Date> for Date {
19 fn from(date: jiff::civil::Date) -> Self {
20 Date {
21 year: date.year(),
22 month: date.month(),
23 day: date.day(),
24 }
25 }
26}
27
28#[cfg(feature = "jiff")]
29impl From<Date> for jiff::civil::Date {
30 fn from(date: Date) -> Self {
31 jiff::civil::date(date.year, date.month, date.day)
32 }
33}
34
35#[cfg(feature = "chrono")]
36impl From<chrono::NaiveDate> for Date {
37 fn from(date: chrono::NaiveDate) -> Self {
38 use chrono::Datelike;
39 Date {
40 year: date.year() as i16,
41 month: date.month() as i8,
42 day: date.day() as i8,
43 }
44 }
45}
46
47#[cfg(feature = "chrono")]
48impl From<Date> for chrono::NaiveDate {
49 fn from(date: Date) -> Self {
50 chrono::NaiveDate::from_ymd_opt(date.year as i32, date.month as u32, date.day as u32)
51 .unwrap()
52 }
53}