Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/practice/gigasecond/.meta/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
local gigasecond = {}

function gigasecond.anniversary(any_date)
return os.date('%x', any_date + math.pow(10, 9))
return os.date('!%x', any_date + math.pow(10, 9))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had no idea this was a thing. For anyone else that is unfamiliar, this tells it to use UTC instead of local time.

end

return gigasecond
33 changes: 33 additions & 0 deletions exercises/practice/gigasecond/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local function components(moment)
return moment:match("(%d%d%d%d)%-(%d%d)%-(%d%d)T?(%d*):?(%d*):?(%d*)")
end

local function map_to_num(...)
local t = { ... }
for i, str in ipairs(t) do
if str ~= nil and str ~= '' then
t[i] = tonumber(str)
else
t[i] = 0
end
end
return table.unpack(t)
end

return {
module_name = 'gigasecond',

generate_test = function(case)
local year, month, day, hour, min, sec = map_to_num(components(case.input.moment))
local e_year, e_month, e_day, e_hour, e_min, e_sec = map_to_num(components(case.expected))

local template = [[
local moment_a = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d })
local moment_b = os.time({ year = %d, month = %d, day = %d, hour = %d, min = %d, sec = %d })
local actual = gigasecond.anniversary(moment_a)
local expected = os.date('!%%x', moment_b)
assert.are.equals(expected, actual)]]

return template:format(year, month, day, hour, min, sec, e_year, e_month, e_day, e_hour, e_min, e_sec)
end
}
48 changes: 32 additions & 16 deletions exercises/practice/gigasecond/gigasecond_spec.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
local gigasecond = require('gigasecond')

describe('gigasecond', function()
it('test 1', function()
local actual = gigasecond.anniversary(os.time({ year = 2011, month = 3, day = 25, hour = 0, min = 0, sec = 0 }))
local expectedDate = os.date('%x', os.time({ year = 2042, month = 12, day = 1, hour = 0, min = 0, sec = 0 }))
assert.are.equals(expectedDate, actual)
it('date only specification of time', function()
local moment_a = os.time({ year = 2011, month = 4, day = 25, hour = 0, min = 0, sec = 0 })
local moment_b = os.time({ year = 2043, month = 1, day = 1, hour = 1, min = 46, sec = 40 })
local actual = gigasecond.anniversary(moment_a)
local expected = os.date('!%x', moment_b)
assert.are.equals(expected, actual)
end)

it('test 2', function()
local actual = gigasecond.anniversary(os.time({ year = 1977, month = 5, day = 13, hour = 0, min = 0, sec = 0 }))
local expectedDate = os.date('%x', os.time({ year = 2009, month = 1, day = 19 }))
assert.are.equals(expectedDate, actual)
it('second test for date only specification of time', function()
local moment_a = os.time({ year = 1977, month = 6, day = 13, hour = 0, min = 0, sec = 0 })
local moment_b = os.time({ year = 2009, month = 2, day = 19, hour = 1, min = 46, sec = 40 })
local actual = gigasecond.anniversary(moment_a)
local expected = os.date('!%x', moment_b)
assert.are.equals(expected, actual)
end)

it('test 3', function()
local actual = gigasecond.anniversary(os.time({ year = 1999, month = 7, day = 19 }))
local expectedDate = os.date('%x', os.time({ year = 2031, month = 3, day = 27 }))
assert.are.equals(expectedDate, actual)
it('third test for date only specification of time', function()
local moment_a = os.time({ year = 1959, month = 7, day = 19, hour = 0, min = 0, sec = 0 })
local moment_b = os.time({ year = 1991, month = 3, day = 27, hour = 1, min = 46, sec = 40 })
local actual = gigasecond.anniversary(moment_a)
local expected = os.date('!%x', moment_b)
assert.are.equals(expected, actual)
end)

it('test 4', function()
local actual = gigasecond.anniversary(os.time({ year = 1993, month = 8, day = 17 }))
local expectedDate = os.date('%x', os.time({ year = 2025, month = 4, day = 25 }))
assert.are.equals(expectedDate, actual)
it('full time specified', function()
local moment_a = os.time({ year = 2015, month = 1, day = 24, hour = 22, min = 0, sec = 0 })
local moment_b = os.time({ year = 2046, month = 10, day = 2, hour = 23, min = 46, sec = 40 })
local actual = gigasecond.anniversary(moment_a)
local expected = os.date('!%x', moment_b)
assert.are.equals(expected, actual)
end)

it('full time with day roll-over', function()
local moment_a = os.time({ year = 2015, month = 1, day = 24, hour = 23, min = 59, sec = 59 })
local moment_b = os.time({ year = 2046, month = 10, day = 3, hour = 1, min = 46, sec = 39 })
local actual = gigasecond.anniversary(moment_a)
local expected = os.date('!%x', moment_b)
assert.are.equals(expected, actual)
end)
end)
Loading