Skip to content

Commit 14262cd

Browse files
authored
Fix panic when formatting timestamp with timezone specifier (#983)
Fixes a panic that occurs when a timestamp is formatted using `date` with any timezone-related specifiers (`%Z`, `%z`) when a timezone argument is not provided. Datetime strings already handle this case by assuming the value is in UTC, so the fix here is to do the same for timestamps as well.
1 parent 4e5145e commit 14262cd

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/builtins/filters/common.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub fn date(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
177177
);
178178
match timezone {
179179
Some(timezone) => timezone.from_utc_datetime(&date).format(&format),
180-
None => date.format(&format),
180+
None => date.and_utc().format(&format),
181181
}
182182
}
183183
None => return Err(Error::msg(format!("Filter `date` was invoked on a float: {}", n))),
@@ -442,6 +442,16 @@ mod tests {
442442
assert_eq!(result.unwrap(), to_value("2022-03-26").unwrap());
443443
}
444444

445+
#[cfg(feature = "builtins")]
446+
#[test]
447+
fn date_timestamp_without_timezone_with_timezone_specifier() {
448+
let mut args = HashMap::new();
449+
args.insert("format".to_string(), to_value("%Y-%m-%d %H:%M:%S %Z (%z)").unwrap());
450+
let result = date(&to_value(1648302603).unwrap(), &args);
451+
assert!(result.is_ok());
452+
assert_eq!(result.unwrap(), to_value("2022-03-26 13:50:03 UTC (+0000)").unwrap());
453+
}
454+
445455
#[cfg(feature = "date-locale")]
446456
#[test]
447457
fn date_timestamp_with_timezone_and_locale() {

0 commit comments

Comments
 (0)