From 38cb1f49b0e0be7018298018bb69e8fa3f785ad9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 9 Nov 2014 02:11:55 -0500 Subject: [PATCH] luatz/timetable: Fix `rfc_3339()` bad rounding behaviour. Closes issue #4 --- luatz/timetable.lua | 5 +++-- spec/timetable_spec.lua | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/luatz/timetable.lua b/luatz/timetable.lua index fc89fca..7058d7f 100644 --- a/luatz/timetable.lua +++ b/luatz/timetable.lua @@ -171,8 +171,9 @@ function timetable_methods:timestamp ( ) end function timetable_methods:rfc_3339 ( ) - -- %06.3f gives 3 (=6-3) digits after decimal - return strformat ( "%04u-%02u-%02uT%02u:%02u:%06.3f" , self:unpack ( ) ) + local year , month , day , hour , min , sec = self:unpack ( ) + local sec , msec = borrow ( sec , 0 , 1000 ) + return strformat ( "%04u-%02u-%02uT%02u:%02u:%02d.%03d" , year , month , day , hour , min , sec , msec ) end function timetable_methods:strftime ( format_string ) diff --git a/spec/timetable_spec.lua b/spec/timetable_spec.lua index 569864f..7f6c704 100644 --- a/spec/timetable_spec.lua +++ b/spec/timetable_spec.lua @@ -104,4 +104,11 @@ describe ( "Timetable library" , function ( ) round_trip_add(timetable.new(2014,8,28,19,23,0), "month", 0.4) round_trip_add(timetable.new(2014,14.5,28,0,0,0), "month", 0.4) 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()) + assert.same("1969-12-31T23:59:59.999", timetable.new_from_timestamp(-0.001):rfc_3339()) + assert.same("1969-12-31T23:59:00.000", timetable.new_from_timestamp(-59.9999999):rfc_3339()) + end) end ) -- 2.39.5