-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_util.lua
140 lines (128 loc) · 2.69 KB
/
time_util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
local time_util = {}
-- https://leafo.net/lapis/reference/utilities.html
---@param later number
---@param sooner number
---@return table
function time_util.date_diff(later, sooner)
if later < sooner then
sooner, later = later, sooner
end
local diff = later - sooner
local times = {}
local days = math.floor(diff / 86400)
if days >= 365 then
local years = math.floor(days / 365)
times.years = years
table.insert(times, {
"years",
years
})
diff = diff - years * 365 * 86400
days = days - (years * 365)
end
if days >= 1 then
times.days = days
table.insert(times, {
"days",
days
})
diff = diff - days * 86400
end
local hours = math.floor(diff / 3600)
if hours >= 1 then
times.hours = hours
table.insert(times, {
"hours",
hours
})
diff = diff - hours * 3600
end
local minutes = math.floor(diff / 60)
if minutes >= 1 then
times.minutes = minutes
table.insert(times, {
"minutes",
minutes
})
diff = diff - minutes * 60
end
local seconds = math.floor(diff)
if seconds >= 1 or not next(times) then
times.seconds = seconds
table.insert(times, {
"seconds",
seconds
})
diff = diff - seconds
end
return times
end
---@param time number
---@return table
function time_util.time_ago(time)
return time_util.date_diff(os.time(), time)
end
local singular = {
years = "year",
days = "day",
hours = "hour",
minutes = "minute",
seconds = "second"
}
---@param time number
---@param parts number?
---@param suffix string?
---@return string
function time_util.time_ago_in_words(time, parts, suffix)
if not parts then
parts = 1
end
if not suffix then
suffix = "ago"
end
local ago = type(time) == "table" and time or time_util.time_ago(time)
local out = ""
local i = 1
while parts > 0 do
parts = parts - 1
local segment = ago[i]
i = i + 1
if not segment then
break
end
local val = segment[2]
local word = val == 1 and singular[segment[1]] or segment[1]
if #out > 0 then
out = out .. ", "
end
out = out .. val .. " " .. word
end
if suffix and suffix ~= "" then
return out .. " " .. suffix
end
return out
end
---@param time number
---@param decimals number?
---@return string
function time_util.format(time, decimals)
local sign = time >= 0 and "" or "-"
time = math.abs(time)
if time == math.huge then
return sign .. "inf"
end
local hours = math.floor(time / 3600)
local minutes = math.floor(time % 3600 / 60)
local seconds = math.floor(time % 60)
local s
if time >= 3600 then
s = ("%d:%02d:%02d"):format(hours, minutes, seconds)
else
s = ("%02d:%02d"):format(minutes, seconds)
end
if decimals then
s = s .. ("%0." .. decimals .. "f"):format(time % 1):sub(2)
end
return sign .. s
end
return time_util