diff --git a/src/format/parsed.rs b/src/format/parsed.rs index 8480d0eb22..392f621b93 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -926,7 +926,7 @@ impl Parsed { /// - `OUT_OF_RANGE` if the offset is out of range for a `FixedOffset`. /// - `NOT_ENOUGH` if the offset field is not set. pub fn to_fixed_offset(&self) -> ParseResult { - FixedOffset::east(self.offset.ok_or(NOT_ENOUGH)?).ok_or(OUT_OF_RANGE) + FixedOffset::east(self.offset.ok_or(NOT_ENOUGH)?).map_err(|_| OUT_OF_RANGE) } /// Returns a parsed timezone-aware date and time out of given fields. @@ -955,7 +955,7 @@ impl Parsed { (None, None) => return Err(NOT_ENOUGH), }; let datetime = self.to_naive_datetime_with_offset(offset)?; - let offset = FixedOffset::east(offset).ok_or(OUT_OF_RANGE)?; + let offset = FixedOffset::east(offset).map_err(|_| OUT_OF_RANGE)?; match offset.from_local_datetime(&datetime) { LocalResult::None => Err(IMPOSSIBLE), diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index ebd953c045..2f16e1ff2c 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -11,7 +11,7 @@ use rkyv::{Archive, Deserialize, Serialize}; use super::{LocalResult, Offset, TimeZone}; use crate::format::{scan, OUT_OF_RANGE}; -use crate::{NaiveDateTime, ParseError}; +use crate::{Error, NaiveDateTime, ParseError}; /// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59. /// @@ -35,7 +35,9 @@ impl FixedOffset { /// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference. /// The negative `secs` means the Western Hemisphere. /// - /// Returns `None` on the out-of-bound `secs`. + /// # Errors + /// + /// Returns [`Error::OutOfRange`] on the out-of-bound `secs`. /// /// # Example /// @@ -49,19 +51,19 @@ impl FixedOffset { /// .unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00") /// ``` - #[must_use] - pub const fn east(secs: i32) -> Option { - if -86_400 < secs && secs < 86_400 { - Some(FixedOffset { local_minus_utc: secs }) - } else { - None + pub const fn east(secs: i32) -> Result { + match -86_400 < secs && secs < 86_400 { + true => Ok(FixedOffset { local_minus_utc: secs }), + false => Err(Error::OutOfRange), } } /// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference. /// The negative `secs` means the Eastern Hemisphere. /// - /// Returns `None` on the out-of-bound `secs`. + /// # Errors + /// + /// Returns [`Error::OutOfRange`] on the out-of-bound `secs`. /// /// # Example /// @@ -75,12 +77,10 @@ impl FixedOffset { /// .unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00") /// ``` - #[must_use] - pub const fn west(secs: i32) -> Option { - if -86_400 < secs && secs < 86_400 { - Some(FixedOffset { local_minus_utc: -secs }) - } else { - None + pub const fn west(secs: i32) -> Result { + match -86_400 < secs && secs < 86_400 { + true => Ok(FixedOffset { local_minus_utc: -secs }), + false => Err(Error::OutOfRange), } } @@ -102,7 +102,7 @@ impl FromStr for FixedOffset { type Err = ParseError; fn from_str(s: &str) -> Result { let (_, offset) = scan::timezone_offset(s, scan::consume_colon_maybe, false, false, true)?; - Self::east(offset).ok_or(OUT_OF_RANGE) + Self::east(offset).map_err(|_| OUT_OF_RANGE) } } @@ -154,9 +154,7 @@ impl fmt::Display for FixedOffset { impl arbitrary::Arbitrary<'_> for FixedOffset { fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result { let secs = u.int_in_range(-86_399..=86_399)?; - let fixed_offset = FixedOffset::east(secs) - .expect("Could not generate a valid chrono::FixedOffset. It looks like implementation of Arbitrary for FixedOffset is erroneous."); - Ok(fixed_offset) + Ok(FixedOffset::east(secs).unwrap()) } } diff --git a/src/offset/local/unix.rs b/src/offset/local/unix.rs index 66aa5f2d39..587b30aa98 100644 --- a/src/offset/local/unix.rs +++ b/src/offset/local/unix.rs @@ -156,8 +156,8 @@ impl Cache { .offset(); return match FixedOffset::east(offset) { - Some(offset) => LocalResult::Single(offset), - None => LocalResult::None, + Ok(offset) => LocalResult::Single(offset), + Err(_) => LocalResult::None, }; } diff --git a/src/offset/local/windows.rs b/src/offset/local/windows.rs index 2842b8bd92..51b5227843 100644 --- a/src/offset/local/windows.rs +++ b/src/offset/local/windows.rs @@ -142,8 +142,8 @@ impl TzInfo { tz_info.assume_init() }; Some(TzInfo { - std_offset: FixedOffset::west((tz_info.Bias + tz_info.StandardBias) * 60)?, - dst_offset: FixedOffset::west((tz_info.Bias + tz_info.DaylightBias) * 60)?, + std_offset: FixedOffset::west((tz_info.Bias + tz_info.StandardBias) * 60).ok()?, + dst_offset: FixedOffset::west((tz_info.Bias + tz_info.DaylightBias) * 60).ok()?, std_transition: system_time_from_naive_date_time(tz_info.StandardDate, year), dst_transition: system_time_from_naive_date_time(tz_info.DaylightDate, year), })