Skip to content

Commit 999582e

Browse files
committed
Convert FixedOffset::{east, west} to return Result
1 parent 6fd71d3 commit 999582e

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

src/format/parsed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ impl Parsed {
926926
/// - `OUT_OF_RANGE` if the offset is out of range for a `FixedOffset`.
927927
/// - `NOT_ENOUGH` if the offset field is not set.
928928
pub fn to_fixed_offset(&self) -> ParseResult<FixedOffset> {
929-
FixedOffset::east(self.offset.ok_or(NOT_ENOUGH)?).ok_or(OUT_OF_RANGE)
929+
FixedOffset::east(self.offset.ok_or(NOT_ENOUGH)?).map_err(|_| OUT_OF_RANGE)
930930
}
931931

932932
/// Returns a parsed timezone-aware date and time out of given fields.
@@ -955,7 +955,7 @@ impl Parsed {
955955
(None, None) => return Err(NOT_ENOUGH),
956956
};
957957
let datetime = self.to_naive_datetime_with_offset(offset)?;
958-
let offset = FixedOffset::east(offset).ok_or(OUT_OF_RANGE)?;
958+
let offset = FixedOffset::east(offset).map_err(|_| OUT_OF_RANGE)?;
959959

960960
match offset.from_local_datetime(&datetime) {
961961
LocalResult::None => Err(IMPOSSIBLE),

src/offset/fixed.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rkyv::{Archive, Deserialize, Serialize};
1111

1212
use super::{LocalResult, Offset, TimeZone};
1313
use crate::format::{scan, OUT_OF_RANGE};
14-
use crate::{NaiveDateTime, ParseError};
14+
use crate::{Error, NaiveDateTime, ParseError};
1515

1616
/// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59.
1717
///
@@ -35,7 +35,9 @@ impl FixedOffset {
3535
/// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference.
3636
/// The negative `secs` means the Western Hemisphere.
3737
///
38-
/// Returns `None` on the out-of-bound `secs`.
38+
/// # Errors
39+
///
40+
/// Returns [`Error::OutOfRange`] on the out-of-bound `secs`.
3941
///
4042
/// # Example
4143
///
@@ -49,19 +51,19 @@ impl FixedOffset {
4951
/// .unwrap();
5052
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00")
5153
/// ```
52-
#[must_use]
53-
pub const fn east(secs: i32) -> Option<FixedOffset> {
54-
if -86_400 < secs && secs < 86_400 {
55-
Some(FixedOffset { local_minus_utc: secs })
56-
} else {
57-
None
54+
pub const fn east(secs: i32) -> Result<FixedOffset, Error> {
55+
match -86_400 < secs && secs < 86_400 {
56+
true => Ok(FixedOffset { local_minus_utc: secs }),
57+
false => Err(Error::OutOfRange),
5858
}
5959
}
6060

6161
/// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference.
6262
/// The negative `secs` means the Eastern Hemisphere.
6363
///
64-
/// Returns `None` on the out-of-bound `secs`.
64+
/// # Errors
65+
///
66+
/// Returns [`Error::OutOfRange`] on the out-of-bound `secs`.
6567
///
6668
/// # Example
6769
///
@@ -75,12 +77,10 @@ impl FixedOffset {
7577
/// .unwrap();
7678
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00")
7779
/// ```
78-
#[must_use]
79-
pub const fn west(secs: i32) -> Option<FixedOffset> {
80-
if -86_400 < secs && secs < 86_400 {
81-
Some(FixedOffset { local_minus_utc: -secs })
82-
} else {
83-
None
80+
pub const fn west(secs: i32) -> Result<FixedOffset, Error> {
81+
match -86_400 < secs && secs < 86_400 {
82+
true => Ok(FixedOffset { local_minus_utc: -secs }),
83+
false => Err(Error::OutOfRange),
8484
}
8585
}
8686

@@ -102,7 +102,7 @@ impl FromStr for FixedOffset {
102102
type Err = ParseError;
103103
fn from_str(s: &str) -> Result<Self, Self::Err> {
104104
let (_, offset) = scan::timezone_offset(s, scan::consume_colon_maybe, false, false, true)?;
105-
Self::east(offset).ok_or(OUT_OF_RANGE)
105+
Self::east(offset).map_err(|_| OUT_OF_RANGE)
106106
}
107107
}
108108

@@ -154,9 +154,7 @@ impl fmt::Display for FixedOffset {
154154
impl arbitrary::Arbitrary<'_> for FixedOffset {
155155
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<FixedOffset> {
156156
let secs = u.int_in_range(-86_399..=86_399)?;
157-
let fixed_offset = FixedOffset::east(secs)
158-
.expect("Could not generate a valid chrono::FixedOffset. It looks like implementation of Arbitrary for FixedOffset is erroneous.");
159-
Ok(fixed_offset)
157+
Ok(FixedOffset::east(secs).unwrap())
160158
}
161159
}
162160

src/offset/local/unix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ impl Cache {
156156
.offset();
157157

158158
return match FixedOffset::east(offset) {
159-
Some(offset) => LocalResult::Single(offset),
160-
None => LocalResult::None,
159+
Ok(offset) => LocalResult::Single(offset),
160+
Err(_) => LocalResult::None,
161161
};
162162
}
163163

src/offset/local/windows.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ impl TzInfo {
142142
tz_info.assume_init()
143143
};
144144
Some(TzInfo {
145-
std_offset: FixedOffset::west((tz_info.Bias + tz_info.StandardBias) * 60)?,
146-
dst_offset: FixedOffset::west((tz_info.Bias + tz_info.DaylightBias) * 60)?,
145+
std_offset: FixedOffset::west((tz_info.Bias + tz_info.StandardBias) * 60).ok()?,
146+
dst_offset: FixedOffset::west((tz_info.Bias + tz_info.DaylightBias) * 60).ok()?,
147147
std_transition: system_time_from_naive_date_time(tz_info.StandardDate, year),
148148
dst_transition: system_time_from_naive_date_time(tz_info.DaylightDate, year),
149149
})

0 commit comments

Comments
 (0)