aboutsummaryrefslogtreecommitdiff
path: root/src/primitive/datetime.rs
blob: e6946b9a8e82fb5ba29850afd556896521a20147 (plain)
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
use crate::Deserialize;
use crate::Serialize;

/// The DateTime struct represents a DateTime as received in IRC
///
/// DateTime is, like all other struct based types, serialized sequentially.
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct DateTime {
    /// Day in Julian calendar, unknown if signed or unsigned
    julian_day: i32,
    /// Milliseconds since start of day
    millis_of_day: i32,
    /// Timezone of DateTime, 0x00 is local, 0x01 is UTC
    zone: u8,
}

impl Serialize for DateTime {
    fn serialize(&self) -> Result<Vec<std::primitive::u8>, failure::Error> {
        let mut values: Vec<u8> = Vec::new();

        values.append(&mut i32::serialize(&self.julian_day)?);
        values.append(&mut i32::serialize(&self.millis_of_day)?);
        values.append(&mut u8::serialize(&(self.zone))?);

        Ok(values)
    }
}

impl Deserialize for DateTime {
    fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error>
    where
        Self: Sized,
    {
        let (_, julian_day) = i32::parse(&b[0..4])?;
        let (_, millis_of_day) = i32::parse(&b[4..8])?;
        let (_, zone) = u8::parse(&b[8..9])?;

        return Ok((
            9,
            DateTime {
                julian_day,
                millis_of_day,
                zone,
            },
        ));
    }
}

/// The Date struct represents a Date as received in IRC
///
/// Date is, like all other struct based types, serialized sequentially.
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct Date {
    /// Day in Julian calendar, unknown if signed or unsigned
    julian_day: i32,
}

impl Serialize for Date {
    fn serialize(&self) -> Result<Vec<std::primitive::u8>, failure::Error> {
        let mut values: Vec<u8> = Vec::new();

        values.append(&mut i32::serialize(&self.julian_day)?);

        Ok(values)
    }
}

impl Deserialize for Date {
    fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error>
    where
        Self: Sized,
    {
        let (_, julian_day) = i32::parse(&b[0..4])?;

        return Ok((9, Date { julian_day }));
    }
}

/// The Time struct represents a Time as received in IRC
///
/// Time is, like all other struct based types, serialized sequentially.
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub struct Time {
    /// Milliseconds since start of day
    millis_of_day: i32,
}

impl Serialize for Time {
    fn serialize(&self) -> Result<Vec<std::primitive::u8>, failure::Error> {
        let mut values: Vec<u8> = Vec::new();

        values.append(&mut i32::serialize(&self.millis_of_day)?);

        Ok(values)
    }
}

impl Deserialize for Time {
    fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error>
    where
        Self: Sized,
    {
        let (_, millis_of_day) = i32::parse(&b[0..4])?;

        return Ok((4, Time { millis_of_day }));
    }
}