Skip to content

Commit

Permalink
Rename LocalResult to TzResolution, add alias
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 9, 2024
1 parent bcb5386 commit bc2710e
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 259 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Chrono aims to provide all functionality needed to do correct operations on date
* The [`DateTime`](https://docs.rs/chrono/latest/chrono/struct.DateTime.html) type is timezone-aware
by default, with separate timezone-naive types.
* Operations that may produce an invalid or ambiguous date and time return `Option` or
[`LocalResult`](https://docs.rs/chrono/latest/chrono/offset/enum.LocalResult.html).
[`TzResolution`](https://docs.rs/chrono/latest/chrono/offset/enum.TzResolution.html).
* Configurable parsing and formatting with an `strftime` inspired date and time formatting syntax.
* The [`Local`](https://docs.rs/chrono/latest/chrono/offset/struct.Local.html) timezone works with
the current timezone of the OS.
Expand Down
12 changes: 6 additions & 6 deletions src/datetime/rustc_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::DateTime;
use crate::format::SecondsFormat;
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, LocalResult, TimeZone, Utc};
use crate::offset::{FixedOffset, TimeZone, TzResolution, Utc};
use core::fmt;
use core::ops::Deref;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
Expand All @@ -13,16 +13,16 @@ impl<Tz: TimeZone> Encodable for DateTime<Tz> {
}
}

// lik? function to convert a LocalResult into a serde-ish Result
fn from<T, D>(me: LocalResult<T>, d: &mut D) -> Result<T, D::Error>
// lik? function to convert a TzResolution into a serde-ish Result
fn from<T, D>(me: TzResolution<T>, d: &mut D) -> Result<T, D::Error>
where
D: Decoder,
T: fmt::Display,
{
match me {
LocalResult::None => Err(d.error("value is not a legal timestamp")),
LocalResult::Ambiguous(..) => Err(d.error("value is an ambiguous timestamp")),
LocalResult::Single(val) => Ok(val),
TzResolution::None => Err(d.error("value is not a legal timestamp")),
TzResolution::Ambiguous(..) => Err(d.error("value is an ambiguous timestamp")),
TzResolution::Single(val) => Ok(val),
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/datetime/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ mod tests {

#[test]
fn test_serde_no_offset_debug() {
use crate::{LocalResult, NaiveDate, NaiveDateTime, Offset};
use crate::{NaiveDate, NaiveDateTime, Offset, TzResolution};
use core::fmt::Debug;

#[derive(Clone)]
Expand All @@ -1266,14 +1266,14 @@ mod tests {
fn from_offset(_state: &TestTimeZone) -> TestTimeZone {
TestTimeZone
}
fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<TestTimeZone> {
LocalResult::Single(TestTimeZone)
fn offset_from_local_date(&self, _local: &NaiveDate) -> TzResolution<TestTimeZone> {
TzResolution::Single(TestTimeZone)
}
fn offset_from_local_datetime(
&self,
_local: &NaiveDateTime,
) -> LocalResult<TestTimeZone> {
LocalResult::Single(TestTimeZone)
) -> TzResolution<TestTimeZone> {
TzResolution::Single(TestTimeZone)
}
fn offset_from_utc_date(&self, _utc: &NaiveDate) -> TestTimeZone {
TestTimeZone
Expand Down
14 changes: 7 additions & 7 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::naive::{NaiveDate, NaiveTime};
use crate::offset::{FixedOffset, TimeZone, Utc};
#[cfg(feature = "clock")]
use crate::offset::{Local, Offset};
use crate::{Datelike, Days, LocalResult, Months, NaiveDateTime, TimeDelta, Timelike, Weekday};
use crate::{Datelike, Days, Months, NaiveDateTime, TimeDelta, Timelike, TzResolution, Weekday};

#[derive(Clone)]
struct DstTester;
Expand Down Expand Up @@ -31,14 +31,14 @@ impl TimeZone for DstTester {
DstTester
}

fn offset_from_local_date(&self, _: &NaiveDate) -> crate::LocalResult<Self::Offset> {
fn offset_from_local_date(&self, _: &NaiveDate) -> crate::TzResolution<Self::Offset> {
unimplemented!()
}

fn offset_from_local_datetime(
&self,
local: &NaiveDateTime,
) -> crate::LocalResult<Self::Offset> {
) -> crate::TzResolution<Self::Offset> {
let local_to_winter_transition_start = NaiveDate::from_ymd_opt(
local.year(),
DstTester::TO_WINTER_MONTH_DAY.0,
Expand Down Expand Up @@ -72,19 +72,19 @@ impl TimeZone for DstTester {
.and_time(DstTester::transition_start_local() + TimeDelta::try_hours(1).unwrap());

if *local < local_to_winter_transition_end || *local >= local_to_summer_transition_end {
LocalResult::Single(DstTester::summer_offset())
TzResolution::Single(DstTester::summer_offset())
} else if *local >= local_to_winter_transition_start
&& *local < local_to_summer_transition_start
{
LocalResult::Single(DstTester::winter_offset())
TzResolution::Single(DstTester::winter_offset())
} else if *local >= local_to_winter_transition_end
&& *local < local_to_winter_transition_start
{
LocalResult::Ambiguous(DstTester::winter_offset(), DstTester::summer_offset())
TzResolution::Ambiguous(DstTester::winter_offset(), DstTester::summer_offset())
} else if *local >= local_to_summer_transition_start
&& *local < local_to_summer_transition_end
{
LocalResult::None
TzResolution::None
} else {
panic!("Unexpected local time {}", local)
}
Expand Down
14 changes: 7 additions & 7 deletions src/format/parsed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use super::{ParseResult, IMPOSSIBLE, NOT_ENOUGH, OUT_OF_RANGE};
use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
use crate::offset::{FixedOffset, LocalResult, Offset, TimeZone};
use crate::offset::{FixedOffset, Offset, TimeZone, TzResolution};
use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};

/// A type to hold parsed fields of date and time that can check all fields are consistent.
Expand Down Expand Up @@ -888,9 +888,9 @@ impl Parsed {
let offset = FixedOffset::east_opt(offset).ok_or(OUT_OF_RANGE)?;

match offset.from_local_datetime(&datetime) {
LocalResult::None => Err(IMPOSSIBLE),
LocalResult::Single(t) => Ok(t),
LocalResult::Ambiguous(..) => Err(NOT_ENOUGH),
TzResolution::None => Err(IMPOSSIBLE),
TzResolution::Single(t) => Ok(t),
TzResolution::Ambiguous(..) => Err(NOT_ENOUGH),
}
}

Expand Down Expand Up @@ -944,15 +944,15 @@ impl Parsed {
// it will be 0 otherwise, but this is fine as the algorithm ignores offset for that case.
let datetime = self.to_naive_datetime_with_offset(guessed_offset)?;
match tz.from_local_datetime(&datetime) {
LocalResult::None => Err(IMPOSSIBLE),
LocalResult::Single(t) => {
TzResolution::None => Err(IMPOSSIBLE),
TzResolution::Single(t) => {
if check_offset(&t) {
Ok(t)
} else {
Err(IMPOSSIBLE)
}
}
LocalResult::Ambiguous(min, max) => {
TzResolution::Ambiguous(min, max) => {
// try to disambiguate two possible local dates by offset.
match (check_offset(&min), check_offset(&max)) {
(false, false) => Err(IMPOSSIBLE),
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! * The [`DateTime`](https://docs.rs/chrono/latest/chrono/struct.DateTime.html) type is timezone-aware
//! by default, with separate timezone-naive types.
//! * Operations that may produce an invalid or ambiguous date and time return `Option` or
//! [`LocalResult`](https://docs.rs/chrono/latest/chrono/offset/enum.LocalResult.html).
//! [`TzResolution`](https://docs.rs/chrono/latest/chrono/offset/enum.TzResolution.html).
//! * Configurable parsing and formatting with a `strftime` inspired date and time formatting syntax.
//! * The [`Local`](https://docs.rs/chrono/latest/chrono/offset/struct.Local.html) timezone works with
//! the current timezone of the OS.
Expand Down Expand Up @@ -130,7 +130,7 @@
//!
#![cfg_attr(not(feature = "now"), doc = "```ignore")]
#![cfg_attr(feature = "now", doc = "```rust")]
//! use chrono::offset::LocalResult;
//! use chrono::offset::TzResolution;
//! use chrono::prelude::*;
//!
//! # fn doctest() -> Option<()> {
Expand Down Expand Up @@ -174,12 +174,12 @@
//! // dynamic verification
//! assert_eq!(
//! Utc.with_ymd_and_hms(2014, 7, 8, 21, 15, 33),
//! LocalResult::Single(
//! TzResolution::Single(
//! NaiveDate::from_ymd_opt(2014, 7, 8)?.and_hms_opt(21, 15, 33)?.and_utc()
//! )
//! );
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 8, 80, 15, 33), LocalResult::None);
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 38, 21, 15, 33), LocalResult::None);
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 8, 80, 15, 33), TzResolution::None);
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 38, 21, 15, 33), TzResolution::None);
//!
//! # #[cfg(feature = "clock")] {
//! // other time zone objects can be used to construct a local datetime.
Expand Down Expand Up @@ -590,9 +590,9 @@ pub mod offset;
#[cfg(feature = "clock")]
#[doc(inline)]
pub use offset::Local;
pub use offset::LocalResult;
#[doc(inline)]
pub use offset::{FixedOffset, Offset, TimeZone, Utc};
pub use offset::{LocalResult, TzResolution};

pub mod round;
pub use round::{DurationRound, RoundingError, SubsecRound};
Expand Down
6 changes: 3 additions & 3 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::naive::{Days, IsoWeek, NaiveDate, NaiveTime};
use crate::offset::Utc;
use crate::time_delta::NANOS_PER_SEC;
use crate::{
expect, try_opt, DateTime, Datelike, FixedOffset, LocalResult, Months, TimeDelta, TimeZone,
Timelike, Weekday,
expect, try_opt, DateTime, Datelike, FixedOffset, Months, TimeDelta, TimeZone, Timelike,
TzResolution, Weekday,
};
#[cfg(feature = "rustc-serialize")]
pub(super) mod rustc_serialize;
Expand Down Expand Up @@ -929,7 +929,7 @@ impl NaiveDateTime {
/// assert_eq!(dt.timezone(), tz);
/// ```
#[must_use]
pub fn and_local_timezone<Tz: TimeZone>(&self, tz: Tz) -> LocalResult<DateTime<Tz>> {
pub fn and_local_timezone<Tz: TimeZone>(&self, tz: Tz) -> TzResolution<DateTime<Tz>> {
tz.from_local_datetime(self)
}

Expand Down
6 changes: 3 additions & 3 deletions src/naive/datetime/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::NaiveDateTime;
use crate::{Datelike, FixedOffset, LocalResult, NaiveDate, TimeDelta, Utc};
use crate::{Datelike, FixedOffset, NaiveDate, TimeDelta, TzResolution, Utc};

#[test]
fn test_datetime_add() {
Expand Down Expand Up @@ -385,13 +385,13 @@ fn test_and_timezone_min_max_dates() {
if offset_hour >= 0 {
assert_eq!(local_max.unwrap().naive_local(), NaiveDateTime::MAX);
} else {
assert_eq!(local_max, LocalResult::None);
assert_eq!(local_max, TzResolution::None);
}
let local_min = NaiveDateTime::MIN.and_local_timezone(offset);
if offset_hour <= 0 {
assert_eq!(local_min.unwrap().naive_local(), NaiveDateTime::MIN);
} else {
assert_eq!(local_min, LocalResult::None);
assert_eq!(local_min, TzResolution::None);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/offset/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::str::FromStr;
#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

use super::{LocalResult, Offset, TimeZone};
use super::{Offset, TimeZone, TzResolution};
use crate::format::{scan, ParseError, OUT_OF_RANGE};
use crate::naive::{NaiveDate, NaiveDateTime};

Expand Down Expand Up @@ -129,11 +129,11 @@ impl TimeZone for FixedOffset {
*offset
}

fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<FixedOffset> {
LocalResult::Single(*self)
fn offset_from_local_date(&self, _local: &NaiveDate) -> TzResolution<FixedOffset> {
TzResolution::Single(*self)
}
fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<FixedOffset> {
LocalResult::Single(*self)
fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> TzResolution<FixedOffset> {
TzResolution::Single(*self)
}

fn offset_from_utc_date(&self, _utc: &NaiveDate) -> FixedOffset {
Expand Down
Loading

0 comments on commit bc2710e

Please sign in to comment.