From 7f054721f67cb4318b735f8c8b9bc22eba67f1cd Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Mon, 29 Jan 2024 11:07:14 +0100 Subject: [PATCH] Rename `FixedOffset::{east_opt, west_opt}` --- bench/benches/chrono.rs | 8 +-- src/datetime/mod.rs | 26 ++++----- src/datetime/serde.rs | 2 +- src/datetime/tests.rs | 105 +++++++++++++++++------------------- src/format/formatting.rs | 14 ++--- src/format/parse.rs | 4 +- src/format/parsed.rs | 18 +++---- src/format/strftime.rs | 10 ++-- src/lib.rs | 8 +-- src/naive/datetime/mod.rs | 2 +- src/naive/datetime/tests.rs | 16 +++--- src/naive/time/mod.rs | 2 +- src/naive/time/tests.rs | 4 +- src/offset/fixed.rs | 24 ++++----- src/offset/local/mod.rs | 10 ++-- src/offset/local/unix.rs | 4 +- src/offset/local/windows.rs | 4 +- src/offset/mod.rs | 2 +- src/offset/utc.rs | 4 +- src/round.rs | 16 +++--- tests/wasm.rs | 10 ++-- 21 files changed, 142 insertions(+), 151 deletions(-) diff --git a/bench/benches/chrono.rs b/bench/benches/chrono.rs index ab6a324453..e66a911863 100644 --- a/bench/benches/chrono.rs +++ b/bench/benches/chrono.rs @@ -37,7 +37,7 @@ fn bench_datetime_from_str(c: &mut Criterion) { } fn bench_datetime_to_rfc2822(c: &mut Criterion) { - let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let pst = FixedOffset::east(8 * 60 * 60).unwrap(); let dt = pst .from_local_datetime( &NaiveDate::from_ymd_opt(2018, 1, 11) @@ -50,7 +50,7 @@ fn bench_datetime_to_rfc2822(c: &mut Criterion) { } fn bench_datetime_to_rfc3339(c: &mut Criterion) { - let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let pst = FixedOffset::east(8 * 60 * 60).unwrap(); let dt = pst .from_local_datetime( &NaiveDate::from_ymd_opt(2018, 1, 11) @@ -63,7 +63,7 @@ fn bench_datetime_to_rfc3339(c: &mut Criterion) { } fn bench_datetime_to_rfc3339_opts(c: &mut Criterion) { - let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let pst = FixedOffset::east(8 * 60 * 60).unwrap(); let dt = pst .from_local_datetime( &NaiveDate::from_ymd_opt(2018, 1, 11) @@ -205,7 +205,7 @@ fn bench_naivedate_add_signed(c: &mut Criterion) { } fn bench_datetime_with(c: &mut Criterion) { - let dt = FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2023, 9, 23, 7, 36, 0).unwrap(); + let dt = FixedOffset::east(3600).unwrap().with_ymd_and_hms(2023, 9, 23, 7, 36, 0).unwrap(); c.bench_function("bench_datetime_with", |b| { b.iter(|| black_box(black_box(dt).with_hour(12)).unwrap()) }); diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 5ae11f4f5c..581ae8fcd0 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -98,7 +98,7 @@ impl DateTime { /// use chrono::prelude::*; /// /// let date: DateTime = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap(); - /// let other: DateTime = FixedOffset::east_opt(23).unwrap().with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap(); + /// let other: DateTime = FixedOffset::east(23).unwrap().with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap(); /// assert_eq!(date.date_naive(), other.date_naive()); /// ``` #[inline] @@ -489,7 +489,7 @@ impl DateTime { /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), /// "2018-01-26T18:30:09Z"); /// - /// let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + /// let pst = FixedOffset::east(8 * 60 * 60).unwrap(); /// let dt = pst.from_local_datetime(&NaiveDate::from_ymd_opt(2018, 1, 26).unwrap().and_hms_micro_opt(10, 30, 9, 453_829).unwrap()).unwrap(); /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), /// "2018-01-26T10:30:09+08:00"); @@ -595,7 +595,7 @@ impl Default for DateTime { impl Default for DateTime { fn default() -> Self { - FixedOffset::west_opt(0).unwrap().from_utc_datetime(&NaiveDateTime::default()) + FixedOffset::west(0).unwrap().from_utc_datetime(&NaiveDateTime::default()) } } @@ -606,7 +606,7 @@ impl From> for DateTime { /// Conversion is done via [`DateTime::with_timezone`]. Note that the converted value returned by /// this will be created with a fixed timezone offset of 0. fn from(src: DateTime) -> Self { - src.with_timezone(&FixedOffset::east_opt(0).unwrap()) + src.with_timezone(&FixedOffset::east(0).unwrap()) } } @@ -708,7 +708,7 @@ impl DateTime { /// # use chrono::{DateTime, FixedOffset, TimeZone}; /// assert_eq!( /// DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT").unwrap(), - /// FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap() + /// FixedOffset::east(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap() /// ); /// ``` pub fn parse_from_rfc2822(s: &str) -> ParseResult> { @@ -759,7 +759,7 @@ impl DateTime { /// /// let dt = DateTime::parse_from_str( /// "1983 Apr 13 12:09:14.274 +0000", "%Y %b %d %H:%M:%S%.3f %z"); - /// assert_eq!(dt, Ok(FixedOffset::east_opt(0).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(1983, 4, 13).unwrap().and_hms_milli_opt(12, 9, 14, 274).unwrap()).unwrap())); + /// assert_eq!(dt, Ok(FixedOffset::east(0).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(1983, 4, 13).unwrap().and_hms_milli_opt(12, 9, 14, 274).unwrap()).unwrap())); /// ``` pub fn parse_from_str(s: &str, fmt: &str) -> ParseResult> { let mut parsed = Parsed::new(); @@ -788,7 +788,7 @@ impl DateTime { /// "2015-02-18 23:16:09 +0200 trailing text", "%Y-%m-%d %H:%M:%S %z").unwrap(); /// assert_eq!( /// datetime, - /// FixedOffset::east_opt(2*3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap() + /// FixedOffset::east(2*3600).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap() /// ); /// assert_eq!(remainder, " trailing text"); /// ``` @@ -1138,8 +1138,8 @@ impl PartialOrd> for DateTime { /// ``` /// use chrono::prelude::*; /// - /// let earlier = Utc.with_ymd_and_hms(2015, 5, 15, 2, 0, 0).unwrap().with_timezone(&FixedOffset::west_opt(1 * 3600).unwrap()); - /// let later = Utc.with_ymd_and_hms(2015, 5, 15, 3, 0, 0).unwrap().with_timezone(&FixedOffset::west_opt(5 * 3600).unwrap()); + /// let earlier = Utc.with_ymd_and_hms(2015, 5, 15, 2, 0, 0).unwrap().with_timezone(&FixedOffset::west(1 * 3600).unwrap()); + /// let later = Utc.with_ymd_and_hms(2015, 5, 15, 3, 0, 0).unwrap().with_timezone(&FixedOffset::west(5 * 3600).unwrap()); /// /// assert_eq!(earlier.to_string(), "2015-05-15 01:00:00 -01:00"); /// assert_eq!(later.to_string(), "2015-05-14 22:00:00 -05:00"); @@ -1646,14 +1646,14 @@ where assert_eq!( to_string_fixed( - &FixedOffset::east_opt(3660).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap() + &FixedOffset::east(3660).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap() ) .ok(), Some(r#""2014-07-24T12:34:06+01:01""#.into()) ); assert_eq!( to_string_fixed( - &FixedOffset::east_opt(3650).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap() + &FixedOffset::east(3650).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap() ) .ok(), // An offset with seconds is not allowed by RFC 3339, so we round it to the nearest minute. @@ -1690,13 +1690,13 @@ fn test_decodable_json( assert_eq!( norm(&fixed_from_str(r#""2014-07-24T12:34:06Z""#).ok()), norm(&Some( - FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap() + FixedOffset::east(0).unwrap().with_ymd_and_hms(2014, 7, 24, 12, 34, 6).unwrap() )) ); assert_eq!( norm(&fixed_from_str(r#""2014-07-24T13:57:06+01:23""#).ok()), norm(&Some( - FixedOffset::east_opt(60 * 60 + 23 * 60) + FixedOffset::east(60 * 60 + 23 * 60) .unwrap() .with_ymd_and_hms(2014, 7, 24, 13, 57, 6) .unwrap() diff --git a/src/datetime/serde.rs b/src/datetime/serde.rs index 6971388554..53dce43f38 100644 --- a/src/datetime/serde.rs +++ b/src/datetime/serde.rs @@ -1225,7 +1225,7 @@ mod tests { } impl Offset for TestTimeZone { fn fix(&self) -> FixedOffset { - FixedOffset::east_opt(15 * 60 * 60).unwrap() + FixedOffset::east(15 * 60 * 60).unwrap() } } diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 6d76a8bf8d..f30eb913fa 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -10,10 +10,10 @@ struct DstTester; impl DstTester { fn winter_offset() -> FixedOffset { - FixedOffset::east_opt(8 * 60 * 60).unwrap() + FixedOffset::east(8 * 60 * 60).unwrap() } fn summer_offset() -> FixedOffset { - FixedOffset::east_opt(9 * 60 * 60).unwrap() + FixedOffset::east(9 * 60 * 60).unwrap() } const TO_WINTER_MONTH_DAY: (u32, u32) = (4, 15); @@ -117,8 +117,8 @@ impl TimeZone for DstTester { #[test] fn test_datetime_add_days() { - let est = FixedOffset::west_opt(5 * 60 * 60).unwrap(); - let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap(); + let est = FixedOffset::west(5 * 60 * 60).unwrap(); + let kst = FixedOffset::east(9 * 60 * 60).unwrap(); assert_eq!( format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Days::new(5)), @@ -159,8 +159,8 @@ fn test_datetime_add_days() { #[test] fn test_datetime_sub_days() { - let est = FixedOffset::west_opt(5 * 60 * 60).unwrap(); - let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap(); + let est = FixedOffset::west(5 * 60 * 60).unwrap(); + let kst = FixedOffset::east(9 * 60 * 60).unwrap(); assert_eq!( format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Days::new(5)), @@ -183,8 +183,8 @@ fn test_datetime_sub_days() { #[test] fn test_datetime_add_months() { - let est = FixedOffset::west_opt(5 * 60 * 60).unwrap(); - let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap(); + let est = FixedOffset::west(5 * 60 * 60).unwrap(); + let kst = FixedOffset::east(9 * 60 * 60).unwrap(); assert_eq!( format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() + Months::new(1)), @@ -207,8 +207,8 @@ fn test_datetime_add_months() { #[test] fn test_datetime_sub_months() { - let est = FixedOffset::west_opt(5 * 60 * 60).unwrap(); - let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap(); + let est = FixedOffset::west(5 * 60 * 60).unwrap(); + let kst = FixedOffset::east(9 * 60 * 60).unwrap(); assert_eq!( format!("{}", est.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap() - Months::new(1)), @@ -326,9 +326,9 @@ fn ymdhms_milli_utc( #[test] fn test_datetime_offset() { - let est = FixedOffset::west_opt(5 * 60 * 60).unwrap(); - let edt = FixedOffset::west_opt(4 * 60 * 60).unwrap(); - let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap(); + let est = FixedOffset::west(5 * 60 * 60).unwrap(); + let edt = FixedOffset::west(4 * 60 * 60).unwrap(); + let kst = FixedOffset::east(9 * 60 * 60).unwrap(); assert_eq!( format!("{}", Utc.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap()), @@ -414,17 +414,17 @@ fn signed_duration_since_autoref() { #[test] fn test_datetime_date_and_time() { - let tz = FixedOffset::east_opt(5 * 60 * 60).unwrap(); + let tz = FixedOffset::east(5 * 60 * 60).unwrap(); let d = tz.with_ymd_and_hms(2014, 5, 6, 7, 8, 9).unwrap(); assert_eq!(d.time(), NaiveTime::from_hms(7, 8, 9).unwrap()); assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2014, 5, 6).unwrap()); - let tz = FixedOffset::east_opt(4 * 60 * 60).unwrap(); + let tz = FixedOffset::east(4 * 60 * 60).unwrap(); let d = tz.with_ymd_and_hms(2016, 5, 4, 3, 2, 1).unwrap(); assert_eq!(d.time(), NaiveTime::from_hms(3, 2, 1).unwrap()); assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2016, 5, 4).unwrap()); - let tz = FixedOffset::west_opt(13 * 60 * 60).unwrap(); + let tz = FixedOffset::west(13 * 60 * 60).unwrap(); let d = tz.with_ymd_and_hms(2017, 8, 9, 12, 34, 56).unwrap(); assert_eq!(d.time(), NaiveTime::from_hms(12, 34, 56).unwrap()); assert_eq!(d.date_naive(), NaiveDate::from_ymd_opt(2017, 8, 9).unwrap()); @@ -445,7 +445,7 @@ fn test_datetime_with_timezone() { #[test] #[cfg(feature = "alloc")] fn test_datetime_rfc2822() { - let edt = FixedOffset::east_opt(5 * 60 * 60).unwrap(); + let edt = FixedOffset::east(5 * 60 * 60).unwrap(); // timezone 0 assert_eq!( @@ -506,11 +506,11 @@ fn test_datetime_rfc2822() { assert_eq!( DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 +0000"), - Ok(FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()) + Ok(FixedOffset::east(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()) ); assert_eq!( DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 -0000"), - Ok(FixedOffset::east_opt(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()) + Ok(FixedOffset::east(0).unwrap().with_ymd_and_hms(2015, 2, 18, 23, 16, 9).unwrap()) ); assert_eq!( ymdhms_micro(&edt, 2015, 2, 18, 23, 59, 59, 1_234_567).to_rfc2822(), @@ -539,7 +539,7 @@ fn test_datetime_rfc2822() { ), Ok( ymdhms( - &FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), + &FixedOffset::east(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0, ) ) @@ -549,9 +549,7 @@ fn test_datetime_rfc2822() { DateTime::parse_from_rfc2822( "Thu,\n\t13\n Feb\n 1969\n 23:32\n -0330" ), - Ok( - ymdhms(&FixedOffset::east_opt(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0,) - ) + Ok(ymdhms(&FixedOffset::east(-3 * 60 * 60 - 30 * 60).unwrap(), 1969, 2, 13, 23, 32, 0,)) ); // bad year @@ -571,8 +569,8 @@ fn test_datetime_rfc2822() { #[test] #[cfg(feature = "alloc")] fn test_datetime_rfc3339() { - let edt5 = FixedOffset::east_opt(5 * 60 * 60).unwrap(); - let edt0 = FixedOffset::east_opt(0).unwrap(); + let edt5 = FixedOffset::east(5 * 60 * 60).unwrap(); + let edt0 = FixedOffset::east(0).unwrap(); // timezone 0 assert_eq!( @@ -658,7 +656,7 @@ fn test_datetime_rfc3339() { #[cfg(feature = "alloc")] fn test_rfc3339_opts() { use crate::SecondsFormat::*; - let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let pst = FixedOffset::east(8 * 60 * 60).unwrap(); let dt = pst .from_local_datetime( &NaiveDate::from_ymd_opt(2018, 1, 11) @@ -688,7 +686,7 @@ fn test_rfc3339_opts() { fn test_datetime_from_str() { assert_eq!( "2015-02-18T23:16:9.15Z".parse::>(), - Ok(FixedOffset::east_opt(0) + Ok(FixedOffset::east(0) .unwrap() .from_local_datetime( &NaiveDate::from_ymd_opt(2015, 2, 18) @@ -745,7 +743,7 @@ fn test_datetime_from_str() { assert_eq!( "2015-2-18T23:16:9.15Z".parse::>(), - Ok(FixedOffset::east_opt(0) + Ok(FixedOffset::east(0) .unwrap() .from_local_datetime( &NaiveDate::from_ymd_opt(2015, 2, 18) @@ -757,7 +755,7 @@ fn test_datetime_from_str() { ); assert_eq!( "2015-2-18T13:16:9.15-10:00".parse::>(), - Ok(FixedOffset::west_opt(10 * 3600) + Ok(FixedOffset::west(10 * 3600) .unwrap() .from_local_datetime( &NaiveDate::from_ymd_opt(2015, 2, 18) @@ -891,9 +889,9 @@ fn test_parse_datetime_utc() { #[test] fn test_parse_from_str() { - let edt = FixedOffset::east_opt(570 * 60).unwrap(); - let edt0 = FixedOffset::east_opt(0).unwrap(); - let wdt = FixedOffset::west_opt(10 * 3600).unwrap(); + let edt = FixedOffset::east(570 * 60).unwrap(); + let edt0 = FixedOffset::east(0).unwrap(); + let wdt = FixedOffset::west(10 * 3600).unwrap(); assert_eq!( DateTime::parse_from_str("2014-5-7T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"), Ok(ymdhms(&edt, 2014, 5, 7, 12, 34, 56)) @@ -948,7 +946,7 @@ fn test_parse_from_str() { #[test] fn test_datetime_parse_from_str() { - let dt = ymdhms(&FixedOffset::east_opt(-9 * 60 * 60).unwrap(), 2013, 8, 9, 23, 54, 35); + let dt = ymdhms(&FixedOffset::east(-9 * 60 * 60).unwrap(), 2013, 8, 9, 23, 54, 35); let parse = DateTime::parse_from_str; // timezone variations @@ -1094,10 +1092,10 @@ fn test_to_string_round_trip() { let dt = Utc.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap(); let _dt: DateTime = dt.to_string().parse().unwrap(); - let ndt_fixed = dt.with_timezone(&FixedOffset::east_opt(3600).unwrap()); + let ndt_fixed = dt.with_timezone(&FixedOffset::east(3600).unwrap()); let _dt: DateTime = ndt_fixed.to_string().parse().unwrap(); - let ndt_fixed = dt.with_timezone(&FixedOffset::east_opt(0).unwrap()); + let ndt_fixed = dt.with_timezone(&FixedOffset::east(0).unwrap()); let _dt: DateTime = ndt_fixed.to_string().parse().unwrap(); } @@ -1207,11 +1205,11 @@ fn test_from_system_time() { assert_eq!(SystemTime::from(epoch.with_timezone(&Local)), UNIX_EPOCH); } assert_eq!( - SystemTime::from(epoch.with_timezone(&FixedOffset::east_opt(32400).unwrap())), + SystemTime::from(epoch.with_timezone(&FixedOffset::east(32400).unwrap())), UNIX_EPOCH ); assert_eq!( - SystemTime::from(epoch.with_timezone(&FixedOffset::west_opt(28800).unwrap())), + SystemTime::from(epoch.with_timezone(&FixedOffset::west(28800).unwrap())), UNIX_EPOCH ); } @@ -1257,13 +1255,13 @@ fn test_datetime_add_assign() { datetime_add += TimeDelta::seconds(60); assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); - let timezone = FixedOffset::east_opt(60 * 60).unwrap(); + let timezone = FixedOffset::east(60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_add = datetime_add.with_timezone(&timezone); assert_eq!(datetime_add, datetime + TimeDelta::seconds(60)); - let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let timezone = FixedOffset::west(2 * 60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_add = datetime_add.with_timezone(&timezone); @@ -1294,13 +1292,13 @@ fn test_datetime_sub_assign() { datetime_sub -= TimeDelta::minutes(90); assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90)); - let timezone = FixedOffset::east_opt(60 * 60).unwrap(); + let timezone = FixedOffset::east(60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_sub = datetime_sub.with_timezone(&timezone); assert_eq!(datetime_sub, datetime - TimeDelta::minutes(90)); - let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let timezone = FixedOffset::west(2 * 60 * 60).unwrap(); let datetime = datetime.with_timezone(&timezone); let datetime_sub = datetime_sub.with_timezone(&timezone); @@ -1309,9 +1307,9 @@ fn test_datetime_sub_assign() { #[test] fn test_min_max_getters() { - let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let offset_min = FixedOffset::west(2 * 60 * 60).unwrap(); let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN); - let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap(); + let offset_max = FixedOffset::east(2 * 60 * 60).unwrap(); let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX); assert_eq!(format!("{:?}", beyond_min), "-262144-12-31T22:00:00-02:00"); @@ -1367,9 +1365,9 @@ fn test_min_max_getters() { #[test] fn test_min_max_setters() { - let offset_min = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let offset_min = FixedOffset::west(2 * 60 * 60).unwrap(); let beyond_min = offset_min.from_utc_datetime(&NaiveDateTime::MIN); - let offset_max = FixedOffset::east_opt(2 * 60 * 60).unwrap(); + let offset_max = FixedOffset::east(2 * 60 * 60).unwrap(); let beyond_max = offset_max.from_utc_datetime(&NaiveDateTime::MAX); assert_eq!(beyond_min.with_year(2020).unwrap().year(), 2020); @@ -1416,14 +1414,14 @@ fn test_min_max_setters() { #[test] #[should_panic] fn test_local_beyond_min_datetime() { - let min = FixedOffset::west_opt(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MIN); + let min = FixedOffset::west(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MIN); let _ = min.naive_local(); } #[test] #[should_panic] fn test_local_beyond_max_datetime() { - let max = FixedOffset::east_opt(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MAX); + let max = FixedOffset::east(2 * 60 * 60).unwrap().from_utc_datetime(&NaiveDateTime::MAX); let _ = max.naive_local(); } @@ -1516,18 +1514,17 @@ fn test_datetime_fixed_offset() { let naivedatetime = NaiveDate::from_ymd_opt(2023, 1, 1).unwrap().and_hms_opt(0, 0, 0).unwrap(); let datetime = Utc.from_utc_datetime(&naivedatetime); - let fixed_utc = FixedOffset::east_opt(0).unwrap(); + let fixed_utc = FixedOffset::east(0).unwrap(); assert_eq!(datetime.fixed_offset(), fixed_utc.from_local_datetime(&naivedatetime).unwrap()); - let fixed_offset = FixedOffset::east_opt(3600).unwrap(); + let fixed_offset = FixedOffset::east(3600).unwrap(); let datetime_fixed = fixed_offset.from_local_datetime(&naivedatetime).unwrap(); assert_eq!(datetime_fixed.fixed_offset(), datetime_fixed); } #[test] fn test_datetime_to_utc() { - let dt = - FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 2, 22, 23, 24, 25).unwrap(); + let dt = FixedOffset::east(3600).unwrap().with_ymd_and_hms(2020, 2, 22, 23, 24, 25).unwrap(); let dt_utc: DateTime = dt.to_utc(); assert_eq!(dt, dt_utc); } @@ -1552,10 +1549,8 @@ fn test_add_sub_months() { #[test] fn test_auto_conversion() { let utc_dt = Utc.with_ymd_and_hms(2018, 9, 5, 23, 58, 0).unwrap(); - let cdt_dt = FixedOffset::west_opt(5 * 60 * 60) - .unwrap() - .with_ymd_and_hms(2018, 9, 5, 18, 58, 0) - .unwrap(); + let cdt_dt = + FixedOffset::west(5 * 60 * 60).unwrap().with_ymd_and_hms(2018, 9, 5, 18, 58, 0).unwrap(); let utc_dt2: DateTime = cdt_dt.into(); assert_eq!(utc_dt, utc_dt2); } diff --git a/src/format/formatting.rs b/src/format/formatting.rs index 6aa00f7b9c..fddad60996 100644 --- a/src/format/formatting.rs +++ b/src/format/formatting.rs @@ -812,13 +812,13 @@ mod tests { } // +03:45, -03:30, +11:00, -11:00:22, +02:34:26, -12:34:30, +00:00 let offsets = [ - FixedOffset::east_opt(13_500).unwrap(), - FixedOffset::east_opt(-12_600).unwrap(), - FixedOffset::east_opt(39_600).unwrap(), - FixedOffset::east_opt(-39_622).unwrap(), - FixedOffset::east_opt(9266).unwrap(), - FixedOffset::east_opt(-45270).unwrap(), - FixedOffset::east_opt(0).unwrap(), + FixedOffset::east(13_500).unwrap(), + FixedOffset::east(-12_600).unwrap(), + FixedOffset::east(39_600).unwrap(), + FixedOffset::east(-39_622).unwrap(), + FixedOffset::east(9266).unwrap(), + FixedOffset::east(-45270).unwrap(), + FixedOffset::east(0).unwrap(), ]; check(precision, Colons::Colon, Pad::Zero, false, offsets, expected[0]); check(precision, Colons::Colon, Pad::Zero, true, offsets, expected[1]); diff --git a/src/format/parse.rs b/src/format/parse.rs index 51878a28b1..3116c2e26f 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -1629,7 +1629,7 @@ mod tests { #[test] fn test_rfc2822() { let ymd_hmsn = |y, m, d, h, n, s, nano, off| { - FixedOffset::east_opt(off * 60 * 60) + FixedOffset::east(off * 60 * 60) .unwrap() .with_ymd_and_hms(y, m, d, h, n, s) .unwrap() @@ -1794,7 +1794,7 @@ mod tests { #[test] fn test_rfc3339() { let ymd_hmsn = |y, m, d, h, n, s, nano, off| { - FixedOffset::east_opt(off * 60 * 60) + FixedOffset::east(off * 60 * 60) .unwrap() .with_ymd_and_hms(y, m, d, h, n, s) .unwrap() diff --git a/src/format/parsed.rs b/src/format/parsed.rs index f11dc9c125..d7992d788e 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -610,7 +610,7 @@ impl Parsed { /// Returns a parsed fixed time zone offset out of given fields. pub fn to_fixed_offset(&self) -> ParseResult { - self.offset.and_then(FixedOffset::east_opt).ok_or(OUT_OF_RANGE) + self.offset.and_then(FixedOffset::east).ok_or(OUT_OF_RANGE) } /// Returns a parsed timezone-aware date and time out of given fields. @@ -627,7 +627,7 @@ impl Parsed { (None, None) => return Err(NOT_ENOUGH), }; let datetime = self.to_naive_datetime_with_offset(offset)?; - let offset = FixedOffset::east_opt(offset).ok_or(OUT_OF_RANGE)?; + let offset = FixedOffset::east(offset).ok_or(OUT_OF_RANGE)?; match offset.from_local_datetime(&datetime) { LocalResult::None => Err(IMPOSSIBLE), @@ -1176,7 +1176,7 @@ mod tests { } let ymdhmsn = |y, m, d, h, n, s, nano, off| { - Ok(FixedOffset::east_opt(off) + Ok(FixedOffset::east(off) .unwrap() .from_local_datetime( &NaiveDate::from_ymd_opt(y, m, d) @@ -1244,16 +1244,16 @@ mod tests { Err(IMPOSSIBLE) ); assert_eq!( - parse!(FixedOffset::east_opt(32400).unwrap(); + parse!(FixedOffset::east(32400).unwrap(); year: 2014, ordinal: 365, hour_div_12: 0, hour_mod_12: 4, minute: 26, second: 40, nanosecond: 12_345_678, offset: 0), Err(IMPOSSIBLE) ); assert_eq!( - parse!(FixedOffset::east_opt(32400).unwrap(); + parse!(FixedOffset::east(32400).unwrap(); year: 2014, ordinal: 365, hour_div_12: 1, hour_mod_12: 1, minute: 26, second: 40, nanosecond: 12_345_678, offset: 32400), - Ok(FixedOffset::east_opt(32400) + Ok(FixedOffset::east(32400) .unwrap() .from_local_datetime( &NaiveDate::from_ymd_opt(2014, 12, 31) @@ -1271,12 +1271,12 @@ mod tests { ); assert_eq!(parse!(Utc; timestamp: 1_420_000_000, offset: 32400), Err(IMPOSSIBLE)); assert_eq!( - parse!(FixedOffset::east_opt(32400).unwrap(); timestamp: 1_420_000_000, offset: 0), + parse!(FixedOffset::east(32400).unwrap(); timestamp: 1_420_000_000, offset: 0), Err(IMPOSSIBLE) ); assert_eq!( - parse!(FixedOffset::east_opt(32400).unwrap(); timestamp: 1_420_000_000, offset: 32400), - Ok(FixedOffset::east_opt(32400) + parse!(FixedOffset::east(32400).unwrap(); timestamp: 1_420_000_000, offset: 32400), + Ok(FixedOffset::east(32400) .unwrap() .with_ymd_and_hms(2014, 12, 31, 13, 26, 40) .unwrap()) diff --git a/src/format/strftime.rs b/src/format/strftime.rs index 557034d15a..b1e7f46d37 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -664,7 +664,7 @@ mod tests { #[test] #[cfg(feature = "alloc")] fn test_strftime_docs() { - let dt = FixedOffset::east_opt(34200) + let dt = FixedOffset::east(34200) .unwrap() .from_local_datetime( &NaiveDate::from_ymd_opt(2001, 7, 8) @@ -775,7 +775,7 @@ mod tests { #[test] #[cfg(all(feature = "unstable-locales", feature = "alloc"))] fn test_strftime_docs_localized() { - let dt = FixedOffset::east_opt(34200) + let dt = FixedOffset::east(34200) .unwrap() .with_ymd_and_hms(2001, 7, 8, 0, 34, 59) .unwrap() @@ -832,7 +832,7 @@ mod tests { use crate::{FixedOffset, TimeZone}; use std::fmt::Write; - let dt = FixedOffset::east_opt(34200) + let dt = FixedOffset::east(34200) .unwrap() .from_local_datetime( &NaiveDate::from_ymd_opt(2001, 7, 8) @@ -849,7 +849,7 @@ mod tests { #[test] #[cfg(all(feature = "unstable-locales", feature = "alloc"))] fn test_strftime_localized_korean() { - let dt = FixedOffset::east_opt(34200) + let dt = FixedOffset::east(34200) .unwrap() .with_ymd_and_hms(2001, 7, 8, 0, 34, 59) .unwrap() @@ -878,7 +878,7 @@ mod tests { #[test] #[cfg(all(feature = "unstable-locales", feature = "alloc"))] fn test_strftime_localized_japanese() { - let dt = FixedOffset::east_opt(34200) + let dt = FixedOffset::east(34200) .unwrap() .with_ymd_and_hms(2001, 7, 8, 0, 34, 59) .unwrap() diff --git a/src/lib.rs b/src/lib.rs index 6f4482acd8..fa802408a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,7 +149,7 @@ //! // other time zone objects can be used to construct a local datetime. //! // obviously, `local_dt` is normally different from `dt`, but `fixed_dt` should be identical. //! let local_dt = Local.from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(9, 10, 11, 12).unwrap()).unwrap(); -//! let fixed_dt = FixedOffset::east_opt(9 * 3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(18, 10, 11, 12).unwrap()).unwrap(); +//! let fixed_dt = FixedOffset::east(9 * 3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 7, 8).unwrap().and_hms_milli_opt(18, 10, 11, 12).unwrap()).unwrap(); //! assert_eq!(dt, fixed_dt); //! # let _ = local_dt; //! # } @@ -169,7 +169,7 @@ //! use chrono::TimeDelta; //! //! // assume this returned `2014-11-28T21:45:59.324310806+09:00`: -//! let dt = FixedOffset::east_opt(9*3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(21, 45, 59, 324310806).unwrap()).unwrap(); +//! let dt = FixedOffset::east(9*3600).unwrap().from_local_datetime(&NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(21, 45, 59, 324310806).unwrap()).unwrap(); //! //! // property accessors //! assert_eq!((dt.year(), dt.month(), dt.day()), (2014, 11, 28)); @@ -182,7 +182,7 @@ //! //! // time zone accessor and manipulation //! assert_eq!(dt.offset().fix().local_minus_utc(), 9 * 3600); -//! assert_eq!(dt.timezone(), FixedOffset::east_opt(9 * 3600).unwrap()); +//! assert_eq!(dt.timezone(), FixedOffset::east(9 * 3600).unwrap()); //! assert_eq!(dt.with_timezone(&Utc), NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(12, 45, 59, 324310806).unwrap().and_local_timezone(Utc).unwrap()); //! //! // a sample of property manipulations (validates dynamically) @@ -277,7 +277,7 @@ //! use chrono::prelude::*; //! //! let dt = Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(); -//! let fixed_dt = dt.with_timezone(&FixedOffset::east_opt(9*3600).unwrap()); +//! let fixed_dt = dt.with_timezone(&FixedOffset::east(9*3600).unwrap()); //! //! // method 1 //! assert_eq!("2014-11-28T12:00:09Z".parse::>(), Ok(dt.clone())); diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 2e10187948..238c23cbec 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -993,7 +993,7 @@ impl NaiveDateTime { /// ``` /// use chrono::{NaiveDate, FixedOffset}; /// let hour = 3600; - /// let tz = FixedOffset::east_opt(5 * hour).unwrap(); + /// let tz = FixedOffset::east(5 * hour).unwrap(); /// let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap().and_local_timezone(tz).unwrap(); /// assert_eq!(dt.timezone(), tz); /// ``` diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index e41388d1a8..6f30d5bf89 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -491,7 +491,7 @@ fn test_and_local_timezone() { assert_eq!(dt_utc.naive_local(), ndt); assert_eq!(dt_utc.timezone(), Utc); - let offset_tz = FixedOffset::west_opt(4 * 3600).unwrap(); + let offset_tz = FixedOffset::west(4 * 3600).unwrap(); let dt_offset = ndt.and_local_timezone(offset_tz).unwrap(); assert_eq!(dt_offset.naive_local(), ndt); assert_eq!(dt_offset.timezone(), offset_tz); @@ -511,7 +511,7 @@ fn test_checked_add_offset() { NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_milli_opt(h, mn, s, mi) }; - let positive_offset = FixedOffset::east_opt(2 * 60 * 60).unwrap(); + let positive_offset = FixedOffset::east(2 * 60 * 60).unwrap(); // regular date let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap(); assert_eq!(dt.checked_add_offset(positive_offset), ymdhmsm(2023, 5, 5, 22, 10, 0, 0)); @@ -521,7 +521,7 @@ fn test_checked_add_offset() { // out of range assert!(NaiveDateTime::MAX.checked_add_offset(positive_offset).is_none()); - let negative_offset = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let negative_offset = FixedOffset::west(2 * 60 * 60).unwrap(); // regular date let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap(); assert_eq!(dt.checked_add_offset(negative_offset), ymdhmsm(2023, 5, 5, 18, 10, 0, 0)); @@ -538,7 +538,7 @@ fn test_checked_sub_offset() { NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_milli_opt(h, mn, s, mi) }; - let positive_offset = FixedOffset::east_opt(2 * 60 * 60).unwrap(); + let positive_offset = FixedOffset::east(2 * 60 * 60).unwrap(); // regular date let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap(); assert_eq!(dt.checked_sub_offset(positive_offset), ymdhmsm(2023, 5, 5, 18, 10, 0, 0)); @@ -548,7 +548,7 @@ fn test_checked_sub_offset() { // out of range assert!(NaiveDateTime::MIN.checked_sub_offset(positive_offset).is_none()); - let negative_offset = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let negative_offset = FixedOffset::west(2 * 60 * 60).unwrap(); // regular date let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0).unwrap(); assert_eq!(dt.checked_sub_offset(negative_offset), ymdhmsm(2023, 5, 5, 22, 10, 0, 0)); @@ -567,7 +567,7 @@ fn test_overflowing_add_offset() { let ymdhmsm = |y, m, d, h, mn, s, mi| { NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_milli_opt(h, mn, s, mi).unwrap() }; - let positive_offset = FixedOffset::east_opt(2 * 60 * 60).unwrap(); + let positive_offset = FixedOffset::east(2 * 60 * 60).unwrap(); // regular date let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0); assert_eq!(dt.overflowing_add_offset(positive_offset), ymdhmsm(2023, 5, 5, 22, 10, 0, 0)); @@ -577,7 +577,7 @@ fn test_overflowing_add_offset() { // out of range assert!(NaiveDateTime::MAX.overflowing_add_offset(positive_offset) > NaiveDateTime::MAX); - let negative_offset = FixedOffset::west_opt(2 * 60 * 60).unwrap(); + let negative_offset = FixedOffset::west(2 * 60 * 60).unwrap(); // regular date let dt = ymdhmsm(2023, 5, 5, 20, 10, 0, 0); assert_eq!(dt.overflowing_add_offset(negative_offset), ymdhmsm(2023, 5, 5, 18, 10, 0, 0)); @@ -592,7 +592,7 @@ fn test_overflowing_add_offset() { fn test_and_timezone_min_max_dates() { for offset_hour in -23..=23 { dbg!(offset_hour); - let offset = FixedOffset::east_opt(offset_hour * 60 * 60).unwrap(); + let offset = FixedOffset::east(offset_hour * 60 * 60).unwrap(); let local_max = NaiveDateTime::MAX.and_local_timezone(offset); if offset_hour >= 0 { diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index e5e9e81457..c83146f9b1 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -180,7 +180,7 @@ mod tests; /// ``` /// use chrono::{FixedOffset, NaiveDate, TimeZone}; /// -/// let paramaribo_pre1945 = FixedOffset::east_opt(-13236).unwrap(); // -03:40:36 +/// let paramaribo_pre1945 = FixedOffset::east(-13236).unwrap(); // -03:40:36 /// let leap_sec_2015 = /// NaiveDate::from_ymd_opt(2015, 6, 30).unwrap().and_hms_milli_opt(23, 59, 59, 1_000).unwrap(); /// let dt1 = paramaribo_pre1945.from_utc_datetime(&leap_sec_2015); diff --git a/src/naive/time/tests.rs b/src/naive/time/tests.rs index 72e1738c14..29e6df518b 100644 --- a/src/naive/time/tests.rs +++ b/src/naive/time/tests.rs @@ -353,7 +353,7 @@ fn test_time_parse_from_str() { fn test_overflowing_offset() { let hmsm = |h, m, s, n| NaiveTime::from_hms_milli(h, m, s, n).unwrap(); - let positive_offset = FixedOffset::east_opt(4 * 60 * 60).unwrap(); + let positive_offset = FixedOffset::east(4 * 60 * 60).unwrap(); // regular time let t = hmsm(5, 6, 7, 890); assert_eq!(t.overflowing_add_offset(positive_offset), (hmsm(9, 6, 7, 890), 0)); @@ -366,7 +366,7 @@ fn test_overflowing_offset() { let t = hmsm(1, 2, 3, 456); assert_eq!(t.overflowing_sub_offset(positive_offset), (hmsm(21, 2, 3, 456), -1)); // an odd offset - let negative_offset = FixedOffset::west_opt(((2 * 60) + 3) * 60 + 4).unwrap(); + let negative_offset = FixedOffset::west(((2 * 60) + 3) * 60 + 4).unwrap(); let t = hmsm(5, 6, 7, 890); assert_eq!(t.overflowing_add_offset(negative_offset), (hmsm(3, 3, 3, 890), 0)); assert_eq!(t.overflowing_sub_offset(negative_offset), (hmsm(7, 9, 11, 890), 0)); diff --git a/src/offset/fixed.rs b/src/offset/fixed.rs index d9fc690d1b..fa1f62e373 100644 --- a/src/offset/fixed.rs +++ b/src/offset/fixed.rs @@ -17,8 +17,8 @@ use crate::{NaiveDateTime, ParseError}; /// /// Using the [`TimeZone`](./trait.TimeZone.html) methods /// on a `FixedOffset` struct is the preferred way to construct -/// `DateTime` instances. See the [`east_opt`](#method.east_opt) and -/// [`west_opt`](#method.west_opt) methods for examples. +/// `DateTime` instances. See the [`east`](#method.east) and +/// [`west`](#method.west) methods for examples. #[derive(PartialEq, Eq, Hash, Copy, Clone)] #[cfg_attr( any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"), @@ -43,14 +43,14 @@ impl FixedOffset { #[cfg_attr(feature = "std", doc = "```")] /// use chrono::{FixedOffset, TimeZone}; /// let hour = 3600; - /// let datetime = FixedOffset::east_opt(5 * hour) + /// let datetime = FixedOffset::east(5 * hour) /// .unwrap() /// .with_ymd_and_hms(2016, 11, 08, 0, 0, 0) /// .unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00") /// ``` #[must_use] - pub const fn east_opt(secs: i32) -> Option { + pub const fn east(secs: i32) -> Option { if -86_400 < secs && secs < 86_400 { Some(FixedOffset { local_minus_utc: secs }) } else { @@ -69,14 +69,14 @@ impl FixedOffset { #[cfg_attr(feature = "std", doc = "```")] /// use chrono::{FixedOffset, TimeZone}; /// let hour = 3600; - /// let datetime = FixedOffset::west_opt(5 * hour) + /// let datetime = FixedOffset::west(5 * hour) /// .unwrap() /// .with_ymd_and_hms(2016, 11, 08, 0, 0, 0) /// .unwrap(); /// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00") /// ``` #[must_use] - pub const fn west_opt(secs: i32) -> Option { + pub const fn west(secs: i32) -> Option { if -86_400 < secs && secs < 86_400 { Some(FixedOffset { local_minus_utc: -secs }) } else { @@ -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_opt(offset).ok_or(OUT_OF_RANGE) + Self::east(offset).ok_or(OUT_OF_RANGE) } } @@ -154,7 +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_opt(secs) + 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) } @@ -170,22 +170,22 @@ mod tests { fn test_date_extreme_offset() { // starting from 0.3 we don't have an offset exceeding one day. // this makes everything easier! - let offset = FixedOffset::east_opt(86399).unwrap(); + let offset = FixedOffset::east(86399).unwrap(); assert_eq!( format!("{:?}", offset.with_ymd_and_hms(2012, 2, 29, 5, 6, 7).unwrap()), "2012-02-29T05:06:07+23:59:59" ); - let offset = FixedOffset::east_opt(-86399).unwrap(); + let offset = FixedOffset::east(-86399).unwrap(); assert_eq!( format!("{:?}", offset.with_ymd_and_hms(2012, 2, 29, 5, 6, 7).unwrap()), "2012-02-29T05:06:07-23:59:59" ); - let offset = FixedOffset::west_opt(86399).unwrap(); + let offset = FixedOffset::west(86399).unwrap(); assert_eq!( format!("{:?}", offset.with_ymd_and_hms(2012, 3, 4, 5, 6, 7).unwrap()), "2012-03-04T05:06:07-23:59:59" ); - let offset = FixedOffset::west_opt(-86399).unwrap(); + let offset = FixedOffset::west(-86399).unwrap(); assert_eq!( format!("{:?}", offset.with_ymd_and_hms(2012, 3, 4, 5, 6, 7).unwrap()), "2012-03-04T05:06:07+23:59:59" diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index c27bc82fcd..174ce62a73 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -35,13 +35,13 @@ mod inner { use crate::{FixedOffset, LocalResult, NaiveDateTime}; pub(super) fn offset_from_utc_datetime(_utc_time: &NaiveDateTime) -> LocalResult { - LocalResult::Single(FixedOffset::east_opt(0).unwrap()) + LocalResult::Single(FixedOffset::east(0).unwrap()) } pub(super) fn offset_from_local_datetime( _local_time: &NaiveDateTime, ) -> LocalResult { - LocalResult::Single(FixedOffset::east_opt(0).unwrap()) + LocalResult::Single(FixedOffset::east(0).unwrap()) } } @@ -55,7 +55,7 @@ mod inner { pub(super) fn offset_from_utc_datetime(utc: &NaiveDateTime) -> LocalResult { let offset = js_sys::Date::from(utc.and_utc()).get_timezone_offset(); - LocalResult::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap()) + LocalResult::Single(FixedOffset::west((offset as i32) * 60).unwrap()) } pub(super) fn offset_from_local_datetime(local: &NaiveDateTime) -> LocalResult { @@ -78,7 +78,7 @@ mod inner { ); let offset = js_date.get_timezone_offset(); // We always get a result, even if this time does not exist or is ambiguous. - LocalResult::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap()) + LocalResult::Single(FixedOffset::west((offset as i32) * 60).unwrap()) } } @@ -136,7 +136,7 @@ impl Local { /// /// // Current time in some timezone (let's use +05:00) /// // Note that it is usually more efficient to use `Utc::now` for this use case. - /// let offset = FixedOffset::east_opt(5 * 60 * 60).unwrap(); + /// let offset = FixedOffset::east(5 * 60 * 60).unwrap(); /// let now_with_offset = Local::now().with_timezone(&offset); /// ``` pub fn now() -> DateTime { diff --git a/src/offset/local/unix.rs b/src/offset/local/unix.rs index ce96a6e3bb..66aa5f2d39 100644 --- a/src/offset/local/unix.rs +++ b/src/offset/local/unix.rs @@ -155,7 +155,7 @@ impl Cache { .expect("unable to select local time type") .offset(); - return match FixedOffset::east_opt(offset) { + return match FixedOffset::east(offset) { Some(offset) => LocalResult::Single(offset), None => LocalResult::None, }; @@ -166,6 +166,6 @@ impl Cache { self.zone .find_local_time_type_from_local(d.timestamp(), d.year()) .expect("unable to select local time type") - .map(|o| FixedOffset::east_opt(o.offset()).unwrap()) + .map(|o| FixedOffset::east(o.offset()).unwrap()) } } diff --git a/src/offset/local/windows.rs b/src/offset/local/windows.rs index 539b6880fd..fc7616ae1e 100644 --- a/src/offset/local/windows.rs +++ b/src/offset/local/windows.rs @@ -66,7 +66,7 @@ fn from_utc_time(utc_time: SYSTEMTIME) -> Result { let utc_secs = system_time_as_unix_seconds(&utc_time)?; let local_secs = system_time_as_unix_seconds(&local_time)?; let offset = (local_secs - utc_secs) as i32; - Ok(FixedOffset::east_opt(offset).unwrap()) + Ok(FixedOffset::east(offset).unwrap()) } fn from_local_time(local_time: SYSTEMTIME) -> Result { @@ -74,7 +74,7 @@ fn from_local_time(local_time: SYSTEMTIME) -> Result { let utc_secs = system_time_as_unix_seconds(&utc_time)?; let local_secs = system_time_as_unix_seconds(&local_time)?; let offset = (local_secs - utc_secs) as i32; - Ok(FixedOffset::east_opt(offset).unwrap()) + Ok(FixedOffset::east(offset).unwrap()) } fn system_time_from_naive_date_time(dt: &NaiveDateTime) -> SYSTEMTIME { diff --git a/src/offset/mod.rs b/src/offset/mod.rs index ce83f31c4b..943bbb2d3f 100644 --- a/src/offset/mod.rs +++ b/src/offset/mod.rs @@ -274,7 +274,7 @@ mod tests { fn test_fixed_offset_min_max_dates() { for offset_hour in -23..=23 { dbg!(offset_hour); - let offset = FixedOffset::east_opt(offset_hour * 60 * 60).unwrap(); + let offset = FixedOffset::east(offset_hour * 60 * 60).unwrap(); let local_max = offset.from_utc_datetime(&NaiveDateTime::MAX); assert_eq!(local_max.naive_utc(), NaiveDateTime::MAX); diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 8e5433d4f4..626c9b0b34 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -71,7 +71,7 @@ impl Utc { /// let today_utc = now_utc.date_naive(); /// /// // Current time in some timezone (let's use +05:00) - /// let offset = FixedOffset::east_opt(5 * 60 * 60).unwrap(); + /// let offset = FixedOffset::east(5 * 60 * 60).unwrap(); /// let now_with_offset = Utc::now().with_timezone(&offset); /// ``` #[cfg(not(all( @@ -119,7 +119,7 @@ impl TimeZone for Utc { impl Offset for Utc { fn fix(&self) -> FixedOffset { - FixedOffset::east_opt(0).unwrap() + FixedOffset::east(0).unwrap() } } diff --git a/src/round.rs b/src/round.rs index 13131df25e..6a5d1a559b 100644 --- a/src/round.rs +++ b/src/round.rs @@ -309,7 +309,7 @@ mod tests { #[test] fn test_round_subsecs() { - let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let pst = FixedOffset::east(8 * 60 * 60).unwrap(); let dt = pst .from_local_datetime( &NaiveDate::from_ymd_opt(2018, 1, 11) @@ -373,7 +373,7 @@ mod tests { #[test] fn test_trunc_subsecs() { - let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); + let pst = FixedOffset::east(8 * 60 * 60).unwrap(); let dt = pst .from_local_datetime( &NaiveDate::from_ymd_opt(2018, 1, 11) @@ -501,8 +501,7 @@ mod tests { ); // timezone east - let dt = - FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); + let dt = FixedOffset::east(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), "2020-10-28 00:00:00 +01:00" @@ -513,8 +512,7 @@ mod tests { ); // timezone west - let dt = - FixedOffset::west_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); + let dt = FixedOffset::west(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( dt.duration_round(TimeDelta::days(1)).unwrap().to_string(), "2020-10-28 00:00:00 -01:00" @@ -663,8 +661,7 @@ mod tests { ); // timezone east - let dt = - FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); + let dt = FixedOffset::east(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), "2020-10-27 00:00:00 +01:00" @@ -675,8 +672,7 @@ mod tests { ); // timezone west - let dt = - FixedOffset::west_opt(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); + let dt = FixedOffset::west(3600).unwrap().with_ymd_and_hms(2020, 10, 27, 15, 0, 0).unwrap(); assert_eq!( dt.duration_trunc(TimeDelta::days(1)).unwrap().to_string(), "2020-10-27 00:00:00 -01:00" diff --git a/tests/wasm.rs b/tests/wasm.rs index 6937da9f71..46a74fbae5 100644 --- a/tests/wasm.rs +++ b/tests/wasm.rs @@ -38,11 +38,11 @@ fn now() { // Ensure offset retrieved when getting local time is correct let expected_offset = match tz { - "ACST-9:30" => FixedOffset::east_opt(19 * 30 * 60).unwrap(), - "Asia/Katmandu" => FixedOffset::east_opt(23 * 15 * 60).unwrap(), // No DST thankfully - "EDT" | "EST4" | "-0400" => FixedOffset::east_opt(-4 * 60 * 60).unwrap(), - "EST" | "-0500" => FixedOffset::east_opt(-5 * 60 * 60).unwrap(), - "UTC0" | "+0000" => FixedOffset::east_opt(0).unwrap(), + "ACST-9:30" => FixedOffset::east(19 * 30 * 60).unwrap(), + "Asia/Katmandu" => FixedOffset::east(23 * 15 * 60).unwrap(), // No DST thankfully + "EDT" | "EST4" | "-0400" => FixedOffset::east(-4 * 60 * 60).unwrap(), + "EST" | "-0500" => FixedOffset::east(-5 * 60 * 60).unwrap(), + "UTC0" | "+0000" => FixedOffset::east(0).unwrap(), tz => panic!("unexpected TZ {}", tz), }; assert_eq!(