Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "time"
- class Time
- def humanize
- time = to_est(self.round_off(900)).split(",")
- p time
- first = time[0].to_i
- time_to_word = {12=> "twelve", 11 => "eleven", 10 => "ten", 9 => "nine",
- 8 => "eight", 7 => "seven", 6 => "six", 5 => "five",
- 4 => "four", 3 => "three", 2 => "two", 1 => "one"}
- case time[1]
- when "00" then (first == 12)? "About Midnight" : "About #{time_to_word[first]}"
- when "15" then "About a quarter past #{time_to_word[first]}"
- when "30" then (first == 12)? "About #{first}:30" : "About half past #{time_to_word[first]}"
- when "45" then "About a quarter till #{time_to_word[first.next]}"
- end
- end
- def round_off(seconds = 60)
- Time.at((self.to_f / seconds).round * seconds)
- end
- def to_est(ist)
- ist.strftime("%I,%M")
- end
- end
- p Time.parse("10:47 pm").humanize
- # p Time.parse("10:31 pm").humanize
- # p Time.parse("10:07 pm").humanize
- # p Time.parse("23:58").humanize
- # p Time.parse("00:29").humanize
Advertisement
Add Comment
Please, Sign In to add comment