Skip to content

Commit cc457ba

Browse files
committed
Post rebase cleanup
1 parent dbad4fe commit cc457ba

File tree

3 files changed

+7
-140
lines changed

3 files changed

+7
-140
lines changed

src/components/tz.rs

Lines changed: 3 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
//! This module implements the Temporal `TimeZone` and components.
22
3-
use alloc::borrow::ToOwned;
43
use alloc::borrow::ToOwned;
54
use alloc::string::String;
65
use alloc::vec::Vec;
76
use core::{iter::Peekable, str::Chars};
87

9-
use core::{iter::Peekable, str::Chars};
10-
118
use num_traits::ToPrimitive;
129

1310
use crate::{components::Instant, iso::IsoDateTime, TemporalError, TemporalResult};
@@ -24,16 +21,16 @@ pub static TZ_PROVIDER: LazyLock<Mutex<FsTzdbProvider>> =
2421
use super::ZonedDateTime;
2522

2623
pub trait TzProvider {
27-
fn check_identifier(&mut self, identifier: &str) -> bool;
24+
fn check_identifier(&self, identifier: &str) -> bool;
2825

2926
fn get_named_tz_epoch_nanoseconds(
30-
&mut self,
27+
&self,
3128
identifier: &str,
3229
iso_datetime: IsoDateTime,
3330
) -> TemporalResult<Vec<i128>>;
3431

3532
fn get_named_tz_offset_nanoseconds(
36-
&mut self,
33+
&self,
3734
identifier: &str,
3835
epoch_nanoseconds: i128,
3936
) -> TemporalResult<i128>;
@@ -76,61 +73,17 @@ impl From<String> for TimeZone {
7673
}
7774
}
7875

79-
impl From<&str> for TimeZone {
80-
fn from(value: &str) -> Self {
81-
Self(value.to_owned())
82-
}
83-
pub enum ParsedTimeZone<'a> {
84-
IanaIdentifier { identifier: &'a str },
85-
Offset { minutes: i16 },
86-
}
87-
88-
impl<'a> ParsedTimeZone<'a> {
89-
pub fn from_str(s: &'a str, provider: &mut impl TzProvider) -> TemporalResult<Self> {
90-
if s == "Z" {
91-
return Ok(Self::Offset { minutes: 0 });
92-
}
93-
let mut cursor = s.chars().peekable();
94-
if cursor.peek().map_or(false, is_ascii_sign) {
95-
return parse_offset(&mut cursor);
96-
} else if provider.check_identifier(s) {
97-
return Ok(Self::IanaIdentifier { identifier: s });
98-
}
99-
Err(TemporalError::range().with_message("Valid time zone was not provided."))
100-
}
101-
}
102-
103-
#[derive(Debug, Clone, PartialEq, Eq)]
104-
pub struct TimeZone(pub String);
105-
106-
impl From<&ZonedDateTime> for TimeZone {
107-
fn from(value: &ZonedDateTime) -> Self {
108-
value.tz().clone()
109-
}
110-
}
111-
112-
impl From<String> for TimeZone {
113-
fn from(value: String) -> Self {
114-
Self(value)
115-
}
116-
}
117-
11876
impl From<&str> for TimeZone {
11977
fn from(value: &str) -> Self {
12078
Self(value.to_owned())
12179
}
12280
}
12381

12482
impl TimeZone {
125-
pub(crate) fn get_iso_datetime_for(
12683
pub(crate) fn get_iso_datetime_for(
12784
&self,
12885
instant: &Instant,
12986
provider: &mut impl TzProvider,
130-
) -> TemporalResult<IsoDateTime> {
131-
let nanos = self.get_offset_nanos_for(instant.epoch_nanos, provider)?;
132-
IsoDateTime::from_epoch_nanos(&instant.epoch_nanos, nanos.to_f64().unwrap_or(0.0))
133-
provider: &mut impl TzProvider,
13487
) -> TemporalResult<IsoDateTime> {
13588
let nanos = self.get_offset_nanos_for(instant.epoch_nanos, provider)?;
13689
IsoDateTime::from_epoch_nanos(&instant.epoch_nanos, nanos.to_f64().unwrap_or(0.0))
@@ -139,21 +92,6 @@ impl TimeZone {
13992

14093
impl TimeZone {
14194
/// Get the offset for this current `TimeZoneSlot`.
142-
pub fn get_offset_nanos_for(
143-
&self,
144-
epoch_ns: i128,
145-
provider: &mut impl TzProvider,
146-
) -> TemporalResult<i128> {
147-
// 1. Let parseResult be ! ParseTimeZoneIdentifier(timeZone).
148-
let parsed = ParsedTimeZone::from_str(&self.0, provider)?;
149-
match parsed {
150-
// 2. If parseResult.[[OffsetMinutes]] is not empty, return parseResult.[[OffsetMinutes]] × (60 × 10**9).
151-
ParsedTimeZone::Offset { minutes } => Ok(i128::from(minutes) * 60_000_000_000i128),
152-
// 3. Return GetNamedTimeZoneOffsetNanoseconds(parseResult.[[Name]], epochNs).
153-
ParsedTimeZone::IanaIdentifier { identifier } => {
154-
provider.get_named_tz_offset_nanoseconds(identifier, epoch_ns)
155-
}
156-
}
15795
pub fn get_offset_nanos_for(
15896
&self,
15997
epoch_ns: i128,
@@ -243,65 +181,3 @@ fn non_ascii_digit() -> TemporalError {
243181
fn is_ascii_sign(ch: &char) -> bool {
244182
*ch == '+' || *ch == '-'
245183
}
246-
247-
#[inline]
248-
fn parse_offset<'a>(chars: &mut Peekable<Chars<'_>>) -> TemporalResult<ParsedTimeZone<'a>> {
249-
let sign = chars.next().map_or(1, |c| if c == '+' { 1 } else { -1 });
250-
// First offset portion
251-
let hours = parse_digit_pair(chars)?;
252-
253-
let sep = chars.peek().map_or(false, |ch| *ch == ':');
254-
if sep {
255-
let _ = chars.next();
256-
}
257-
258-
let digit_peek = chars.peek().map(|ch| ch.is_ascii_digit());
259-
260-
let minutes = match digit_peek {
261-
Some(true) => parse_digit_pair(chars)?,
262-
Some(false) => return Err(non_ascii_digit()),
263-
None => 0,
264-
};
265-
266-
Ok(ParsedTimeZone::Offset {
267-
minutes: (hours * 60 + minutes) * sign,
268-
})
269-
}
270-
271-
fn parse_digit_pair(chars: &mut Peekable<Chars<'_>>) -> TemporalResult<i16> {
272-
let valid = chars
273-
.peek()
274-
.map_or(Err(abrupt_end()), |ch| Ok(ch.is_ascii_digit()))?;
275-
let first = if valid {
276-
chars.next().expect("validated.")
277-
} else {
278-
return Err(non_ascii_digit());
279-
};
280-
let valid = chars
281-
.peek()
282-
.map_or(Err(abrupt_end()), |ch| Ok(ch.is_ascii_digit()))?;
283-
let second = if valid {
284-
chars.next().expect("validated.")
285-
} else {
286-
return Err(non_ascii_digit());
287-
};
288-
289-
let tens = (first.to_digit(10).expect("validated") * 10) as i16;
290-
let ones = second.to_digit(10).expect("validated") as i16;
291-
292-
Ok(tens + ones)
293-
}
294-
295-
// NOTE: Spec calls for throwing a RangeError when parse node is a list of errors for timezone.
296-
297-
fn abrupt_end() -> TemporalError {
298-
TemporalError::range().with_message("Abrupt end while parsing offset string")
299-
}
300-
301-
fn non_ascii_digit() -> TemporalError {
302-
TemporalError::range().with_message("Non ascii digit found while parsing offset string")
303-
}
304-
305-
fn is_ascii_sign(ch: &char) -> bool {
306-
*ch == '+' || *ch == '-'
307-
}

src/iso.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,12 +507,6 @@ pub struct IsoTime {
507507
pub millisecond: u16, // 0..=999
508508
pub microsecond: u16, // 0..=999
509509
pub nanosecond: u16, // 0..=999
510-
pub hour: u8, // 0..=23
511-
pub minute: u8, // 0..=59
512-
pub second: u8, // 0..=59
513-
pub millisecond: u16, // 0..=999
514-
pub microsecond: u16, // 0..=999
515-
pub nanosecond: u16, // 0..=999
516510
}
517511

518512
impl IsoTime {

src/tzdb.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ use alloc::string::{String, ToString};
3434
use alloc::{vec, vec::Vec};
3535
use core::cell::RefCell;
3636

37-
use alloc::string::{String, ToString};
38-
use alloc::{vec, vec::Vec};
39-
4037
use combine::Parser;
4138

4239
use tzif::{
@@ -150,7 +147,7 @@ impl Tzif {
150147
let Ok((parse_result, _)) = tzif::parse::tzif::tzif().parse(data) else {
151148
return Err(TemporalError::general("Illformed Tzif data."));
152149
};
153-
Ok(Self(parse_result))
150+
Ok(Self::from(parse_result))
154151
}
155152

156153
fn read_tzif(identifier: &str) -> TemporalResult<Self> {
@@ -166,7 +163,7 @@ impl Tzif {
166163
}
167164

168165
pub fn posix_tz_string(&self) -> Option<&PosixTzString> {
169-
self.0.footer.as_ref()
166+
self.footer.as_ref()
170167
}
171168

172169
pub fn get_data_block2(&self) -> TemporalResult<&DataBlock> {
@@ -175,7 +172,7 @@ impl Tzif {
175172
.ok_or(TemporalError::general("Only Tzif V2+ is supported."))
176173
}
177174

178-
pub fn get(&self, epoch_seconds: &Seconds) -> TemporalResult<LocalTimeTypeRecord> {
175+
pub fn get(&self, epoch_seconds: &Seconds) -> TemporalResult<LocalTimeRecord> {
179176
let db = self.get_data_block2()?;
180177
let result = db.transition_times.binary_search(epoch_seconds);
181178

@@ -216,7 +213,7 @@ impl Tzif {
216213

217214
let estimated_idx = match b_search_result {
218215
// TODO: Double check returning early here with tests.
219-
Ok(idx) => return Ok(vec![get_local_record(db, idx)]),
216+
Ok(idx) => return Ok(get_local_record(db, idx).into()),
220217
Err(idx) if idx == 0 => {
221218
return Ok(LocalTimeRecordResult::Single(
222219
get_local_record(db, idx).into(),

0 commit comments

Comments
 (0)