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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::datetime::decode_decimal;
use crate::*;
use core::convert::TryFrom;
use core::fmt;
#[cfg(feature = "datetime")]
use time::OffsetDateTime;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct UtcTime(pub ASN1DateTime);
impl UtcTime {
pub const fn new(datetime: ASN1DateTime) -> Self {
UtcTime(datetime)
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
let (year, month, day, hour, minute, rem) = match bytes {
[year1, year2, mon1, mon2, day1, day2, hour1, hour2, min1, min2, rem @ ..] => {
let year = decode_decimal(Self::TAG, *year1, *year2)?;
let month = decode_decimal(Self::TAG, *mon1, *mon2)?;
let day = decode_decimal(Self::TAG, *day1, *day2)?;
let hour = decode_decimal(Self::TAG, *hour1, *hour2)?;
let minute = decode_decimal(Self::TAG, *min1, *min2)?;
(year, month, day, hour, minute, rem)
}
_ => return Err(Self::TAG.invalid_value("malformed time string (not yymmddhhmm)")),
};
if rem.is_empty() {
return Err(Self::TAG.invalid_value("malformed time string"));
}
let (second, rem) = match rem {
[sec1, sec2, rem @ ..] => {
let second = decode_decimal(Self::TAG, *sec1, *sec2)?;
(second, rem)
}
_ => (0, rem),
};
if month > 12 || day > 31 || hour > 23 || minute > 59 || second > 59 {
return Err(Self::TAG.invalid_value("time components with invalid values"));
}
if rem.is_empty() {
return Err(Self::TAG.invalid_value("malformed time string"));
}
let tz = match rem {
[b'Z'] => ASN1TimeZone::Z,
[b'+', h1, h2, m1, m2] => {
let hh = decode_decimal(Self::TAG, *h1, *h2)?;
let mm = decode_decimal(Self::TAG, *m1, *m2)?;
ASN1TimeZone::Offset(hh as i8, mm as i8)
}
[b'-', h1, h2, m1, m2] => {
let hh = decode_decimal(Self::TAG, *h1, *h2)?;
let mm = decode_decimal(Self::TAG, *m1, *m2)?;
ASN1TimeZone::Offset(-(hh as i8), mm as i8)
}
_ => return Err(Self::TAG.invalid_value("malformed time string: no time zone")),
};
Ok(UtcTime(ASN1DateTime::new(
year as u32,
month,
day,
hour,
minute,
second,
None,
tz,
)))
}
#[cfg(feature = "datetime")]
#[cfg_attr(docsrs, doc(cfg(feature = "datetime")))]
#[inline]
pub fn utc_datetime(&self) -> Result<OffsetDateTime> {
self.0.to_datetime()
}
#[cfg(feature = "datetime")]
#[cfg_attr(docsrs, doc(cfg(feature = "datetime")))]
#[inline]
pub fn utc_adjusted_datetime(&self) -> Result<OffsetDateTime> {
self.0.to_datetime().and_then(|dt| {
let year = dt.year();
let year = if year >= 50 { year + 1900 } else { year + 2000 };
time::Date::from_calendar_date(year, dt.month(), dt.day())
.map(|d| dt.replace_date(d))
.map_err(|_e| Self::TAG.invalid_value("Invalid adjusted date"))
})
}
#[cfg(feature = "datetime")]
#[cfg_attr(docsrs, doc(cfg(feature = "datetime")))]
pub fn timestamp(&self) -> Result<i64> {
let dt = self.0.to_datetime()?;
Ok(dt.unix_timestamp())
}
}
impl<'a> TryFrom<Any<'a>> for UtcTime {
type Error = Error;
fn try_from(any: Any<'a>) -> Result<UtcTime> {
TryFrom::try_from(&any)
}
}
impl<'a, 'b> TryFrom<&'b Any<'a>> for UtcTime {
type Error = Error;
fn try_from(any: &'b Any<'a>) -> Result<UtcTime> {
any.tag().assert_eq(Self::TAG)?;
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_visible(b: &u8) -> bool {
0x20 <= *b && *b <= 0x7f
}
if !any.data.iter().all(is_visible) {
return Err(Error::StringInvalidCharset);
}
UtcTime::from_bytes(any.data)
}
}
impl fmt::Display for UtcTime {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let dt = &self.0;
match dt.tz {
ASN1TimeZone::Z | ASN1TimeZone::Undefined => write!(
f,
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}Z",
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second
),
ASN1TimeZone::Offset(hh, mm) => {
let (s, hh) = if hh > 0 { ('+', hh) } else { ('-', -hh) };
write!(
f,
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}{}{:02}{:02}",
dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, s, hh, mm
)
}
}
}
}
impl CheckDerConstraints for UtcTime {
fn check_constraints(_any: &Any) -> Result<()> {
Ok(())
}
}
impl DerAutoDerive for UtcTime {}
impl Tagged for UtcTime {
const TAG: Tag = Tag::UtcTime;
}
#[cfg(feature = "std")]
impl ToDer for UtcTime {
fn to_der_len(&self) -> Result<usize> {
Ok(15)
}
fn write_der_header(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
writer.write(&[Self::TAG.0 as u8, 13]).map_err(Into::into)
}
fn write_der_content(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
let _ = write!(
writer,
"{:02}{:02}{:02}{:02}{:02}{:02}Z",
self.0.year, self.0.month, self.0.day, self.0.hour, self.0.minute, self.0.second,
)?;
Ok(13)
}
}