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(core): Add support for unix timestamps in datetime parsing #577

Merged
merged 1 commit into from
Feb 4, 2025
Merged
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
17 changes: 17 additions & 0 deletions crates/core/src/model/scalar/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ fn parse_date_time(s: &str) -> Option<DateTimeImpl> {
None
} else if let "now" | "today" = s.to_lowercase().trim() {
Some(DateTimeImpl::now_utc())
} else if s.parse::<i64>().is_ok() {
DateTimeImpl::parse(s, format_description!("[unix_timestamp]")).ok()
} else {
let offset_re = Regex::new(r"[+-][01][0-9]{3}$").unwrap();

Expand Down Expand Up @@ -375,6 +377,21 @@ mod test {
assert!(actual.unwrap().unix_timestamp() == 1455616800);
}

#[test]
fn parse_date_time_unix_timestamp_format() {
let input = "0"; // epoch
let actual = parse_date_time(input);
assert!(actual.unwrap().unix_timestamp() == 0);

let input = "1455616800"; // positive
let actual = parse_date_time(input);
assert!(actual.unwrap().unix_timestamp() == 1455616800);

let input = "-1455616800"; // negative
let actual = parse_date_time(input);
assert!(actual.unwrap().unix_timestamp() == -1455616800);
}

#[test]
fn parse_date_time_to_string() {
let date = DateTime::now();
Expand Down
Loading