authorgravatar for jens.goldberg@gmail.comJens Goldberg <jens.goldberg@gmail.com> 2021-12-30 15:12:20+00:00
committergravatar for jens.goldberg@gmail.comJens Goldberg <jens.goldberg@gmail.com> 2021-12-30 15:12:20+00:00
logbc5c4677558799696251b489d318847d92186aad
tree2184f2fd2d6fb5f4252f02dc7147c37dee7a9dfa
parent06286b05a41c6a6dcc28b62edfc41063e61e1588

Support parsing tz timezone data


2 files changed, 218 insertions(+), 0 deletions(-)

lib/std/tz.zig created+218
......@@ -0,0 +1,218 @@
1const std = @import("std.zig");
2const builtin = @import("builtin");
3
4pub const Transition = struct {
5 ts: i64,
6 timetype: *Timetype,
7};
8
9pub const Timetype = struct {
10 offset: i32,
11 flags: u8,
12 name_data: [6:0]u8,
13
14 pub fn name(self: Timetype) [:0]const u8 {
15 return std.mem.sliceTo(self.name_data[0..], 0);
16 }
17
18 pub fn isDst(self: Timetype) bool {
19 return (self.flags & 0x01) > 0;
20 }
21
22 pub fn standardTimeIndicator(self: Timetype) bool {
23 return (self.flags & 0x02) > 0;
24 }
25
26 pub fn utIndicator(self: Timetype) bool {
27 return (self.flags & 0x04) > 0;
28 }
29};
30
31pub const Leapsecond = struct {
32 occurrence: i48,
33 correction: i16,
34};
35
36pub const Tz = struct {
37 allocator: std.mem.Allocator,
38 transitions: []const Transition,
39 timetypes: []const Timetype,
40 leapseconds: []const Leapsecond,
41 footer: []const u8,
42
43 pub fn parse(allocator: std.mem.Allocator, data: []const u8) !Tz {
44 const header_size = 4 + 1 + 15 + 6 * 4;
45 if (data.len < header_size) return error.BadSize;
46
47 const magic_l = data[0..4];
48 const version_l = data[4];
49 if (!std.mem.eql(u8, magic_l, "TZif")) return error.BadHeader;
50 if (version_l != '2' and version_l != '3') return error.BadVersion;
51
52 // Parse the legacy header and skip the entire thing
53 const isutcnt_l = std.mem.readIntBig(u32, data[20..24]);
54 const isstdcnt_l = std.mem.readIntBig(u32, data[24..28]);
55 const leapcnt_l = std.mem.readIntBig(u32, data[28..32]);
56 const timecnt_l = std.mem.readIntBig(u32, data[32..36]);
57 const typecnt_l = std.mem.readIntBig(u32, data[36..40]);
58 const charcnt_l = std.mem.readIntBig(u32, data[40..44]);
59 const data_block_size_legacy = timecnt_l * 5 + typecnt_l * 6 + charcnt_l + leapcnt_l * 8 + isstdcnt_l + isutcnt_l;
60 if (data.len < header_size + data_block_size_legacy) return error.BadSize;
61
62 const data2 = data[header_size + data_block_size_legacy ..];
63 if (data2.len < header_size) return error.BadSize;
64
65 const magic = data2[0..4];
66 const version = data2[4];
67 if (!std.mem.eql(u8, magic, "TZif")) return error.BadHeader;
68 if (version != '2' and version != '3') return error.BadVersion;
69
70 const isutcnt = std.mem.readIntBig(u32, data2[20..24]);
71 const isstdcnt = std.mem.readIntBig(u32, data2[24..28]);
72 const leapcnt = std.mem.readIntBig(u32, data2[28..32]);
73 const timecnt = std.mem.readIntBig(u32, data2[32..36]);
74 const typecnt = std.mem.readIntBig(u32, data2[36..40]);
75 const charcnt = std.mem.readIntBig(u32, data2[40..44]);
76
77 if (isstdcnt != 0 and isstdcnt != typecnt) return error.Malformed; // rfc8536: isstdcnt [...] MUST either be zero or equal to "typecnt"
78 if (isutcnt != 0 and isutcnt != typecnt) return error.Malformed; // rfc8536: isutcnt [...] MUST either be zero or equal to "typecnt"
79 if (typecnt == 0) return error.Malformed; // rfc8536: typecnt [...] MUST NOT be zero
80 if (charcnt == 0) return error.Malformed; // rfc8536: charcnt [...] MUST NOT be zero
81
82 const data_block_size = timecnt * 9 + typecnt * 6 + charcnt + leapcnt * 12 + isstdcnt + isutcnt;
83 if (data2.len < header_size + data_block_size) return error.BadSize;
84
85 var leapseconds = try allocator.alloc(Leapsecond, leapcnt);
86 errdefer allocator.free(leapseconds);
87 var transitions = try allocator.alloc(Transition, timecnt);
88 errdefer allocator.free(transitions);
89 var timetypes = try allocator.alloc(Timetype, typecnt);
90 errdefer allocator.free(timetypes);
91
92 var p: usize = header_size;
93
94 // First, parse timezone designators ahead of time so that we can reject malformed files early
95 const designators = data2[header_size + timecnt * 9 + typecnt * 6 .. header_size + timecnt * 9 + typecnt * 6 + charcnt];
96 if (designators[designators.len - 1] != 0) return error.Malformed; // rfc8536: charcnt [...] includes the trailing NUL (0x00) octet
97
98 // Parse transition types
99 var i: usize = 0;
100 while (i < timecnt) : (i += 1) {
101 transitions[i].ts = std.mem.readIntSliceBig(i64, data2[p .. p + 8]);
102 p += 8;
103 }
104
105 i = 0;
106 while (i < timecnt) : (i += 1) {
107 const tt = data2[p];
108 if (tt >= timetypes.len) return error.Malformed; // rfc8536: Each type index MUST be in the range [0, "typecnt" - 1]
109 transitions[i].timetype = &timetypes[tt];
110 p += 1;
111 }
112
113 // Parse time types
114 i = 0;
115 while (i < typecnt) : (i += 1) {
116 const offset = std.mem.readIntSliceBig(i32, data2[p .. p + 4]);
117 if (offset < -2147483648) return error.Malformed; // rfc8536: utoff [...] MUST NOT be -2**31
118 const dst = data2[p + 4];
119 if (dst != 0 and dst != 1) return error.Malformed; // rfc8536: (is)dst [...] The value MUST be 0 or 1.
120 const idx = data2[p + 5];
121 if (idx > designators.len - 1) return error.Malformed; // rfc8536: (desig)idx [...] Each index MUST be in the range [0, "charcnt" - 1]
122
123 const name = std.mem.sliceTo(designators[idx..], 0);
124
125 // We are mandating the "SHOULD" 6-character limit so we can pack the struct better, and to conform to POSIX.
126 if (name.len > 6) return error.Malformed; // rfc8536: Time zone designations SHOULD consist of at least three (3) and no more than six (6) ASCII characters.
127
128 timetypes[i] = .{
129 .offset = offset,
130 .flags = dst,
131 .name_data = undefined,
132 };
133
134 std.mem.copy(u8, timetypes[i].name_data[0..], name);
135 timetypes[i].name_data[name.len] = 0;
136
137 p += 6;
138 }
139
140 // Skip the designators we got earlier
141 p += charcnt;
142
143 // Parse leap seconds
144 i = 0;
145 while (i < leapcnt) : (i += 1) {
146 const occur = std.mem.readIntSliceBig(i64, data2[p .. p + 8]);
147 if (occur < 0) return error.Malformed; // rfc8536: occur [...] MUST be nonnegative
148 if (i > 0 and leapseconds[i - 1].occurrence + 2419199 > occur) return error.Malformed; // rfc8536: occur [...] each later value MUST be at least 2419199 greater than the previous value
149 if (occur > std.math.maxInt(i48)) return error.Malformed; // Unreasonably far into the future
150
151 const corr = std.mem.readIntSliceBig(i32, data2[p + 8 .. p + 12]);
152 if (i == 0 and corr != -1 and corr != 1) return error.Malformed; // rfc8536: The correction value in the first leap-second record, if present, MUST be either one (1) or minus one (-1)
153 if (i > 0 and leapseconds[i - 1].correction != corr + 1 and leapseconds[i - 1].correction != corr - 1) return error.Malformed; // rfc8536: The correction values in adjacent leap- second records MUST differ by exactly one (1)
154 if (corr > std.math.maxInt(i16)) return error.Malformed; // Unreasonably large correction
155
156 leapseconds[i] = .{
157 .occurrence = @intCast(i48, occur),
158 .correction = @intCast(i16, corr),
159 };
160 p += 12;
161 }
162
163 // Parse standard/wall indicators
164 i = 0;
165 while (i < isstdcnt) : (i += 1) {
166 const stdtime = data2[p];
167 if (stdtime == 1) {
168 timetypes[i].flags |= 0x02;
169 }
170 p += 1;
171 }
172
173 // Parse UT/local indicators
174 i = 0;
175 while (i < isutcnt) : (i += 1) {
176 const ut = data2[p];
177 if (ut == 1) {
178 timetypes[i].flags |= 0x04;
179 if (!timetypes[i].standardTimeIndicator()) return error.Malformed; // rfc8536: standard/wall value MUST be one (1) if the UT/local value is one (1)
180 }
181 p += 1;
182 }
183
184 // Footer
185 if (data2[p..].len < 2) return error.Malformed; // rfc8536 requires at least 2 newlines
186 if (data2[p] != '\n') return error.Malformed; // Not a rfc8536 footer
187 const footer_end = std.mem.indexOfScalar(u8, data2[p + 1 ..], '\n') orelse return error.Malformed; // No 2nd rfc8536 newline
188 const footer = try allocator.dupe(u8, data2[p + 1 .. p + 1 + footer_end]);
189 errdefer allocator.free(footer);
190
191 return Tz{
192 .allocator = allocator,
193 .transitions = transitions,
194 .timetypes = timetypes,
195 .leapseconds = leapseconds,
196 .footer = footer,
197 };
198 }
199
200 pub fn deinit(self: *Tz) void {
201 self.allocator.free(self.footer);
202 self.allocator.free(self.leapseconds);
203 self.allocator.free(self.transitions);
204 self.allocator.free(self.timetypes);
205 }
206};
207
208test "parse" {
209 // Asia/Tokyo is good for embedding, as Japan only had DST for a short while during the US occupation
210 const data = @embedFile("tz/asia_tokyo.tzif");
211 var tz = try Tz.parse(std.testing.allocator, data);
212 defer tz.deinit();
213
214 try std.testing.expectEqual(tz.transitions.len, 9);
215 try std.testing.expect(std.mem.eql(u8, tz.transitions[3].timetype.name(), "JDT"));
216 try std.testing.expectEqual(tz.transitions[5].ts, -620298000); // 1950-05-06 15:00:00 (UTC)
217 try std.testing.expectEqual(tz.leapseconds[13].occurrence, 567993613); // 1988-01-01 00:00:13 (IAT)
218}
lib/std/tz/asia_tokyo.tzif created
Binary files /dev/null and b/lib/std/tz/asia_tokyo.tzif differ