Skip to content

Commit

Permalink
refactor(utils) Timestamps refactored (#1854)
Browse files Browse the repository at this point in the history
* add test for timestamp content

* refactor timestamps and added a millisecond function
  • Loading branch information
Tieske authored Dec 1, 2016
1 parent a94a0ad commit ece1752
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 39 deletions.
91 changes: 53 additions & 38 deletions kong/tools/timestamp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,71 @@
-- @module kong.tools.timestamp

local luatz = require "luatz"
local _M = {}
local tz_time = luatz.time
local tt_from_timestamp = luatz.timetable.new_from_timestamp
local tt = luatz.timetable.new
local math_floor = math.floor

--- Current UTC time
-- @return UTC time
function _M.get_utc()
return math.floor(luatz.time()) * 1000
-- @return UTC time in milliseconds since epoch, but with SECOND precision.
local function get_utc()
return math_floor(tz_time()) * 1000
end

function _M.get_timetable(now)
local timestamp = now and now or _M.get_utc()
if #tostring(timestamp) == 13 then
timestamp = timestamp / 1000
end
return luatz.timetable.new_from_timestamp(timestamp)
--- Current UTC time
-- @return UTC time in milliseconds since epoch.
local function get_utc_ms()
return tz_time() * 1000
end

--- Creates a timestamp
-- @param now (optional) Time to generate a timestamp from, if omitted current UTC time will be used
-- @return Timestamp table containing fields; second, minute, hour, day, month, year
function _M.get_timestamps(now)
local timetable = _M.get_timetable(now)
-- setup a validation value, any value above this is assumed to be in MS
-- instead of S (a year value beyond the year 20000), it assumes current times
-- as in 2016 and later.
local ms_check = tt(20000 , 1 , 1 , 0 , 0 , 0):timestamp()

local second = luatz.timetable.new(timetable.year, timetable.month,
timetable.day, timetable.hour,
timetable.min, timetable.sec)
-- Returns a time-table.
-- @param now (optional) time to generate the time-table from. If omitted
-- current utc will be used. It can be specified either in seconds or
-- milliseconds, it will be converted automatically.
local function get_timetable(now)
local timestamp = now and now or get_utc()
if timestamp > ms_check then
return tt_from_timestamp(timestamp/1000)
end
return tt_from_timestamp(timestamp)
end

local minute = luatz.timetable.new(timetable.year, timetable.month,
timetable.day, timetable.hour,
timetable.min, 0)
--- Creates a timestamp table containing time by different precision levels.
-- @param now (optional) Time to generate timestamps from, if omitted current UTC time will be used
-- @return Timestamp table containing fields/precisions; second, minute, hour, day, month, year
local function get_timestamps(now)
local timetable = get_timetable(now)
local stamps = {}

timetable.sec = math_floor(timetable.sec) -- reduce to second precision
stamps.second = timetable:timestamp() * 1000

local hour = luatz.timetable.new(timetable.year, timetable.month,
timetable.day, timetable.hour,
0, 0)
timetable.sec = 0
stamps.minute = timetable:timestamp() * 1000

timetable.min = 0
stamps.hour = timetable:timestamp() * 1000

local day = luatz.timetable.new(timetable.year, timetable.month,
timetable.day, 0, 0, 0)
timetable.hour = 0
stamps.day = timetable:timestamp() * 1000

local month = luatz.timetable.new(timetable.year, timetable.month,
1, 0, 0, 0)
timetable.day = 1
stamps.month = timetable:timestamp() * 1000

local year = luatz.timetable.new(timetable.year, 1, 1, 0, 0, 0)
timetable.month = 1
stamps.year = timetable:timestamp() * 1000

return {
second = math.floor(second:timestamp() * 1000),
minute = minute:timestamp() * 1000,
hour = hour:timestamp() * 1000,
day = day:timestamp() * 1000,
month = month:timestamp() * 1000,
year = year:timestamp() * 1000
}
return stamps
end

return _M
return {
get_utc = get_utc,
get_utc_ms = get_utc_ms,
get_timetable = get_timetable,
get_timestamps = get_timestamps,
}
28 changes: 27 additions & 1 deletion spec/01-unit/06-timestamp_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local timestamp = require "kong.tools.timestamp"
local luatz = require "luatz"

describe("Timestamp", function()
local table_size = function(t)
Expand All @@ -25,7 +26,7 @@ describe("Timestamp", function()
assert.truthy(timestamps.year)
end)

it("should get timestamps table when no timestamp is provided", function()
it("should get timestamps table when the timestamp is provided", function()
local timestamps = timestamp.get_timestamps(timestamp.get_utc())
assert.truthy(timestamps)
assert.are.same(6, table_size(timestamps))
Expand Down Expand Up @@ -55,4 +56,29 @@ describe("Timestamp", function()
assert.are.same(timestamps_one, timestamps_two)
end)

it("should provide correct timestamp values", function()
for i = 1, 2 do
local factor
if i == 1 then
factor = 1 -- use base time in seconds
else
factor = 1000 -- use base time in milliseconds
end
local base = luatz.timetable.new(2016, 10, 10, 10, 10, 10):timestamp()
local ts = timestamp.get_timestamps(base * factor)
-- timestamps are always in milliseconds
assert.equal(base * 1000, ts.second)
base = base - 10
assert.equal(base * 1000, ts.minute)
base = base - 10 * 60
assert.equal(base * 1000, ts.hour)
base = base - 10 * 60 * 60
assert.equal(base * 1000, ts.day)
base = base - 9 * 60 * 60 * 24
assert.equal(base * 1000, ts.month)
base = base - (31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30) * 60 * 60 * 24
assert.equal(base * 1000, ts.year)
end
end)

end)

0 comments on commit ece1752

Please sign in to comment.