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
180
181
182
183
184
185
186
187
188
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::{DateTime, get_digit, get_digit_unchecked};
use crate::error::Error;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct Time {
pub micro: u32,
pub sec: u8,
pub min: u8,
pub hour: u8,
}
impl Time {
pub(crate) fn parse_bytes_partial(bytes: &[u8], offset: usize) -> Result<(Self, usize), Error> {
if bytes.len() - offset < 5 {
return Err(Error::E("TooShort".to_string()));
}
let hour: u8;
let minute: u8;
unsafe {
let h1 = get_digit_unchecked!(bytes, offset, "InvalidCharHour");
let h2 = get_digit_unchecked!(bytes, offset + 1, "InvalidCharHour");
hour = h1 * 10 + h2;
match bytes.get_unchecked(offset + 2) {
b':' => (),
_ => ()
}
let m1 = get_digit_unchecked!(bytes, offset + 3, "InvalidCharMinute");
let m2 = get_digit_unchecked!(bytes, offset + 4, "InvalidCharMinute");
minute = m1 * 10 + m2;
}
if hour > 23 {
return Err(Error::E("OutOfRangeHour".to_string()));
}
if minute > 59 {
return Err(Error::E("OutOfRangeMinute".to_string()));
}
let mut length: usize = 5;
let (second, microsecond) = {
let s1 = get_digit!(bytes, offset + 6, "InvalidCharSecond");
let s2 = get_digit!(bytes, offset + 7, "InvalidCharSecond");
let second = s1 * 10 + s2;
if second > 59 {
return Err(Error::E("OutOfRangeSecond".to_string()));
}
length = 8;
let mut microsecond = 0;
let frac_sep = bytes.get(offset + 8).copied();
if frac_sep == Some(b'.') || frac_sep == Some(b',') {
length = 9;
let mut i: usize = 0;
loop {
match bytes.get(offset + length + i) {
Some(c) if (b'0'..=b'9').contains(c) => {
microsecond *= 10;
microsecond += (c - b'0') as u32;
}
_ => {
break;
}
}
i += 1;
if i > 6 {
return Err(Error::E("SecondFractionTooLong".to_string()));
}
}
if i == 0 {
return Err(Error::E("SecondFractionMissing".to_string()));
}
if i < 6 {
microsecond *= 10_u32.pow(6 - i as u32);
}
length += i;
}
(second, microsecond)
};
let t = Self {
micro: microsecond,
sec: second,
min: minute,
hour,
};
Ok((t, length))
}
}
impl From<DateTime> for Time {
fn from(arg: DateTime) -> Self {
Time {
micro: arg.micro,
sec: arg.sec,
min: arg.min,
hour: arg.hour,
}
}
}
impl FromStr for Time {
type Err = Error;
fn from_str(s: &str) -> Result<Time, Error> {
let (t, _) = Time::parse_bytes_partial(s.as_bytes(), 0)?;
Ok(t)
}
}
impl Display for Time {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let mut buf: [u8; 15] = *b"00:00:00.000000";
buf[0] = b'0' + (self.hour / 10) as u8;
buf[1] = b'0' + (self.hour % 10) as u8;
buf[3] = b'0' + (self.min / 10) as u8;
buf[4] = b'0' + (self.min % 10) as u8;
buf[6] = b'0' + (self.sec / 10) as u8;
buf[7] = b'0' + (self.sec % 10) as u8;
buf[9] = b'0' + (self.micro / 100000 % 10) as u8;
buf[10] = b'0' + (self.micro / 10000 % 10) as u8;
buf[11] = b'0' + (self.micro / 1000 % 10) as u8;
buf[12] = b'0' + (self.micro / 100 % 10) as u8;
buf[13] = b'0' + (self.micro / 10 % 10) as u8;
buf[14] = b'0' + (self.micro % 10) as u8;
f.write_str(std::str::from_utf8(&buf[..]).unwrap())
}
}
impl Serialize for Time{
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 Time{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
use serde::de::Error;
Time::from_str(&String::deserialize(deserializer)?).map_err(|e| Error::custom(e.to_string()))
}
}
impl From<&DateTime> for Time{
fn from(arg: &DateTime) -> Self {
Time{
micro: arg.micro,
sec: arg.sec,
min: arg.min,
hour: arg.hour
}
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use crate::Time;
#[test]
fn test_date() {
let d = Time::from_str("11:12:13.123456").unwrap();
println!("{}", d);
assert_eq!("11:12:13.123456".to_string(), d.to_string());
let d = Time::from_str("11:12:13.12345").unwrap();
println!("{}", d);
assert_eq!("11:12:13.012345".to_string(), d.to_string());
let d = Time::from_str("11:12:13.1234").unwrap();
println!("{}", d);
assert_eq!("11:12:13.001234".to_string(), d.to_string());
}
}