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 timetable_mt = require "luatz.timetable".timetable_mt
4 local function to_timestamp ( o )
5 if type ( ts_local ) == "number" then
7 elseif getmetatable ( o ) == timetable_mt then
8 return ts_local:timestamp ( )
12 local tz_info_methods = { }
14 __index = tz_info_methods ;
17 __tostring = function ( self )
18 return string.format ( "tt_info:%s=%d" , self.abbr , self.gmtoff )
23 local function _find_current ( tzinfo , target , i , j )
24 if i >= j then return j end
26 local half = math.ceil ( (j+i) / 2 )
28 if target >= tzinfo [ half ].transition_time then
29 return _find_current ( tzinfo , target , half , j )
31 return _find_current ( tzinfo , target , i , half-1 )
35 local function find_current_local ( tzinfo , ts_local )
36 -- Find two best possibilities by searching back and forward a day (assumes transition is never by more than 24 hours)
37 local tz_first = _find_current ( tzinfo , ts_local-86400 , 0 , #tzinfo )
38 local tz_last = _find_current ( tzinfo , ts_local+86400 , 0 , #tzinfo )
40 local n_candidates = tz_last - tz_first + 1
42 if n_candidates == 1 then
44 elseif n_candidates == 2 then
45 local tz_first_ob = tzinfo [ tz_first ]
46 local tz_last_ob = tzinfo [ tz_last ]
48 local first_gmtoffset = tz_first_ob.info.gmtoff
49 local last_gmtoffset = tz_last_ob .info.gmtoff
51 local t_start = tz_last_ob.transition_time + first_gmtoffset
52 local t_end = tz_last_ob.transition_time + last_gmtoffset
54 -- If timestamp is before start or after end
55 if ts_local < t_start then
57 elseif ts_local > t_end then
61 -- If we get this far, the local time is ambiguous
62 return tz_first , tz_last
64 error ( "Too many transitions in a 2 day period" )
68 function tz_info_methods:find_current ( current )
69 current = assert ( to_timestamp ( current ) , "invalid timestamp to :find_current" )
70 return self [ _find_current ( self , current , 0 , #self ) ].info
73 function tz_info_methods:localise ( utc_ts )
74 utc_ts = utc_ts or gettime ( )
75 return utc_ts + self:find_current ( utc_ts ).gmtoff
77 tz_info_methods.localize = tz_info_methods.localise
79 function tz_info_methods:utctime ( ts_local )
80 ts_local = assert ( to_timestamp ( ts_local ) , "invalid timestamp to :utctime" )
81 local tz1 , tz2 = find_current_local ( self , ts_local )
82 tz1 = self [ tz1 ].info
84 return ts_local - tz1.gmtoff
85 else -- Local time is ambiguous
86 tz2 = self [ tz2 ].info
88 return ts_local - tz2.gmtoff , ts_local - tz2.gmtoff
93 tz_info_mt = tz_info_mt ;
94 tt_info_mt = tt_info_mt ;