Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri6037 committed Mar 1, 2024
2 parents c523a06 + 750db68 commit 2fd8274
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 78 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn parse_win_cldr_db() -> phf_codegen::Map<String> {
while let Some(item) = split.next() {
if item.trim().is_empty() {
continue;
}
}
str += "&internal_tz_new(&";
str += item;
str.push(')');
Expand Down
3 changes: 1 addition & 2 deletions src/binary_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ mod tests {
None
);
}

}
}
28 changes: 13 additions & 15 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

use time::{OffsetDateTime, PrimitiveDateTime};

use crate::{TimeZone, zoned, OffsetResult, Offset, timezones, ToTimezone, OffsetError};
use crate::{timezones, zoned, Offset, OffsetError, OffsetResult, TimeZone, ToTimezone};

mod sealing {
use crate::OffsetResult;
Expand Down Expand Up @@ -120,15 +120,13 @@ impl PrimitiveDateTimeExt for PrimitiveDateTime {
fn assume_timezone<T: TimeZone>(&self, tz: &T) -> OffsetResult<OffsetDateTime> {
match tz.get_offset_local(&self.assume_utc()) {
Ok(a) => Ok(self.assume_offset(a.to_utc())),
Err(e) => {
match e {
OffsetError::Ambiguous(a, b) => Err(OffsetError::Ambiguous(
self.assume_offset(a.to_utc()),
self.assume_offset(b.to_utc())
)),
OffsetError::None => Err(OffsetError::None)
}
}
Err(e) => match e {
OffsetError::Ambiguous(a, b) => Err(OffsetError::Ambiguous(
self.assume_offset(a.to_utc()),
self.assume_offset(b.to_utc()),
)),
OffsetError::None => Err(OffsetError::None),
},
}
}

Expand Down Expand Up @@ -175,7 +173,7 @@ impl<T> OffsetResultExt<T> for OffsetResult<T> {
fn map_all<R, F: Fn(&T) -> R>(&self, f: F) -> OffsetResult<R> {
match self {
Ok(a) => Ok(f(a)),
Err(e) => Err(e.map(f))
Err(e) => Err(e.map(f)),
}
}

Expand All @@ -190,14 +188,14 @@ impl<T> OffsetResultExt<T> for OffsetResult<T> {
fn take_first(self) -> Option<T> {
match self {
Ok(a) => Some(a),
Err(e) => e.take_first()
Err(e) => e.take_first(),
}
}

fn take_second(self) -> Option<T> {
match self {
Ok(a) => Some(a),
Err(e) => e.take_second()
Err(e) => e.take_second(),
}
}

Expand All @@ -208,14 +206,14 @@ impl<T> OffsetResultExt<T> for OffsetResult<T> {
fn is_none(&self) -> bool {
match self {
Ok(_) => false,
Err(e) => e.is_none()
Err(e) => e.is_none(),
}
}

fn is_ambiguous(&self) -> bool {
match self {
Ok(_) => false,
Err(e) => e.is_ambiguous()
Err(e) => e.is_ambiguous(),
}
}
}
16 changes: 8 additions & 8 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,63 +85,63 @@ pub enum OffsetError<T> {
Ambiguous(T, T),

/// The date time is invalid.
None
None,
}

impl<T> OffsetError<T> {
/// Returns true if this [OffsetError] is None.
pub fn is_none(&self) -> bool {
match self {
OffsetError::Ambiguous(_, _) => false,
OffsetError::None => true
OffsetError::None => true,
}
}

/// Returns true if this [OffsetError] is ambiguous.
pub fn is_ambiguous(&self) -> bool {
match self {
OffsetError::Ambiguous(_, _) => true,
OffsetError::None => false
OffsetError::None => false,
}
}

/// Unwraps this [OffsetError] resolving ambiguity by taking the first result.
pub fn unwrap_first(self) -> T {
match self {
OffsetError::Ambiguous(a, _) => a,
OffsetError::None => panic!("Attempt to unwrap an invalid offset")
OffsetError::None => panic!("Attempt to unwrap an invalid offset"),
}
}

/// Unwraps this [OffsetError] resolving ambiguity by taking the second result.
pub fn unwrap_second(self) -> T {
match self {
OffsetError::Ambiguous(_, b) => b,
OffsetError::None => panic!("Attempt to unwrap an invalid offset")
OffsetError::None => panic!("Attempt to unwrap an invalid offset"),
}
}

/// Turns this [OffsetError] into an Option resolving ambiguity by taking the first result.
pub fn take_first(self) -> Option<T> {
match self {
OffsetError::Ambiguous(a, _) => Some(a),
OffsetError::None => None
OffsetError::None => None,
}
}

/// Turns this [OffsetError] into an Option resolving ambiguity by taking the second result.
pub fn take_second(self) -> Option<T> {
match self {
OffsetError::Ambiguous(_, b) => Some(b),
OffsetError::None => None
OffsetError::None => None,
}
}

/// Maps this [OffsetError] to a different result type.
pub fn map<R, F: Fn(&T) -> R>(&self, f: F) -> OffsetError<R> {
match self {
OffsetError::Ambiguous(a, b) => OffsetError::Ambiguous(f(a), f(b)),
OffsetError::None => OffsetError::None
OffsetError::None => OffsetError::None,
}
}
}
Expand Down
63 changes: 41 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ mod timezone_impl;
#[cfg(feature = "db")]
pub mod timezones;

pub use interface::*;
pub use ext::*;
pub use interface::*;

#[cfg(feature = "system")]
pub mod system;
Expand All @@ -57,13 +57,13 @@ pub use timezone_impl::Tz;

#[cfg(test)]
mod tests {
use crate::{OffsetResultExt, ToTimezone};
use crate::timezones;
use crate::zoned::Duration;
use crate::Offset;
use crate::OffsetDateTimeExt;
use crate::PrimitiveDateTimeExt;
use crate::TimeZone;
use crate::zoned::Duration;
use crate::{OffsetResultExt, ToTimezone};
use time::macros::{datetime, offset};
use time::OffsetDateTime;

Expand Down Expand Up @@ -184,7 +184,8 @@ mod tests {
assert_eq!(
datetime!(2023-03-26 6:00 UTC)
.with_timezone(timezones::db::europe::STOCKHOLM)
.replace_time(time::Time::MIDNIGHT).unwrap_first()
.replace_time(time::Time::MIDNIGHT)
.unwrap_first()
.offset_date_time(),
datetime!(2023-03-25 23:00 UTC)
);
Expand All @@ -194,39 +195,57 @@ mod tests {
fn zoned_date_time_add_duration() {
assert_eq!(
(datetime!(2023-01-01 22:00)
.with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::days(1))
.offset_date_time(),
datetime!(2023-01-02 22:00).with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first().offset_date_time()
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
+ Duration::days(1))
.offset_date_time(),
datetime!(2023-01-02 22:00)
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
.offset_date_time()
);
assert_eq!(
(datetime!(2023-03-25 22:00)
.with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::days(1))
.offset_date_time(),
datetime!(2023-03-26 22:00).with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first().offset_date_time()
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
+ Duration::days(1))
.offset_date_time(),
datetime!(2023-03-26 22:00)
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
.offset_date_time()
);
assert_eq!(
(datetime!(2023-03-25 22:00)
.with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::hours(24))
.offset_date_time(),
datetime!(2023-03-26 23:00).with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first().offset_date_time()
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
+ Duration::hours(24))
.offset_date_time(),
datetime!(2023-03-26 23:00)
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
.offset_date_time()
);
assert_eq!(
(datetime!(2023-03-26 1:00)
.with_timezone(timezones::db::europe::STOCKHOLM).unwrap_first() + Duration::hours(1))
.offset_date_time(),
datetime!(2023-03-26 3:00).with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first().offset_date_time()
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
+ Duration::hours(1))
.offset_date_time(),
datetime!(2023-03-26 3:00)
.with_timezone(timezones::db::europe::STOCKHOLM)
.unwrap_first()
.offset_date_time()
);
}

#[test]
fn errors() {
let datetime = datetime!(2024-03-31 02:30:00).assume_timezone(timezones::db::europe::BUDAPEST);
let datetime =
datetime!(2024-03-31 02:30:00).assume_timezone(timezones::db::europe::BUDAPEST);
assert!(datetime.is_none());
let datetime = datetime!(2024-03-31 02:29:00).assume_timezone(timezones::db::europe::BUDAPEST);
let datetime =
datetime!(2024-03-31 02:29:00).assume_timezone(timezones::db::europe::BUDAPEST);
assert!(datetime.is_none());
}
}
6 changes: 3 additions & 3 deletions src/posix_tz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::{Offset, TimeZone, timezone_impl::Tz, ToTimezone};
use crate::{timezone_impl::Tz, Offset, TimeZone, ToTimezone};
use std::fmt::{Display, Formatter};
use thiserror::Error;
use time::{OffsetDateTime, UtcOffset};
Expand Down Expand Up @@ -91,7 +91,7 @@ pub enum Error {

/// The resulting date time would be out of range.
#[error("resulting date time would be out of range")]
OutOfRange
OutOfRange,
}

/// A POSIX "timezone" offset.
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<'a> PosixTz<'a> {
let offset = self.get_offset(date_time)?;
match date_time.checked_to_offset(offset.to_utc()) {
Some(v) => Ok(v),
None => Err(Error::OutOfRange)
None => Err(Error::OutOfRange),
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//! Support for getting time zone information from the target system.
//!
//!
//! Currently only supported for Windows, Unix, and WASM targets.
use crate::timezones::get_by_name;
use crate::timezone_impl::Tz;
use crate::timezones::get_by_name;
use thiserror::Error;

#[cfg(target_family = "wasm")]
use js_sys::{ Intl, Reflect, Array, Object };
use js_sys::{Array, Intl, Object, Reflect};
#[cfg(target_family = "wasm")]
use wasm_bindgen::JsValue;

Expand All @@ -61,16 +61,16 @@ pub enum Error {
/// The timezone doesn't exist in the crate's database.
#[error("unknown timezone name")]
Unknown,

/// The target platform is not supported. Windows, Unix, and WASM targets are the only supported for the system feature at this moment.
#[error("unsupported platform")]
Unsupported,
}

/// Gets the current timezone from the system.
///
///
/// Currently only supported for Windows, Unix, and WASM targets.
///
///
/// # Errors
/// Returns an [Error](enum@Error) if the timezone cannot be determined.
pub fn get_timezone() -> Result<&'static Tz, Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/timezones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use crate::ToTimezone;
use crate::timezone_impl::internal_tz_new;
use crate::timezone_impl::FixedTimespan;
use crate::timezone_impl::FixedTimespanSet;
use crate::ToTimezone;

use crate::timezone_impl::Tz;
use phf::Map;
Expand Down
Loading

0 comments on commit 2fd8274

Please sign in to comment.