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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use serde::{self, Deserialize, Deserializer, Serialize, Serializer};
use serde::de::Visitor;
use chrono::{NaiveDate, Datelike, Duration, Local};
use std::ops::Deref;
use std::fmt;

#[derive(Debug)]
pub struct UntisDate(NaiveDate);

impl UntisDate {
    pub fn week_begin_from(date: NaiveDate) -> Self {
        let days_from_monday = date.weekday().num_days_from_monday();
        let monday = date - Duration::days(days_from_monday as i64);
        UntisDate(monday)
    }

    pub fn week_end_from(date: NaiveDate) -> Self {
        let days_from_monday = date.weekday().num_days_from_monday() as i64;
        let days_left_till_saturday = 5 - days_from_monday;
        let saturday = date + Duration::days(days_left_till_saturday);
        UntisDate(saturday)
    }

    pub fn week_begin() -> Self {
        Self::week_begin_from(Local::today().naive_local())
    }

    pub fn week_end() -> Self {
        Self::week_end_from(Local::today().naive_local())
    }
}

impl Deref for UntisDate {
    type Target = NaiveDate;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Serialize for UntisDate {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_u32(chrono_to_untis_date(**self))
    }
}

impl<'de> Deserialize<'de> for UntisDate {
    fn deserialize<D>(deserializer: D) -> Result<UntisDate, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_u32(UntisDateVisitor)
    }
}

struct UntisDateVisitor;

impl<'de> Visitor<'de> for UntisDateVisitor {
    type Value = UntisDate;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter
            .write_str("an integer in the format YEARMONTHDAY like 20180316 for the 16. March 2018")
    }

    fn visit_i8<E>(self, value: i8) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }

    fn visit_i16<E>(self, value: i16) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }

    fn visit_i32<E>(self, value: i32) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }

    fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }

    fn visit_u8<E>(self, value: u8) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }

    fn visit_u16<E>(self, value: u16) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }

    fn visit_u32<E>(self, value: u32) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }

    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
        where E: serde::de::Error,
    {
        Ok(UntisDate(chrono_from_untis_date(value as u32)))
    }
}

fn chrono_to_untis_date(date: NaiveDate) -> u32 {
    let string = format!("{}", date.format("%Y%m%d"));
    let number = string.parse::<u32>().unwrap();
    number
}

fn chrono_from_untis_date(value: u32) -> NaiveDate {
    let string = format!("{}", value);
    let year = string[0..4].parse::<i32>().unwrap();
    let month = string[4..6].parse::<u32>().unwrap();
    let day = string[6..8].parse::<u32>().unwrap();

    NaiveDate::from_ymd(year, month, day)
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Weekday;

    #[test]
    fn convert_untis_date_forth_and_back() {
        let number = 20180316;
        let date = chrono_from_untis_date(number);
        let new_number = chrono_to_untis_date(date);

        assert_eq!(number, new_number);
    }

    #[test]
    fn untis_date_week_begin_is_monday() {
        let monday = UntisDate::week_begin();
        assert_eq!(monday.0.weekday(), Weekday::Mon);
    }

    #[test]
    fn untis_date_week_end_is_saturday() {
        let saturday = UntisDate::week_end();
        assert_eq!(saturday.0.weekday(), Weekday::Sat);
    }
}