peepraeza

Untitled

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