Skip to content

Commit

Permalink
Simplify / fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Jul 12, 2023
1 parent d39415f commit 3f61f4c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
36 changes: 7 additions & 29 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,16 +279,17 @@ impl Time {
minute: ((second % 3600) / 60) as u8,
second: (second % 60) as u8,
microsecond,
tz_offset: match config.default_time_offset {
DefaultTimeOffset::Naive => None,
DefaultTimeOffset::Utc => Some(0),
},
tz_offset: config.unix_timestamp_offset,
})
}

/// Parse a time from bytes with a starting index, extra characters at the end of the string result in an error
pub(crate) fn parse_bytes_offset(bytes: &[u8], offset: usize, config: &TimeConfig) -> Result<Self, ParseError> {
let pure_time = PureTime::parse(bytes, offset, config)?;
let config = TimeConfig {
unix_timestamp_offset: None,
..config.clone()
};
let pure_time = PureTime::parse(bytes, offset, &config)?;

// Parse the timezone offset
let mut tz_offset: Option<i32> = None;
Expand Down Expand Up @@ -349,11 +350,6 @@ impl Time {
}
}

let tz_offset = match (tz_offset, config.default_time_offset) {
(None, DefaultTimeOffset::Utc) => Some(0),
_ => tz_offset,
};

if bytes.len() > position {
return Err(ParseError::ExtraCharacters);
}
Expand Down Expand Up @@ -581,26 +577,8 @@ impl TryFrom<&str> for MicrosecondsPrecisionOverflowBehavior {
}
}

#[derive(Debug, Clone, Default, Copy)]
pub enum DefaultTimeOffset {
#[default]
Naive,
Utc,
}

impl TryFrom<&str> for DefaultTimeOffset {
type Error = ConfigError;
fn try_from(value: &str) -> Result<Self, ConfigError> {
match value.to_lowercase().as_str() {
"naive" => Ok(Self::Naive),
"utc" => Ok(Self::Utc),
_ => Err(ConfigError::UnknownTimestampDefaultOffsetString),
}
}
}

#[derive(Debug, Clone, Default)]
pub struct TimeConfig {
pub microseconds_precision_overflow_behavior: MicrosecondsPrecisionOverflowBehavior,
pub default_time_offset: DefaultTimeOffset,
pub unix_timestamp_offset: Option<i32>,
}
47 changes: 37 additions & 10 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,37 +1286,37 @@ fn test_duration_parse_truncate_seconds() {
}

#[test]
fn test_time_parse_default_offset() {
fn test_time_parse_bytes_does_not_add_offset_for_rfc3339() {
let time = Time::parse_bytes_with_config(
"12:13:12".as_bytes(),
&TimeConfig {
default_time_offset: "utc".try_into().unwrap(),
unix_timestamp_offset: Some(0),
..Default::default()
},
)
.unwrap();
assert_eq!(time.to_string(), "12:13:12Z");
assert_eq!(time.to_string(), "12:13:12");
}

#[test]
fn test_datetime_parse_default_offset() {
fn test_datetime_parse_bytes_does_not_add_offset_for_rfc3339() {
let time = DateTime::parse_bytes_with_config(
"2020-01-01T12:13:12".as_bytes(),
&TimeConfig {
default_time_offset: "utc".try_into().unwrap(),
unix_timestamp_offset: Some(0),
..Default::default()
},
)
.unwrap();
assert_eq!(time.to_string(), "2020-01-01T12:13:12Z");
assert_eq!(time.to_string(), "2020-01-01T12:13:12");
}

#[test]
fn test_datetime_from_timestamp_with_default_offset() {
fn test_datetime_pare_unix_timestamp_from_bytes_with_utc_offset() {
let time = DateTime::parse_bytes_with_config(
"1689102037.5586429".as_bytes(),
&TimeConfig {
default_time_offset: "utc".try_into().unwrap(),
unix_timestamp_offset: Some(0),
..Default::default()
},
)
Expand All @@ -1325,15 +1325,42 @@ fn test_datetime_from_timestamp_with_default_offset() {
}

#[test]
fn test_time_from_timestamp_default_offset() {
fn test_datetime_pare_unix_timestamp_from_bytes_as_naive() {
let time = DateTime::parse_bytes_with_config(
"1689102037.5586429".as_bytes(),
&TimeConfig {
unix_timestamp_offset: None,
..Default::default()
},
)
.unwrap();
assert_eq!(time.to_string(), "2023-07-11T19:00:37.558643");
}

#[test]
fn test_time_parse_unix_timestamp_from_bytes_with_utc_offset() {
let time = Time::from_timestamp_with_config(
1,
2,
&TimeConfig {
default_time_offset: "utc".try_into().unwrap(),
unix_timestamp_offset: Some(0),
..Default::default()
},
)
.unwrap();
assert_eq!(time.to_string(), "00:00:01.000002Z");
}

#[test]
fn test_time_parse_unix_timestamp_from_bytes_as_naive() {
let time = Time::from_timestamp_with_config(
1,
2,
&TimeConfig {
unix_timestamp_offset: None,
..Default::default()
},
)
.unwrap();
assert_eq!(time.to_string(), "00:00:01.000002");
}

0 comments on commit 3f61f4c

Please sign in to comment.