Debian Codegraph @

debian/missing-sources/jiff/src/tz/testdata.rs

raw
1 #[cfg(not(miri))]
2 use crate::tz::tzif::TzifOwned;
3
4 /// A concatenated list of TZif data with a header and an index block.
5 ///
6 /// This was exactracted from an Android emulator file system via `adb`.
7 pub(crate) static ANDROID_CONCATENATED_TZIF: &'static [u8] =
8 include_bytes!("testdata/android/tzdata");
9
10 /// A list of all TZif files in our testdata directory.
11 ///
12 /// Feel free to add more if there are other "interesting" cases. Note that
13 /// some tests iterate over this slice to check each entry. So adding a new
14 /// test file here might require updating some tests.
15 ///
16 /// # Revisions
17 ///
18 /// * 2024-03-27: Initial set pulled from my local copy of `tzdata 2024a`.
19 /// * 2024-07-05: Added `UTC`.
20 /// * 2024-11-30: Added special Sydney time zone from RHEL8.
21 pub(crate) static TZIF_TEST_FILES: &[TzifTestFile] = &[
22 TzifTestFile {
23 name: "America/New_York",
24 data: include_bytes!("testdata/america-new-york.tzif"),
25 },
26 TzifTestFile {
27 name: "America/Sitka",
28 data: include_bytes!("testdata/america-sitka.tzif"),
29 },
30 TzifTestFile {
31 name: "America/St_Johns",
32 data: include_bytes!("testdata/america-st-johns.tzif"),
33 },
34 TzifTestFile {
35 name: "right/America/New_York",
36 data: include_bytes!("testdata/right-america-new-york.tzif"),
37 },
38 TzifTestFile {
39 name: "Antarctica/Troll",
40 data: include_bytes!("testdata/antarctica-troll.tzif"),
41 },
42 TzifTestFile {
43 name: "Australia/Tasmania",
44 data: include_bytes!("testdata/australia-tasmania.tzif"),
45 },
46 TzifTestFile {
47 name: "Europe/Dublin",
48 data: include_bytes!("testdata/europe-dublin.tzif"),
49 },
50 TzifTestFile {
51 name: "Pacific/Honolulu",
52 data: include_bytes!("testdata/pacific-honolulu.tzif"),
53 },
54 // This TZif file comes from a RHEL8 installation[1]. Apparently RHEL
55 // added a special first transition far back in the past. It is probably
56 // similar in purpose to what Jiff does internally, which is to add a
57 // dummy "minimum" transition. We test this case here because RHEL's dummy
58 // transition falls outside of Jiff's supported `Timestamp` range, which
59 // did result in an error at parse time. But we should be able to parse it,
60 // so we test it here.
61 //
62 // [1]: https://github.com/BurntSushi/jiff/issues/163
63 TzifTestFile {
64 name: "Australia/Sydney/RHEL8",
65 data: include_bytes!("testdata/australia-sydney-rhel8.tzif"),
66 },
67 // I added this to test finding previous time zone transitions in a time
68 // zone that had somewhat recently eliminated DST. The bug was that Jiff
69 // wasn't reporting *any* previous time zone transitions.
70 TzifTestFile {
71 name: "America/Sao_Paulo",
72 data: include_bytes!("testdata/america-sao-paulo.tzif"),
73 },
74 // Another test file I added for a region that eliminated DST and thus
75 // has a "final" time zone transition.
76 TzifTestFile {
77 name: "America/Boa_Vista",
78 data: include_bytes!("testdata/america-boa-vista.tzif"),
79 },
80 TzifTestFile { name: "UTC", data: include_bytes!("testdata/utc.tzif") },
81 ];
82
83 /// A single TZif datum.
84 ///
85 /// It contains the name of the time zone and the raw bytes of the
86 /// corresponding TZif file.
87 #[derive(Clone, Copy)]
88 pub(crate) struct TzifTestFile {
89 pub(crate) name: &'static str,
90 pub(crate) data: &'static [u8],
91 }
92
93 impl TzifTestFile {
94 /// Look up the TZif test file for the given time zone name.
95 ///
96 /// If one doesn't exist, then this panics and fails the current
97 /// test.
98 pub(crate) fn get(name: &str) -> TzifTestFile {
99 for &tzif_file in TZIF_TEST_FILES {
100 if tzif_file.name == name {
101 return tzif_file;
102 }
103 }
104 panic!("could not find TZif test file for {name:?}")
105 }
106
107 /// Parse this test TZif data into a structured representation.
108 #[cfg(not(miri))]
109 pub(crate) fn parse(self) -> TzifOwned {
110 use alloc::string::ToString;
111
112 let name = Some(self.name.to_string());
113 TzifOwned::parse(name, self.data).unwrap_or_else(|err| {
114 panic!("failed to parse TZif test file for {:?}: {err}", self.name)
115 })
116 }
117
118 /// Parse this test TZif data as if it were V1.
119 #[cfg(not(miri))]
120 pub(crate) fn parse_v1(self) -> TzifOwned {
121 use alloc::string::ToString;
122
123 let name = Some(self.name.to_string());
124 let mut data = self.data.to_vec();
125 data[4] = 0;
126 TzifOwned::parse(name, &data).unwrap_or_else(|err| {
127 panic!(
128 "failed to parse V1 TZif test file for {:?}: {err}",
129 self.name
130 )
131 })
132 }
133 }