Skip to content

Commit 59ab76d

Browse files
committed
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.
1 parent 96f9399 commit 59ab76d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/filters.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,14 @@ pub fn date(input: &Value, args: &[Value]) -> FilterResult {
627627
let input_string = input
628628
.as_str()
629629
.ok_or_else(|| FilterError::InvalidType(" expected".to_owned()))?;
630-
let date = DateTime::parse_from_str(input_string, "%d %B %Y %H:%M:%S %z");
630+
let formats = ["%d %B %Y %H:%M:%S %z", "%Y-%m-%d %H:%M:%S %z"];
631+
let date = formats
632+
.iter()
633+
.filter_map(|f| DateTime::parse_from_str(input_string, f).ok())
634+
.next();
631635
let date = match date {
632-
Ok(d) => d,
633-
Err(_) => {
636+
Some(d) => d,
637+
None => {
634638
return Ok(input.clone());
635639
}
636640
};
@@ -1025,6 +1029,12 @@ mod tests {
10251029
tos!("2016-06-13"));
10261030
}
10271031

1032+
#[test]
1033+
fn unit_date_cobalt_format() {
1034+
assert_eq!(unit!(date, tos!("2016-06-13 02:30:00 +0300"), &[tos!("%Y-%m-%d")]),
1035+
tos!("2016-06-13"));
1036+
}
1037+
10281038
#[test]
10291039
fn unit_date_bad_input_type() {
10301040
assert_eq!(failed!(date, Value::Num(0f32), &[tos!("%Y-%m-%d")]),

0 commit comments

Comments
 (0)