Created
June 30, 2009 04:11
-
-
Save chrisconley/137985 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ScheduleTime | |
attr_reader :seconds, :orig_hour, :hour, :minute, :second | |
def initialize(time, time_of_day) | |
#raise ArgumentError | |
@time = time | |
@time_of_day = time_of_day | |
parse_time | |
end | |
def parse_time | |
m = @time.scan(/(\d+)/) | |
@orig_hour, @minute, @second = m.map!{|m| m[0].to_i} | |
end | |
def second | |
@second || 0 | |
end | |
def hour | |
case @time_of_day | |
when "morning" | |
@orig_hour | |
when "afternoon" | |
@orig_hour+12 if @orig_hour < 7 | |
when "midnight" | |
@orig_hour+24 if @orig_hour < 7 | |
end | |
end | |
def seconds | |
hour*60+minute*60+second | |
end | |
def to(format) | |
format.new(self) | |
end | |
def next_time_of_day | |
case @time_of_day | |
when "morning" | |
"afternoon" if hour >= 12 | |
when "afternoon" | |
"midnight" if hour >= 24 | |
when "midnight" | |
end | |
end | |
[:>=, :<=, :==, :<, :>].each do |comparator| | |
define_method comparator do |time| | |
self.seconds.send(comparator, time.seconds) | |
end | |
end | |
end | |
class ArmyTime < ScheduleTime | |
def hour | |
case @time_of_day | |
when "morning" | |
@orig_hour | |
when "afternoon" | |
@orig_hour | |
when "midnight" | |
@orig_hour+24 if @orig_hour < 13 | |
end | |
end | |
end | |
class DesiredTime | |
attr_reader :result, :new_time_of_day | |
def initialize(schedule_time, options={}) | |
@new_time_of_day = schedule_time.next_time_of_day | |
end | |
end | |
class GtfsTime < DesiredTime | |
def initialize(schedule_time, options={}) | |
super | |
@result = "#{schedule_time.hour}:#{schedule_time.minute}:#{schedule_time.second}" | |
end | |
end | |
class ISeptaTime < DesiredTime | |
def initialize(schedule_time, options={}) | |
super | |
@result = Time.local(2009, 01, 01, schedule_time.hour, schedule_time.minute, schedule_time.second) | |
end | |
end | |
puts ArmyTime.new('14.43', 'afternoon').to(GtfsTime).result | |
puts ScheduleTime.new('1.43', 'afternoon').to(ISeptaTime).result | |
puts ScheduleTime.new('1.43', 'afternoon') <= ScheduleTime.new('1.43:01', 'afternoon') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment