Skip to content

Commit 6c68842

Browse files
committed
Convert DateTime::from_timestamp_micros to return Result
1 parent 9da1b4c commit 6c68842

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

src/datetime/mod.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::offset::Local;
2828
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
2929
#[cfg(any(feature = "clock", feature = "std"))]
3030
use crate::OutOfRange;
31-
use crate::{ok, try_err, try_ok_or};
31+
use crate::{try_err, try_ok_or};
3232
use crate::{Datelike, Error, Months, TimeDelta, Timelike, Weekday};
3333

3434
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
@@ -823,31 +823,29 @@ impl DateTime<Utc> {
823823
///
824824
/// # Errors
825825
///
826-
/// Returns `None` if the number of microseconds would be out of range for a `NaiveDateTime`
827-
/// (more than ca. 262,000 years away from common era)
826+
/// Returns [`Error::OutOfRange`] if the timestamp in microseconds is outside the range of a
827+
/// `DateTime` (more than ca. 262,000 years away from common era).
828828
///
829829
/// # Example
830830
///
831831
/// ```
832832
/// use chrono::DateTime;
833833
///
834834
/// let timestamp_micros: i64 = 1662921288000000; // Sun, 11 Sep 2022 18:34:48 UTC
835-
/// let dt = DateTime::from_timestamp_micros(timestamp_micros);
836-
/// assert!(dt.is_some());
837-
/// assert_eq!(timestamp_micros, dt.expect("invalid timestamp").timestamp_micros());
835+
/// let dt = DateTime::from_timestamp_micros(timestamp_micros)?;
836+
/// assert_eq!(timestamp_micros, dt.timestamp_micros());
838837
///
839838
/// // Negative timestamps (before the UNIX epoch) are supported as well.
840839
/// let timestamp_micros: i64 = -2208936075000000; // Mon, 1 Jan 1900 14:38:45 UTC
841-
/// let dt = DateTime::from_timestamp_micros(timestamp_micros);
842-
/// assert!(dt.is_some());
843-
/// assert_eq!(timestamp_micros, dt.expect("invalid timestamp").timestamp_micros());
840+
/// let dt = DateTime::from_timestamp_micros(timestamp_micros)?;
841+
/// assert_eq!(timestamp_micros, dt.timestamp_micros());
842+
/// # Ok::<(), chrono::Error>(())
844843
/// ```
845844
#[inline]
846-
#[must_use]
847-
pub const fn from_timestamp_micros(micros: i64) -> Option<Self> {
845+
pub const fn from_timestamp_micros(micros: i64) -> Result<Self, Error> {
848846
let secs = micros.div_euclid(1_000_000);
849847
let nsecs = micros.rem_euclid(1_000_000) as u32 * 1000;
850-
ok!(Self::from_timestamp(secs, nsecs))
848+
Self::from_timestamp(secs, nsecs)
851849
}
852850

853851
/// Creates a new [`DateTime<Utc>`] from the number of non-leap microseconds

src/datetime/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn test_datetime_from_timestamp_micros() {
176176

177177
for timestamp_micros in invalid.iter().copied() {
178178
let datetime = DateTime::from_timestamp_micros(timestamp_micros);
179-
assert!(datetime.is_none());
179+
assert_eq!(datetime, Err(Error::OutOfRange));
180180
}
181181

182182
// Test that the result of `TimeZone::timestamp_micros` compares equal to
@@ -185,7 +185,7 @@ fn test_datetime_from_timestamp_micros() {
185185
for secs in secs_test.iter().copied() {
186186
assert_eq!(
187187
DateTime::from_timestamp_micros(secs * 1_000_000),
188-
DateTime::from_timestamp(secs, 0).ok()
188+
DateTime::from_timestamp(secs, 0)
189189
);
190190
}
191191
}

src/offset/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ pub trait TimeZone: Sized + Clone {
215215
/// ```
216216
fn timestamp_micros(&self, micros: i64) -> LocalResult<DateTime<Self>> {
217217
match DateTime::from_timestamp_micros(micros) {
218-
Some(dt) => LocalResult::Single(self.from_utc_datetime(&dt.naive_utc())),
219-
None => LocalResult::None,
218+
Ok(dt) => LocalResult::Single(self.from_utc_datetime(&dt.naive_utc())),
219+
Err(_) => LocalResult::None,
220220
}
221221
}
222222

0 commit comments

Comments
 (0)