Skip to content

Commit a8e26ab

Browse files
authored
Merge pull request #209 from yuankunzhang/replace-chrono-with-jiff-2
(jiff series #2) refactor: optimize time type sizes and add conversion from `Time` to `jiff::civil::Time`
2 parents a79ce1f + 920cfa5 commit a8e26ab

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/items/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ impl DateTimeBuilder {
223223
dt.year(),
224224
dt.month(),
225225
dt.day(),
226-
hour,
227-
minute,
228-
second,
226+
hour as u32,
227+
minute as u32,
228+
second as u32,
229229
nanosecond,
230230
offset,
231231
)?;

src/items/time.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,25 @@ use super::{
5151

5252
#[derive(PartialEq, Clone, Debug, Default)]
5353
pub(crate) struct Time {
54-
pub hour: u32,
55-
pub minute: u32,
56-
pub second: u32,
57-
pub nanosecond: u32,
58-
pub offset: Option<Offset>,
54+
pub(crate) hour: u8,
55+
pub(crate) minute: u8,
56+
pub(crate) second: u8,
57+
pub(crate) nanosecond: u32,
58+
pub(crate) offset: Option<Offset>,
59+
}
60+
61+
impl TryFrom<Time> for jiff::civil::Time {
62+
type Error = &'static str;
63+
64+
fn try_from(time: Time) -> Result<Self, Self::Error> {
65+
jiff::civil::Time::new(
66+
time.hour as i8,
67+
time.minute as i8,
68+
time.second as i8,
69+
time.nanosecond as i32,
70+
)
71+
.map_err(|_| "time is not valid")
72+
}
5973
}
6074

6175
#[derive(Clone)]
@@ -134,25 +148,26 @@ fn am_pm_time(input: &mut &str) -> ModalResult<Time> {
134148
})
135149
}
136150

137-
/// Parse a number of hours in `0..24`(preceded by whitespace)
138-
pub(super) fn hour24(input: &mut &str) -> ModalResult<u32> {
151+
/// Parse a number of hours in `0..24`.
152+
pub(super) fn hour24(input: &mut &str) -> ModalResult<u8> {
139153
s(dec_uint).verify(|x| *x < 24).parse_next(input)
140154
}
141155

142-
/// Parse a number of hours in `0..=12` (preceded by whitespace)
143-
fn hour12(input: &mut &str) -> ModalResult<u32> {
156+
/// Parse a number of hours in `0..=12`.
157+
fn hour12(input: &mut &str) -> ModalResult<u8> {
144158
s(dec_uint).verify(|x| *x <= 12).parse_next(input)
145159
}
146160

147-
/// Parse a number of minutes (preceded by whitespace)
148-
pub(super) fn minute(input: &mut &str) -> ModalResult<u32> {
161+
/// Parse a number of minutes in `0..60`.
162+
pub(super) fn minute(input: &mut &str) -> ModalResult<u8> {
149163
s(dec_uint).verify(|x| *x < 60).parse_next(input)
150164
}
151165

152-
/// Parse a number of seconds (preceded by whitespace)
153-
fn second(input: &mut &str) -> ModalResult<(u32, u32)> {
166+
/// Parse a number of seconds in `0..60` and an optional number of nanoseconds
167+
/// (default to 0 if not set).
168+
fn second(input: &mut &str) -> ModalResult<(u8, u32)> {
154169
sec_and_nsec
155-
.verify_map(|(s, ns)| if s < 60 { Some((s as u32, ns)) } else { None })
170+
.verify_map(|(s, ns)| if s < 60 { Some((s as u8, ns)) } else { None })
156171
.parse_next(input)
157172
}
158173

0 commit comments

Comments
 (0)