Created
May 19, 2018 15:01
-
-
Save ozergul/30a87ccc63c4d0f9a3ffca539b77730a to your computer and use it in GitHub Desktop.
Human time for Crystal
This file contains hidden or 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
def human_time(time) | |
time = time.gsub("T", " ") | |
time = Time.parse(time, "%F %T", Time::Kind::Utc) | |
now = Time.utc_now | |
diff = (now - time) | |
if diff < 5.minutes | |
"just now" | |
elsif diff < 1.hour | |
"#{diff.total_minutes.to_i} minutes ago" | |
elsif diff < 2.hours | |
"1 hour ago" | |
elsif diff < 1.day | |
"#{diff.total_hours.to_i} hours ago" | |
elsif diff < 2.days | |
"1 day ago" | |
else | |
"#{diff.total_days.to_i} days ago" | |
end | |
end | |
puts human_time("2018-05-19T13:57:34+0000") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment