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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::{get_digit_unchecked, DateTime};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::{self, Display, Formatter};
use std::str::FromStr;
use crate::error::Error;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Date {
pub day: u8,
pub mon: u8,
pub year: u16,
}
impl Date {
pub(crate) fn parse_bytes_partial(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() < 10 {
return Err(Error::E("TooShort".to_string()));
}
let year: u16;
let month: u8;
let day: u8;
unsafe {
let y1 = get_digit_unchecked!(bytes, 0, "InvalidCharYear") as u16;
let y2 = get_digit_unchecked!(bytes, 1, "InvalidCharYear") as u16;
let y3 = get_digit_unchecked!(bytes, 2, "InvalidCharYear") as u16;
let y4 = get_digit_unchecked!(bytes, 3, "InvalidCharYear") as u16;
year = y1 * 1000 + y2 * 100 + y3 * 10 + y4;
match bytes.get_unchecked(4) {
b'-' => (),
_ => (), }
let m1 = get_digit_unchecked!(bytes, 5, "InvalidCharMonth");
let m2 = get_digit_unchecked!(bytes, 6, "InvalidCharMonth");
month = m1 * 10 + m2;
match bytes.get_unchecked(7) {
b'-' => (),
_ => (), }
let d1 = get_digit_unchecked!(bytes, 8, "InvalidCharDay");
let d2 = get_digit_unchecked!(bytes, 9, "InvalidCharDay");
day = d1 * 10 + d2;
}
let max_days = match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => {
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
29
} else {
28
}
}
_ => return Err(Error::E("OutOfRangeMonth".to_string())),
};
if day < 1 || day > max_days {
return Err(Error::E("OutOfRangeDay".to_string()));
}
Ok(Self {
day,
mon: month,
year,
})
}
pub fn set_day(mut self, arg: u8) -> Self {
self.day = arg;
self
}
pub fn set_mon(mut self, arg: u8) -> Self {
self.mon = arg;
self
}
pub fn set_year(mut self, arg: u16) -> Self {
self.year = arg;
self
}
pub fn get_day(&self) -> u8 {
self.day
}
pub fn get_mon(&self) -> u8 {
self.mon
}
pub fn get_year(&self) -> u16 {
self.year
}
pub fn display_date(&self, start: usize, buf: &mut [u8]) -> usize {
buf[start + 0] = b'0' + (self.year / 1000) as u8;
buf[start + 1] = b'0' + (self.year / 100 % 10) as u8;
buf[start + 2] = b'0' + (self.year / 10 % 10) as u8;
buf[start + 3] = b'0' + (self.year % 10) as u8;
buf[start + 5] = b'0' + (self.mon / 10) as u8;
buf[start + 6] = b'0' + (self.mon % 10) as u8;
buf[start + 8] = b'0' + (self.day / 10) as u8;
buf[start + 9] = b'0' + (self.day % 10) as u8;
start + 10
}
}
impl FromStr for Date {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let d = Date::parse_bytes_partial(s.as_bytes())?;
Ok(d)
}
}
impl Display for Date {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut buf: [u8; 10] = *b"0000-00-00";
self.display_date(0, &mut buf);
f.write_str(std::str::from_utf8(&buf[..]).unwrap())
}
}
impl Serialize for Date {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for Date {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
Date::from_str(&String::deserialize(deserializer)?)
.map_err(|e| D::Error::custom(e.to_string()))
}
}
impl From<DateTime> for Date {
fn from(arg: DateTime) -> Self {
Date {
day: arg.day,
mon: arg.mon,
year: arg.year,
}
}
}