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 gettime = require "luatz.gettime".gettime
2 local tz_info_mt = require "luatz.tzinfo".tz_info_mt
4 local function read_int32be ( fd )
5 local data , err = fd:read ( 4 )
6 if data == nil then return nil , err end
7 local o1 , o2 , o3 , o4 = data:byte ( 1 , 4 )
9 local unsigned = o4 + o3*2^8 + o2*2^16 + o1*2^24
10 if unsigned >= 2^31 then
11 return unsigned - 2^32
17 local function read_int64be ( fd )
18 local data , err = fd:read ( 8 )
19 if data == nil then return nil , err end
20 local o1 , o2 , o3 , o4 , o5 , o6 , o7 , o8 = data:byte ( 1 , 8 )
22 local unsigned = o8 + o7*2^8 + o6*2^16 + o5*2^24 + o4*2^32 + o3*2^40 + o2*2^48 + o1*2^56
23 if unsigned >= 2^63 then
24 return unsigned - 2^64
30 local function read_flags ( fd , n )
31 local data , err = fd:read ( n )
32 if data == nil then return nil , err end
36 res[i] = data:byte(i,i) ~= 0
41 local fifteen_nulls = ("\0"):rep(15)
42 local function read_tz ( fd )
43 assert ( fd:read(4) == "TZif" )
44 local version = assert ( fd:read(1) )
45 if version == "\0" or version == "2" then
46 local MIN_TIME = -2^32+1
48 assert ( assert ( fd:read(15) ) == fifteen_nulls , "Expected 15 nulls" )
50 -- The number of UTC/local indicators stored in the file.
51 local tzh_ttisgmtcnt = assert ( read_int32be ( fd ) )
53 -- The number of standard/wall indicators stored in the file.
54 local tzh_ttisstdcnt = assert ( read_int32be ( fd ) )
56 -- The number of leap seconds for which data is stored in the file.
57 local tzh_leapcnt = assert ( read_int32be ( fd ) )
59 -- The number of "transition times" for which data is stored in the file.
60 local tzh_timecnt = assert ( read_int32be ( fd ) )
62 -- The number of "local time types" for which data is stored in the file (must not be zero).
63 local tzh_typecnt = assert ( read_int32be ( fd ) )
65 -- The number of characters of "timezone abbreviation strings" stored in the file.
66 local tzh_charcnt = assert ( read_int32be ( fd ) )
68 local transition_times = { }
69 for i=1, tzh_timecnt do
70 transition_times [ i ] = assert ( read_int32be ( fd ) )
72 local transition_time_ind = { assert ( fd:read ( tzh_timecnt ) ):byte ( 1 , -1 ) }
75 for i=1, tzh_typecnt do
77 gmtoff = assert ( read_int32be ( fd ) ) ;
78 isdst = assert ( fd:read ( 1 ) ) ~= "\0" ;
79 abbrind = assert ( fd:read ( 1 ) ):byte ( ) ;
83 local abbreviations = assert ( fd:read ( tzh_charcnt ) )
85 local leap_seconds = { }
86 for i=1, tzh_leapcnt do
87 leap_seconds [ i ] = {
88 offset = assert ( read_int32be ( fd ) ) ;
89 n = assert ( read_int32be ( fd ) ) ;
93 local isstd = assert ( read_flags ( fd , tzh_ttisstdcnt ) )
95 local isgmt = assert ( read_flags ( fd , tzh_ttisgmtcnt ) )
97 if version == "2" then
99 For version-2-format timezone files, the above header and data is followed by a second header and data,
100 identical in format except that eight bytes are used for each transition time or leap-second time.
102 assert ( fd:read(5) == "TZif2" )
103 assert ( assert ( fd:read(15) ) == fifteen_nulls , "Expected 15 nulls" )
107 -- The number of UTC/local indicators stored in the file.
108 tzh_ttisgmtcnt = assert ( read_int32be ( fd ) )
110 -- The number of standard/wall indicators stored in the file.
111 tzh_ttisstdcnt = assert ( read_int32be ( fd ) )
113 -- The number of leap seconds for which data is stored in the file.
114 tzh_leapcnt = assert ( read_int32be ( fd ) )
116 -- The number of "transition times" for which data is stored in the file.
117 tzh_timecnt = assert ( read_int32be ( fd ) )
119 -- The number of "local time types" for which data is stored in the file (must not be zero).
120 tzh_typecnt = assert ( read_int32be ( fd ) )
122 -- The number of characters of "timezone abbreviation strings" stored in the file.
123 tzh_charcnt = assert ( read_int32be ( fd ) )
125 transition_times = { }
126 for i=1, tzh_timecnt do
127 transition_times [ i ] = assert ( read_int64be ( fd ) )
129 transition_time_ind = { assert ( fd:read ( tzh_timecnt ) ):byte ( 1 , -1 ) }
132 for i=1, tzh_typecnt do
134 gmtoff = assert ( read_int32be ( fd ) ) ;
135 isdst = assert ( fd:read ( 1 ) ) ~= "\0" ;
136 abbrind = assert ( fd:read ( 1 ) ):byte ( ) ;
140 abbreviations = assert ( fd:read ( tzh_charcnt ) )
143 for i=1, tzh_leapcnt do
144 leap_seconds [ i ] = {
145 offset = assert ( read_int64be ( fd ) ) ;
146 n = assert ( read_int32be ( fd ) ) ;
150 isstd = assert ( read_flags ( fd , tzh_ttisstdcnt ) )
152 isgmt = assert ( read_flags ( fd , tzh_ttisgmtcnt ) )
155 After the second header and data comes a newline-enclosed, POSIX-TZ-environment-variable-style string
156 for use in handling instants after the last transition time stored in the file
157 (with nothing between the newlines if there is no POSIX representation for such instants).
161 for i=1, tzh_typecnt do
162 local v = ttinfos [ i ]
163 v.abbr = abbreviations:sub ( v.abbrind+1 , v.abbrind+3 )
164 v.isstd = isstd [ i ] or false
165 v.isgmt = isgmt [ i ] or false
169 Use the first standard-time ttinfo structure in the file
170 (or simply the first ttinfo structure in the absence of a standard-time structure)
171 if either tzh_timecnt is zero or the time argument is less than the first transition time recorded in the file.
175 for i=1, tzh_ttisstdcnt do
185 transition_time = MIN_TIME ;
186 info = ttinfos [ first ] ;
189 for i=1, tzh_timecnt do
191 transition_time = transition_times [ i ] ;
192 info = ttinfos [ transition_time_ind [ i ]+1 ] ;
195 return setmetatable ( res , tz_info_mt )
197 error ( "Unsupported version" )
201 local function read_tzfile ( path )
202 local fd = assert ( io.open ( path ) )
203 local tzinfo = read_tz ( fd )
210 read_tzfile = read_tzfile ;