Skip to content

Commit

Permalink
Fix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Jul 10, 2023
1 parent 735f512 commit fb459df
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
43 changes: 39 additions & 4 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ impl DateTime {
pub fn parse_str(str: &str) -> Result<Self, ParseError> {
Self::parse_bytes(str.as_bytes())
}

/// Parse a datetime from bytes using RFC 3339 format
///
/// # Arguments
Expand Down Expand Up @@ -237,6 +236,42 @@ impl DateTime {
/// assert_eq!(dt.to_string(), "2022-01-01T12:13:14Z");
/// ```
pub fn parse_bytes_rfc3339(bytes: &[u8]) -> Result<Self, ParseError> {
DateTime::parse_bytes_rfc3339_with_config(bytes, TimeConfig::default())
}

/// Same as `parse_bytes_rfc3339` with with a `TimeConfig` parameter.
///
/// # Arguments
///
/// * `bytes` - The bytes to parse
/// * `config` - The `TimeConfig` to use
///
/// # Examples
///
/// ```
/// use speedate::{DateTime, Date, Time, TimeConfig};
///
/// let dt = DateTime::parse_bytes_rfc3339_with_config(b"2022-01-01T12:13:14Z", TimeConfig::default()).unwrap();
/// assert_eq!(
/// dt,
/// DateTime {
/// date: Date {
/// year: 2022,
/// month: 1,
/// day: 1,
/// },
/// time: Time {
/// hour: 12,
/// minute: 13,
/// second: 14,
/// microsecond: 0,
/// tz_offset: Some(0),
/// },
/// }
/// );
/// assert_eq!(dt.to_string(), "2022-01-01T12:13:14Z");
/// ```
pub fn parse_bytes_rfc3339_with_config(bytes: &[u8], config: TimeConfig) -> Result<Self, ParseError> {
// First up, parse the full date if we can
let date = Date::parse_bytes_partial(bytes)?;

Expand All @@ -247,7 +282,7 @@ impl DateTime {
}

// Next try to parse the time
let time = Time::parse_bytes_offset(bytes, 11, TimeConfig::default())?;
let time = Time::parse_bytes_offset(bytes, 11, config)?;

Ok(Self { date, time })
}
Expand Down Expand Up @@ -288,8 +323,8 @@ impl DateTime {
/// let dt = DateTime::parse_bytes_with_config(b"2022-01-01T12:13:14Z", TimeConfig::default()).unwrap();
/// assert_eq!(dt.to_string(), "2022-01-01T12:13:14Z");
/// ```
pub fn parse_bytes_with_config(bytes: &[u8], _config: TimeConfig) -> Result<Self, ParseError> {
match Self::parse_bytes_rfc3339(bytes) {
pub fn parse_bytes_with_config(bytes: &[u8], config: TimeConfig) -> Result<Self, ParseError> {
match Self::parse_bytes_rfc3339_with_config(bytes, config) {
Ok(d) => Ok(d),
Err(e) => match float_parse_bytes(bytes) {
IntFloat::Int(int) => Self::from_timestamp(int, 0),
Expand Down
8 changes: 5 additions & 3 deletions src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,10 @@ impl PureTime {
loop {
match bytes.get(offset + length + i) {
Some(c) if c.is_ascii_digit() => {
microsecond *= 10;
microsecond += (c - b'0') as u32;
if i < 6 {
microsecond *= 10;
microsecond += (c - b'0') as u32;
}
}
_ => {
break;
Expand All @@ -491,7 +493,7 @@ impl PureTime {
i += 1;
if i > 6 {
match config.seconds_precision_overflow_behavior {
SecondsPrecisionOverflowBehavior::Truncate => break,
SecondsPrecisionOverflowBehavior::Truncate => continue,
SecondsPrecisionOverflowBehavior::Error => {
return Err(ParseError::SecondFractionTooLong)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ fn test_datetime_parse_truncate_seconds() {
#[test]
fn test_duration_parse_truncate_seconds() {
let time = Duration::parse_bytes_with_config(
"PT0.123456789S".as_bytes(),
"00:00:00.1234567".as_bytes(),
TimeConfig {
seconds_precision_overflow_behavior: SecondsPrecisionOverflowBehavior::Truncate,
},
Expand Down

0 comments on commit fb459df

Please sign in to comment.