<jack>

If it’s too hard you’re doing it wrong.

tod - Time of Day for Ruby

Posted at — Nov 29, 2010

I just released tod – a new ruby gem for time of day handling. It handles parsing, strftime, comparison, and arithmetic.

require 'tod'

TimeOfDay.parse "8"                            # => 08:00:00
TimeOfDay.parse "8pm"                          # => 20:00:00
TimeOfDay.parse "1230"                         # => 12:30:00
TimeOfDay.parse "3:25:58"                      # => 03:25:58
TimeOfDay.parse "515p"                         # => 17:15:00

TimeOfDay.new(8) + 3600                        # => 09:00:00
TimeOfDay.new(8) - 3600                        # => 07:00:00
TimeOfDay.new(23,59,45) + 30                   # => 00:00:15

TimeOfDay.new(8) < TimeOfDay.new(9)            # => true

TimeOfDay.new(8,30).strftime("%H:%M")          # => "08:30"
TimeOfDay.new(17,15).strftime("%I:%M %p")      # => "05:15 PM"

Head over to Github for more details.