Skip to content
Closed
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: 2 additions & 2 deletions src/format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ fn test_rfc2822_comments() {
("( x ( x ) x ( x ) x )", Ok("")),
];

for (test_in, expected) in testdata {
for (test_in, expected) in &testdata {
let actual = comment_2822(test_in).map(|(s, _)| s);
assert_eq!(
expected, actual,
expected, &actual,
"{:?} expected to produce {:?}, but produced {:?}.",
test_in, expected, actual
);
Expand Down
40 changes: 23 additions & 17 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use num_integer::div_mod_floor;
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

use self::u31::U31;
#[cfg(any(feature = "alloc", feature = "std", test))]
use crate::format::DelayedFormat;
use crate::format::{parse, ParseError, ParseResult, Parsed, StrftimeItems};
Expand All @@ -24,6 +25,8 @@ mod serde;
#[cfg(test)]
mod tests;

mod u31;

/// ISO 8601 time without timezone.
/// Allows for the nanosecond precision and optional leap second representation.
///
Expand Down Expand Up @@ -187,7 +190,7 @@ mod tests;
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
pub struct NaiveTime {
secs: u32,
frac: u32,
frac: U31,
}

impl NaiveTime {
Expand Down Expand Up @@ -392,7 +395,8 @@ impl NaiveTime {
return None;
}
let secs = hour * 3600 + min * 60 + sec;
Some(NaiveTime { secs, frac: nano })
let frac = unsafe { U31::new_unchecked(nano) };
Some(NaiveTime { secs, frac })
}

/// Makes a new `NaiveTime` from the number of seconds since midnight and nanosecond.
Expand Down Expand Up @@ -443,7 +447,8 @@ impl NaiveTime {
if secs >= 86_400 || nano >= 2_000_000_000 {
return None;
}
Some(NaiveTime { secs, frac: nano })
let frac = unsafe { U31::new_unchecked(nano) };
Some(NaiveTime { secs, frac })
}

/// Parses a string with the specified format string and returns a new `NaiveTime`.
Expand Down Expand Up @@ -534,7 +539,7 @@ impl NaiveTime {
/// ```
pub fn overflowing_add_signed(&self, mut rhs: TimeDelta) -> (NaiveTime, i64) {
let mut secs = self.secs;
let mut frac = self.frac;
let mut frac = self.frac.get();

// check if `self` is a leap second and adding `rhs` would escape that leap second.
// if it's the case, update `self` and `rhs` to involve no leap second;
Expand All @@ -551,6 +556,7 @@ impl NaiveTime {
} else {
frac = (i64::from(frac) + rhs.num_nanoseconds().unwrap()) as u32;
debug_assert!(frac < 2_000_000_000);
let frac = unsafe { U31::new_unchecked(frac) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like some comments here that clearly document how/where the invariant is upheld in release builds.

Copy link
Contributor Author

@Kijewski Kijewski Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed chrono is well enough tested, so I can assume the invariant to hold.

If I read the code correctly it is tested that frac < 1_000_000_000 and rhs.num_nanoseconds() cannot return a value >= 1e9. Is the second assumption true? Then I this as a // SAFETY comment.

return (NaiveTime { secs, frac }, 0);
}
}
Expand Down Expand Up @@ -592,7 +598,8 @@ impl NaiveTime {
}
debug_assert!((0..86_400).contains(&secs));

(NaiveTime { secs: secs as u32, frac: frac as u32 }, morerhssecs)
let frac = unsafe { U31::new_unchecked(frac as u32) };
(NaiveTime { secs: secs as u32, frac }, morerhssecs)
}

/// Subtracts given `TimeDelta` from the current time,
Expand Down Expand Up @@ -688,20 +695,20 @@ impl NaiveTime {
use core::cmp::Ordering;

let secs = i64::from(self.secs) - i64::from(rhs.secs);
let frac = i64::from(self.frac) - i64::from(rhs.frac);
let frac = i64::from(self.frac.get()) - i64::from(rhs.frac.get());

// `secs` may contain a leap second yet to be counted
let adjust = match self.secs.cmp(&rhs.secs) {
Ordering::Greater => {
if rhs.frac >= 1_000_000_000 {
if rhs.frac.get() >= 1_000_000_000 {
1
} else {
0
}
}
Ordering::Equal => 0,
Ordering::Less => {
if self.frac >= 1_000_000_000 {
if self.frac.get() >= 1_000_000_000 {
-1
} else {
0
Expand Down Expand Up @@ -798,8 +805,8 @@ impl NaiveTime {
(hour, min, sec)
}

pub(super) const MIN: Self = Self { secs: 0, frac: 0 };
pub(super) const MAX: Self = Self { secs: 23 * 3600 + 59 * 60 + 59, frac: 999_999_999 };
pub(super) const MIN: Self = Self { secs: 0, frac: U31::ZERO };
pub(super) const MAX: Self = Self { secs: 23 * 3600 + 59 * 60 + 59, frac: U31::SEC_MINUS_1 };
}

impl Timelike for NaiveTime {
Expand Down Expand Up @@ -884,7 +891,7 @@ impl Timelike for NaiveTime {
/// ```
#[inline]
fn nanosecond(&self) -> u32 {
self.frac
self.frac.get()
}

/// Makes a new `NaiveTime` with the hour number changed.
Expand Down Expand Up @@ -988,7 +995,8 @@ impl Timelike for NaiveTime {
if nano >= 2_000_000_000 {
return None;
}
Some(NaiveTime { frac: nano, ..*self })
let frac = unsafe { U31::new_unchecked(nano) };
Some(NaiveTime { frac, ..*self })
}

/// Returns the number of non-leap seconds past the last midnight.
Expand Down Expand Up @@ -1222,11 +1230,9 @@ impl Sub<NaiveTime> for NaiveTime {
impl fmt::Debug for NaiveTime {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (hour, min, sec) = self.hms();
let (sec, nano) = if self.frac >= 1_000_000_000 {
(sec + 1, self.frac - 1_000_000_000)
} else {
(sec, self.frac)
};
let frac = self.frac.get();
let (sec, nano) =
if frac >= 1_000_000_000 { (sec + 1, frac - 1_000_000_000) } else { (sec, frac) };

write!(f, "{:02}:{:02}:{:02}", hour, min, sec)?;
if nano == 0 {
Expand Down
Loading