From: daurnimator Date: Fri, 19 Jun 2015 00:24:53 +0000 (+1000) Subject: luatz/timetable: Floor milliseconds before passing to string.format X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/fe885e6eddd5bf1c893d6c6b6767f2cbba54e8a0?ds=inline luatz/timetable: Floor milliseconds before passing to string.format The %d format specifier expects an integer. Since lua 5.3, instead of doing undefined rounding, it throws an error --- diff --git a/luatz/timetable.lua b/luatz/timetable.lua index 783d60b..5c234b6 100644 --- a/luatz/timetable.lua +++ b/luatz/timetable.lua @@ -181,6 +181,7 @@ end function timetable_methods:rfc_3339 ( ) local year , month , day , hour , min , sec = self:unpack ( ) local sec , msec = borrow ( sec , 0 , 1000 ) + msec = math.floor(msec) return strformat ( "%04u-%02u-%02uT%02u:%02u:%02d.%03d" , year , month , day , hour , min , sec , msec ) end diff --git a/spec/timetable_spec.lua b/spec/timetable_spec.lua index 7f6c704..1fab8ce 100644 --- a/spec/timetable_spec.lua +++ b/spec/timetable_spec.lua @@ -105,6 +105,11 @@ describe ( "Timetable library" , function ( ) round_trip_add(timetable.new(2014,14.5,28,0,0,0), "month", 0.4) end ) + it("#rfc_3339 works with fractional milliseconds", function() + -- on lua 5.3 this used to throw an error due to milliseconds not being an integer + timetable.new_from_timestamp(1415141759.999911111):rfc_3339() + end) + it("#rfc_3339 doesn't round seconds up to 60 (issue #4)", function() assert.same("2014-11-04T22:55:59.999", timetable.new_from_timestamp(1415141759.999911111):rfc_3339()) assert.same("1970-01-01T00:00:59.999", timetable.new_from_timestamp(59.9999999):rfc_3339())