Skip to content

Commit

Permalink
Make timezone name optional
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Sep 9, 2024
1 parent 2e95510 commit 0acd431
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
16 changes: 12 additions & 4 deletions chrono-tz-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ fn format_rest(rest: Vec<(i64, FixedTimespan)>) -> String {
},
) in rest
{
let timespan_name = match name.as_ref() {
"%z" => None,
name => Some(name),
};
ret.push_str(&format!(
" ({start}, FixedTimespan {{ \
utc_offset: {utc}, dst_offset: {dst}, name: \"{name}\" \
utc_offset: {utc}, dst_offset: {dst}, name: {name:?} \
}}),\n",
start = start,
utc = utc_offset,
dst = dst_offset,
name = name,
name = timespan_name,
));
}
ret.push_str(" ]");
Expand Down Expand Up @@ -230,6 +234,10 @@ impl FromStr for Tz {{
for zone in &zones {
let timespans = table.timespans(zone).unwrap();
let zone_name = convert_bad_chars(zone);
let timespan_name = match timespans.first.name.as_ref() {
"%z" => None,
name => Some(name),
};
writeln!(
timezone_file,
" Tz::{zone} => {{
Expand All @@ -238,7 +246,7 @@ impl FromStr for Tz {{
first: FixedTimespan {{
utc_offset: {utc},
dst_offset: {dst},
name: \"{name}\",
name: {name:?},
}},
rest: REST
}}
Expand All @@ -247,7 +255,7 @@ impl FromStr for Tz {{
rest = format_rest(timespans.rest),
utc = timespans.first.utc_offset,
dst = timespans.first.dst_offset,
name = timespans.first.name,
name = timespan_name,
)?;
}
write!(
Expand Down
41 changes: 33 additions & 8 deletions chrono-tz/src/timezone_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::cmp::Ordering;
use core::fmt::{Debug, Display, Error, Formatter};
use core::fmt::{Debug, Display, Error, Formatter, Write};

use chrono::{
Duration, FixedOffset, LocalResult, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone,
Expand All @@ -26,7 +26,7 @@ pub struct FixedTimespan {
/// The additional offset from UTC for this timespan; typically for daylight saving time
pub dst_offset: i32,
/// The name of this timezone, for example the difference between `EDT`/`EST`
pub name: &'static str,
pub name: Option<&'static str>,
}

impl Offset for FixedTimespan {
Expand All @@ -37,13 +37,38 @@ impl Offset for FixedTimespan {

impl Display for FixedTimespan {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "{}", self.name)
if let Some(name) = self.name {
return write!(f, "{}", name);
}
let offset = self.utc_offset + self.dst_offset;
let (sign, off) = if offset < 0 {
('-', -offset)
} else {
('+', offset)
};

let minutes = off / 60;
let secs = (off % 60) as u8;
let mins = (minutes % 60) as u8;
let hours = (minutes / 60) as u8;

assert!(
secs == 0,
"numeric names are not used if the offset has fractional minutes"
);

f.write_char(sign)?;
write!(f, "{:02}", hours)?;
if mins != 0 {
write!(f, "{:02}", mins)?;
}
Ok(())
}
}

impl Debug for FixedTimespan {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
write!(f, "{}", self.name)
Display::fmt(self, f)
}
}

Expand Down Expand Up @@ -104,13 +129,13 @@ pub trait OffsetComponents {
/// let london_time = London.ymd(2016, 2, 10).and_hms(12, 0, 0);
/// assert_eq!(london_time.offset().tz_id(), "Europe/London");
/// // London is normally on GMT
/// assert_eq!(london_time.offset().abbreviation(), "GMT");
/// assert_eq!(london_time.offset().abbreviation(), Some("GMT"));
///
/// let london_summer_time = London.ymd(2016, 5, 10).and_hms(12, 0, 0);
/// // The TZ ID remains constant year round
/// assert_eq!(london_summer_time.offset().tz_id(), "Europe/London");
/// // During the summer, this becomes British Summer Time
/// assert_eq!(london_summer_time.offset().abbreviation(), "BST");
/// assert_eq!(london_summer_time.offset().abbreviation(), Some("BST"));
/// # }
/// ```
pub trait OffsetName {
Expand All @@ -121,7 +146,7 @@ pub trait OffsetName {
/// This takes into account any special offsets that may be in effect.
/// For example, at a given instant, the time zone with ID *America/New_York*
/// may be either *EST* or *EDT*.
fn abbreviation(&self) -> &str;
fn abbreviation(&self) -> Option<&str>;
}

impl TzOffset {
Expand Down Expand Up @@ -155,7 +180,7 @@ impl OffsetName for TzOffset {
self.tz.name()
}

fn abbreviation(&self) -> &str {
fn abbreviation(&self) -> Option<&str> {
self.offset.name
}
}
Expand Down
40 changes: 20 additions & 20 deletions chrono-tz/src/timezones.rs.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,79 +39,79 @@ impl TimeSpans for Tz {
match *self {
Tz::America__New_York => {
const REST: &'static [(i64, FixedTimespan)] = &[
(-2717650800, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: "EST" }),
(-1633280400, FixedTimespan { utc_offset: -18000, dst_offset: 3600, name: "EDT" }),
(-1615140000, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: "EST" }),
(-2717650800, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: Some("EST") }),
(-1633280400, FixedTimespan { utc_offset: -18000, dst_offset: 3600, name: Some("EDT") }),
(-1615140000, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: Some("EST") }),
];
FixedTimespanSet {
first: FixedTimespan {
utc_offset: -17762,
dst_offset: 0,
name: "LMT",
name: Some("LMT"),
},
rest: REST
}
},

Tz::America__Toronto => {
const REST: &'static [(i64, FixedTimespan)] = &[
(-2366736148, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: "EST" }),
(-1632070800, FixedTimespan { utc_offset: -18000, dst_offset: 3600, name: "EDT" }),
(-1615140000, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: "EST" }),
(-2366736148, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: Some("EST") }),
(-1632070800, FixedTimespan { utc_offset: -18000, dst_offset: 3600, name: Some("EDT") }),
(-1615140000, FixedTimespan { utc_offset: -18000, dst_offset: 0, name: Some("EST") }),
];
FixedTimespanSet {
first: FixedTimespan {
utc_offset: -19052,
dst_offset: 0,
name: "LMT",
name: Some("LMT"),
},
rest: REST
}
},

Tz::Europe__London => {
const REST: &'static [(i64, FixedTimespan)] = &[
(-3852662325, FixedTimespan { utc_offset: 0, dst_offset: 0, name: "GMT" }),
(-1691964000, FixedTimespan { utc_offset: 0, dst_offset: 3600, name: "BST" }),
(-1680472800, FixedTimespan { utc_offset: 0, dst_offset: 0, name: "GMT" }),
(-3852662325, FixedTimespan { utc_offset: 0, dst_offset: 0, name: Some("GMT") }),
(-1691964000, FixedTimespan { utc_offset: 0, dst_offset: 3600, name: Some("BST") }),
(-1680472800, FixedTimespan { utc_offset: 0, dst_offset: 0, name: Some("GMT") }),
];
FixedTimespanSet {
first: FixedTimespan {
utc_offset: -75,
dst_offset: 0,
name: "LMT",
name: Some("LMT"),
},
rest: REST
}
},

Tz::Europe__Moscow => {
const REST: &'static [(i64, FixedTimespan)] = &[
(-2840149817, FixedTimespan { utc_offset: 9017, dst_offset: 0, name: "MMT" }),
(-1688265017, FixedTimespan { utc_offset: 9079, dst_offset: 0, name: "MMT" }),
(-1656819079, FixedTimespan { utc_offset: 9079, dst_offset: 3600, name: "MST" }),
(-2840149817, FixedTimespan { utc_offset: 9017, dst_offset: 0, name: Some("MMT") }),
(-1688265017, FixedTimespan { utc_offset: 9079, dst_offset: 0, name: Some("MMT") }),
(-1656819079, FixedTimespan { utc_offset: 9079, dst_offset: 3600, name: Some("MST") }),
];
FixedTimespanSet {
first: FixedTimespan {
utc_offset: 9017,
dst_offset: 0,
name: "LMT",
name: Some("LMT"),
},
rest: REST
}
},

Tz::Europe__Rome => {
const REST: &'static [(i64, FixedTimespan)] = &[
(-3259097396, FixedTimespan { utc_offset: 2996, dst_offset: 0, name: "RMT" }),
(-2403564596, FixedTimespan { utc_offset: 3600, dst_offset: 0, name: "CET" }),
(-1690851600, FixedTimespan { utc_offset: 3600, dst_offset: 3600, name: "CEST" }),
(-3259097396, FixedTimespan { utc_offset: 2996, dst_offset: 0, name: Some("RMT") }),
(-2403564596, FixedTimespan { utc_offset: 3600, dst_offset: 0, name: Some("CET") }),
(-1690851600, FixedTimespan { utc_offset: 3600, dst_offset: 3600, name: Some("CEST") }),
];
FixedTimespanSet {
first: FixedTimespan {
utc_offset: 2996,
dst_offset: 0,
name: "LMT",
name: Some("LMT"),
},
rest: REST
}
Expand Down

0 comments on commit 0acd431

Please sign in to comment.