Skip to content
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
53 changes: 49 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ zoneinfo_rs = { version = "~0.0.14", path = "./zoneinfo" }
# Dependencies
tinystr = "0.8.1"
icu_calendar = { version = "2.0.3", default-features = false }
icu_time = { version = "2.0.0", default-features = false }
icu_locale = "2.0.0"
rustc-hash = "2.1.0"
num-traits = { version = "0.2.19", default-features = false }
Expand Down
8 changes: 8 additions & 0 deletions provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ tzif = ["dep:tzif",
"dep:combine",
"std"]

# Performing timezone resolution using the `zoneinfo64` crate
# (ICU4C zoneinfo64.res)
zoneinfo64 = ["dep:zoneinfo64", "dep:icu_time"]

[dependencies]

# Provider dependency
Expand All @@ -52,12 +56,16 @@ tinystr = { workspace = true, features = ["zerovec"] }

# IANA dependency
zoneinfo_rs = { workspace = true, features = ["std"], optional = true }
icu_time = { workspace = true, optional = true}

# tzif dependency
tzif = { workspace = true, optional = true }
jiff-tzdb = { workspace = true, optional = true }
combine = { workspace = true, optional = true }

# zoneinfo64 dependency
zoneinfo64 = { version = "0.1.0", optional = true }

# Databake dependencies
serde = { version = "1.0.219", features = ["derive"], optional = true }
databake = { version = "0.2.0", features = ["derive"], optional = true }
Expand Down
8 changes: 7 additions & 1 deletion provider/src/epoch_nanoseconds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::TimeZoneProviderError;
#[doc(hidden)]
pub const NS_PER_DAY: u64 = MS_PER_DAY as u64 * 1_000_000;

const NS_IN_S: i128 = 1_000_000_000;
pub(crate) const NS_IN_S: i128 = 1_000_000_000;

/// Milliseconds per day constant: 8.64e+7
pub(crate) const MS_PER_DAY: u32 = 24 * 60 * 60 * 1000;
Expand Down Expand Up @@ -55,3 +55,9 @@ impl From<tzif::data::time::Seconds> for EpochNanoseconds {
EpochNanoseconds::from_seconds(value.0)
}
}

#[inline]
#[cfg(any(feature = "tzif", feature = "zoneinfo64"))]
pub(crate) fn seconds_to_nanoseconds(seconds: i64) -> i128 {
seconds as i128 * NS_IN_S
}
3 changes: 3 additions & 0 deletions provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub mod tzif;
#[cfg(feature = "experimental_tzif")]
pub mod experimental_tzif;

#[cfg(feature = "zoneinfo64")]
pub mod zoneinfo64;

pub mod epoch_nanoseconds;

#[doc(hidden)]
Expand Down
40 changes: 12 additions & 28 deletions provider/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::utils;
use crate::{epoch_nanoseconds::EpochNanoseconds, TimeZoneProviderError};
use alloc::borrow::Cow;

pub(crate) type TimeZoneProviderResult<T> = Result<T, TimeZoneProviderError>;

/// `UtcOffsetSeconds` represents the amount of seconds we need to add to the UTC to reach the local time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct UtcOffsetSeconds(pub i64);
Expand Down Expand Up @@ -82,15 +84,6 @@ pub struct EpochNanosecondsAndOffset {
pub offset: UtcOffsetSeconds,
}

/// `TimeZoneTransitionInfo` represents information about a timezone transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeZoneTransitionInfo {
/// The transition time epoch at which the offset needs to be applied.
pub transition_epoch: Option<i64>,
/// The time zone offset in seconds.
pub offset: UtcOffsetSeconds,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TransitionDirection {
Next,
Expand Down Expand Up @@ -189,61 +182,52 @@ impl CandidateEpochNanoseconds {
/// The `TimeZoneProvider` trait provides methods required for a provider
/// to implement in order to source time zone data from that provider.
pub trait TimeZoneProvider {
fn normalize_identifier(&self, ident: &'_ [u8]) -> Result<Cow<'_, str>, TimeZoneProviderError>;
fn normalize_identifier(&self, ident: &'_ [u8]) -> TimeZoneProviderResult<Cow<'_, str>>;

fn canonicalize_identifier(
&self,
ident: &'_ [u8],
) -> Result<Cow<'_, str>, TimeZoneProviderError>;
fn canonicalize_identifier(&self, ident: &'_ [u8]) -> TimeZoneProviderResult<Cow<'_, str>>;

fn get_named_tz_epoch_nanoseconds(
&self,
identifier: &str,
local_datetime: IsoDateTime,
) -> Result<CandidateEpochNanoseconds, TimeZoneProviderError>;
) -> TimeZoneProviderResult<CandidateEpochNanoseconds>;

fn get_named_tz_offset_nanoseconds(
&self,
identifier: &str,
epoch_nanoseconds: i128,
) -> Result<TimeZoneTransitionInfo, TimeZoneProviderError>;
) -> TimeZoneProviderResult<UtcOffsetSeconds>;

fn get_named_tz_transition(
&self,
identifier: &str,
epoch_nanoseconds: i128,
direction: TransitionDirection,
) -> Result<Option<EpochNanoseconds>, TimeZoneProviderError>;
) -> TimeZoneProviderResult<Option<EpochNanoseconds>>;
}

pub struct NeverProvider;

impl TimeZoneProvider for NeverProvider {
fn normalize_identifier(
&self,
_ident: &'_ [u8],
) -> Result<Cow<'_, str>, TimeZoneProviderError> {
fn normalize_identifier(&self, _ident: &'_ [u8]) -> TimeZoneProviderResult<Cow<'_, str>> {
unimplemented!()
}
fn canonicalize_identifier(
&self,
_ident: &'_ [u8],
) -> Result<Cow<'_, str>, TimeZoneProviderError> {
fn canonicalize_identifier(&self, _ident: &'_ [u8]) -> TimeZoneProviderResult<Cow<'_, str>> {
unimplemented!()
}
fn get_named_tz_epoch_nanoseconds(
&self,
_: &str,
_: IsoDateTime,
) -> Result<CandidateEpochNanoseconds, TimeZoneProviderError> {
) -> TimeZoneProviderResult<CandidateEpochNanoseconds> {
unimplemented!()
}

fn get_named_tz_offset_nanoseconds(
&self,
_: &str,
_: i128,
) -> Result<TimeZoneTransitionInfo, TimeZoneProviderError> {
) -> TimeZoneProviderResult<UtcOffsetSeconds> {
unimplemented!()
}

Expand All @@ -252,7 +236,7 @@ impl TimeZoneProvider for NeverProvider {
_: &str,
_: i128,
_: TransitionDirection,
) -> Result<Option<EpochNanoseconds>, TimeZoneProviderError> {
) -> TimeZoneProviderResult<Option<EpochNanoseconds>> {
unimplemented!()
}
}
41 changes: 41 additions & 0 deletions provider/src/tzdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

use alloc::borrow::Cow;

#[cfg(any(feature = "tzif", feature = "zoneinfo64"))]
use crate::provider::TimeZoneProviderResult;
#[cfg(any(feature = "tzif", feature = "zoneinfo64"))]
use crate::TimeZoneProviderError;
#[cfg(any(feature = "tzif", feature = "zoneinfo64"))]
use crate::SINGLETON_IANA_NORMALIZER;
use zerotrie::ZeroAsciiIgnoreCaseTrie;
use zerovec::{VarZeroVec, ZeroVec};

Expand Down Expand Up @@ -37,3 +43,38 @@ pub struct IanaIdentifierNormalizer<'data> {
#[cfg_attr(feature = "datagen", serde(borrow))]
pub normalized_identifiers: VarZeroVec<'data, str>,
}

#[cfg(any(feature = "tzif", feature = "zoneinfo64"))]
pub(crate) fn normalize_identifier_with_compiled(
identifier: &[u8],
) -> TimeZoneProviderResult<Cow<'static, str>> {
if let Some(index) = SINGLETON_IANA_NORMALIZER.available_id_index.get(identifier) {
return SINGLETON_IANA_NORMALIZER
.normalized_identifiers
.get(index)
.map(Cow::Borrowed)
.ok_or(TimeZoneProviderError::Range("Unknown time zone identifier"));
}

Err(TimeZoneProviderError::Range("Unknown time zone identifier"))
}

#[cfg(any(feature = "tzif", feature = "zoneinfo64"))]
pub(crate) fn canonicalize_identifier_with_compiled(
identifier: &[u8],
) -> TimeZoneProviderResult<Cow<'static, str>> {
let idx = SINGLETON_IANA_NORMALIZER
.non_canonical_identifiers
.get(identifier)
.or(SINGLETON_IANA_NORMALIZER.available_id_index.get(identifier));

if let Some(index) = idx {
return SINGLETON_IANA_NORMALIZER
.normalized_identifiers
.get(index)
.map(Cow::Borrowed)
.ok_or(TimeZoneProviderError::Range("Unknown time zone identifier"));
}

Err(TimeZoneProviderError::Range("Unknown time zone identifier"))
}
Loading
Loading