Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make date filter work on a RFC3339 string as well #960

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.20.1 (2025-03-00)

- Make `date` filter work on a RFC3339 string as well thanks to @blfpd

## 1.20.0 (2024-05-27)

- Support parenthesis in if statemetns
Expand Down
2 changes: 2 additions & 0 deletions docs/content/docs/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,8 @@ Example:
{{ "2019-09-19T13:18:48.731Z" | date(format="%Y-%m-%d %H:%M", timezone="Asia/Shanghai") }}

{{ 1648252203 | date(timezone="Europe/Berlin") }}

{{ "Thu, 19 09 2019 13:18:48 GMT" | date(timezone="UTC") }}
```

Locale can be specified (excepted when the input is a timestamp without timezone argument), default being POSIX. (only available if the `date-locale` feature is enabled)
Expand Down
43 changes: 40 additions & 3 deletions src/builtins/filters/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn json_encode(value: &Value, args: &HashMap<String, Value>) -> Result<Value
/// Returns a formatted time according to the given `format` argument.
/// `format` defaults to the ISO 8601 `YYYY-MM-DD` format.
///
/// Input can be an i64 timestamp (seconds since epoch) or an RFC3339 string
/// Input can be an i64 timestamp (seconds since epoch) or an RFC3339 or RFC2822 string
/// (default serialization format for `chrono::DateTime`).
///
/// a full reference for the time formatting syntax is available
Expand Down Expand Up @@ -121,7 +121,17 @@ pub fn date(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
}
},
Value::String(s) => {
if s.contains('T') {
if s.chars().filter(|&c| c == ' ').count() >= 3 {
match DateTime::parse_from_rfc2822(s) {
Ok(val) => val.format(&format),
Err(_) => {
return Err(Error::msg(format!(
"Error parsing `{:?}` as rfc2822 date",
s
)));
}
}
} else if s.contains('T') {
match s.parse::<DateTime<FixedOffset>>() {
Ok(val) => match timezone {
Some(timezone) => {
Expand Down Expand Up @@ -183,7 +193,14 @@ pub fn date(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
None => return Err(Error::msg(format!("Filter `date` was invoked on a float: {}", n))),
},
Value::String(s) => {
if s.contains('T') {
if s.chars().filter(|&c| c == ' ').count() >= 3 {
match DateTime::parse_from_rfc2822(s) {
Ok(val) => val.format(&format),
Err(_) => {
return Err(Error::msg(format!("Error parsing `{:?}` as rfc2822 date", s)));
}
}
} else if s.contains('T') {
match s.parse::<DateTime<FixedOffset>>() {
Ok(val) => match timezone {
Some(timezone) => val.with_timezone(&timezone).format(&format),
Expand Down Expand Up @@ -371,6 +388,26 @@ mod tests {
assert_eq!(result.unwrap(), to_value("1996-12-19 -0800").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_rfc2822() {
let args = HashMap::new();
let dt: DateTime<Local> = Local::now();
let result = date(&to_value(dt.to_rfc2822()).unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value(dt.format("%Y-%m-%d").to_string()).unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_rfc2822_preserves_timezone() {
let mut args = HashMap::new();
args.insert("format".to_string(), to_value("%Y-%m-%d %z").unwrap());
let result = date(&to_value("Wed, 18 Feb 2015 23:16:09 GMT").unwrap(), &args);
assert!(result.is_ok());
assert_eq!(result.unwrap(), to_value("2015-02-18 +0000").unwrap());
}

#[cfg(feature = "builtins")]
#[test]
fn date_yyyy_mm_dd() {
Expand Down
Loading