Skip to content

Add to_string functionality for timezone identifer #161

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

Merged
merged 2 commits into from
Jan 16, 2025
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: 50 additions & 3 deletions src/components/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use core::{iter::Peekable, str::Chars};
use num_traits::ToPrimitive;

use crate::components::duration::DateDuration;
use crate::Calendar;
use crate::parsers::{FormattableOffset, FormattableTime, Precision};
use crate::{
components::{duration::normalized::NormalizedTimeDuration, EpochNanoseconds, Instant},
iso::{IsoDate, IsoDateTime, IsoTime},
options::Disambiguation,
TemporalError, TemporalResult, ZonedDateTime,
};
use crate::{Calendar, Sign};

#[cfg(feature = "experimental")]
use crate::tzdb::FsTzdbProvider;
Expand Down Expand Up @@ -74,6 +75,7 @@ impl TzProvider for NeverProvider {
}
}

// TODO: migrate to Cow<'a, str>
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum TimeZone {
IanaIdentifier(String),
Expand Down Expand Up @@ -216,8 +218,31 @@ impl TimeZone {
}

/// Returns the current `TimeZoneSlot`'s identifier.
pub fn id(&self) -> TemporalResult<String> {
Err(TemporalError::range().with_message("Not yet implemented."))
pub fn identifier(&self) -> TemporalResult<String> {
match self {
TimeZone::IanaIdentifier(s) => Ok(s.clone()),
TimeZone::OffsetMinutes(m) => {
let sign = if *m < 0 {
Sign::Negative
} else {
Sign::Positive
};
let hour = (m.abs() / 60) as u8;
let minute = (m.abs() % 60) as u8;
let formattable_offset = FormattableOffset {
sign,
time: FormattableTime {
hour,
minute,
second: 0,
nanosecond: 0,
precision: Precision::Minute,
include_sep: true,
},
};
Ok(formattable_offset.to_string())
}
}
}
}

Expand Down Expand Up @@ -475,3 +500,25 @@ fn non_ascii_digit() -> TemporalError {
fn is_ascii_sign(ch: &char) -> bool {
*ch == '+' || *ch == '-'
}

#[cfg(all(test, feature = "tzdb"))]
mod tests {
use super::TimeZone;
use crate::tzdb::FsTzdbProvider;

#[test]
fn from_and_to_string() {
let provider = &FsTzdbProvider::default();
let src = "+09:30";
let tz = TimeZone::try_from_str_with_provider(src, provider).unwrap();
assert_eq!(tz.identifier().unwrap(), src);

let src = "-09:30";
let tz = TimeZone::try_from_str_with_provider(src, provider).unwrap();
assert_eq!(tz.identifier().unwrap(), src);

let src = "-12:30";
let tz = TimeZone::try_from_str_with_provider(src, provider).unwrap();
assert_eq!(tz.identifier().unwrap(), src);
}
}
2 changes: 1 addition & 1 deletion src/components/zoneddatetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ impl ZonedDateTime {
let offset = self.tz.get_offset_nanos_for(result, provider)?;
let datetime = self.tz.get_iso_datetime_for(&self.instant, provider)?;
let (sign, hour, minute) = nanoseconds_to_formattable_offset_minutes(offset)?;
let timezone_id = self.timezone().id()?;
let timezone_id = self.timezone().identifier()?;

let ixdtf_string = IxdtfStringBuilder::default()
.with_date(datetime.date)
Expand Down
Loading