From 59ab76dcd343a6d9d0fff497e6ba2ff1140b3f2a Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 29 Dec 2017 14:53:25 -0700 Subject: [PATCH] feat(filters): date can parse YYYY-MM-DD HH:MM:SS TTTT This is a small step to #48 for the purpose of supporting cobalt changing its format. --- src/filters.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/filters.rs b/src/filters.rs index 52e443b79..bede53065 100644 --- a/src/filters.rs +++ b/src/filters.rs @@ -627,10 +627,14 @@ pub fn date(input: &Value, args: &[Value]) -> FilterResult { let input_string = input .as_str() .ok_or_else(|| FilterError::InvalidType(" expected".to_owned()))?; - let date = DateTime::parse_from_str(input_string, "%d %B %Y %H:%M:%S %z"); + let formats = ["%d %B %Y %H:%M:%S %z", "%Y-%m-%d %H:%M:%S %z"]; + let date = formats + .iter() + .filter_map(|f| DateTime::parse_from_str(input_string, f).ok()) + .next(); let date = match date { - Ok(d) => d, - Err(_) => { + Some(d) => d, + None => { return Ok(input.clone()); } }; @@ -1025,6 +1029,12 @@ mod tests { tos!("2016-06-13")); } + #[test] + fn unit_date_cobalt_format() { + assert_eq!(unit!(date, tos!("2016-06-13 02:30:00 +0300"), &[tos!("%Y-%m-%d")]), + tos!("2016-06-13")); + } + #[test] fn unit_date_bad_input_type() { assert_eq!(failed!(date, Value::Num(0f32), &[tos!("%Y-%m-%d")]),