From f05d2310d1760132d947e01a684caeea9d63fccc Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Tue, 2 Jul 2024 14:10:07 -0400 Subject: [PATCH] progress --- bench/src/bench.rs | 31 +++++++++++++++---------------- src/civil/date.rs | 16 ++++++++-------- src/civil/datetime.rs | 24 +++++++++++------------- src/fmt/temporal/mod.rs | 8 ++++---- src/round.rs | 6 +++--- src/timestamp.rs | 23 +++++++++++------------ src/tz/mod.rs | 8 ++++---- src/zoned.rs | 33 +++++++++++++++------------------ 8 files changed, 71 insertions(+), 78 deletions(-) diff --git a/bench/src/bench.rs b/bench/src/bench.rs index 1e5d83e..10fc79f 100644 --- a/bench/src/bench.rs +++ b/bench/src/bench.rs @@ -17,7 +17,7 @@ fn civil_datetime_to_instant_with_tzdb_lookup(c: &mut Criterion) { const TZNAME: &str = "America/New_York"; const STAMP: i64 = 1719755160; - if jiff::TimeZone::get(TZNAME).is_ok() { + if jiff::tz::TimeZone::get(TZNAME).is_ok() { let dt = jiff::civil::date(2024, 6, 30).at(9, 46, 0, 0); c.bench_function(&format!("jiff/{NAME}"), |b| { b.iter(|| { @@ -60,11 +60,11 @@ fn civil_datetime_to_instant_static(c: &mut Criterion) { const TZNAME: &str = "America/New_York"; const STAMP: i64 = 1719755160; - if let Ok(tz) = jiff::TimeZone::get(TZNAME) { + if let Ok(tz) = jiff::tz::TimeZone::get(TZNAME) { let dt = jiff::civil::date(2024, 6, 30).at(9, 46, 0, 0); c.bench_function(&format!("jiff/{NAME}"), |b| { b.iter(|| { - // The natural way to do this is `dt.to_zoned_with(..)`, but + // The natural way to do this is `dt.to_zoned(..)`, but // Jiff doesn't actually required one to materialize a `Zoned` // to disambiguate a civil datetime. Although, it is a little // more verbose. @@ -123,13 +123,13 @@ fn instant_to_civil_datetime_static(c: &mut Criterion) { const TZNAME: &str = "America/New_York"; const STAMP: i64 = 1719755160; - if let Ok(tz) = jiff::TimeZone::get(TZNAME) { + if let Ok(tz) = jiff::tz::TimeZone::get(TZNAME) { let expected = jiff::civil::date(2024, 6, 30).at(9, 46, 0, 0); let ts = jiff::Timestamp::from_second(STAMP).unwrap(); c.bench_function(&format!("jiff/{NAME}"), |b| { b.iter(|| { - let zdt = bb(ts).to_zoned_with(bb(&tz).clone()); + let zdt = bb(ts).to_zoned(bb(&tz).clone()); assert_eq!(zdt.to_datetime(), expected); }) }); @@ -187,11 +187,11 @@ fn instant_to_civil_datetime_offset(c: &mut Criterion) { const STAMP: i64 = 1719755160; let expected = jiff::civil::date(2024, 6, 30).at(9, 46, 0, 0); - let tz = jiff::TimeZone::fixed(jiff::tz::offset(OFFSET)); + let tz = jiff::tz::TimeZone::fixed(jiff::tz::offset(OFFSET)); let ts = jiff::Timestamp::from_second(STAMP).unwrap(); c.bench_function(&format!("jiff/{NAME}"), |b| { b.iter(|| { - let zdt = bb(ts).to_zoned_with(bb(&tz).clone()); + let zdt = bb(ts).to_zoned(bb(&tz).clone()); assert_eq!(zdt.to_datetime(), expected); }) }); @@ -245,8 +245,8 @@ fn offset_to_civil_datetime(c: &mut Criterion) { const STAMP: i64 = 1719755160; let expected = jiff::civil::date(2024, 6, 30).at(9, 46, 0, 0); - let tz = jiff::TimeZone::fixed(jiff::tz::offset(OFFSET)); - let zdt = jiff::Timestamp::from_second(STAMP).unwrap().to_zoned_with(tz); + let tz = jiff::tz::TimeZone::fixed(jiff::tz::offset(OFFSET)); + let zdt = jiff::Timestamp::from_second(STAMP).unwrap().to_zoned(tz); c.bench_function(&format!("jiff/{NAME}"), |b| { b.iter(|| { assert_eq!(bb(&zdt).to_datetime(), expected); @@ -299,8 +299,8 @@ fn offset_to_instant(c: &mut Criterion) { const OFFSET: i8 = -4; const STAMP: i64 = 1719755160; - let tz = jiff::TimeZone::fixed(jiff::tz::offset(OFFSET)); - let zdt = jiff::Timestamp::from_second(STAMP).unwrap().to_zoned_with(tz); + let tz = jiff::tz::TimeZone::fixed(jiff::tz::offset(OFFSET)); + let zdt = jiff::Timestamp::from_second(STAMP).unwrap().to_zoned(tz); c.bench_function(&format!("jiff/{NAME}"), |b| { b.iter(|| { assert_eq!(bb(&zdt).timestamp().as_second(), STAMP); @@ -343,12 +343,11 @@ fn zoned_add_time_duration(c: &mut Criterion) { const STAMP: i64 = 1719755160; const EXPECTED: i64 = STAMP + (24 * 60 * 60); - let tz = jiff::TimeZone::fixed(jiff::tz::offset(OFFSET)); - let expected = jiff::Timestamp::from_second(EXPECTED) - .unwrap() - .to_zoned_with(tz.clone()); + let tz = jiff::tz::TimeZone::fixed(jiff::tz::offset(OFFSET)); + let expected = + jiff::Timestamp::from_second(EXPECTED).unwrap().to_zoned(tz.clone()); let start = - jiff::Timestamp::from_second(STAMP).unwrap().to_zoned_with(tz.clone()); + jiff::Timestamp::from_second(STAMP).unwrap().to_zoned(tz.clone()); let span = jiff::Span::new().hours(24); c.bench_function(&format!("jiff/{NAME}"), |b| { b.iter(|| { diff --git a/src/civil/date.rs b/src/civil/date.rs index ba4a9ef..a041f81 100644 --- a/src/civil/date.rs +++ b/src/civil/date.rs @@ -1508,16 +1508,16 @@ impl Date { #[inline] pub fn intz(self, time_zone_name: &str) -> Result { let tz = crate::tz::db().get(time_zone_name)?; - self.to_zoned_with(tz) + self.to_zoned(tz) } /// Converts a civil datetime to a [`Zoned`] datetime by adding the given /// [`TimeZone`] and setting the clock time to midnight. /// /// This is a convenience function for - /// `date.to_datetime(midnight).to_zoned_with(tz)`. See - /// [`DateTime::to_zoned_with`] for more details. Note that ambiguous - /// datetimes are handled in the same way as `DateTime::to_zoned_with`. + /// `date.to_datetime(midnight).to_zoned(tz)`. See [`DateTime::to_zoned`] + /// for more details. Note that ambiguous datetimes are handled in the same + /// way as `DateTime::to_zoned`. /// /// # Errors /// @@ -1534,7 +1534,7 @@ impl Date { /// use jiff::{civil::date, tz::{Offset, TimeZone}}; /// /// let tz = TimeZone::fixed(Offset::constant(-4)); - /// let zdt = date(2024, 6, 20).to_zoned_with(tz)?; + /// let zdt = date(2024, 6, 20).to_zoned(tz)?; /// // A time zone annotation is still included in the printable version /// // of the Zoned value, but it is fixed to a particular offset. /// assert_eq!(zdt.to_string(), "2024-06-20T00:00:00-04:00[-04:00]"); @@ -1542,8 +1542,8 @@ impl Date { /// # Ok::<(), Box>(()) /// ``` #[inline] - pub fn to_zoned_with(self, tz: TimeZone) -> Result { - DateTime::from(self).to_zoned_with(tz) + pub fn to_zoned(self, tz: TimeZone) -> Result { + DateTime::from(self).to_zoned(tz) } /// Given a [`Time`], this constructs a [`DateTime`] value with its time @@ -3284,7 +3284,7 @@ mod tests { #[test] fn t_from_unix() { fn date_from_timestamp(timestamp: Timestamp) -> Date { - timestamp.to_zoned_with(TimeZone::UTC).to_datetime().date() + timestamp.to_zoned(TimeZone::UTC).to_datetime().date() } assert_eq!( diff --git a/src/civil/datetime.rs b/src/civil/datetime.rs index 1e87972..771f146 100644 --- a/src/civil/datetime.rs +++ b/src/civil/datetime.rs @@ -606,9 +606,9 @@ impl DateTime { /// /// The name given is resolved to a [`TimeZone`] by using the default /// [`TimeZoneDatabase`](crate::tz::TimeZoneDatabase) created by - /// [`tz::db`](crate::tz::db). Indeed, this is a convenience function - /// for [`DateTime::to_zoned_with`] where the time zone database lookup - /// is done automatically. + /// [`tz::db`](crate::tz::db). Indeed, this is a convenience function for + /// [`DateTime::to_zoned`] where the time zone database lookup is done + /// automatically. /// /// In some cases, a civil datetime may be ambiguous in a /// particular time zone. This routine automatically utilizes the @@ -713,11 +713,11 @@ impl DateTime { /// // In fact, the only valid offset one can use to turn the maximum civil /// // datetime into a Zoned value is the maximum offset: /// let tz = TimeZone::fixed(Offset::constant_seconds(93_599)); - /// assert!(dt.to_zoned_with(tz).is_ok()); + /// assert!(dt.to_zoned(tz).is_ok()); /// // One second less than the maximum offset results in a failure at the /// // maximum datetime boundary. /// let tz = TimeZone::fixed(Offset::constant_seconds(93_598)); - /// assert!(dt.to_zoned_with(tz).is_err()); + /// assert!(dt.to_zoned(tz).is_err()); /// ``` /// /// This behavior exists because it guarantees that every possible `Zoned` @@ -730,7 +730,7 @@ impl DateTime { #[inline] pub fn intz(self, time_zone_name: &str) -> Result { let tz = crate::tz::db().get(time_zone_name)?; - self.to_zoned_with(tz) + self.to_zoned(tz) } /// Converts a civil datetime to a [`Zoned`] datetime by adding the given @@ -763,11 +763,10 @@ impl DateTime { /// offset: /// /// ``` - /// use jiff::{civil::DateTime, tz::{self, TimeZone}}; + /// use jiff::{civil::date, tz::{self, TimeZone}}; /// - /// let dt = DateTime::constant(2024, 6, 20, 17, 3, 0, 0); /// let tz = TimeZone::fixed(tz::offset(-4)); - /// let zdt = dt.to_zoned_with(tz)?; + /// let zdt = date(2024, 6, 20).at(17, 3, 0, 0).to_zoned(tz)?; /// // A time zone annotation is still included in the printable version /// // of the Zoned value, but it is fixed to a particular offset. /// assert_eq!(zdt.to_string(), "2024-06-20T17:03:00-04:00[-04:00]"); @@ -783,11 +782,10 @@ impl DateTime { /// minutes, which is atypical. /// /// ``` - /// use jiff::{civil::DateTime, tz::TimeZone}; + /// use jiff::{civil::date, tz::TimeZone}; /// - /// let dt = DateTime::constant(2024, 6, 20, 17, 3, 0, 0); /// let tz = TimeZone::posix("NST3:30NDT,M3.2.0,M11.1.0")?; - /// let zdt = dt.to_zoned_with(tz)?; + /// let zdt = date(2024, 6, 20).at(17, 3, 0, 0).to_zoned(tz)?; /// // There isn't any agreed upon mechanism for transmitting a POSIX time /// // zone string within an RFC 9557 TZ annotation, so Jiff just emits the /// // offset. In practice, POSIX TZ strings are rarely user facing anyway. @@ -798,7 +796,7 @@ impl DateTime { /// # Ok::<(), Box>(()) /// ``` #[inline] - pub fn to_zoned_with(self, tz: TimeZone) -> Result { + pub fn to_zoned(self, tz: TimeZone) -> Result { tz.into_ambiguous_zoned(self).compatible() } diff --git a/src/fmt/temporal/mod.rs b/src/fmt/temporal/mod.rs index 9b03f8e..bb4a1c9 100644 --- a/src/fmt/temporal/mod.rs +++ b/src/fmt/temporal/mod.rs @@ -392,14 +392,14 @@ impl DateTimeParser { /// `2024-06-08T07:00-04` as-is, you should parse it into an [`Timestamp`]: /// /// ``` - /// use jiff::{civil::DateTime, fmt::temporal::DateTimeParser, tz}; + /// use jiff::{civil::date, fmt::temporal::DateTimeParser, tz}; /// /// static PARSER: DateTimeParser = DateTimeParser::new(); /// /// let timestamp = PARSER.parse_timestamp("2024-06-08T07:00-04")?; - /// let zdt = timestamp.to_zoned_with(tz::TimeZone::UTC); - /// assert_eq!(zdt.to_datetime(), DateTime::constant(2024, 6, 8, 11, 0, 0, 0)); - /// assert_eq!(zdt.to_offset(), tz::Offset::constant(0)); + /// let zdt = timestamp.to_zoned(tz::TimeZone::UTC); + /// assert_eq!(zdt.to_datetime(), date(2024, 6, 8).at(11, 0, 0, 0)); + /// assert_eq!(zdt.to_offset(), tz::offset(0)); /// /// # Ok::<(), Box>(()) /// ``` diff --git a/src/round.rs b/src/round.rs index 4a28165..3656e3c 100644 --- a/src/round.rs +++ b/src/round.rs @@ -642,7 +642,7 @@ impl RoundRelativeTo { let range = match self.kind { RelativeToKind::Civil(datetime) => { let timestamp = datetime - .to_zoned_with(TimeZone::UTC) + .to_zoned(TimeZone::UTC) .with_context(|| { err!("failed to convert {datetime} to timestamp") })? @@ -666,7 +666,7 @@ impl RoundRelativeTo { match self.kind { RelativeToKind::Civil(dt) => { let timestamp = dt - .to_zoned_with(TimeZone::UTC) + .to_zoned(TimeZone::UTC) .with_context(|| { err!("failed to convert {dt} to timestamp") })? @@ -784,7 +784,7 @@ impl RelativeCivil { ) })?; let timestamp = datetime - .to_zoned_with(TimeZone::UTC) + .to_zoned(TimeZone::UTC) .with_context(|| { err!("failed to convert {datetime} to timestamp") })? diff --git a/src/timestamp.rs b/src/timestamp.rs index 8f3cb0d..9c9419b 100644 --- a/src/timestamp.rs +++ b/src/timestamp.rs @@ -1057,7 +1057,7 @@ impl Timestamp { /// The name given is resolved to a [`TimeZone`] by using the default /// [`TimeZoneDatabase`](crate::tz::TimeZoneDatabase) created by /// [`tz::db`](crate::tz::db). Indeed, this is a convenience function - /// for [`Timestamp::to_zoned_with`] where the time zone database lookup + /// for [`Timestamp::to_zoned`] where the time zone database lookup /// is done automatically. /// /// Assuming the time zone name could be resolved to a [`TimeZone`], this @@ -1115,7 +1115,7 @@ impl Timestamp { #[inline] pub fn intz(self, time_zone_name: &str) -> Result { let tz = crate::tz::db().get(time_zone_name)?; - Ok(self.to_zoned_with(tz)) + Ok(self.to_zoned(tz)) } /// Creates a [`Zoned`] value by attaching the given time zone to this @@ -1124,7 +1124,7 @@ impl Timestamp { /// This is infallible and never results in any ambiguity since both a /// [`Timestamp`] and a [`Zoned`] correspond to precise instant in time. /// This is unlike - /// [`civil::DateTime::to_zoned_with`](crate::civil::DateTime::to_zoned_with), + /// [`civil::DateTime::to_zoned`](crate::civil::DateTime::to_zoned), /// where a civil datetime might correspond to more than one instant in /// time (i.e., a fold, typically DST ending) or no instants in time (i.e., /// a gap, typically DST starting). @@ -1139,7 +1139,7 @@ impl Timestamp { /// /// let ts = Timestamp::new(123_456_789, 0).unwrap(); /// let tz = TimeZone::fixed(tz::offset(-4)); - /// let zdt = ts.to_zoned_with(tz); + /// let zdt = ts.to_zoned(tz); /// // A time zone annotation is still included in the printable version /// // of the Zoned value, but it is fixed to a particular offset. /// assert_eq!(zdt.to_string(), "1973-11-29T17:33:09-04:00[-04:00]"); @@ -1157,7 +1157,7 @@ impl Timestamp { /// /// let ts = Timestamp::new(123_456_789, 0)?; /// let tz = TimeZone::posix("NST3:30NDT,M3.2.0,M11.1.0")?; - /// let zdt = ts.to_zoned_with(tz); + /// let zdt = ts.to_zoned(tz); /// // There isn't any agreed upon mechanism for transmitting a POSIX time /// // zone string within an RFC 9557 TZ annotation, so Jiff just emits the /// // offset. In practice, POSIX TZ strings are rarely user facing anyway. @@ -1168,7 +1168,7 @@ impl Timestamp { /// # Ok::<(), Box>(()) /// ``` #[inline] - pub fn to_zoned_with(self, tz: TimeZone) -> Zoned { + pub fn to_zoned(self, tz: TimeZone) -> Zoned { Zoned::new(self, tz) } @@ -2940,7 +2940,7 @@ mod tests { ); assert_eq!( timestamp, - datetime.to_zoned_with(TimeZone::UTC).unwrap().timestamp(), + datetime.to_zoned(TimeZone::UTC).unwrap().timestamp(), "datetime: {datetime:?}" ); } @@ -2970,9 +2970,8 @@ mod tests { continue; } let t = Timestamp::new(second, nano).unwrap(); - let Ok(got) = Offset::UTC - .to_datetime(t) - .to_zoned_with(TimeZone::UTC) + let Ok(got) = + Offset::UTC.to_datetime(t).to_zoned(TimeZone::UTC) else { continue; }; @@ -3024,8 +3023,8 @@ mod tests { quickcheck::quickcheck! { fn prop_unix_seconds_roundtrip(t: Timestamp) -> quickcheck::TestResult { - let dt = t.to_zoned_with(TimeZone::UTC).to_datetime(); - let Ok(got) = dt.to_zoned_with(TimeZone::UTC) else { + let dt = t.to_zoned(TimeZone::UTC).to_datetime(); + let Ok(got) = dt.to_zoned(TimeZone::UTC) else { return quickcheck::TestResult::discard(); }; quickcheck::TestResult::from_bool(t == got.timestamp()) diff --git a/src/tz/mod.rs b/src/tz/mod.rs index 5eea038..5f3b5cb 100644 --- a/src/tz/mod.rs +++ b/src/tz/mod.rs @@ -685,7 +685,7 @@ impl AmbiguousZoned { tz = self.time_zone().diagnostic_name(), ) })?; - Ok(ts.to_zoned_with(self.tz)) + Ok(ts.to_zoned(self.tz)) } #[inline] @@ -697,7 +697,7 @@ impl AmbiguousZoned { tz = self.time_zone().diagnostic_name(), ) })?; - Ok(ts.to_zoned_with(self.tz)) + Ok(ts.to_zoned(self.tz)) } #[inline] @@ -709,7 +709,7 @@ impl AmbiguousZoned { tz = self.time_zone().diagnostic_name(), ) })?; - Ok(ts.to_zoned_with(self.tz)) + Ok(ts.to_zoned(self.tz)) } #[inline] @@ -721,7 +721,7 @@ impl AmbiguousZoned { tz = self.time_zone().diagnostic_name(), ) })?; - Ok(ts.to_zoned_with(self.tz)) + Ok(ts.to_zoned(self.tz)) } /// Disambiguates this (possibly ambiguous) timestamp into a concrete diff --git a/src/zoned.rs b/src/zoned.rs index 287b7ff..f2c90da 100644 --- a/src/zoned.rs +++ b/src/zoned.rs @@ -82,20 +82,19 @@ impl Zoned { /// ``` /// /// ``` - /// use jiff::{civil::DateTime, tz::{TimeZone, Offset}, Zoned}; + /// use jiff::{civil::date, tz::{TimeZone, Offset}, Zoned}; /// /// // While -9999-01-03T04:00:00+25:59:59 is representable as a Zoned /// // value, the start of the corresponding day is not! - /// let dt = DateTime::constant(-9999, 1, 3, 4, 0, 0, 0); - /// let inst: Zoned = dt.to_zoned_with(TimeZone::fixed(Offset::MAX))?; - /// assert!(inst.start_of_day().is_err()); + /// let tz = TimeZone::fixed(Offset::MAX); + /// let zdt = date(-9999, 1, 3).at(4, 0, 0, 0).to_zoned(tz.clone())?; + /// assert!(zdt.start_of_day().is_err()); /// // The next day works fine since -9999-01-04T00:00:00+25:59:59 is /// // representable. - /// let dt = DateTime::constant(-9999, 1, 4, 15, 0, 0, 0); - /// let inst: Zoned = dt.to_zoned_with(TimeZone::fixed(Offset::MAX))?; + /// let zdt = date(-9999, 1, 4).at(15, 0, 0, 0).to_zoned(tz)?; /// assert_eq!( - /// inst.start_of_day()?.to_datetime(), - /// DateTime::constant(-9999, 1, 4, 0, 0, 0, 0), + /// zdt.start_of_day()?.to_datetime(), + /// date(-9999, 1, 4).at(0, 0, 0, 0), /// ); /// /// # Ok::<(), Box>(()) @@ -165,7 +164,7 @@ impl Zoned { (which was created from {dt})" ) })?; - Ok(ts.to_zoned_with(tz.clone())) + Ok(ts.to_zoned(tz.clone())) } #[inline] @@ -229,9 +228,8 @@ impl Zoned { ) })? .to_datetime(dt1.time()); - let mut zmid: Zoned = mid - .to_zoned_with(self.time_zone().clone()) - .with_context(|| { + let mut zmid: Zoned = + mid.to_zoned(self.time_zone().clone()).with_context(|| { err!( "failed to convert intermediate datetime {mid} \ to zoned timestamp in time zone {tz}", @@ -253,15 +251,14 @@ impl Zoned { ) })? .to_datetime(dt1.time()); - zmid = mid.to_zoned_with(self.time_zone().clone()).with_context( - || { + zmid = + mid.to_zoned(self.time_zone().clone()).with_context(|| { err!( "failed to convert intermediate datetime {mid} \ to zoned timestamp in time zone {tz}", tz = self.time_zone().diagnostic_name(), ) - }, - )?; + })?; if t::sign(zdt2, &zmid) == -sign { panic!("this should be an error too"); } @@ -323,7 +320,7 @@ impl Zoned { .mode(options.get_mode()) .increment(options.get_increment().get()); let end = rounder.round(day_length, start)?; - end.to_zoned_with(self.time_zone().clone()) + end.to_zoned(self.time_zone().clone()) } } @@ -432,7 +429,7 @@ impl quickcheck::Arbitrary for Zoned { #[inline] fn start_of_day(date: Date, tz: TimeZone) -> Result { let dt = date.to_datetime(Time::midnight()); - dt.to_zoned_with(tz) + dt.to_zoned(tz) } #[inline]