Skip to content

Commit

Permalink
Merge pull request #156 from epage/date
Browse files Browse the repository at this point in the history
feat(filters): date can parse YYYY-MM-DD HH:MM:SS TTTT
  • Loading branch information
epage authored Dec 29, 2017
2 parents ba8f1b3 + 59ab76d commit ff6e613
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 ff6e613

Please sign in to comment.