Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement operations for core::time::Duration on DateTime types #1229

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Changes from 1 commit
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
Next Next commit
Conversion between chrono::Duration and core::time::Duration doesn't …
…need std
  • Loading branch information
djc committed Aug 29, 2023
commit 45353ce9542ac52f0ec63812711440635d22a821
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