Skip to content

Instantly share code, notes, and snippets.

@znepb
Last active November 29, 2021 22:56
Show Gist options
  • Select an option

  • Save znepb/f02fae94d16edda0cdc59fbdf7fb5e42 to your computer and use it in GitHub Desktop.

Select an option

Save znepb/f02fae94d16edda0cdc59fbdf7fb5e42 to your computer and use it in GitHub Desktop.
local function getDiff(t)
return t - os.epoch("utc") / 1000
end
local function dispTime(t)
t = math.abs(t)
local y = math.floor(t / 31536000)
local d = math.floor((t % 31536000) / 86400)
local h = math.floor((t % 86400) / 3600)
local m = math.floor((t % 3600) / 60)
local s = math.floor((t % 60))
return y, d, h, m, s
end
local function getDistanceFrom(nTime)
local diff = getDiff(nTime)
local y, d, h, m, s = dispTime(diff)
if diff > 0 then
if y > 1 then
return ("in %d years"):format(y), 0
elseif y == 1 then
return ("in 1 year"), 0
elseif d > 1 then
return ("in %d days"):format(d), 0
elseif d == 1 then
return "tommorow", 0
elseif h == 1 then
return "in 1 hour", 0
elseif h > 0 then
return ("in %d hours"):format(h), 0
elseif m == 1 then
return "in 1 minute", 0
elseif m > 0 then
return ("in %d minutes"):format(m), 0
elseif s == 1 then
return "in 1 second", 0
elseif s > 0 then
return ("in %d seconds"):format(s), 0
else
return "in 0 seconds", 0
end
return math.floor(diff), 0
elseif diff < 0 then
if y > 1 then
return ("%d years ago"):format(y), 1
elseif y == 1 then
return ("1 year ago"), 1
elseif d > 1 then
return ("%d days ago"):format(d), 1
elseif d == 1 then
return "yesterday", 1
elseif h == 1 then
return ("1 hour ago"), 1
elseif h > 0 then
return ("%d hours ago"):format(h), 1
elseif m == 1 then
return ("1 minute ago"), 1
elseif m > 0 then
return ("%d minutes ago"):format(m), 1
elseif s >= 30 then
return ("%d seconds ago"):format(s), 1
else
return "just now", 1
end
else
return "just now"
end
end
return getDistanceFrom
@autione

autione commented Nov 29, 2021

Copy link
Copy Markdown

oh lol this exists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment