Skip to content
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

Bump to 0.9.1 with 2024b #185

Merged
merged 4 commits into from
Sep 9, 2024
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
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
2 changes: 1 addition & 1 deletion chrono-tz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chrono-tz"
version = "0.9.0"
version = "0.10.0"
edition = "2021"
rust-version = "1.60"
build = "build.rs"
Expand Down
18 changes: 18 additions & 0 deletions chrono-tz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ pub use crate::IANA_TZDB_VERSION;

#[cfg(test)]
mod tests {
use super::Africa::Addis_Ababa;
use super::America::Danmarkshavn;
use super::America::Scoresbysund;
use super::Antarctica::Casey;
use super::Asia::Dhaka;
use super::Australia::Adelaide;
use super::Europe::Berlin;
Expand Down Expand Up @@ -496,4 +499,19 @@ mod tests {
assert_eq!(4, numbers.len());
assert!(IANA_TZDB_VERSION.ends_with(|c: char| c.is_ascii_lowercase()));
}

#[test]
fn test_numeric_names() {
let dt = Scoresbysund
.with_ymd_and_hms(2024, 05, 01, 0, 0, 0)
.unwrap();
assert_eq!(format!("{}", dt.offset()), "-01");
assert_eq!(format!("{:?}", dt.offset()), "-01");
let dt = Casey.with_ymd_and_hms(2022, 11, 01, 0, 0, 0).unwrap();
assert_eq!(format!("{}", dt.offset()), "+11");
assert_eq!(format!("{:?}", dt.offset()), "+11");
let dt = Addis_Ababa.with_ymd_and_hms(1937, 02, 01, 0, 0, 0).unwrap();
assert_eq!(format!("{}", dt.offset()), "+0245");
assert_eq!(format!("{:?}", dt.offset()), "+0245");
}
}
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
2 changes: 1 addition & 1 deletion chrono-tz/tz
Submodule tz updated 37 files
+6 −2 .gitignore
+11 −7 CONTRIBUTING
+216 −196 Makefile
+122 −2 NEWS
+42 −31 africa
+29 −29 antarctica
+16 −3 asctime.c
+235 −207 asia
+65 −65 australasia
+14 −1 backward
+27 −8 backzone
+2 −2 checknow.awk
+27 −27 etcetera
+450 −263 europe
+15 −15 leap-seconds.list
+17 −74 localtime.c
+58 −44 newctime.3
+15 −15 newstrftime.3
+53 −13 newtzset.3
+104 −41 northamerica
+69 −45 private.h
+243 −243 southamerica
+143 −71 theory.html
+5 −0 tz-art.html
+68 −45 tz-link.html
+29 −15 tzfile.5
+5 −3 tzfile.h
+55 −70 tzselect.ksh
+1 −2 workman.sh
+2 −1 zdump.8
+5 −5 zdump.c
+24 −18 zic.8
+15 −16 zic.c
+14 −18 ziguard.awk
+1 −2 zone.tab
+1 −2 zone1970.tab
+2 −6 zonenow.tab
Loading