Skip to content

Instantly share code, notes, and snippets.

@astroud
Created December 18, 2011 00:05
Show Gist options
  • Save astroud/1491867 to your computer and use it in GitHub Desktop.
Save astroud/1491867 to your computer and use it in GitHub Desktop.
Fuzzy-Time is a new method for Ruby's Time class that presents the time in a more human-friendly format. Inspired by Objectpark's FuzzyClock.
class Time
def fuzzy
# Converts 24 hour format to 12 hours
self.hour >= 12 ? hour_num = self.hour - 12 : hour_num = self.hour
min = self.min
case hour_num # Converting the current hour to a string
when 0 then hour = "twelve"
when 1 then hour = "one"
when 2 then hour = "two"
when 3 then hour = "three"
when 4 then hour = "four"
when 5 then hour = "five"
when 6 then hour = "six"
when 7 then hour = "seven"
when 8 then hour = "eight"
when 9 then hour = "nine"
when 10 then hour = "ten"
when 11 then hour = "eleven"
when 12 then hour = "twelve"
end
hour_num += 1 # Converting the next hour to a string
case hour_num
when 1 then nexthour = "one"
when 2 then nexthour = "two"
when 3 then nexthour = "three"
when 4 then nexthour = "four"
when 5 then nexthour = "five"
when 6 then nexthour = "six"
when 7 then nexthour = "seven"
when 8 then nexthour = "eight"
when 9 then nexthour = "nine"
when 10 then nexthour = "ten"
when 11 then nexthour = "eleven"
when 12 then nexthour = "twelve"
when 13 then nexthour = "one"
end
if min < 3 # Returns the current time, off by +/- three minutes
"shortly after #{hour}"
elsif min >= 3 and min < 7
"five past #{hour}"
elsif min >= 7 and min < 13
"ten past #{hour}"
elsif min >= 13 and min < 18
"quarter past #{hour}"
elsif min >= 18 and min < 23
"twenty past #{hour}"
elsif min >= 23 and min < 28
"twentyfive past #{hour}"
elsif min >= 28 and min < 33
"half past #{hour}"
elsif min >= 33 and min < 38
"twentyfive to #{nexthour}"
elsif min >= 38 and min < 43
"twenty to #{nexthour}"
elsif min >= 43 and min < 48
"quarter to #{nexthour}"
elsif min >= 48 and min < 53
"ten to #{nexthour}"
elsif min >= 53 and min < 57
"five to #{nexthour}"
elsif min >= 57
"nearly #{nexthour}"
end
end
end
@astroud
Copy link
Author

astroud commented Dec 18, 2011

Inspired by Objectpark's FuzzyClock.

@patricklewis
Copy link

I've always liked FuzzyClock too and came up with something similar to this: https://github.com/patricklewis/fuzzy_time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment