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

Add NaiveTime::overflowing_(add|sub)_offset #1310

Merged
merged 3 commits into from
Sep 23, 2023
Merged
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
46 changes: 45 additions & 1 deletion src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use crate::format::{
parse, parse_and_remainder, write_hundreds, Fixed, Item, Numeric, Pad, ParseError, ParseResult,
Parsed, StrftimeItems,
};
use crate::Timelike;
use crate::{expect, try_opt};
use crate::{FixedOffset, Timelike};

#[cfg(feature = "rustc-serialize")]
mod rustc_serialize;
Expand Down Expand Up @@ -754,6 +754,32 @@ impl NaiveTime {
OldDuration::seconds(secs + adjust) + OldDuration::nanoseconds(frac)
}

/// Adds given `FixedOffset` to the current time, and returns the number of days that should be
/// added to a date as a result of the offset (either `-1`, `0`, or `1` because the offset is
/// always less than 24h).
///
/// This method is similar to [`overflowing_add_signed`](#method.overflowing_add_signed), but
/// preserves leap seconds.
pub(super) const fn overflowing_add_offset(&self, offset: FixedOffset) -> (NaiveTime, i32) {
let secs = self.secs as i32 + offset.local_minus_utc();
let days = secs.div_euclid(86_400);
let secs = secs.rem_euclid(86_400);
(NaiveTime { secs: secs as u32, frac: self.frac }, days)
}

/// Subtracts given `FixedOffset` from the current time, and returns the number of days that
/// should be added to a date as a result of the offset (either `-1`, `0`, or `1` because the
/// offset is always less than 24h).
///
/// This method is similar to [`overflowing_sub_signed`](#method.overflowing_sub_signed), but
/// preserves leap seconds.
pub(super) const fn overflowing_sub_offset(&self, offset: FixedOffset) -> (NaiveTime, i32) {
let secs = self.secs as i32 - offset.local_minus_utc();
let days = secs.div_euclid(86_400);
let secs = secs.rem_euclid(86_400);
(NaiveTime { secs: secs as u32, frac: self.frac }, days)
}

/// Formats the time with the specified formatting items.
/// Otherwise it is the same as the ordinary [`format`](#method.format) method.
///
Expand Down Expand Up @@ -1154,6 +1180,15 @@ impl AddAssign<Duration> for NaiveTime {
}
}

impl Add<FixedOffset> for NaiveTime {
type Output = NaiveTime;

#[inline]
fn add(self, rhs: FixedOffset) -> NaiveTime {
self.overflowing_add_offset(rhs).0
}
}

/// A subtraction of `Duration` from `NaiveTime` wraps around and never overflows or underflows.
/// In particular the addition ignores integral number of days.
/// It is the same as the addition with a negated `Duration`.
Expand Down Expand Up @@ -1236,6 +1271,15 @@ impl SubAssign<Duration> for NaiveTime {
}
}

impl Sub<FixedOffset> for NaiveTime {
type Output = NaiveTime;

#[inline]
fn sub(self, rhs: FixedOffset) -> NaiveTime {
self.overflowing_sub_offset(rhs).0
}
}

/// Subtracts another `NaiveTime` from the current time.
/// Returns a `Duration` within +/- 1 day.
/// This does not overflow or underflow at all.
Expand Down
28 changes: 27 additions & 1 deletion src/naive/time/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::NaiveTime;
use crate::duration::Duration as OldDuration;
use crate::Timelike;
use crate::{FixedOffset, Timelike};

#[test]
fn test_time_from_hms_milli() {
Expand Down Expand Up @@ -350,3 +350,29 @@ fn test_time_parse_from_str() {
assert!(NaiveTime::parse_from_str("12:59 PM", "%H:%M %P").is_ok());
assert!(NaiveTime::parse_from_str("12:3456", "%H:%M:%S").is_err());
}

#[test]
fn test_overflowing_offset() {
let hmsm = |h, m, s, n| NaiveTime::from_hms_milli_opt(h, m, s, n).unwrap();

let positive_offset = FixedOffset::east_opt(4 * 60 * 60).unwrap();
// regular time
let t = hmsm(5, 6, 7, 890);
assert_eq!(t.overflowing_add_offset(positive_offset), (hmsm(9, 6, 7, 890), 0));
assert_eq!(t.overflowing_sub_offset(positive_offset), (hmsm(1, 6, 7, 890), 0));
// leap second is preserved, and wrap to next day
let t = hmsm(23, 59, 59, 1_000);
assert_eq!(t.overflowing_add_offset(positive_offset), (hmsm(3, 59, 59, 1_000), 1));
assert_eq!(t.overflowing_sub_offset(positive_offset), (hmsm(19, 59, 59, 1_000), 0));
// wrap to previous day
let t = hmsm(1, 2, 3, 456);
assert_eq!(t.overflowing_sub_offset(positive_offset), (hmsm(21, 2, 3, 456), -1));
// an odd offset
let negative_offset = FixedOffset::west_opt(((2 * 60) + 3) * 60 + 4).unwrap();
let t = hmsm(5, 6, 7, 890);
assert_eq!(t.overflowing_add_offset(negative_offset), (hmsm(3, 3, 3, 890), 0));
assert_eq!(t.overflowing_sub_offset(negative_offset), (hmsm(7, 9, 11, 890), 0));

assert_eq!(t.overflowing_add_offset(positive_offset).0, t + positive_offset);
assert_eq!(t.overflowing_sub_offset(positive_offset).0, t - positive_offset);
}
20 changes: 1 addition & 19 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rkyv::{Archive, Deserialize, Serialize};
use super::{LocalResult, Offset, TimeZone};
use crate::duration::Duration as OldDuration;
use crate::format::{scan, OUT_OF_RANGE};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
use crate::naive::{NaiveDate, NaiveDateTime};
use crate::{DateTime, ParseError, Timelike};

/// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59.
Expand Down Expand Up @@ -199,24 +199,6 @@ where
(lhs + OldDuration::seconds(i64::from(rhs))).with_nanosecond(nanos).unwrap()
}

impl Add<FixedOffset> for NaiveTime {
type Output = NaiveTime;

#[inline]
fn add(self, rhs: FixedOffset) -> NaiveTime {
add_with_leapsecond(&self, rhs.local_minus_utc)
}
}

impl Sub<FixedOffset> for NaiveTime {
type Output = NaiveTime;

#[inline]
fn sub(self, rhs: FixedOffset) -> NaiveTime {
add_with_leapsecond(&self, -rhs.local_minus_utc)
}
}

impl Add<FixedOffset> for NaiveDateTime {
type Output = NaiveDateTime;

Expand Down
Loading