From: daurnimator Date: Wed, 6 Dec 2017 06:12:47 +0000 (+1100) Subject: luatz/timetable.lua: Fix incorrect normalisation logic for negative day field X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/cdaad9ec35371fcfc3b398b7302f1e8fe313b4f6 luatz/timetable.lua: Fix incorrect normalisation logic for negative day field Fixes #13 --- diff --git a/NEWS b/NEWS index d49c342..b4cc02d 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,6 @@ UNRELEASED - - Fix timetable normalisation carry bug (#10) + - Fix timetable normalisation carry bugs (#10, #13) - Clean up of docs - No longer throw errors in parse module on error (now return nil, err) - Support version 3 tzfiles diff --git a/luatz/timetable.lua b/luatz/timetable.lua index 0dbf4c8..6bae913 100644 --- a/luatz/timetable.lua +++ b/luatz/timetable.lua @@ -107,8 +107,12 @@ local function normalise ( year , month , day , hour , min , sec ) -- Add a whole year of days at a time, this is later resolved by adding months -- TODO[OPTIMIZE]: This could be slow if `day` is far out of range while day < 0 do - year = year - 1 - day = day + year_length ( year ) + month = month - 1 + if month < 0 then + year = year - 1 + month = 11 + end + day = day + month_length ( month + 1 , year ) end year , month = carry ( year , month , 12 ) diff --git a/spec/timetable_spec.lua b/spec/timetable_spec.lua index 930d36b..6c9617e 100644 --- a/spec/timetable_spec.lua +++ b/spec/timetable_spec.lua @@ -77,8 +77,8 @@ describe ( "Timetable library" , function ( ) assert.same({ 2012,3,23,0,0,0 }, { timetable.normalise(2012,2,52,0,0,0) }) assert.same({ 2013,3,24,0,0,0 }, { timetable.normalise(2013,2,52,0,0,0) }) - assert.same({ 2012,2,26,0,0,0 }, { timetable.normalise(2012,3,-2,0,0,0) }) - assert.same({ 2013,2,27,0,0,0 }, { timetable.normalise(2013,3,-2,0,0,0) }) + assert.same({ 2012,2,27,0,0,0 }, { timetable.normalise(2012,3,-2,0,0,0) }) + assert.same({ 2013,2,26,0,0,0 }, { timetable.normalise(2013,3,-2,0,0,0) }) -- Also when more fields are out of range assert.same({ 2016,7,22,0,0,0 }, { timetable.normalise(2013,42,52,0,0,0) }) @@ -98,6 +98,12 @@ describe ( "Timetable library" , function ( ) assert.same({ 2017,02,3,0,0,0 }, { timetable.normalise(2017,02,13,0,-14400,0) }) end ) + it ( "#normalise handles negative day carry (issue #13)", function() + assert.same({ 2016,11,30,00,00,00 }, { timetable.normalise(2016,12,0,0,0,0) }) + assert.same({ 2017,11,30,00,00,00 }, { timetable.normalise(2017,12,0,0,0,0) }) + assert.same({ 2018,11,30,00,00,00 }, { timetable.normalise(2018,12,0,0,0,0) }) + end ) + local function round_trip_add(t, field, x) local before = t:clone() t[field]=t[field]+x;