]> 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:

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