Rename to (round/trunc)_subsecs; add doc tests
This commit is contained in:
parent
74ec7aa5b2
commit
cb3a73aa86
128
src/round.rs
128
src/round.rs
|
@ -15,17 +15,33 @@ pub trait SubSecondRound {
|
|||
/// Return a copy rounded to the specified number of subsecond digits. With
|
||||
/// 9 or more digits, self is returned unmodified. Halfway values are
|
||||
/// rounded up (away from zero).
|
||||
fn round(self, digits: u16) -> Self;
|
||||
///
|
||||
/// # Example
|
||||
/// ``` rust
|
||||
/// # use chrono::{DateTime, SubSecondRound, Timelike, TimeZone, Utc};
|
||||
/// let dt = Utc.ymd(2018, 1, 11).and_hms_milli(12, 0, 0, 154);
|
||||
/// assert_eq!(dt.round_subsecs(2).nanosecond(), 150_000_000);
|
||||
/// assert_eq!(dt.round_subsecs(1).nanosecond(), 200_000_000);
|
||||
/// ```
|
||||
fn round_subsecs(self, digits: u16) -> Self;
|
||||
|
||||
/// Return a copy truncated to the specified number of subsecond
|
||||
/// digits. With 9 or more digits, self is returned unmodified.
|
||||
fn trunc(self, digits: u16) -> Self;
|
||||
///
|
||||
/// # Example
|
||||
/// ``` rust
|
||||
/// # use chrono::{DateTime, SubSecondRound, Timelike, TimeZone, Utc};
|
||||
/// let dt = Utc.ymd(2018, 1, 11).and_hms_milli(12, 0, 0, 154);
|
||||
/// assert_eq!(dt.trunc_subsecs(2).nanosecond(), 150_000_000);
|
||||
/// assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000);
|
||||
/// ```
|
||||
fn trunc_subsecs(self, digits: u16) -> Self;
|
||||
}
|
||||
|
||||
impl<T> SubSecondRound for T
|
||||
where T: Timelike + Add<Duration, Output=T> + Sub<Duration, Output=T>
|
||||
{
|
||||
fn round(self, digits: u16) -> T {
|
||||
fn round_subsecs(self, digits: u16) -> T {
|
||||
let span = span_for_digits(digits);
|
||||
let delta_down = self.nanosecond() % span;
|
||||
if delta_down > 0 {
|
||||
|
@ -40,7 +56,7 @@ where T: Timelike + Add<Duration, Output=T> + Sub<Duration, Output=T>
|
|||
}
|
||||
}
|
||||
|
||||
fn trunc(self, digits: u16) -> T {
|
||||
fn trunc_subsecs(self, digits: u16) -> T {
|
||||
let span = span_for_digits(digits);
|
||||
let delta_down = self.nanosecond() % span;
|
||||
if delta_down > 0 {
|
||||
|
@ -79,42 +95,42 @@ mod tests {
|
|||
let pst = FixedOffset::east(8 * 60 * 60);
|
||||
let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_684);
|
||||
|
||||
assert_eq!(dt.round(10), dt);
|
||||
assert_eq!(dt.round(9), dt);
|
||||
assert_eq!(dt.round(8).nanosecond(), 084_660_680);
|
||||
assert_eq!(dt.round(7).nanosecond(), 084_660_700);
|
||||
assert_eq!(dt.round(6).nanosecond(), 084_661_000);
|
||||
assert_eq!(dt.round(5).nanosecond(), 084_660_000);
|
||||
assert_eq!(dt.round(4).nanosecond(), 084_700_000);
|
||||
assert_eq!(dt.round(3).nanosecond(), 085_000_000);
|
||||
assert_eq!(dt.round(2).nanosecond(), 080_000_000);
|
||||
assert_eq!(dt.round(1).nanosecond(), 100_000_000);
|
||||
assert_eq!(dt.round_subsecs(10), dt);
|
||||
assert_eq!(dt.round_subsecs(9), dt);
|
||||
assert_eq!(dt.round_subsecs(8).nanosecond(), 084_660_680);
|
||||
assert_eq!(dt.round_subsecs(7).nanosecond(), 084_660_700);
|
||||
assert_eq!(dt.round_subsecs(6).nanosecond(), 084_661_000);
|
||||
assert_eq!(dt.round_subsecs(5).nanosecond(), 084_660_000);
|
||||
assert_eq!(dt.round_subsecs(4).nanosecond(), 084_700_000);
|
||||
assert_eq!(dt.round_subsecs(3).nanosecond(), 085_000_000);
|
||||
assert_eq!(dt.round_subsecs(2).nanosecond(), 080_000_000);
|
||||
assert_eq!(dt.round_subsecs(1).nanosecond(), 100_000_000);
|
||||
|
||||
assert_eq!(dt.round(0).nanosecond(), 0);
|
||||
assert_eq!(dt.round(0).second(), 13);
|
||||
assert_eq!(dt.round_subsecs(0).nanosecond(), 0);
|
||||
assert_eq!(dt.round_subsecs(0).second(), 13);
|
||||
|
||||
let dt = Utc.ymd(2018, 1, 11).and_hms_nano(10, 5, 27, 750_500_000);
|
||||
assert_eq!(dt.round(9), dt);
|
||||
assert_eq!(dt.round(4), dt);
|
||||
assert_eq!(dt.round(3).nanosecond(), 751_000_000);
|
||||
assert_eq!(dt.round(2).nanosecond(), 750_000_000);
|
||||
assert_eq!(dt.round(1).nanosecond(), 800_000_000);
|
||||
assert_eq!(dt.round_subsecs(9), dt);
|
||||
assert_eq!(dt.round_subsecs(4), dt);
|
||||
assert_eq!(dt.round_subsecs(3).nanosecond(), 751_000_000);
|
||||
assert_eq!(dt.round_subsecs(2).nanosecond(), 750_000_000);
|
||||
assert_eq!(dt.round_subsecs(1).nanosecond(), 800_000_000);
|
||||
|
||||
assert_eq!(dt.round(0).nanosecond(), 0);
|
||||
assert_eq!(dt.round(0).second(), 28);
|
||||
assert_eq!(dt.round_subsecs(0).nanosecond(), 0);
|
||||
assert_eq!(dt.round_subsecs(0).second(), 28);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_round_leap_nanos() {
|
||||
let dt = Utc.ymd(2016, 12, 31).and_hms_nano(23, 59, 59, 1_750_500_000);
|
||||
assert_eq!(dt.round(9), dt);
|
||||
assert_eq!(dt.round(4), dt);
|
||||
assert_eq!(dt.round(2).nanosecond(), 1_750_000_000);
|
||||
assert_eq!(dt.round(1).nanosecond(), 1_800_000_000);
|
||||
assert_eq!(dt.round(1).second(), 59);
|
||||
assert_eq!(dt.round_subsecs(9), dt);
|
||||
assert_eq!(dt.round_subsecs(4), dt);
|
||||
assert_eq!(dt.round_subsecs(2).nanosecond(), 1_750_000_000);
|
||||
assert_eq!(dt.round_subsecs(1).nanosecond(), 1_800_000_000);
|
||||
assert_eq!(dt.round_subsecs(1).second(), 59);
|
||||
|
||||
assert_eq!(dt.round(0).nanosecond(), 0);
|
||||
assert_eq!(dt.round(0).second(), 0);
|
||||
assert_eq!(dt.round_subsecs(0).nanosecond(), 0);
|
||||
assert_eq!(dt.round_subsecs(0).second(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -122,41 +138,41 @@ mod tests {
|
|||
let pst = FixedOffset::east(8 * 60 * 60);
|
||||
let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_684);
|
||||
|
||||
assert_eq!(dt.trunc(10), dt);
|
||||
assert_eq!(dt.trunc(9), dt);
|
||||
assert_eq!(dt.trunc(8).nanosecond(), 084_660_680);
|
||||
assert_eq!(dt.trunc(7).nanosecond(), 084_660_600);
|
||||
assert_eq!(dt.trunc(6).nanosecond(), 084_660_000);
|
||||
assert_eq!(dt.trunc(5).nanosecond(), 084_660_000);
|
||||
assert_eq!(dt.trunc(4).nanosecond(), 084_600_000);
|
||||
assert_eq!(dt.trunc(3).nanosecond(), 084_000_000);
|
||||
assert_eq!(dt.trunc(2).nanosecond(), 080_000_000);
|
||||
assert_eq!(dt.trunc(1).nanosecond(), 0);
|
||||
assert_eq!(dt.trunc_subsecs(10), dt);
|
||||
assert_eq!(dt.trunc_subsecs(9), dt);
|
||||
assert_eq!(dt.trunc_subsecs(8).nanosecond(), 084_660_680);
|
||||
assert_eq!(dt.trunc_subsecs(7).nanosecond(), 084_660_600);
|
||||
assert_eq!(dt.trunc_subsecs(6).nanosecond(), 084_660_000);
|
||||
assert_eq!(dt.trunc_subsecs(5).nanosecond(), 084_660_000);
|
||||
assert_eq!(dt.trunc_subsecs(4).nanosecond(), 084_600_000);
|
||||
assert_eq!(dt.trunc_subsecs(3).nanosecond(), 084_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(2).nanosecond(), 080_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(1).nanosecond(), 0);
|
||||
|
||||
assert_eq!(dt.trunc(0).nanosecond(), 0);
|
||||
assert_eq!(dt.trunc(0).second(), 13);
|
||||
assert_eq!(dt.trunc_subsecs(0).nanosecond(), 0);
|
||||
assert_eq!(dt.trunc_subsecs(0).second(), 13);
|
||||
|
||||
let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 27, 750_500_000);
|
||||
assert_eq!(dt.trunc(9), dt);
|
||||
assert_eq!(dt.trunc(4), dt);
|
||||
assert_eq!(dt.trunc(3).nanosecond(), 750_000_000);
|
||||
assert_eq!(dt.trunc(2).nanosecond(), 750_000_000);
|
||||
assert_eq!(dt.trunc(1).nanosecond(), 700_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(9), dt);
|
||||
assert_eq!(dt.trunc_subsecs(4), dt);
|
||||
assert_eq!(dt.trunc_subsecs(3).nanosecond(), 750_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(2).nanosecond(), 750_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(1).nanosecond(), 700_000_000);
|
||||
|
||||
assert_eq!(dt.trunc(0).nanosecond(), 0);
|
||||
assert_eq!(dt.trunc(0).second(), 27);
|
||||
assert_eq!(dt.trunc_subsecs(0).nanosecond(), 0);
|
||||
assert_eq!(dt.trunc_subsecs(0).second(), 27);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_trunc_leap_nanos() {
|
||||
let dt = Utc.ymd(2016, 12, 31).and_hms_nano(23, 59, 59, 1_750_500_000);
|
||||
assert_eq!(dt.trunc(9), dt);
|
||||
assert_eq!(dt.trunc(4), dt);
|
||||
assert_eq!(dt.trunc(2).nanosecond(), 1_750_000_000);
|
||||
assert_eq!(dt.trunc(1).nanosecond(), 1_700_000_000);
|
||||
assert_eq!(dt.trunc(1).second(), 59);
|
||||
assert_eq!(dt.trunc_subsecs(9), dt);
|
||||
assert_eq!(dt.trunc_subsecs(4), dt);
|
||||
assert_eq!(dt.trunc_subsecs(2).nanosecond(), 1_750_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(1).nanosecond(), 1_700_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(1).second(), 59);
|
||||
|
||||
assert_eq!(dt.trunc(0).nanosecond(), 1_000_000_000);
|
||||
assert_eq!(dt.trunc(0).second(), 59);
|
||||
assert_eq!(dt.trunc_subsecs(0).nanosecond(), 1_000_000_000);
|
||||
assert_eq!(dt.trunc_subsecs(0).second(), 59);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue