peepraeza

Untitled

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