From: daurnimator Date: Wed, 21 Oct 2015 19:05:20 +0000 (-0400) Subject: luatz/parse: On a non-conforming string, return `nil, err` instead of throwing an... X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/d5edaa6f361af75bebab47497b4654cbe428a1b7 luatz/parse: On a non-conforming string, return `nil, err` instead of throwing an error Closes #6 --- diff --git a/doc/parse.md b/doc/parse.md index 39529dd..649ee81 100644 --- a/doc/parse.md +++ b/doc/parse.md @@ -6,4 +6,7 @@ Functions take the source string and an optional initial postition. ### `rfc_3339 ( string [, init] )` -Returns a luatz timetable and the (optional) time zone offset in seconds +If the string is a valid RFC-3339 timestamp, +returns a luatz timetable and the (optional) time zone offset in seconds. + +Otherwise returns `nil` and an error message diff --git a/luatz/parse.lua b/luatz/parse.lua index 0b3b2dd..8de9a36 100644 --- a/luatz/parse.lua +++ b/luatz/parse.lua @@ -8,7 +8,7 @@ local new_timetable = require "luatz.timetable".new 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" ) + return nil, "Invalid RFC 3339 timestamp" end year = tonumber ( year ) month = tonumber ( month ) diff --git a/spec/parse_spec.lua b/spec/parse_spec.lua index 37d37a0..89c9a26 100644 --- a/spec/parse_spec.lua +++ b/spec/parse_spec.lua @@ -10,5 +10,8 @@ describe ( "Time parsing library" , function ( ) -- Missing offsets parse assert.same ( timetable.new(2013,10,22,14,17,02) , (parse.rfc_3339 "2013-10-22T14:17:02") ) + + -- Invalid + assert.same(nil, (parse.rfc_3339 "an invalid timestamp")) end ) end )