From 1e81f505fc56efba701ba62a6d858d5fb5b70a7f Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 31 Aug 2023 23:22:45 +0200 Subject: Split out dates module --- activity.lua | 26 ++++---------------------- activity/dates.lua | 23 +++++++++++++++++++++++ spec/activity_spec.lua | 43 ------------------------------------------- spec/dates_spec.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 65 deletions(-) create mode 100644 activity/dates.lua delete mode 100644 spec/activity_spec.lua create mode 100644 spec/dates_spec.lua diff --git a/activity.lua b/activity.lua index 1d1ef8e..cece167 100644 --- a/activity.lua +++ b/activity.lua @@ -1,27 +1,9 @@ +local dates = require "activity.dates" local activity = {} -function activity.days_in (year) - if year % 4 ~= 0 or year % 100 == 0 and year % 400 ~= 0 then - return 365 - end - return 366 -end - - -function activity.this_year () - return os.date"%Y" -end - - -function activity.first_week_day (year) - year = year or activity.this_year() - return os.date("*t", os.time{year=year, month=1, day=1}).wday -end - - function activity.generate_table (year) - year = year or activity.this_year() + year = year or dates.this_year() local function spot () return [[]] @@ -52,8 +34,8 @@ function activity.generate_table (year) local rows = "" local weekdays = {"", "Mon", "", "Wed", "", "Fri", ""} - local start_from = activity.first_week_day(year) - local end_at = (start_from + activity.days_in(year) - 1) % 7 + local start_from = dates.first_week_day(year) + local end_at = (start_from + dates.days_in(year) - 1) % 7 for index, weekday in pairs(weekdays) do rows = rows .. row(weekday, index >= start_from, index <= end_at) end diff --git a/activity/dates.lua b/activity/dates.lua new file mode 100644 index 0000000..682ad04 --- /dev/null +++ b/activity/dates.lua @@ -0,0 +1,23 @@ +local dates = {} + + +function dates.days_in (year) + if year % 4 ~= 0 or year % 100 == 0 and year % 400 ~= 0 then + return 365 + end + return 366 +end + + +function dates.this_year () + return os.date"%Y" +end + + +function dates.first_week_day (year) + year = year or activity.this_year() + return os.date("*t", os.time{year=year, month=1, day=1}).wday +end + + +return dates diff --git a/spec/activity_spec.lua b/spec/activity_spec.lua deleted file mode 100644 index 074be22..0000000 --- a/spec/activity_spec.lua +++ /dev/null @@ -1,43 +0,0 @@ -local activity = require "activity" - -describe("Days in a year", function() - it("should support regular years", function() - assert.are.equal(365, activity.days_in(1970)) - assert.are.equal(365, activity.days_in(2001)) - end) - - it("should support lean years", function() - assert.are.equal(366, activity.days_in(2004)) - assert.are.equal(366, activity.days_in(1980)) - end) - - it("should support *00 years", function() - assert.are.equal(366, activity.days_in(2000)) - assert.are.equal(365, activity.days_in(2100)) - assert.are.equal(365, activity.days_in(2200)) - assert.are.equal(365, activity.days_in(2300)) - assert.are.equal(366, activity.days_in(2400)) - end) -end) - -describe("This year is", function() - local this_year = os.date "%Y" -- Quite useless test - - it(tostring(this_year), function() - assert.are.equal(this_year, activity.this_year()) - end) -end) - -describe("First week day of", function() - it("2023 is Sunday", function() - assert.are.equal(1, activity.first_week_day(2023)) - end) - - it("2022 is Saturday", function() - assert.are.equal(7, activity.first_week_day(2022)) - end) - - it("2020 is Saturday", function() - assert.are.equal(4, activity.first_week_day(2020)) - end) -end) diff --git a/spec/dates_spec.lua b/spec/dates_spec.lua new file mode 100644 index 0000000..6239fdc --- /dev/null +++ b/spec/dates_spec.lua @@ -0,0 +1,43 @@ +local dates = require "activity.dates" + +describe("Days in a year", function() + it("should support regular years", function() + assert.are.equal(365, dates.days_in(1970)) + assert.are.equal(365, dates.days_in(2001)) + end) + + it("should support lean years", function() + assert.are.equal(366, dates.days_in(2004)) + assert.are.equal(366, dates.days_in(1980)) + end) + + it("should support *00 years", function() + assert.are.equal(366, dates.days_in(2000)) + assert.are.equal(365, dates.days_in(2100)) + assert.are.equal(365, dates.days_in(2200)) + assert.are.equal(365, dates.days_in(2300)) + assert.are.equal(366, dates.days_in(2400)) + end) +end) + +describe("This year is", function() + local this_year = os.date "%Y" -- Quite useless test + + it(tostring(this_year), function() + assert.are.equal(this_year, dates.this_year()) + end) +end) + +describe("First week day of", function() + it("2023 is Sunday", function() + assert.are.equal(1, dates.first_week_day(2023)) + end) + + it("2022 is Saturday", function() + assert.are.equal(7, dates.first_week_day(2022)) + end) + + it("2020 is Saturday", function() + assert.are.equal(4, dates.first_week_day(2020)) + end) +end) -- cgit v1.1