parent
08b7e0bc6b
commit
449d7277b9
|
@ -409,7 +409,7 @@ pub use date::{Date, MIN_DATE, MAX_DATE};
|
||||||
pub use datetime::{DateTime, SecondsFormat};
|
pub use datetime::{DateTime, SecondsFormat};
|
||||||
#[cfg(feature = "rustc-serialize")] pub use datetime::rustc_serialize::TsSeconds;
|
#[cfg(feature = "rustc-serialize")] pub use datetime::rustc_serialize::TsSeconds;
|
||||||
pub use format::{ParseError, ParseResult};
|
pub use format::{ParseError, ParseResult};
|
||||||
pub use round::SubSecondRound;
|
pub use round::SubsecRound;
|
||||||
|
|
||||||
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
|
/// A convenience module appropriate for glob imports (`use chrono::prelude::*;`).
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
|
@ -419,7 +419,7 @@ pub mod prelude {
|
||||||
#[doc(no_inline)] pub use {NaiveDate, NaiveTime, NaiveDateTime};
|
#[doc(no_inline)] pub use {NaiveDate, NaiveTime, NaiveDateTime};
|
||||||
#[doc(no_inline)] pub use Date;
|
#[doc(no_inline)] pub use Date;
|
||||||
#[doc(no_inline)] pub use {DateTime, SecondsFormat};
|
#[doc(no_inline)] pub use {DateTime, SecondsFormat};
|
||||||
#[doc(no_inline)] pub use SubSecondRound;
|
#[doc(no_inline)] pub use SubsecRound;
|
||||||
}
|
}
|
||||||
|
|
||||||
// useful throughout the codebase
|
// useful throughout the codebase
|
||||||
|
|
10
src/round.rs
10
src/round.rs
|
@ -11,14 +11,14 @@ use oldtime::Duration;
|
||||||
/// behavior in Chrono display formatting. Either can be used to guarantee
|
/// behavior in Chrono display formatting. Either can be used to guarantee
|
||||||
/// equality (e.g. for testing) when round-tripping through a lower precision
|
/// equality (e.g. for testing) when round-tripping through a lower precision
|
||||||
/// format.
|
/// format.
|
||||||
pub trait SubSecondRound {
|
pub trait SubsecRound {
|
||||||
/// Return a copy rounded to the specified number of subsecond digits. With
|
/// Return a copy rounded to the specified number of subsecond digits. With
|
||||||
/// 9 or more digits, self is returned unmodified. Halfway values are
|
/// 9 or more digits, self is returned unmodified. Halfway values are
|
||||||
/// rounded up (away from zero).
|
/// rounded up (away from zero).
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ``` rust
|
/// ``` rust
|
||||||
/// # use chrono::{DateTime, SubSecondRound, Timelike, TimeZone, Utc};
|
/// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, Utc};
|
||||||
/// let dt = Utc.ymd(2018, 1, 11).and_hms_milli(12, 0, 0, 154);
|
/// 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(2).nanosecond(), 150_000_000);
|
||||||
/// assert_eq!(dt.round_subsecs(1).nanosecond(), 200_000_000);
|
/// assert_eq!(dt.round_subsecs(1).nanosecond(), 200_000_000);
|
||||||
|
@ -30,7 +30,7 @@ pub trait SubSecondRound {
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ``` rust
|
/// ``` rust
|
||||||
/// # use chrono::{DateTime, SubSecondRound, Timelike, TimeZone, Utc};
|
/// # use chrono::{DateTime, SubsecRound, Timelike, TimeZone, Utc};
|
||||||
/// let dt = Utc.ymd(2018, 1, 11).and_hms_milli(12, 0, 0, 154);
|
/// 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(2).nanosecond(), 150_000_000);
|
||||||
/// assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000);
|
/// assert_eq!(dt.trunc_subsecs(1).nanosecond(), 100_000_000);
|
||||||
|
@ -38,7 +38,7 @@ pub trait SubSecondRound {
|
||||||
fn trunc_subsecs(self, digits: u16) -> Self;
|
fn trunc_subsecs(self, digits: u16) -> Self;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SubSecondRound for T
|
impl<T> SubsecRound for T
|
||||||
where T: Timelike + Add<Duration, Output=T> + Sub<Duration, Output=T>
|
where T: Timelike + Add<Duration, Output=T> + Sub<Duration, Output=T>
|
||||||
{
|
{
|
||||||
fn round_subsecs(self, digits: u16) -> T {
|
fn round_subsecs(self, digits: u16) -> T {
|
||||||
|
@ -88,7 +88,7 @@ fn span_for_digits(digits: u16) -> u32 {
|
||||||
mod tests {
|
mod tests {
|
||||||
use Timelike;
|
use Timelike;
|
||||||
use offset::{FixedOffset, TimeZone, Utc};
|
use offset::{FixedOffset, TimeZone, Utc};
|
||||||
use super::SubSecondRound;
|
use super::SubsecRound;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_round() {
|
fn test_round() {
|
||||||
|
|
Loading…
Reference in New Issue