Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98d4931

Browse files
authored
Merge pull request#169 from yuankunzhang/hour-with-meridiem
fix: hour must be greater than 0 when meridiem is specified
2 parents24fa78a +0b5d3ff commit98d4931

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

‎src/items/time.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl Display for Offset {
143143
}
144144

145145
#[derive(Clone)]
146-
enumSuffix{
146+
enumMeridiem{
147147
Am,
148148
Pm,
149149
}
@@ -178,30 +178,37 @@ pub fn iso(input: &mut &str) -> ModalResult<Time> {
178178
///
179179
/// The hours are restricted to 12 or lower in this format
180180
fnam_pm_time(input:&mut&str) ->ModalResult<Time>{
181-
seq!(
181+
let(h, m, s, meridiem) =seq!(
182182
hour12,
183183
opt(preceded(colon, minute)),
184184
opt(preceded(colon, second)),
185185
alt((
186-
s("am").value(Suffix::Am),
187-
s("a.m.").value(Suffix::Am),
188-
s("pm").value(Suffix::Pm),
189-
s("p.m.").value(Suffix::Pm)
186+
s("am").value(Meridiem::Am),
187+
s("a.m.").value(Meridiem::Am),
188+
s("pm").value(Meridiem::Pm),
189+
s("p.m.").value(Meridiem::Pm)
190190
)),
191191
)
192-
.map(|(h, m, s, suffix)|{
193-
letmut h = h %12;
194-
ifletSuffix::Pm = suffix{
195-
h +=12;
196-
}
197-
Time{
198-
hour: h,
199-
minute: m.unwrap_or(0),
200-
second: s.unwrap_or(0.0),
201-
offset:None,
202-
}
192+
.parse_next(input)?;
193+
194+
if h ==0{
195+
letmut ctx_err =ContextError::new();
196+
ctx_err.push(StrContext::Expected(StrContextValue::Description(
197+
"hour must be greater than 0 when meridiem is specified",
198+
)));
199+
returnErr(ErrMode::Cut(ctx_err));
200+
}
201+
202+
letmut h = h %12;
203+
ifletMeridiem::Pm = meridiem{
204+
h +=12;
205+
}
206+
Ok(Time{
207+
hour: h,
208+
minute: m.unwrap_or(0),
209+
second: s.unwrap_or(0.0),
210+
offset:None,
203211
})
204-
.parse_next(input)
205212
}
206213

207214
/// Parse a colon preceded by whitespace
@@ -633,6 +640,12 @@ mod tests {
633640
}
634641
}
635642

643+
#[test]
644+
fninvalid(){
645+
assert!(parse(&mut"00:00am").is_err());
646+
assert!(parse(&mut"00:00:00am").is_err());
647+
}
648+
636649
#[test]
637650
fnhours_only(){
638651
let reference =Time{

‎tests/time.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ fn test_time_correction_with_overflow(#[case] input: &str, #[case] expected: &st
107107
#[case("23:59:60")]
108108
#[case("13:00:00am")]
109109
#[case("13:00:00pm")]
110-
/* TODO: https://github.com/uutils/parse_datetime/issues/166
111110
#[case("00:00:00am")]
112111
#[case("00:00:00pm")]
113-
*/
114112
#[case("23:59:59 a.m")]
115113
#[case("23:59:59 pm.")]
116114
#[case("23:59:59+24:01")]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp