generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
-- All credit to https://github.com/f-person/lua-timeago | ||
|
||
local language = { | ||
justnow = "just now", | ||
minute = { singular = "a minute ago", plural = "minutes ago" }, | ||
hour = { singular = "an hour ago", plural = "hours ago" }, | ||
day = { singular = "a day ago", plural = "days ago" }, | ||
week = { singular = "a week ago", plural = "weeks ago" }, | ||
month = { singular = "a month ago", plural = "months ago" }, | ||
year = { singular = "a year ago", plural = "years ago" }, | ||
} | ||
|
||
local M = {} | ||
|
||
---@param num number | ||
---@return number | ||
local function round(num) | ||
return math.floor(num + 0.5) | ||
end | ||
|
||
---@param time integer | ||
---@return string | ||
function M.format(time) | ||
local now = os.time(os.date("!*t")) | ||
local diff_seconds = os.difftime(now, time) | ||
if diff_seconds < 45 then | ||
return language.justnow | ||
end | ||
|
||
local diff_minutes = diff_seconds / 60 | ||
if diff_minutes < 1.5 then | ||
return language.minute.singular | ||
end | ||
if diff_minutes < 59.5 then | ||
return round(diff_minutes) .. " " .. language.minute.plural | ||
end | ||
|
||
local diff_hours = diff_minutes / 60 | ||
if diff_hours < 1.5 then | ||
return language.hour.singular | ||
end | ||
if diff_hours < 23.5 then | ||
return round(diff_hours) .. " " .. language.hour.plural | ||
end | ||
|
||
local diff_days = diff_hours / 24 | ||
if diff_days < 1.5 then | ||
return language.day.singular | ||
end | ||
if diff_days < 7.5 then | ||
return round(diff_days) .. " " .. language.day.plural | ||
end | ||
|
||
local diff_weeks = diff_days / 7 | ||
if diff_weeks < 1.5 then | ||
return language.week.singular | ||
end | ||
if diff_weeks < 4.5 then | ||
return round(diff_weeks) .. " " .. language.week.plural | ||
end | ||
|
||
local diff_months = diff_days / 30 | ||
if diff_months < 1.5 then | ||
return language.month.singular | ||
end | ||
if diff_months < 11.5 then | ||
return round(diff_months) .. " " .. language.month.plural | ||
end | ||
|
||
local diff_years = diff_days / 365.25 | ||
if diff_years < 1.5 then | ||
return language.year.singular | ||
end | ||
return round(diff_years) .. " " .. language.year.plural | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters