Skip to content

Instantly share code, notes, and snippets.

@jinks
Created February 18, 2017 10:09
Show Gist options
  • Save jinks/51daaa7ee9faf135d1101418f8c09fa3 to your computer and use it in GitHub Desktop.
Save jinks/51daaa7ee9faf135d1101418f8c09fa3 to your computer and use it in GitHub Desktop.
Lua (approx) elapsed Time calculation
function elapsed (datestring)
local pattern = "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)Z"
local year,month,day,hour,min,sec = datestring:match(pattern)
local start = os.time({year = year, month = month, day = day, hour = hour, min = min, sec = sec, isdst = false })
local utcnow = os.time(os.date("!*t"))
local diff = utcnow-start
local years = math.floor(diff / 31557600)
local remainder = diff % 31557600
local months = math.floor(remainder / 2629800)
local remainder = diff % 2629800
local days = math.floor(remainder / 86400)
local remainder = diff % 86400
local hours = math.floor(remainder / 3600)
local remainder = diff % 3600
local mins = math.floor(remainder / 60)
local rval = ""
if years > 0 then
rval = rval..pluralize(years, "year").." "
end
if months > 0 then
rval = rval..pluralize(months, "month").." "
end
if days > 0 then
rval = rval..pluralize(days, "day").." "
end
if hours > 0 then
rval = rval..pluralize(hours,"hour").." and "..pluralize(mins,"minute")
else
rval = rval..pluralize(mins,"minute")
end
return rval
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment