]> git.madduck.net Git - etc/awesome.git/blob - src/tzinfo.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Move source to subdirectory
[etc/awesome.git] / src / tzinfo.lua
1 local tz_info_methods = { }
2 local tz_info_mt = {
3         __index = tz_info_methods ;
4 }
5
6 -- Binary search
7 local function _find_current ( tzinfo , target , i , j )
8         if i >= j then return j end
9
10         local half = math.ceil ( (j+i) / 2 )
11
12         if target > tzinfo [ half ].transition_time then
13                 return _find_current ( tzinfo , target , half , j )
14         else
15                 return _find_current ( tzinfo , target , i , half-1 )
16         end
17 end
18 function tz_info_methods:find_current ( current )
19         return self [ _find_current ( self , current , 0 , #self ) ].info
20 end
21
22 function tz_info_methods:localize ( utc_ts )
23         utc_ts = utc_ts or gettime ( )
24         return utc_ts + self:find_current ( utc_ts ).gmtoff
25 end
26
27 return {
28         tz_info_mt = tz_info_mt ;
29 }