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:

luatz/timetable.lua: Fix incorrect normalisation logic for negative day field
authordaurnimator <quae@daurnimator.com>
Wed, 6 Dec 2017 06:12:47 +0000 (17:12 +1100)
committerdaurnimator <quae@daurnimator.com>
Wed, 6 Dec 2017 06:12:47 +0000 (17:12 +1100)
Fixes #13

NEWS
luatz/timetable.lua
spec/timetable_spec.lua

diff --git a/NEWS b/NEWS
index d49c3427e8de2150ea36612b7d42b0222a23cf29..b4cc02dd90c80a11842e82504f434ac5d0f93c2c 100644 (file)
--- 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
index 0dbf4c8eedb5d6fdce95d0f80187ef08e1363de0..6bae913470bbd151ea4f2384210b448391f2547e 100644 (file)
@@ -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 )
 
index 930d36b32b94f398f260580352ee980a6e4497d8..6c9617e2c2dcce2a1e59dc94a0f0407c6534568b 100644 (file)
@@ -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;