Skip to content

Commit

Permalink
Convert FixedOffset::{east, west} to return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Feb 28, 2024
1 parent f0e6695 commit 68d9e3e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
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)

Check warning on line 929 in src/format/parsed.rs

View check run for this annotation

Codecov / codecov/patch

src/format/parsed.rs#L929

Added line #L929 was not covered by tests
}

/// Returns a parsed timezone-aware date and time out of given fields.
Expand Down Expand Up @@ -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),
Expand Down
36 changes: 17 additions & 19 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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
///
Expand All @@ -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<FixedOffset> {
if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: secs })
} else {
None
pub const fn east(secs: i32) -> Result<FixedOffset, Error> {
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
///
Expand All @@ -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<FixedOffset> {
if -86_400 < secs && secs < 86_400 {
Some(FixedOffset { local_minus_utc: -secs })
} else {
None
pub const fn west(secs: i32) -> Result<FixedOffset, Error> {
match -86_400 < secs && secs < 86_400 {
true => Ok(FixedOffset { local_minus_utc: -secs }),
false => Err(Error::OutOfRange),

Check warning on line 83 in src/offset/fixed.rs

View check run for this annotation

Codecov / codecov/patch

src/offset/fixed.rs#L83

Added line #L83 was not covered by tests
}
}

Expand All @@ -102,7 +102,7 @@ impl FromStr for FixedOffset {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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)
}
}

Expand Down Expand Up @@ -154,9 +154,7 @@ impl fmt::Display for FixedOffset {
impl arbitrary::Arbitrary<'_> for FixedOffset {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<FixedOffset> {
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())

Check warning on line 157 in src/offset/fixed.rs

View check run for this annotation

Codecov / codecov/patch

src/offset/fixed.rs#L157

Added line #L157 was not covered by tests
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/offset/local/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check warning on line 160 in src/offset/local/unix.rs

View check run for this annotation

Codecov / codecov/patch

src/offset/local/unix.rs#L160

Added line #L160 was not covered by tests
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/offset/local/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
})
Expand Down

0 comments on commit 68d9e3e

Please sign in to comment.