All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 local tz_info_mt = require "luatz.tzinfo".tz_info_mt
2 local tt_info_mt = require "luatz.tzinfo".tt_info_mt
5 local function read_int32be ( fd )
6 local data , err = fd:read ( 4 )
7 if data == nil then return nil , err end
8 local o1 , o2 , o3 , o4 = data:byte ( 1 , 4 )
10 local unsigned = o4 + o3*2^8 + o2*2^16 + o1*2^24
11 if unsigned >= 2^31 then
12 return unsigned - 2^32
18 local function read_int64be ( fd )
19 local data , err = fd:read ( 8 )
20 if data == nil then return nil , err end
21 local o1 , o2 , o3 , o4 , o5 , o6 , o7 , o8 = data:byte ( 1 , 8 )
23 local unsigned = o8 + o7*2^8 + o6*2^16 + o5*2^24 + o4*2^32 + o3*2^40 + o2*2^48 + o1*2^56
24 if unsigned >= 2^63 then
25 return unsigned - 2^64
31 local function read_flags ( fd , n )
32 local data , err = fd:read ( n )
33 if data == nil then return nil , err end
37 res[i] = data:byte(i,i) ~= 0
42 local fifteen_nulls = ("\0"):rep(15)
43 local function read_tz ( fd )
44 assert ( fd:read(4) == "TZif" , "Invalid TZ file" )
45 local version = assert ( fd:read(1) )
46 if version == "\0" or version == "2" then
47 local MIN_TIME = -2^32+1
49 assert ( assert ( fd:read(15) ) == fifteen_nulls , "Expected 15 nulls" )
51 -- The number of UTC/local indicators stored in the file.
52 local tzh_ttisgmtcnt = assert ( read_int32be ( fd ) )
54 -- The number of standard/wall indicators stored in the file.
55 local tzh_ttisstdcnt = assert ( read_int32be ( fd ) )
57 -- The number of leap seconds for which data is stored in the file.
58 local tzh_leapcnt = assert ( read_int32be ( fd ) )
60 -- The number of "transition times" for which data is stored in the file.
61 local tzh_timecnt = assert ( read_int32be ( fd ) )
63 -- The number of "local time types" for which data is stored in the file (must not be zero).
64 local tzh_typecnt = assert ( read_int32be ( fd ) )
66 -- The number of characters of "timezone abbreviation strings" stored in the file.
67 local tzh_charcnt = assert ( read_int32be ( fd ) )
69 local transition_times = { }
70 for i=1, tzh_timecnt do
71 transition_times [ i ] = assert ( read_int32be ( fd ) )
73 local transition_time_ind = { assert ( fd:read ( tzh_timecnt ) ):byte ( 1 , -1 ) }
76 for i=1, tzh_typecnt do
78 gmtoff = assert ( read_int32be ( fd ) ) ;
79 isdst = assert ( fd:read ( 1 ) ) ~= "\0" ;
80 abbrind = assert ( fd:read ( 1 ) ):byte ( ) ;
84 local abbreviations = assert ( fd:read ( tzh_charcnt ) )
86 local leap_seconds = { }
87 for i=1, tzh_leapcnt do
88 leap_seconds [ i ] = {
89 offset = assert ( read_int32be ( fd ) ) ;
90 n = assert ( read_int32be ( fd ) ) ;
94 local isstd = assert ( read_flags ( fd , tzh_ttisstdcnt ) )
96 local isgmt = assert ( read_flags ( fd , tzh_ttisgmtcnt ) )
98 if version == "2" then
100 For version-2-format timezone files, the above header and data is followed by a second header and data,
101 identical in format except that eight bytes are used for each transition time or leap-second time.
103 assert ( fd:read(5) == "TZif2" )
104 assert ( assert ( fd:read(15) ) == fifteen_nulls , "Expected 15 nulls" )
108 -- The number of UTC/local indicators stored in the file.
109 tzh_ttisgmtcnt = assert ( read_int32be ( fd ) )
111 -- The number of standard/wall indicators stored in the file.
112 tzh_ttisstdcnt = assert ( read_int32be ( fd ) )
114 -- The number of leap seconds for which data is stored in the file.
115 tzh_leapcnt = assert ( read_int32be ( fd ) )
117 -- The number of "transition times" for which data is stored in the file.
118 tzh_timecnt = assert ( read_int32be ( fd ) )
120 -- The number of "local time types" for which data is stored in the file (must not be zero).
121 tzh_typecnt = assert ( read_int32be ( fd ) )
123 -- The number of characters of "timezone abbreviation strings" stored in the file.
124 tzh_charcnt = assert ( read_int32be ( fd ) )
126 transition_times = { }
127 for i=1, tzh_timecnt do
128 transition_times [ i ] = assert ( read_int64be ( fd ) )
130 transition_time_ind = { assert ( fd:read ( tzh_timecnt ) ):byte ( 1 , -1 ) }
133 for i=1, tzh_typecnt do
135 gmtoff = assert ( read_int32be ( fd ) ) ;
136 isdst = assert ( fd:read ( 1 ) ) ~= "\0" ;
137 abbrind = assert ( fd:read ( 1 ) ):byte ( ) ;
141 abbreviations = assert ( fd:read ( tzh_charcnt ) )
144 for i=1, tzh_leapcnt do
145 leap_seconds [ i ] = {
146 offset = assert ( read_int64be ( fd ) ) ;
147 n = assert ( read_int32be ( fd ) ) ;
151 isstd = assert ( read_flags ( fd , tzh_ttisstdcnt ) )
153 isgmt = assert ( read_flags ( fd , tzh_ttisgmtcnt ) )
156 After the second header and data comes a newline-enclosed, POSIX-TZ-environment-variable-style string
157 for use in handling instants after the last transition time stored in the file
158 (with nothing between the newlines if there is no POSIX representation for such instants).
162 for i=1, tzh_typecnt do
163 local v = ttinfos [ i ]
164 v.abbr = abbreviations:sub ( v.abbrind+1 , v.abbrind+3 )
165 v.isstd = isstd [ i ] or false
166 v.isgmt = isgmt [ i ] or false
167 setmetatable ( v , tt_info_mt )
171 Use the first standard-time ttinfo structure in the file
172 (or simply the first ttinfo structure in the absence of a standard-time structure)
173 if either tzh_timecnt is zero or the time argument is less than the first transition time recorded in the file.
177 for i=1, tzh_ttisstdcnt do
187 transition_time = MIN_TIME ;
188 info = ttinfos [ first ] ;
191 for i=1, tzh_timecnt do
193 transition_time = transition_times [ i ] ;
194 info = ttinfos [ transition_time_ind [ i ]+1 ] ;
197 return setmetatable ( res , tz_info_mt )
199 error ( "Unsupported version" )
203 local function read_tzfile ( path )
204 local fd = assert ( io.open ( path , "rb" ) )
205 local tzinfo = read_tz ( fd )
212 read_tzfile = read_tzfile ;