hgtime: parse "ago" time like "5 minutes ago"

Summary:
This is a feature that does not exist in hg. It has potential to unify the
`date` and `ago` revsets.

By using the `humantime` crate we can get this feature fairly easily.

Reviewed By: sfilipco

Differential Revision: D17504705

fbshipit-source-id: b888517114b91c847ae319552d07fe5a5a41cad3
This commit is contained in:
Jun Wu 2019-10-08 11:07:04 -07:00 committed by Facebook Github Bot
parent 96c30fbd8a
commit 785f0040c6
2 changed files with 19 additions and 0 deletions

View File

@ -5,3 +5,4 @@ edition = "2018"
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
humantime = "1.3"

View File

@ -83,6 +83,13 @@ impl HgTime {
Self::from(Local::today().and_hms(0, 0, 0) - Duration::days(1)) Self::from(Local::today().and_hms(0, 0, 0) - Duration::days(1))
.use_default_offset(), .use_default_offset(),
), ),
date if date.ends_with(" ago") => {
let duration_str = &date[..date.len() - 4];
duration_str
.parse::<humantime::Duration>()
.ok()
.map(|duration| Self::now() - duration.as_secs())
}
_ => Self::parse_absolute(date), _ => Self::parse_absolute(date),
} }
} }
@ -306,6 +313,17 @@ mod tests {
assert_eq!(t("Fri, 20 Sep 2019 12:15:13"), "1568988913 7200"); assert_eq!(t("Fri, 20 Sep 2019 12:15:13"), "1568988913 7200");
} }
#[test]
fn test_parse_ago() {
set_default_offset(7200);
assert_eq!(d("10m ago", Duration::hours(1)), "0");
assert_eq!(d("10 min ago", Duration::hours(1)), "0");
assert_eq!(d("10 minutes ago", Duration::hours(1)), "0");
assert_eq!(d("10 hours ago", Duration::days(1)), "0");
assert_eq!(d("10 h ago", Duration::days(1)), "0");
assert_eq!(t("9999999 years ago"), "0 7200");
}
/// String representation of parse result. /// String representation of parse result.
fn t(date: &str) -> String { fn t(date: &str) -> String {
match HgTime::parse(date) { match HgTime::parse(date) {