1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
mod interval;
mod timestamp;
mod timestamptz;

pub use interval::*;

fn base_date() -> crate::Result<chrono::NaiveDate> {
    base_datetime().map(|x| x.date())
}

fn base_datetime() -> crate::Result<chrono::NaiveDateTime> {
    chrono::NaiveDate::from_ymd_opt(2000, 1, 1)
        .and_then(|x| x.and_hms_opt(0, 0, 0))
        .ok_or_else(|| crate::Error::Chrono("Invalid base date".to_string()))
}

#[cfg_attr(docsrs, doc(cfg(feature = "date")))]
impl crate::ToSql for chrono::NaiveDate {
    fn ty(&self) -> crate::pq::Type {
        crate::pq::types::DATE
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/date.c#L179
     */
    fn to_text(&self) -> crate::Result<Option<String>> {
        self.format("%F").to_string().to_text()
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/date.c#L226
     */
    fn to_binary(&self) -> crate::Result<Option<Vec<u8>>> {
        let t: chrono::Duration = *self - base_date()?;

        let mut buf = Vec::new();
        crate::to_sql::write_i32(&mut buf, t.num_days() as i32)?;

        Ok(Some(buf))
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "date")))]
impl crate::FromSql for chrono::NaiveDate {
    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/date.c#L114
     */
    fn from_text(ty: &crate::pq::Type, raw: Option<&str>) -> crate::Result<Self> {
        match chrono::NaiveDate::parse_from_str(crate::not_null(raw)?, "%F") {
            Ok(date) => Ok(date),
            _ => Err(Self::error(ty, raw)),
        }
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/date.c#L204
     */
    fn from_binary(ty: &crate::pq::Type, raw: Option<&[u8]>) -> crate::Result<Self> {
        let t = i32::from_binary(ty, raw)?;

        Ok(base_date()? + chrono::Duration::days(t.into()))
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "date")))]
impl crate::entity::Simple for chrono::NaiveDate {}

#[cfg(test)]
mod test {
    crate::sql_test!(
        date,
        chrono::NaiveDate,
        [
            (
                "'1970-01-01'",
                chrono::NaiveDate::from_ymd_opt(1970, 1, 1).unwrap()
            ),
            (
                "'2010-01-01'",
                chrono::NaiveDate::from_ymd_opt(2010, 1, 1).unwrap()
            ),
            (
                "'2100-12-30'",
                chrono::NaiveDate::from_ymd_opt(2100, 12, 30).unwrap()
            ),
        ]
    );
}