parse_zoneinfo/
table.rs

1//! Collecting parsed zoneinfo data lines into a set of time zone data.
2//!
3//! This module provides the `Table` struct, which is able to take parsed
4//! lines of input from the `line` module and coalesce them into a single
5//! set of data.
6//!
7//! It’s not as simple as it seems, because the zoneinfo data lines refer to
8//! each other through strings: lines of the form “link zone A to B” could be
9//! *parsed* successfully but still fail to be *interpreted* successfully if
10//! “B” doesn’t exist. So it has to check every step of the way—nothing wrong
11//! with this, it’s just a consequence of reading data from a text file.
12//!
13//! This module only deals with constructing a table from data: any analysis
14//! of the data is done elsewhere.
15//!
16//!
17//! ## Example
18//!
19//! ```
20//! use parse_zoneinfo::line::{Zone, Line, Link};
21//! use parse_zoneinfo::table::TableBuilder;
22//!
23//! let mut builder = TableBuilder::new();
24//!
25//! let zone = "Zone  Pacific/Auckland  11:39:04  -  LMT  1868  Nov  2";
26//! let link = "Link  Pacific/Auckland  Antarctica/McMurdo";
27//!
28//! for line in [zone, link] {
29//!     builder.add_line(Line::new(&line)?).unwrap();
30//! }
31//!
32//! let table = builder.build();
33//!
34//! assert!(table.get_zoneset("Pacific/Auckland").is_some());
35//! assert!(table.get_zoneset("Antarctica/McMurdo").is_some());
36//! assert!(table.get_zoneset("UTC").is_none());
37//! # Ok::<(), parse_zoneinfo::line::Error>(())
38//! ```
39
40use std::collections::hash_map::{Entry, HashMap};
41use std::fmt;
42
43use crate::line::{self, ChangeTime, DaySpec, Line, Month, TimeType, Year};
44
45/// A **table** of all the data in one or more zoneinfo files.
46#[derive(PartialEq, Debug, Default)]
47pub struct Table {
48    /// Mapping of ruleset names to rulesets.
49    pub rulesets: HashMap<String, Vec<RuleInfo>>,
50
51    /// Mapping of zoneset names to zonesets.
52    pub zonesets: HashMap<String, Vec<ZoneInfo>>,
53
54    /// Mapping of link timezone names, to the names they link to.
55    pub links: HashMap<String, String>,
56}
57
58impl Table {
59    /// Tries to find the zoneset with the given name by looking it up in
60    /// either the zonesets map or the links map.
61    pub fn get_zoneset(&self, zone_name: &str) -> Option<&[ZoneInfo]> {
62        if self.zonesets.contains_key(zone_name) {
63            Some(&*self.zonesets[zone_name])
64        } else if self.links.contains_key(zone_name) {
65            let target = &self.links[zone_name];
66            Some(&*self.zonesets[target])
67        } else {
68            None
69        }
70    }
71}
72
73/// An owned rule definition line.
74///
75/// This mimics the `Rule` struct in the `line` module, only its uses owned
76/// Strings instead of string slices, and has had some pre-processing
77/// applied to it.
78#[derive(PartialEq, Debug)]
79pub struct RuleInfo {
80    /// The year that this rule *starts* applying.
81    pub from_year: Year,
82
83    /// The year that this rule *finishes* applying, inclusive, or `None` if
84    /// it applies up until the end of this timespan.
85    pub to_year: Option<Year>,
86
87    /// The month it applies on.
88    pub month: Month,
89
90    /// The day it applies on.
91    pub day: DaySpec,
92
93    /// The exact time it applies on.
94    pub time: i64,
95
96    /// The type of time that time is.
97    pub time_type: TimeType,
98
99    /// The amount of time to save.
100    pub time_to_add: i64,
101
102    /// Any extra letters that should be added to this time zone’s
103    /// abbreviation, in place of `%s`.
104    pub letters: Option<String>,
105}
106
107impl<'line> From<line::Rule<'line>> for RuleInfo {
108    fn from(info: line::Rule) -> RuleInfo {
109        RuleInfo {
110            from_year: info.from_year,
111            to_year: info.to_year,
112            month: info.month,
113            day: info.day,
114            time: info.time.0.as_seconds(),
115            time_type: info.time.1,
116            time_to_add: info.time_to_add.as_seconds(),
117            letters: info.letters.map(str::to_owned),
118        }
119    }
120}
121
122impl RuleInfo {
123    /// Returns whether this rule is in effect during the given year.
124    pub fn applies_to_year(&self, year: i64) -> bool {
125        use line::Year::*;
126
127        match (self.from_year, self.to_year) {
128            (Number(from), None) => year == from,
129            (Number(from), Some(Maximum)) => year >= from,
130            (Number(from), Some(Number(to))) => year >= from && year <= to,
131            _ => unreachable!(),
132        }
133    }
134
135    pub fn absolute_datetime(&self, year: i64, utc_offset: i64, dst_offset: i64) -> i64 {
136        let offset = match self.time_type {
137            TimeType::UTC => 0,
138            TimeType::Standard => utc_offset,
139            TimeType::Wall => utc_offset + dst_offset,
140        };
141
142        let changetime = ChangeTime::UntilDay(Year::Number(year), self.month, self.day);
143        let unused = 0;
144        changetime.to_timestamp(unused, unused) + self.time - offset
145    }
146}
147
148/// An owned zone definition line.
149///
150/// This struct mimics the `ZoneInfo` struct in the `line` module, *not* the
151/// `Zone` struct, which is the key name in the map—this is just the value.
152///
153/// As with `RuleInfo`, this struct uses owned Strings rather than string
154/// slices.
155#[derive(PartialEq, Debug)]
156pub struct ZoneInfo {
157    /// The number of seconds that need to be added to UTC to get the
158    /// standard time in this zone.
159    pub offset: i64,
160
161    /// The name of all the rules that should apply in the time zone, or the
162    /// amount of daylight-saving time to add.
163    pub saving: Saving,
164
165    /// The format for time zone abbreviations.
166    pub format: Format,
167
168    /// The time at which the rules change for this time zone, or `None` if
169    /// these rules are in effect until the end of time (!).
170    pub end_time: Option<ChangeTime>,
171}
172
173impl<'line> From<line::ZoneInfo<'line>> for ZoneInfo {
174    fn from(info: line::ZoneInfo) -> ZoneInfo {
175        ZoneInfo {
176            offset: info.utc_offset.as_seconds(),
177            saving: match info.saving {
178                line::Saving::NoSaving => Saving::NoSaving,
179                line::Saving::Multiple(s) => Saving::Multiple(s.to_owned()),
180                line::Saving::OneOff(t) => Saving::OneOff(t.as_seconds()),
181            },
182            format: Format::new(info.format),
183            end_time: info.time,
184        }
185    }
186}
187
188/// The amount of daylight saving time (DST) to apply to this timespan. This
189/// is a special type for a certain field in a zone line, which can hold
190/// different types of value.
191///
192/// This is the owned version of the `Saving` type in the `line` module.
193#[derive(PartialEq, Debug)]
194pub enum Saving {
195    /// Just stick to the base offset.
196    NoSaving,
197
198    /// This amount of time should be saved while this timespan is in effect.
199    /// (This is the equivalent to there being a single one-off rule with the
200    /// given amount of time to save).
201    OneOff(i64),
202
203    /// All rules with the given name should apply while this timespan is in
204    /// effect.
205    Multiple(String),
206}
207
208/// The format string to generate a time zone abbreviation from.
209#[derive(PartialEq, Debug, Clone)]
210pub enum Format {
211    /// A constant format, which remains the same throughout both standard
212    /// and DST timespans.
213    Constant(String),
214
215    /// An alternate format, such as “PST/PDT”, which changes between
216    /// standard and DST timespans.
217    Alternate {
218        /// Abbreviation to use during Standard Time.
219        standard: String,
220
221        /// Abbreviation to use during Summer Time.
222        dst: String,
223    },
224
225    /// A format with a placeholder `%s`, which uses the `letters` field in
226    /// a `RuleInfo` to generate the time zone abbreviation.
227    Placeholder(String),
228}
229
230impl Format {
231    /// Convert the template into one of the `Format` variants. This can’t
232    /// fail, as any syntax that doesn’t match one of the two formats will
233    /// just be a ‘constant’ format.
234    pub fn new(template: &str) -> Format {
235        if let Some(pos) = template.find('/') {
236            Format::Alternate {
237                standard: template[..pos].to_owned(),
238                dst: template[pos + 1..].to_owned(),
239            }
240        } else if template.contains("%s") {
241            Format::Placeholder(template.to_owned())
242        } else {
243            Format::Constant(template.to_owned())
244        }
245    }
246
247    pub fn format(&self, dst_offset: i64, letters: Option<&String>) -> String {
248        let letters = match letters {
249            Some(l) => &**l,
250            None => "",
251        };
252
253        match *self {
254            Format::Constant(ref s) => s.clone(),
255            Format::Placeholder(ref s) => s.replace("%s", letters),
256            Format::Alternate { ref standard, .. } if dst_offset == 0 => standard.clone(),
257            Format::Alternate { ref dst, .. } => dst.clone(),
258        }
259    }
260
261    pub fn format_constant(&self) -> String {
262        if let Format::Constant(ref s) = *self {
263            s.clone()
264        } else {
265            panic!("Expected a constant formatting string");
266        }
267    }
268}
269
270/// A builder for `Table` values based on various line definitions.
271#[derive(PartialEq, Debug)]
272pub struct TableBuilder {
273    /// The table that’s being built up.
274    table: Table,
275
276    /// If the last line was a zone definition, then this holds its name.
277    /// `None` otherwise. This is so continuation lines can be added to the
278    /// same zone as the original zone line.
279    current_zoneset_name: Option<String>,
280}
281
282impl Default for TableBuilder {
283    fn default() -> Self {
284        Self::new()
285    }
286}
287
288impl TableBuilder {
289    /// Creates a new builder with an empty table.
290    pub fn new() -> TableBuilder {
291        TableBuilder {
292            table: Table::default(),
293            current_zoneset_name: None,
294        }
295    }
296
297    pub fn add_line<'line>(&mut self, line: Line<'line>) -> Result<(), Error<'line>> {
298        match line {
299            Line::Zone(zone) => self.add_zone_line(zone),
300            Line::Continuation(cont) => self.add_continuation_line(cont),
301            Line::Rule(rule) => self.add_rule_line(rule),
302            Line::Link(link) => self.add_link_line(link),
303            Line::Space => Ok(()),
304        }
305    }
306
307    /// Adds a new line describing a zone definition.
308    ///
309    /// Returns an error if there’s already a zone with the same name, or the
310    /// zone refers to a ruleset that hasn’t been defined yet.
311    pub fn add_zone_line<'line>(
312        &mut self,
313        zone_line: line::Zone<'line>,
314    ) -> Result<(), Error<'line>> {
315        if let line::Saving::Multiple(ruleset_name) = zone_line.info.saving {
316            if !self.table.rulesets.contains_key(ruleset_name) {
317                return Err(Error::UnknownRuleset(ruleset_name));
318            }
319        }
320
321        let zoneset = match self.table.zonesets.entry(zone_line.name.to_owned()) {
322            Entry::Occupied(_) => return Err(Error::DuplicateZone),
323            Entry::Vacant(e) => e.insert(Vec::new()),
324        };
325
326        zoneset.push(zone_line.info.into());
327        self.current_zoneset_name = Some(zone_line.name.to_owned());
328        Ok(())
329    }
330
331    /// Adds a new line describing the *continuation* of a zone definition.
332    ///
333    /// Returns an error if the builder wasn’t expecting a continuation line
334    /// (meaning, the previous line wasn’t a zone line)
335    pub fn add_continuation_line<'line>(
336        &mut self,
337        continuation_line: line::ZoneInfo<'line>,
338    ) -> Result<(), Error<'line>> {
339        let zoneset = match self.current_zoneset_name {
340            Some(ref name) => self.table.zonesets.get_mut(name).unwrap(),
341            None => return Err(Error::SurpriseContinuationLine),
342        };
343
344        zoneset.push(continuation_line.into());
345        Ok(())
346    }
347
348    /// Adds a new line describing one entry in a ruleset, creating that set
349    /// if it didn’t exist already.
350    pub fn add_rule_line<'line>(
351        &mut self,
352        rule_line: line::Rule<'line>,
353    ) -> Result<(), Error<'line>> {
354        let ruleset = self
355            .table
356            .rulesets
357            .entry(rule_line.name.to_owned())
358            .or_default();
359
360        ruleset.push(rule_line.into());
361        self.current_zoneset_name = None;
362        Ok(())
363    }
364
365    /// Adds a new line linking one zone to another.
366    ///
367    /// Returns an error if there was already a link with that name.
368    pub fn add_link_line<'line>(
369        &mut self,
370        link_line: line::Link<'line>,
371    ) -> Result<(), Error<'line>> {
372        match self.table.links.entry(link_line.new.to_owned()) {
373            Entry::Occupied(_) => Err(Error::DuplicateLink(link_line.new)),
374            Entry::Vacant(e) => {
375                let _ = e.insert(link_line.existing.to_owned());
376                self.current_zoneset_name = None;
377                Ok(())
378            }
379        }
380    }
381
382    /// Returns the table after it’s finished being built.
383    pub fn build(self) -> Table {
384        self.table
385    }
386}
387
388/// Something that can go wrong while constructing a `Table`.
389#[derive(PartialEq, Debug, Copy, Clone)]
390pub enum Error<'line> {
391    /// A continuation line was passed in, but the previous line wasn’t a zone
392    /// definition line.
393    SurpriseContinuationLine,
394
395    /// A zone definition referred to a ruleset that hadn’t been defined.
396    UnknownRuleset(&'line str),
397
398    /// A link line was passed in, but there’s already a link with that name.
399    DuplicateLink(&'line str),
400
401    /// A zone line was passed in, but there’s already a zone with that name.
402    DuplicateZone,
403}
404
405impl<'line> fmt::Display for Error<'line> {
406    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
407        match self {
408            Error::SurpriseContinuationLine => {
409                write!(
410                    f,
411                    "continuation line follows line that isn't a zone definition line"
412                )
413            }
414            Error::UnknownRuleset(_) => {
415                write!(f, "zone definition refers to a ruleset that isn't defined")
416            }
417            Error::DuplicateLink(_) => write!(f, "link line with name that already exists"),
418            Error::DuplicateZone => write!(f, "zone line with name that already exists"),
419        }
420    }
421}
422
423impl<'line> std::error::Error for Error<'line> {}