Utils: Update 'decode_date' to take into account short "x ago" forms

This commit is contained in:
Samantaz Fox 2023-05-29 12:41:53 +02:00
parent 381a0e326d
commit 898066407d
No known key found for this signature in database
GPG key ID: F42821059186176E

View file

@ -111,24 +111,27 @@ def decode_date(string : String)
else nil # Continue else nil # Continue
end end
# String matches format "20 hours ago", "4 months ago"... # String matches format "20 hours ago", "4 months ago", "20s ago", "15min ago"...
date = string.split(" ")[-3, 3] match = string.match(/(?<count>\d+) ?(?<span>[smhdwy]\w*) ago/)
delta = date[0].to_i
case date[1] raise "Could not parse #{string}" if match.nil?
when .includes? "second"
delta = match["count"].to_i
case match["span"]
when .starts_with? "s" # second(s)
delta = delta.seconds delta = delta.seconds
when .includes? "minute" when .starts_with? "mi" # minute(s)
delta = delta.minutes delta = delta.minutes
when .includes? "hour" when .starts_with? "h" # hour(s)
delta = delta.hours delta = delta.hours
when .includes? "day" when .starts_with? "d" # day(s)
delta = delta.days delta = delta.days
when .includes? "week" when .starts_with? "w" # week(s)
delta = delta.weeks delta = delta.weeks
when .includes? "month" when .starts_with? "mo" # month(s)
delta = delta.months delta = delta.months
when .includes? "year" when .starts_with? "y" # year(s)
delta = delta.years delta = delta.years
else else
raise "Could not parse #{string}" raise "Could not parse #{string}"