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

Convert DateTime::with_nanosecond to return Result #1520

Merged
merged 2 commits into from
Mar 18, 2024
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
15 changes: 9 additions & 6 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,18 +748,21 @@ impl<Tz: TimeZone> DateTime<Tz> {

/// Makes a new `DateTime` with nanoseconds since the whole non-leap second changed.
///
/// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// As with the [`NaiveDateTime::nanosecond`] method,
/// the input range can exceed 1,000,000,000 for leap seconds.
/// As with the [`DateTime::nanosecond`] method, the input range can exceed 1,000,000,000 for
/// leap seconds.
///
/// See also the [`NaiveTime::with_nanosecond`] method.
///
/// # Errors
///
/// Returns `None` if `nanosecond >= 2,000,000,000`.
/// Returns [`Error::InvalidArgument`] if `nanosecond >= 2,000,000,000`.
#[inline]
pub fn with_nanosecond(&self, nano: u32) -> Option<DateTime<Tz>> {
map_local(self, |datetime| datetime.with_nanosecond(nano).ok())
pub fn with_nanosecond(&self, nano: u32) -> Result<DateTime<Tz>, Error> {
// `with_nanosecond` does not need to recalculate the offset like the other `with_` methods.
// Like the IANA time zone database and other libraries we assume in chrono that offsets
// have second granularity, and time zone transitions (such as DTS) happen on second
// boundaries.
Ok(Self { datetime: self.datetime.with_nanosecond(nano)?, offset: self.offset.clone() })
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ fn test_min_max_setters() {
assert_eq!(beyond_min.with_hour(5), None);
assert_eq!(beyond_min.with_minute(0), Some(beyond_min));
assert_eq!(beyond_min.with_second(0), Some(beyond_min));
assert_eq!(beyond_min.with_nanosecond(0), Some(beyond_min));
assert_eq!(beyond_min.with_nanosecond(0), Ok(beyond_min));

assert_eq!(beyond_max.with_year(2020).unwrap().year(), 2020);
assert_eq!(beyond_max.with_year(beyond_max.year()), Some(beyond_max));
Expand All @@ -1547,7 +1547,7 @@ fn test_min_max_setters() {
assert_eq!(beyond_max.with_hour(5), None);
assert_eq!(beyond_max.with_minute(beyond_max.minute()), Some(beyond_max));
assert_eq!(beyond_max.with_second(beyond_max.second()), Some(beyond_max));
assert_eq!(beyond_max.with_nanosecond(beyond_max.nanosecond()), Some(beyond_max));
assert_eq!(beyond_max.with_nanosecond(beyond_max.nanosecond()), Ok(beyond_max));
}

#[test]
Expand Down
5 changes: 2 additions & 3 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,9 +1005,8 @@ impl NaiveDateTime {

/// Makes a new `NaiveDateTime` with nanoseconds since the whole non-leap second changed.
///
/// Returns `None` when the resulting `NaiveDateTime` would be invalid.
/// As with the [`NaiveDateTime::nanosecond`] method,
/// the input range can exceed 1,000,000,000 for leap seconds.
/// As with the [`NaiveDateTime::nanosecond`] method, the input range can exceed 1,000,000,000
/// for leap seconds.
///
/// See also the [`NaiveTime::with_nanosecond`] method.
///
Expand Down
Loading