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

Parse datetime with T separator and single digit offset #103

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
42 changes: 31 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ mod format {
pub const YYYYMMDDHHMM_UTC_OFFSET: &str = "%Y%m%d%H%MUTC%z";
pub const YYYYMMDDHHMM_ZULU_OFFSET: &str = "%Y%m%d%H%MZ%z";
pub const YYYYMMDDHHMM_HYPHENATED_OFFSET: &str = "%Y-%m-%d %H:%M %z";
pub const YYYYMMDDHHMMSS_HYPHENATED_OFFSET: &str = "%Y-%m-%d %H:%M:%S %#z";
pub const YYYYMMDDHHMMSS_T_SEP_HYPHENATED_OFFSET: &str = "%Y-%m-%dT%H:%M:%S%#z";
pub const YYYYMMDDHHMMS_T_SEP: &str = "%Y-%m-%dT%H:%M:%S";
pub const UTC_OFFSET: &str = "UTC%#z";
pub const ZULU_OFFSET: &str = "Z%#z";
Expand Down Expand Up @@ -154,9 +156,17 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
// similar

// Formats with offsets don't require NaiveDateTime workaround
//
// HACK: if the string ends with a single digit preceded by a + or -
// sign, then insert a 0 between the sign and the digit to make it
// possible for `chrono` to parse it.
let pattern = Regex::new(r"([\+-])(\d)$").unwrap();
let s = pattern.replace(s.as_ref(), "${1}0${2}");
for fmt in [
format::YYYYMMDDHHMM_OFFSET,
format::YYYYMMDDHHMM_HYPHENATED_OFFSET,
format::YYYYMMDDHHMMSS_HYPHENATED_OFFSET,
format::YYYYMMDDHHMMSS_T_SEP_HYPHENATED_OFFSET,
format::YYYYMMDDHHMM_UTC_OFFSET,
format::YYYYMMDDHHMM_ZULU_OFFSET,
] {
Expand Down Expand Up @@ -230,16 +240,7 @@ pub fn parse_datetime_at_date<S: AsRef<str> + Clone>(
// offsets, so instead we replicate parse_date behaviour by getting
// the current date with local, and create a date time string at midnight,
// before trying offset suffixes
//
// HACK: if the string ends with a single digit preceded by a + or -
// sign, then insert a 0 between the sign and the digit to make it
// possible for `chrono` to parse it.
let pattern = Regex::new(r"([\+-])(\d)$").unwrap();
let ts = format!(
"{}0000{}",
date.format("%Y%m%d"),
pattern.replace(s.as_ref(), "${1}0${2}")
);
let ts = format!("{}0000{}", date.format("%Y%m%d"), s);
for fmt in [
format::UTC_OFFSET,
format::ZULU_OFFSET,
Expand Down Expand Up @@ -322,6 +323,14 @@ mod tests {
assert_eq!(actual.unwrap().timestamp(), TEST_TIME);
}

#[test]
fn test_t_sep_single_digit_offset_no_space() {
env::set_var("TZ", "UTC");
let dt = "2021-02-14T22:37:47-8";
let actual = parse_datetime(dt);
assert_eq!(actual.unwrap().timestamp(), TEST_TIME);
}

#[test]
fn invalid_formats() {
let invalid_dts = vec!["NotADate", "202104", "202104-12T22:37:47"];
Expand Down Expand Up @@ -370,7 +379,7 @@ mod tests {

#[cfg(test)]
mod offsets {
use chrono::Local;
use chrono::{Local, NaiveDate};

use crate::parse_datetime;
use crate::ParseDateTimeError;
Expand Down Expand Up @@ -413,6 +422,17 @@ mod tests {
Err(ParseDateTimeError::InvalidInput)
);
}

#[test]
fn test_datetime_with_offset() {
let actual = parse_datetime("1997-01-19 08:17:48 +0").unwrap();
let expected = NaiveDate::from_ymd_opt(1997, 1, 19)
.unwrap()
.and_hms_opt(8, 17, 48)
.unwrap()
.and_utc();
assert_eq!(actual, expected);
}
}

#[cfg(test)]
Expand Down
Loading