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 FixedOffset::{east, west} to return Result #1468

Merged
merged 1 commit into from
Feb 28, 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
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 @@
/// - `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 @@
(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 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 @@
/// 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 @@
/// .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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I appreciate the conversion to match, in the future I would appreciate it more in a separate commit. 👍

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 @@
/// .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 @@
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 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the unwrapping be prevented by mapping the error? Untested: FixedOffset::east(secs).or(Err(arbitrary::Error))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The range we supply to arbitrary::Unstructured::int_in_range is exactly equal to the range FixedOffset supports, and I don't see that changing. This should simply never fail, so I even considered the expect to be overkill.

}
}

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 @@
.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
Loading