]> git.madduck.net Git - etc/awesome.git/commitdiff

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:

Add a `parse` module
authordaurnimator <quae@daurnimator.com>
Tue, 22 Oct 2013 19:02:39 +0000 (15:02 -0400)
committerdaurnimator <quae@daurnimator.com>
Tue, 22 Oct 2013 19:02:39 +0000 (15:02 -0400)
luatz-scm-0.rockspec
luatz/init.lua
luatz/parse.lua [new file with mode: 0644]
spec/parse_spec.lua [new file with mode: 0644]

index 96709c75f5844f13729710b8a77ad1b69eb357a6..167745ac44a6cadf04878a4f3ea31d7044dca612 100644 (file)
@@ -24,6 +24,7 @@ build = {
        modules = {
                ["luatz.init"]      = "luatz/init.lua" ;
                ["luatz.gettime"]   = "luatz/gettime.lua" ;
        modules = {
                ["luatz.init"]      = "luatz/init.lua" ;
                ["luatz.gettime"]   = "luatz/gettime.lua" ;
+               ["luatz.parse"]     = "luatz/parse.lua" ;
                ["luatz.timetable"] = "luatz/timetable.lua" ;
                ["luatz.tzcache"]   = "luatz/tzcache.lua" ;
                ["luatz.tzfile"]    = "luatz/tzfile.lua" ;
                ["luatz.timetable"] = "luatz/timetable.lua" ;
                ["luatz.tzcache"]   = "luatz/tzcache.lua" ;
                ["luatz.tzfile"]    = "luatz/tzfile.lua" ;
index 1f223d10c9022916ebcc4d059c8a049de848f8af..2ad2ae8ecab2d49313a77f71e513b2374656fed5 100644 (file)
@@ -8,5 +8,6 @@ return {
        get_tz    = get_tz ;
        gettime   = require "luatz.gettime".gettime ;
        gettimein = gettimein ;
        get_tz    = get_tz ;
        gettime   = require "luatz.gettime".gettime ;
        gettimein = gettimein ;
+       parse     = require "luatz.parse" ;
        timetable = require "luatz.timetable" ;
 }
        timetable = require "luatz.timetable" ;
 }
diff --git a/luatz/parse.lua b/luatz/parse.lua
new file mode 100644 (file)
index 0000000..2c5f8d3
--- /dev/null
@@ -0,0 +1,40 @@
+local new_timetable = require "luatz.timetable".new
+
+--- Parse an RFC 3339 datetime at the given position
+-- Returns a time table and the `tz_offset`
+-- Return value is not normalised (this preserves a leap second)
+-- If the timestamp is only partial (i.e. missing "Z" or time offset) then `tz_offset` will be nil
+-- TODO: Validate components are within their boundarys (e.g. 1 <= month <= 12)
+local function rfc_3339 ( str , init )
+       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 )
+       if not year then
+               error ( "Invalid RFC 3339 timestamp" )
+       end
+       year  = tonumber ( year )
+       month = tonumber ( month )
+       day   = tonumber ( day )
+       hour  = tonumber ( hour )
+       min   = tonumber ( min )
+       sec   = tonumber ( sec )
+
+       local tt = new_timetable ( year , month , day , hour , min , sec )
+
+       local tz_offset
+       if str:match ("^[Zz]" , patt_end ) then
+               tz_offset = 0
+       else
+               local hour_offset , min_offset = str:match ( "^([+-]%d%d):(%d%d)" , patt_end )
+               if hour_offset then
+                       tz_offset = tonumber ( hour_offset )*60 + tonumber ( min_offset )
+               else
+                       -- Invalid RFC 3339 timestamp offset (should be Z or (+/-)hour:min
+                       -- tz_offset will be nil
+               end
+       end
+
+       return tt , tz_offset
+end
+
+return {
+       rfc_3339 = rfc_3339 ;
+}
diff --git a/spec/parse_spec.lua b/spec/parse_spec.lua
new file mode 100644 (file)
index 0000000..f66015a
--- /dev/null
@@ -0,0 +1,14 @@
+describe ( "Time parsing library" , function ( )
+       local timetable = require "luatz.timetable"
+       local parse = require "luatz.parse"
+
+       it ( "RFC 3339 Parsing" , function ( )
+               assert.same ( timetable.new(2013,10,22,14,17,02) , (parse.rfc_3339 "2013-10-22T14:17:02Z") )
+
+               -- Numeric offsets accepted
+               assert.same ( { timetable.new(2013,10,22,14,17,02) , 10*60 } , { parse.rfc_3339 "2013-10-22T14:17:02+10:00" } )
+
+               -- Missing offsets parse
+               assert.same ( timetable.new(2013,10,22,14,17,02) , (parse.rfc_3339 "2013-10-22T14:17:02") )
+       end )
+end )