Skip to content

Commit

Permalink
feat(filters): date can parse YYYY-MM-DD HH:MM:SS TTTT
Browse files Browse the repository at this point in the history
This is a small step to #48 for the purpose of supporting cobalt
changing its format.
  • Loading branch information
epage committed Dec 29, 2017
1 parent 96f9399 commit 59ab76d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
};
Expand Down Expand Up @@ -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")]),
Expand Down

0 comments on commit 59ab76d

Please sign in to comment.