Skip to content

Commit ed7ed3a

Browse files
committed
Update CompiledTransitions to use struct over string
1 parent 4fd4c85 commit ed7ed3a

File tree

3 files changed

+78
-74
lines changed

3 files changed

+78
-74
lines changed

provider/src/tzif.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ impl ZeroTzif<'_> {
8282
let mapped_local_records: Vec<LocalTimeRecord> =
8383
tzif.local_time_types.iter().map(Into::into).collect();
8484
let types = ZeroVec::alloc_from_slice(&mapped_local_records);
85-
let posix = ZeroVec::alloc_from_slice(data.posix_string.as_bytes());
85+
// TODO: handle this much better.
86+
let posix = ZeroVec::alloc_from_slice(
87+
data.posix_time_zone
88+
.to_string()
89+
.expect("to_string only should only fail on write failures")
90+
.as_bytes(),
91+
);
8692

8793
Self {
8894
transitions,

zoneinfo/src/compiler.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub struct CompiledTransitions {
9999
/// The POSIX time zone string
100100
///
101101
/// This string should be used to calculate the time zone beyond the last available transition.
102-
pub posix_string: String, // TODO: Implement POSIX string building
102+
pub posix_time_zone: PosixTimeZone,
103103
}
104104

105105
// NOTE: candidate for removal? Should this library offer TZif structs long term?
@@ -186,15 +186,12 @@ impl ZoneInfoCompiler {
186186
}
187187
}
188188

189-
let posix_string = zone_table
190-
.get_posix_time_zone()
191-
.to_string()
192-
.expect("to_string only throws when a `write!` fails.");
189+
let posix_time_zone = zone_table.get_posix_time_zone();
193190

194191
CompiledTransitions {
195192
initial_record,
196193
transitions,
197-
posix_string,
194+
posix_time_zone,
198195
}
199196
}
200197

zoneinfo/src/posix.rs

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,14 @@ use crate::{
77
use alloc::string::String;
88
use core::fmt::Write;
99

10-
#[derive(Debug)]
11-
pub struct MonthWeekDay(pub Month, pub u8, pub WeekDay);
12-
13-
#[derive(Debug)]
14-
pub enum PosixDate {
15-
JulianNoLeap(u16),
16-
JulianLeap(u16),
17-
MonthWeekDay(MonthWeekDay),
18-
}
19-
20-
impl PosixDate {
21-
pub(crate) fn from_rule(rule: &Rule) -> Self {
22-
match rule.on_date {
23-
DayOfMonth::Day(day) if rule.in_month == Month::Jan || rule.in_month == Month::Feb => {
24-
PosixDate::JulianNoLeap(month_to_day(rule.in_month as u8, 1) as u16 + day as u16)
25-
}
26-
DayOfMonth::Day(day) => {
27-
PosixDate::JulianNoLeap(month_to_day(rule.in_month as u8, 1) as u16 + day as u16)
28-
}
29-
DayOfMonth::Last(wd) => PosixDate::MonthWeekDay(MonthWeekDay(rule.in_month, 5, wd)),
30-
DayOfMonth::WeekDayGEThanMonthDay(week_day, day_of_month) => {
31-
let week = 1 + (day_of_month - 1) / 7;
32-
PosixDate::MonthWeekDay(MonthWeekDay(rule.in_month, week, week_day))
33-
}
34-
DayOfMonth::WeekDayLEThanMonthDay(week_day, day_of_month) => {
35-
let week = day_of_month / 7;
36-
PosixDate::MonthWeekDay(MonthWeekDay(rule.in_month, week, week_day))
37-
}
38-
}
39-
}
40-
}
41-
42-
#[derive(Debug)]
43-
pub struct PosixDateTime {
44-
pub date: PosixDate,
45-
pub time: Time,
46-
}
47-
48-
impl PosixDateTime {
49-
pub(crate) fn from_rule_and_transition_info(rule: &Rule, offset: Time, savings: Time) -> Self {
50-
let date = PosixDate::from_rule(rule);
51-
let time = match rule.at {
52-
QualifiedTime::Local(time) => time,
53-
QualifiedTime::Standard(standard_time) => standard_time.add(rule.save),
54-
QualifiedTime::Universal(universal_time) => universal_time.add(offset).add(savings),
55-
};
56-
Self { date, time }
57-
}
58-
}
59-
6010
#[non_exhaustive]
61-
#[derive(Debug)]
62-
pub struct PosixTransition {
63-
pub abbr: PosixAbbreviation,
64-
pub savings: Time,
65-
pub start: PosixDateTime,
66-
pub end: PosixDateTime,
67-
}
68-
69-
#[non_exhaustive]
70-
#[derive(Debug)]
11+
#[derive(Debug, PartialEq)]
7112
pub struct PosixTimeZone {
7213
pub abbr: PosixAbbreviation,
7314
pub offset: Time,
7415
pub transition_info: Option<PosixTransition>,
7516
}
7617

77-
#[non_exhaustive]
78-
#[derive(Debug)]
79-
pub struct PosixAbbreviation {
80-
is_numeric: bool,
81-
formatted: String,
82-
}
83-
8418
impl PosixTimeZone {
8519
pub(crate) fn from_zone_and_savings(entry: &ZoneEntry, savings: Time) -> Self {
8620
let offset = entry.std_offset.add(savings);
@@ -167,6 +101,73 @@ impl PosixTimeZone {
167101
}
168102
}
169103

104+
#[non_exhaustive]
105+
#[derive(Debug, PartialEq)]
106+
pub struct PosixTransition {
107+
pub abbr: PosixAbbreviation,
108+
pub savings: Time,
109+
pub start: PosixDateTime,
110+
pub end: PosixDateTime,
111+
}
112+
113+
#[non_exhaustive]
114+
#[derive(Debug, PartialEq)]
115+
pub struct PosixAbbreviation {
116+
is_numeric: bool,
117+
formatted: String,
118+
}
119+
#[derive(Debug, PartialEq)]
120+
pub struct MonthWeekDay(pub Month, pub u8, pub WeekDay);
121+
122+
#[derive(Debug, PartialEq)]
123+
pub enum PosixDate {
124+
JulianNoLeap(u16),
125+
JulianLeap(u16),
126+
MonthWeekDay(MonthWeekDay),
127+
}
128+
129+
impl PosixDate {
130+
pub(crate) fn from_rule(rule: &Rule) -> Self {
131+
match rule.on_date {
132+
DayOfMonth::Day(day) if rule.in_month == Month::Jan || rule.in_month == Month::Feb => {
133+
PosixDate::JulianNoLeap(month_to_day(rule.in_month as u8, 1) as u16 + day as u16)
134+
}
135+
DayOfMonth::Day(day) => {
136+
PosixDate::JulianNoLeap(month_to_day(rule.in_month as u8, 1) as u16 + day as u16)
137+
}
138+
DayOfMonth::Last(wd) => PosixDate::MonthWeekDay(MonthWeekDay(rule.in_month, 5, wd)),
139+
DayOfMonth::WeekDayGEThanMonthDay(week_day, day_of_month) => {
140+
let week = 1 + (day_of_month - 1) / 7;
141+
PosixDate::MonthWeekDay(MonthWeekDay(rule.in_month, week, week_day))
142+
}
143+
DayOfMonth::WeekDayLEThanMonthDay(week_day, day_of_month) => {
144+
let week = day_of_month / 7;
145+
PosixDate::MonthWeekDay(MonthWeekDay(rule.in_month, week, week_day))
146+
}
147+
}
148+
}
149+
}
150+
151+
#[derive(Debug, PartialEq)]
152+
pub struct PosixDateTime {
153+
pub date: PosixDate,
154+
pub time: Time,
155+
}
156+
157+
impl PosixDateTime {
158+
pub(crate) fn from_rule_and_transition_info(rule: &Rule, offset: Time, savings: Time) -> Self {
159+
let date = PosixDate::from_rule(rule);
160+
let time = match rule.at {
161+
QualifiedTime::Local(time) => time,
162+
QualifiedTime::Standard(standard_time) => standard_time.add(rule.save),
163+
QualifiedTime::Universal(universal_time) => universal_time.add(offset).add(savings),
164+
};
165+
Self { date, time }
166+
}
167+
}
168+
169+
// ==== Helper functions ====
170+
170171
fn is_numeric(str: &str) -> bool {
171172
str.parse::<i16>().is_ok()
172173
}

0 commit comments

Comments
 (0)