@@ -11,7 +11,7 @@ use rkyv::{Archive, Deserialize, Serialize};
11
11
12
12
use super :: { LocalResult , Offset , TimeZone } ;
13
13
use crate :: format:: { scan, OUT_OF_RANGE } ;
14
- use crate :: { NaiveDateTime , ParseError } ;
14
+ use crate :: { Error , NaiveDateTime , ParseError } ;
15
15
16
16
/// The time zone with fixed offset, from UTC-23:59:59 to UTC+23:59:59.
17
17
///
@@ -35,7 +35,9 @@ impl FixedOffset {
35
35
/// Makes a new `FixedOffset` for the Eastern Hemisphere with given timezone difference.
36
36
/// The negative `secs` means the Western Hemisphere.
37
37
///
38
- /// Returns `None` on the out-of-bound `secs`.
38
+ /// # Errors
39
+ ///
40
+ /// Returns [`Error::OutOfRange`] on the out-of-bound `secs`.
39
41
///
40
42
/// # Example
41
43
///
@@ -49,19 +51,19 @@ impl FixedOffset {
49
51
/// .unwrap();
50
52
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00")
51
53
/// ```
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 ) ,
58
58
}
59
59
}
60
60
61
61
/// Makes a new `FixedOffset` for the Western Hemisphere with given timezone difference.
62
62
/// The negative `secs` means the Eastern Hemisphere.
63
63
///
64
- /// Returns `None` on the out-of-bound `secs`.
64
+ /// # Errors
65
+ ///
66
+ /// Returns [`Error::OutOfRange`] on the out-of-bound `secs`.
65
67
///
66
68
/// # Example
67
69
///
@@ -75,12 +77,10 @@ impl FixedOffset {
75
77
/// .unwrap();
76
78
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00")
77
79
/// ```
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 ) ,
84
84
}
85
85
}
86
86
@@ -102,7 +102,7 @@ impl FromStr for FixedOffset {
102
102
type Err = ParseError ;
103
103
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
104
104
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 )
106
106
}
107
107
}
108
108
@@ -154,9 +154,7 @@ impl fmt::Display for FixedOffset {
154
154
impl arbitrary:: Arbitrary < ' _ > for FixedOffset {
155
155
fn arbitrary ( u : & mut arbitrary:: Unstructured ) -> arbitrary:: Result < FixedOffset > {
156
156
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 ( ) )
160
158
}
161
159
}
162
160
0 commit comments