Skip to content

Commit 9da1b4c

Browse files
committed
Convert DateTime::from_timestamp_millis to return Result
1 parent cadd62c commit 9da1b4c

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

src/datetime/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -792,24 +792,25 @@ impl DateTime<Utc> {
792792
///
793793
/// # Errors
794794
///
795-
/// Returns `None` on out-of-range number of milliseconds, otherwise returns `Some(DateTime {...})`.
795+
/// Returns [`Error::OutOfRange`] if the timestamp in milliseconds is outside the range of a
796+
/// `DateTime` (more than ca. 262,000 years away from common era).
796797
///
797798
/// # Example
798799
///
799800
/// ```
800801
/// use chrono::DateTime;
801802
///
802-
/// let dt = DateTime::from_timestamp_millis(947638923004).expect("invalid timestamp");
803+
/// let dt = DateTime::from_timestamp_millis(947638923004)?;
803804
///
804805
/// assert_eq!(dt.to_string(), "2000-01-12 01:02:03.004 UTC");
805-
/// assert_eq!(DateTime::from_timestamp_millis(dt.timestamp_millis()).unwrap(), dt);
806+
/// assert_eq!(DateTime::from_timestamp_millis(dt.timestamp_millis())?, dt);
807+
/// # Ok::<(), chrono::Error>(())
806808
/// ```
807809
#[inline]
808-
#[must_use]
809-
pub const fn from_timestamp_millis(millis: i64) -> Option<Self> {
810+
pub const fn from_timestamp_millis(millis: i64) -> Result<Self, Error> {
810811
let secs = millis.div_euclid(1000);
811812
let nsecs = millis.rem_euclid(1000) as u32 * 1_000_000;
812-
ok!(Self::from_timestamp(secs, nsecs))
813+
Self::from_timestamp(secs, nsecs)
813814
}
814815

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

src/datetime/tests.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,14 @@ fn test_datetime_from_timestamp_millis() {
141141

142142
for timestamp_millis in invalid.iter().copied() {
143143
let datetime = DateTime::from_timestamp_millis(timestamp_millis);
144-
assert!(datetime.is_none());
144+
assert_eq!(datetime, Err(Error::OutOfRange));
145145
}
146146

147147
// Test that the result of `from_timestamp_millis` compares equal to
148148
// that of `from_timestamp_opt`.
149149
let secs_test = [0, 1, 2, 1000, 1234, 12345678, -1, -2, -1000, -12345678];
150150
for secs in secs_test.iter().cloned() {
151-
assert_eq!(
152-
DateTime::from_timestamp_millis(secs * 1000),
153-
DateTime::from_timestamp(secs, 0).ok()
154-
);
151+
assert_eq!(DateTime::from_timestamp_millis(secs * 1000), DateTime::from_timestamp(secs, 0));
155152
}
156153
}
157154

src/offset/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ pub trait TimeZone: Sized + Clone {
182182
/// ```
183183
fn timestamp_millis(&self, millis: i64) -> LocalResult<DateTime<Self>> {
184184
match DateTime::from_timestamp_millis(millis) {
185-
Some(dt) => LocalResult::Single(self.from_utc_datetime(&dt.naive_utc())),
186-
None => LocalResult::None,
185+
Ok(dt) => LocalResult::Single(self.from_utc_datetime(&dt.naive_utc())),
186+
Err(_) => LocalResult::None,
187187
}
188188
}
189189

0 commit comments

Comments
 (0)