Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Jul 2, 2024
1 parent 3aed543 commit f05d231
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 78 deletions.
31 changes: 15 additions & 16 deletions bench/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
})
});
Expand Down Expand Up @@ -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);
})
});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(|| {
Expand Down
16 changes: 8 additions & 8 deletions src/civil/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1508,16 +1508,16 @@ impl Date {
#[inline]
pub fn intz(self, time_zone_name: &str) -> Result<Zoned, Error> {
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
///
Expand All @@ -1534,16 +1534,16 @@ 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]");
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn to_zoned_with(self, tz: TimeZone) -> Result<Zoned, Error> {
DateTime::from(self).to_zoned_with(tz)
pub fn to_zoned(self, tz: TimeZone) -> Result<Zoned, Error> {
DateTime::from(self).to_zoned(tz)
}

/// Given a [`Time`], this constructs a [`DateTime`] value with its time
Expand Down Expand Up @@ -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!(
Expand Down
24 changes: 11 additions & 13 deletions src/civil/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand All @@ -730,7 +730,7 @@ impl DateTime {
#[inline]
pub fn intz(self, time_zone_name: &str) -> Result<Zoned, Error> {
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
Expand Down Expand Up @@ -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]");
Expand All @@ -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.
Expand All @@ -798,7 +796,7 @@ impl DateTime {
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn to_zoned_with(self, tz: TimeZone) -> Result<Zoned, Error> {
pub fn to_zoned(self, tz: TimeZone) -> Result<Zoned, Error> {
tz.into_ambiguous_zoned(self).compatible()
}

Expand Down
8 changes: 4 additions & 4 deletions src/fmt/temporal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>>(())
/// ```
Expand Down
6 changes: 3 additions & 3 deletions src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})?
Expand All @@ -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")
})?
Expand Down Expand Up @@ -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")
})?
Expand Down
23 changes: 11 additions & 12 deletions src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1115,7 +1115,7 @@ impl Timestamp {
#[inline]
pub fn intz(self, time_zone_name: &str) -> Result<Zoned, Error> {
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
Expand All @@ -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).
Expand All @@ -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]");
Expand All @@ -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.
Expand All @@ -1168,7 +1168,7 @@ impl Timestamp {
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn to_zoned_with(self, tz: TimeZone) -> Zoned {
pub fn to_zoned(self, tz: TimeZone) -> Zoned {
Zoned::new(self, tz)
}

Expand Down Expand Up @@ -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:?}"
);
}
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -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())
Expand Down
8 changes: 4 additions & 4 deletions src/tz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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
Expand Down
Loading

0 comments on commit f05d231

Please sign in to comment.