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 ) )
100 if version == "2" or version == "3" then
102 For version-2-format timezone files, the above header and data is followed by a second header and data,
103 identical in format except that eight bytes are used for each transition time or leap-second time.
105 assert ( fd:read(5) == "TZif2" )
106 assert ( assert ( fd:read(15) ) == fifteen_nulls , "Expected 15 nulls" )
110 -- The number of UTC/local indicators stored in the file.
111 tzh_ttisgmtcnt = assert ( read_int32be ( fd ) )
113 -- The number of standard/wall indicators stored in the file.
114 tzh_ttisstdcnt = assert ( read_int32be ( fd ) )
116 -- The number of leap seconds for which data is stored in the file.
117 tzh_leapcnt = assert ( read_int32be ( fd ) )
119 -- The number of "transition times" for which data is stored in the file.
120 tzh_timecnt = assert ( read_int32be ( fd ) )
122 -- The number of "local time types" for which data is stored in the file (must not be zero).
123 tzh_typecnt = assert ( read_int32be ( fd ) )
125 -- The number of characters of "timezone abbreviation strings" stored in the file.
126 tzh_charcnt = assert ( read_int32be ( fd ) )
128 transition_times = { }
129 for i=1, tzh_timecnt do
130 transition_times [ i ] = assert ( read_int64be ( fd ) )
132 transition_time_ind = { assert ( fd:read ( tzh_timecnt ) ):byte ( 1 , -1 ) }
135 for i=1, tzh_typecnt do
137 gmtoff = assert ( read_int32be ( fd ) ) ;
138 isdst = assert ( fd:read ( 1 ) ) ~= "\0" ;
139 abbrind = assert ( fd:read ( 1 ) ):byte ( ) ;
143 abbreviations = assert ( fd:read ( tzh_charcnt ) )
146 for i=1, tzh_leapcnt do
147 leap_seconds [ i ] = {
148 offset = assert ( read_int64be ( fd ) ) ;
149 n = assert ( read_int32be ( fd ) ) ;
153 isstd = assert ( read_flags ( fd , tzh_ttisstdcnt ) )
155 isgmt = assert ( read_flags ( fd , tzh_ttisgmtcnt ) )
158 After the second header and data comes a newline-enclosed, POSIX-TZ-environment-variable-style string
159 for use in handling instants after the last transition time stored in the file
160 (with nothing between the newlines if there is no POSIX representation for such instants).
164 For version-3-format time zone files, the POSIX-TZ-style string may
165 use two minor extensions to the POSIX TZ format, as described in newtzset (3).
166 First, the hours part of its transition times may be signed and range from
167 -167 through 167 instead of the POSIX-required unsigned values
168 from 0 through 24. Second, DST is in effect all year if it starts
169 January 1 at 00:00 and ends December 31 at 24:00 plus the difference
170 between daylight saving and standard time.
173 assert ( assert ( fd:read ( 1 ) ) == "\n" , "Expected newline at end of version 2 header" )
175 TZ = assert ( fd:read ( "*l" ) )
181 for i=1, tzh_typecnt do
182 local v = ttinfos [ i ]
183 v.abbr = abbreviations:sub ( v.abbrind+1 , v.abbrind+3 )
184 v.isstd = isstd [ i ] or false
185 v.isgmt = isgmt [ i ] or false
186 setmetatable ( v , tt_info_mt )
190 Use the first standard-time ttinfo structure in the file
191 (or simply the first ttinfo structure in the absence of a standard-time structure)
192 if either tzh_timecnt is zero or the time argument is less than the first transition time recorded in the file.
196 for i=1, tzh_ttisstdcnt do
206 transition_time = MIN_TIME ;
207 info = ttinfos [ first ] ;
210 for i=1, tzh_timecnt do
212 transition_time = transition_times [ i ] ;
213 info = ttinfos [ transition_time_ind [ i ]+1 ] ;
216 return setmetatable ( res , tz_info_mt )
218 error ( "Unsupported version" )
222 local function read_tzfile ( path )
223 local fd = assert ( io.open ( path , "rb" ) )
224 local tzinfo = read_tz ( fd )
231 read_tzfile = read_tzfile ;