Skip to content

Commit

Permalink
Conversion between chrono::Duration and core::time::Duration doesn't …
Browse files Browse the repository at this point in the history
…need std
  • Loading branch information
djc committed Aug 29, 2023
1 parent 7eac062 commit ca4c1f2
Showing 1 changed file with 1 addition and 11 deletions.
12 changes: 1 addition & 11 deletions src/oldtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//! Temporal quantification

use core::ops::{Add, Div, Mul, Neg, Sub};
#[cfg(feature = "std")]
use core::time::Duration as StdDuration;
use core::{fmt, i64};
#[cfg(feature = "std")]
Expand Down Expand Up @@ -292,7 +291,6 @@ impl Duration {
///
/// This function errors when original duration is larger than the maximum
/// value supported for this type.
#[cfg(feature = "std")]
pub fn from_std(duration: StdDuration) -> Result<Duration, OutOfRangeError> {
// We need to check secs as u64 before coercing to i64
if duration.as_secs() > MAX.secs as u64 {
Expand All @@ -309,7 +307,6 @@ impl Duration {
///
/// This function errors when duration is less than zero. As standard
/// library implementation is limited to non-negative values.
#[cfg(feature = "std")]
pub fn to_std(&self) -> Result<StdDuration, OutOfRangeError> {
if self.secs < 0 {
return Err(OutOfRangeError(()));
Expand Down Expand Up @@ -443,19 +440,16 @@ impl fmt::Display for Duration {
/// The `std::time::Duration` supports a range from zero to `u64::MAX`
/// *seconds*, while this module supports signed range of up to
/// `i64::MAX` of *milliseconds*.
#[cfg(feature = "std")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct OutOfRangeError(());

#[cfg(feature = "std")]
impl fmt::Display for OutOfRangeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Source duration value is out of range for the target type")
}
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Error for OutOfRangeError {
#[allow(deprecated)]
fn description(&self) -> &str {
Expand Down Expand Up @@ -488,11 +482,9 @@ impl arbitrary::Arbitrary<'_> for Duration {

#[cfg(test)]
mod tests {
#[cfg(feature = "std")]
use super::OutOfRangeError;
use super::{Duration, MAX, MIN};
#[cfg(feature = "std")]
use std::time::Duration as StdDuration;
use core::time::Duration as StdDuration;

#[test]
fn test_duration() {
Expand Down Expand Up @@ -712,7 +704,6 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_to_std() {
assert_eq!(Duration::seconds(1).to_std(), Ok(StdDuration::new(1, 0)));
assert_eq!(Duration::seconds(86401).to_std(), Ok(StdDuration::new(86401, 0)));
Expand All @@ -725,7 +716,6 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_from_std() {
assert_eq!(Ok(Duration::seconds(1)), Duration::from_std(StdDuration::new(1, 0)));
assert_eq!(Ok(Duration::seconds(86401)), Duration::from_std(StdDuration::new(86401, 0)));
Expand Down

0 comments on commit ca4c1f2

Please sign in to comment.