-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtz.lua
76 lines (61 loc) · 1.5 KB
/
tz.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- tz
local M = {}
local tstart = 0
local tend = 0
local toffset = 0
local config = require "config"
function M.exists(zone)
return file.exists(zone + ".zone")
end
function M.getzones()
local result = {}
for fn, _ in pairs(file.list()) do
local _, _, prefix = string.find(fn, "(.*).zone")
if prefix then
table.insert(result, prefix)
end
end
return result
end
function M.load(t)
local z = file.open(config.tz .. ".zone", "r")
local hdr = z:read(20)
local magic = struct.unpack("c4 B", hdr)
if magic == "TZif" then
local lens = z:read(24)
local ttisgmt_count, ttisdstcnt, leapcnt, timecnt, typecnt, charcnt = struct.unpack("> LLLLLL", lens)
local times = z:read(4 * timecnt)
local typeindex = z:read(timecnt)
local ttinfos = z:read(6 * typecnt)
z:close()
local offset = 1
local tt
for i = 1, timecnt do
tt = struct.unpack(">l", times, (i - 1) * 4 + 1)
if t < tt then
offset = (i - 2)
tend = tt
break
end
tstart = tt
end
local tindex = struct.unpack("B", typeindex, offset + 1)
toffset = struct.unpack(">l", ttinfos, tindex * 6 + 1)
else
tend = 0x7fffffff
tstart = 0
end
end
function M.getoffset(t)
if t < tstart or t >= tend then
-- Ignore errors
local ok, msg = pcall(function ()
M.load(t)
end)
if not ok then
print (msg)
end
end
return toffset, tstart, tend
end
return M