summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSamantaz Fox <coding@samantaz.fr>2023-05-31 22:28:47 +0200
committerSamantaz Fox <coding@samantaz.fr>2023-05-31 22:28:47 +0200
commit21f0b903541286f2c0ea2f52e6506c7279e26996 (patch)
treee59111e67c36d3c22ff8a8a02598b8a3b93c13ad /src
parent928ea75dbc07d1e7e0615c3bfe0e19d1f4a925e8 (diff)
parent4414c9df70580008c8817ace026b765e83c052aa (diff)
downloadinvidious-21f0b903541286f2c0ea2f52e6506c7279e26996.tar.gz
invidious-21f0b903541286f2c0ea2f52e6506c7279e26996.tar.bz2
invidious-21f0b903541286f2c0ea2f52e6506c7279e26996.zip
Utils: Add support for short "x ago" forms (#3825)
Diffstat (limited to 'src')
-rw-r--r--src/invidious/helpers/utils.cr25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/invidious/helpers/utils.cr b/src/invidious/helpers/utils.cr
index bcf7c963..48bf769f 100644
--- a/src/invidious/helpers/utils.cr
+++ b/src/invidious/helpers/utils.cr
@@ -111,24 +111,27 @@ def decode_date(string : String)
else nil # Continue
end
- # String matches format "20 hours ago", "4 months ago"...
- date = string.split(" ")[-3, 3]
- delta = date[0].to_i
+ # String matches format "20 hours ago", "4 months ago", "20s ago", "15min ago"...
+ match = string.match(/(?<count>\d+) ?(?<span>[smhdwy]\w*) ago/)
- case date[1]
- when .includes? "second"
+ raise "Could not parse #{string}" if match.nil?
+
+ delta = match["count"].to_i
+
+ case match["span"]
+ when .starts_with? "s" # second(s)
delta = delta.seconds
- when .includes? "minute"
+ when .starts_with? "mi" # minute(s)
delta = delta.minutes
- when .includes? "hour"
+ when .starts_with? "h" # hour(s)
delta = delta.hours
- when .includes? "day"
+ when .starts_with? "d" # day(s)
delta = delta.days
- when .includes? "week"
+ when .starts_with? "w" # week(s)
delta = delta.weeks
- when .includes? "month"
+ when .starts_with? "mo" # month(s)
delta = delta.months
- when .includes? "year"
+ when .starts_with? "y" # year(s)
delta = delta.years
else
raise "Could not parse #{string}"