From: daurnimator Date: Fri, 29 Aug 2014 18:56:13 +0000 (-0400) Subject: luatz/timetable: Optimize and export `is_leap`; add tests X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/1f8c95ba28a59bf8459a15c8b2d52234e577e191?ds=inline luatz/timetable: Optimize and export `is_leap`; add tests --- diff --git a/luatz/timetable.lua b/luatz/timetable.lua index f096372..99ac52e 100644 --- a/luatz/timetable.lua +++ b/luatz/timetable.lua @@ -16,7 +16,13 @@ end local sakamoto = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; local function is_leap ( y ) - return (y % 4) == 0 and (y % 100) ~= 0 or (y % 400) == 0 + if (y % 4) ~= 0 then + return false + elseif (y % 100) ~= 0 then + return true + else + return (y % 400) == 0 + end end local function year_length ( y ) @@ -200,6 +206,7 @@ local function new_from_timestamp ( ts ) end return { + is_leap = is_leap ; day_of_year = day_of_year ; day_of_week = day_of_week ; normalise = normalise ; diff --git a/spec/timetable_spec.lua b/spec/timetable_spec.lua index b769911..0ff42b1 100644 --- a/spec/timetable_spec.lua +++ b/spec/timetable_spec.lua @@ -9,6 +9,19 @@ describe ( "Timetable library" , function ( ) }) end + it ( "#is_leap is correct" , function ( ) + assert.same ( false , timetable.is_leap ( 1 ) ) + assert.same ( false , timetable.is_leap ( 3 ) ) + assert.same ( true , timetable.is_leap ( 4 ) ) + assert.same ( true , timetable.is_leap ( 2000 ) ) + assert.same ( true , timetable.is_leap ( 2004 ) ) + assert.same ( true , timetable.is_leap ( 2012 ) ) + assert.same ( false , timetable.is_leap ( 2013 ) ) + assert.same ( false , timetable.is_leap ( 2014 ) ) + assert.same ( false , timetable.is_leap ( 2100 ) ) + assert.same ( true , timetable.is_leap ( 2400 ) ) + end ) + it ( "#normalise gets #wday (day of week) correct" , function ( ) local function assert_same_wday ( year , month , day )