Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename LocalResult to TzResolution, add alias #1501

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
[`MappedLocalTime`](https://docs.rs/chrono/latest/chrono/offset/enum.MappedLocalTime.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 crate::format::SecondsFormat;
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, LocalResult, TimeZone, Utc};
use crate::offset::{FixedOffset, MappedLocalTime, TimeZone, Utc};
use core::fmt;
use core::ops::Deref;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
Expand All @@ -13,16 +13,16 @@
}
}

// 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>
// Function to convert a MappedLocalTime into a serde-ish Result
fn from<T, D>(me: MappedLocalTime<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),
MappedLocalTime::None => Err(d.error("value is not a legal timestamp")),
MappedLocalTime::Ambiguous(..) => Err(d.error("value is an ambiguous timestamp")),

Check warning on line 24 in src/datetime/rustc_serialize.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/rustc_serialize.rs#L23-L24

Added lines #L23 - L24 were not covered by tests
MappedLocalTime::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 @@

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

#[derive(Clone)]
Expand All @@ -1266,14 +1266,14 @@
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) -> MappedLocalTime<TestTimeZone> {
MappedLocalTime::Single(TestTimeZone)

Check warning on line 1270 in src/datetime/serde.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/serde.rs#L1270

Added line #L1270 was not covered by tests
}
fn offset_from_local_datetime(
&self,
_local: &NaiveDateTime,
) -> LocalResult<TestTimeZone> {
LocalResult::Single(TestTimeZone)
) -> MappedLocalTime<TestTimeZone> {
MappedLocalTime::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::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, MappedLocalTime, Months, NaiveDateTime, TimeDelta, Timelike, Weekday};

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

fn offset_from_local_date(&self, _: &NaiveDate) -> crate::LocalResult<Self::Offset> {
fn offset_from_local_date(&self, _: &NaiveDate) -> crate::MappedLocalTime<Self::Offset> {

Check warning on line 34 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L34

Added line #L34 was not covered by tests
unimplemented!()
}

fn offset_from_local_datetime(
&self,
local: &NaiveDateTime,
) -> crate::LocalResult<Self::Offset> {
) -> crate::MappedLocalTime<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 @@
.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())
MappedLocalTime::Single(DstTester::summer_offset())
} else if *local >= local_to_winter_transition_start
&& *local < local_to_summer_transition_start
{
LocalResult::Single(DstTester::winter_offset())
MappedLocalTime::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())
MappedLocalTime::Ambiguous(DstTester::winter_offset(), DstTester::summer_offset())

Check warning on line 83 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L83

Added line #L83 was not covered by tests
} else if *local >= local_to_summer_transition_start
&& *local < local_to_summer_transition_end
{
LocalResult::None
MappedLocalTime::None

Check warning on line 87 in src/datetime/tests.rs

View check run for this annotation

Codecov / codecov/patch

src/datetime/tests.rs#L87

Added line #L87 was not covered by tests
} 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, MappedLocalTime, Offset, TimeZone};
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 @@
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),
MappedLocalTime::None => Err(IMPOSSIBLE),

Check warning on line 891 in src/format/parsed.rs

View check run for this annotation

Codecov / codecov/patch

src/format/parsed.rs#L891

Added line #L891 was not covered by tests
MappedLocalTime::Single(t) => Ok(t),
MappedLocalTime::Ambiguous(..) => Err(NOT_ENOUGH),

Check warning on line 893 in src/format/parsed.rs

View check run for this annotation

Codecov / codecov/patch

src/format/parsed.rs#L893

Added line #L893 was not covered by tests
}
}

Expand Down Expand Up @@ -944,15 +944,15 @@
// 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) => {
MappedLocalTime::None => Err(IMPOSSIBLE),

Check warning on line 947 in src/format/parsed.rs

View check run for this annotation

Codecov / codecov/patch

src/format/parsed.rs#L947

Added line #L947 was not covered by tests
MappedLocalTime::Single(t) => {
if check_offset(&t) {
Ok(t)
} else {
Err(IMPOSSIBLE)
}
}
LocalResult::Ambiguous(min, max) => {
MappedLocalTime::Ambiguous(min, max) => {

Check warning on line 955 in src/format/parsed.rs

View check run for this annotation

Codecov / codecov/patch

src/format/parsed.rs#L955

Added line #L955 was not covered by tests
// 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).
//! [`MappedLocalTime`](https://docs.rs/chrono/latest/chrono/offset/enum.MappedLocalTime.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::MappedLocalTime;
//! 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(
//! MappedLocalTime::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), MappedLocalTime::None);
//! assert_eq!(Utc.with_ymd_and_hms(2014, 7, 38, 21, 15, 33), MappedLocalTime::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, MappedLocalTime};

pub mod round;
pub use round::{DurationRound, RoundingError, SubsecRound};
Expand Down
4 changes: 2 additions & 2 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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,
expect, try_opt, DateTime, Datelike, FixedOffset, MappedLocalTime, Months, TimeDelta, TimeZone,
Timelike, Weekday,
};
#[cfg(feature = "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) -> MappedLocalTime<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, MappedLocalTime, NaiveDate, TimeDelta, 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, MappedLocalTime::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, MappedLocalTime::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 @@
#[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::{MappedLocalTime, Offset, TimeZone};
use crate::format::{scan, ParseError, OUT_OF_RANGE};
use crate::naive::{NaiveDate, NaiveDateTime};

Expand Down Expand Up @@ -129,11 +129,11 @@
*offset
}

fn offset_from_local_date(&self, _local: &NaiveDate) -> LocalResult<FixedOffset> {
LocalResult::Single(*self)
fn offset_from_local_date(&self, _local: &NaiveDate) -> MappedLocalTime<FixedOffset> {
MappedLocalTime::Single(*self)

Check warning on line 133 in src/offset/fixed.rs

View check run for this annotation

Codecov / codecov/patch

src/offset/fixed.rs#L132-L133

Added lines #L132 - L133 were not covered by tests
}
fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> LocalResult<FixedOffset> {
LocalResult::Single(*self)
fn offset_from_local_datetime(&self, _local: &NaiveDateTime) -> MappedLocalTime<FixedOffset> {
MappedLocalTime::Single(*self)
}

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