Skip to content

Commit 0959cda

Browse files
committed
formalize grammar of relative time strings and use nom to parse them
BREAKING CHANGES: this commit introduced two breaking changes: - Keyword `ago` semantic change (to be consistent with GNU date) The `ago` keyword now applies to individual date-time displacement (the one that immediately precedes it) and can be specified multiple times. For example, "1 day ago 2 days ago" gives a combined effect of "3 days ago", and "1 day 2 days ago" gives a combined effect of "1 day ago". In previous implementation, `ago` can only be specified once at the end of the relative time string, and it applies to all date-time displacements as a whole. So "1 day ago 2 days ago" is invalid and "1 day 2 days ago" gives a global effect of "3 days ago". - Relative time strings like "lastweek" or "nextmonth" are now invalid, and space(s) are required in between the shift and the time unit.
1 parent b6795d9 commit 0959cda

File tree

4 files changed

+674
-321
lines changed

4 files changed

+674
-321
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use regex::Regex;
1313
use std::error::Error;
1414
use std::fmt::{self, Display};
1515

16+
pub(crate) mod parse;
17+
1618
// Expose parse_datetime
1719
mod parse_relative_time;
1820
mod parse_timestamp;

src/parse/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// For the full copyright and license information, please view the LICENSE
2+
// file that was distributed with this source code.
3+
use relative_time::relative_times;
4+
5+
mod relative_time;
6+
7+
// TODO: more specific errors?
8+
#[derive(Debug)]
9+
pub(crate) struct ParseError;
10+
11+
pub(crate) use relative_time::RelativeTime;
12+
pub(crate) use relative_time::TimeUnit;
13+
14+
/// Parses a string of relative times into a vector of `RelativeTime` structs.
15+
pub(crate) fn parse_relative_times(input: &str) -> Result<Vec<RelativeTime>, ParseError> {
16+
relative_times(input)
17+
.map(|(_, times)| times)
18+
.map_err(|_| ParseError)
19+
}

0 commit comments

Comments
 (0)