| 1 |
// For the full copyright and license information, please view the LICENSE |
| 2 |
// file that was distributed with this source code. |
| 3 |
|
| 4 |
//! Parse an ISO 8601 date and time item |
| 5 |
//! |
| 6 |
//! The GNU docs state: |
| 7 |
//! |
| 8 |
//! > The ISO 8601 date and time of day extended format consists of an ISO 8601 |
| 9 |
//! > date, a âTâ character separator, and an ISO 8601 time of day. This format |
| 10 |
//! > is also recognized if the âTâ is replaced by a space. |
| 11 |
//! > |
| 12 |
//! > In this format, the time of day should use 24-hour notation. Fractional |
| 13 |
//! > seconds are allowed, with either comma or period preceding the fraction. |
| 14 |
//! > ISO 8601 fractional minutes and hours are not supported. Typically, hosts |
| 15 |
//! > support nanosecond timestamp resolution; excess precision is silently |
| 16 |
//! > discarded. |
| 17 |
use winnow::{ |
| 18 |
combinator::{alt, trace}, |
| 19 |
ModalResult, Parser, |
| 20 |
}; |
| 21 |
|
| 22 |
use crate::items::space; |
| 23 |
|
| 24 |
use super::{date, primitive::s, time}; |
| 25 |
|
| 26 |
#[derive(PartialEq, Debug, Clone, Default)] |
| 27 |
pub(crate) struct DateTime { |
| 28 |
pub(crate) date: date::Date, |
| 29 |
pub(crate) time: time::Time, |
| 30 |
} |
| 31 |
|
| 32 |
fn remaining_starts_with_meridiem(input: &str) -> bool { |
| 33 |
let trimmed = input.trim_start(); |
| 34 |
trimmed.starts_with("am") |
| 35 |
|| trimmed.starts_with("pm") |
| 36 |
|| trimmed.starts_with("a.m.") |
| 37 |
|| trimmed.starts_with("p.m.") |
| 38 |
} |
| 39 |
|
| 40 |
pub(crate) fn parse(input: &mut &str) -> ModalResult<DateTime> { |
| 41 |
let date = trace("iso_date", alt((date::iso1, date::iso2))).parse_next(input)?; |
| 42 |
// Note: the `T` is lowercased by the main parse function |
| 43 |
alt((s('t').void(), (' ', space).void())).parse_next(input)?; |
| 44 |
|
| 45 |
let mut iso_input = *input; |
| 46 |
if let Ok(parsed_time) = trace("iso_time", time::iso).parse_next(&mut iso_input) { |
| 47 |
if !remaining_starts_with_meridiem(iso_input) { |
| 48 |
*input = iso_input; |
| 49 |
return Ok(DateTime { |
| 50 |
date, |
| 51 |
time: parsed_time, |
| 52 |
}); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
let time = trace("iso_time", time::parse).parse_next(input)?; |
| 57 |
Ok(DateTime { date, time }) |
| 58 |
} |
| 59 |
|
| 60 |
#[cfg(test)] |
| 61 |
mod tests { |
| 62 |
use super::{parse, DateTime}; |
| 63 |
use crate::items::{date::Date, time::Time}; |
| 64 |
|
| 65 |
#[test] |
| 66 |
fn some_date() { |
| 67 |
let reference = Some(DateTime { |
| 68 |
date: Date { |
| 69 |
day: 10, |
| 70 |
month: 10, |
| 71 |
year: Some(2022), |
| 72 |
}, |
| 73 |
time: Time { |
| 74 |
hour: 10, |
| 75 |
minute: 10, |
| 76 |
second: 55, |
| 77 |
nanosecond: 0, |
| 78 |
offset: None, |
| 79 |
}, |
| 80 |
}); |
| 81 |
|
| 82 |
for mut s in [ |
| 83 |
"2022-10-10t10:10:55", |
| 84 |
"2022-10-10 10:10:55", |
| 85 |
"2022-10-10 t 10:10:55", |
| 86 |
"2022-10-10 10:10:55", |
| 87 |
"2022-10-10 (A comment!) t 10:10:55", |
| 88 |
"2022-10-10 (A comment!) 10:10:55", |
| 89 |
] { |
| 90 |
let old_s = s.to_owned(); |
| 91 |
assert_eq!(parse(&mut s).ok(), reference, "Failed string: {old_s}") |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
fn date_and_time_ampm() { |
| 97 |
let reference = Some(DateTime { |
| 98 |
date: Date { |
| 99 |
day: 15, |
| 100 |
month: 6, |
| 101 |
year: Some(2024), |
| 102 |
}, |
| 103 |
time: Time { |
| 104 |
hour: 15, |
| 105 |
minute: 0, |
| 106 |
second: 0, |
| 107 |
nanosecond: 0, |
| 108 |
offset: None, |
| 109 |
}, |
| 110 |
}); |
| 111 |
|
| 112 |
for mut s in [ |
| 113 |
"2024-06-15 3:00 pm", |
| 114 |
"2024-06-15 3:00pm", |
| 115 |
"2024-06-15 3:00 p.m.", |
| 116 |
] { |
| 117 |
let old_s = s.to_owned(); |
| 118 |
assert_eq!(parse(&mut s).ok(), reference, "Failed string: {old_s}"); |
| 119 |
} |
| 120 |
} |
| 121 |
} |