]> git.madduck.net Git - etc/awesome.git/blob - luatz/parse.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:

luatz/timetable: Move unpack to be a method rather than local utility function
[etc/awesome.git] / luatz / parse.lua
1 local new_timetable = require "luatz.timetable".new
2
3 --- Parse an RFC 3339 datetime at the given position
4 -- Returns a time table and the `tz_offset`
5 -- Return value is not normalised (this preserves a leap second)
6 -- If the timestamp is only partial (i.e. missing "Z" or time offset) then `tz_offset` will be nil
7 -- TODO: Validate components are within their boundarys (e.g. 1 <= month <= 12)
8 local function rfc_3339 ( str , init )
9         local year , month , day , hour , min , sec , patt_end = str:match ( "^(%d%d%d%d)%-(%d%d)%-(%d%d)[Tt](%d%d%.?%d*):(%d%d):(%d%d)()" , init )
10         if not year then
11                 error ( "Invalid RFC 3339 timestamp" )
12         end
13         year  = tonumber ( year )
14         month = tonumber ( month )
15         day   = tonumber ( day )
16         hour  = tonumber ( hour )
17         min   = tonumber ( min )
18         sec   = tonumber ( sec )
19
20         local tt = new_timetable ( year , month , day , hour , min , sec )
21
22         local tz_offset
23         if str:match ("^[Zz]" , patt_end ) then
24                 tz_offset = 0
25         else
26                 local hour_offset , min_offset = str:match ( "^([+-]%d%d):(%d%d)" , patt_end )
27                 if hour_offset then
28                         tz_offset = tonumber ( hour_offset ) * 3600 + tonumber ( min_offset ) * 60
29                 else
30                         -- Invalid RFC 3339 timestamp offset (should be Z or (+/-)hour:min
31                         -- tz_offset will be nil
32                 end
33         end
34
35         return tt , tz_offset
36 end
37
38 return {
39         rfc_3339 = rfc_3339 ;
40 }