Skip to content

Commit a0f1376

Browse files
authored
Merge pull request #222 from yuankunzhang/improve-timezone
refactor: improve timezone offset parsing and validation
2 parents d6bde13 + 59eac72 commit a0f1376

File tree

3 files changed

+232
-138
lines changed

3 files changed

+232
-138
lines changed

src/items/primitive.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,20 @@ where
107107
O: Uint + FromStr,
108108
E: ParserError<&'a str>,
109109
{
110-
digit1
111-
.void()
112-
.take()
110+
dec_uint_str
113111
.verify_map(|s: &str| s.parse().ok())
114112
.parse_next(input)
115113
}
116114

117-
/// Parse a colon preceded by whitespace
115+
/// Parse an unsigned decimal integer as a string slice.
116+
pub(super) fn dec_uint_str<'a, E>(input: &mut &'a str) -> winnow::Result<&'a str, E>
117+
where
118+
E: ParserError<&'a str>,
119+
{
120+
digit1.void().take().parse_next(input)
121+
}
122+
123+
/// Parse a colon preceded by whitespace.
118124
pub(super) fn colon<'a, E>(input: &mut &'a str) -> winnow::Result<(), E>
119125
where
120126
E: ParserError<&'a str>,

src/items/time.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use winnow::{
4646
use super::{
4747
epoch::sec_and_nsec,
4848
primitive::{colon, ctx_err, dec_uint, s},
49-
timezone::{timezone_num, Offset},
49+
timezone::{timezone_offset, Offset},
5050
};
5151

5252
#[derive(PartialEq, Clone, Debug, Default)]
@@ -55,7 +55,7 @@ pub(crate) struct Time {
5555
pub(crate) minute: u8,
5656
pub(crate) second: u8,
5757
pub(crate) nanosecond: u32,
58-
pub(crate) offset: Option<Offset>,
58+
pub(super) offset: Option<Offset>,
5959
}
6060

6161
impl TryFrom<Time> for jiff::civil::Time {
@@ -87,7 +87,7 @@ pub(crate) fn parse(input: &mut &str) -> ModalResult<Time> {
8787
/// Also used by the [`combined`](super::combined) module
8888
pub(super) fn iso(input: &mut &str) -> ModalResult<Time> {
8989
alt((
90-
(hour24, timezone_num).map(|(hour, offset)| Time {
90+
(hour24, timezone_offset).map(|(hour, offset)| Time {
9191
hour,
9292
minute: 0,
9393
second: 0,
@@ -99,7 +99,7 @@ pub(super) fn iso(input: &mut &str) -> ModalResult<Time> {
9999
colon,
100100
minute,
101101
opt(preceded(colon, second)),
102-
opt(timezone_num),
102+
opt(timezone_offset),
103103
)
104104
.map(|(hour, _, minute, sec_nsec, offset)| Time {
105105
hour,
@@ -337,11 +337,7 @@ mod tests {
337337
minute: 23,
338338
second: 0,
339339
nanosecond: 0,
340-
offset: Some(Offset {
341-
negative: false,
342-
hours: 5,
343-
minutes: 0,
344-
}),
340+
offset: Some((false, 5, 0).try_into().unwrap()),
345341
};
346342

347343
for mut s in [
@@ -368,11 +364,7 @@ mod tests {
368364
minute: 45,
369365
second: 0,
370366
nanosecond: 0,
371-
offset: Some(Offset {
372-
negative: false,
373-
hours: 5,
374-
minutes: 35,
375-
}),
367+
offset: Some((false, 5, 35).try_into().unwrap()),
376368
};
377369

378370
for mut s in [
@@ -401,11 +393,7 @@ mod tests {
401393
minute: 45,
402394
second: 0,
403395
nanosecond: 0,
404-
offset: Some(Offset {
405-
negative: false,
406-
hours: 0,
407-
minutes: 35,
408-
}),
396+
offset: Some((false, 0, 35).try_into().unwrap()),
409397
};
410398

411399
for mut s in [
@@ -433,11 +421,7 @@ mod tests {
433421
minute: 45,
434422
second: 0,
435423
nanosecond: 0,
436-
offset: Some(Offset {
437-
negative: true,
438-
hours: 5,
439-
minutes: 35,
440-
}),
424+
offset: Some((true, 5, 35).try_into().unwrap()),
441425
};
442426

443427
for mut s in [

0 commit comments

Comments
 (0)