Skip to content

Instantly share code, notes, and snippets.

@the-undefined
Last active August 29, 2015 13:57
Show Gist options
  • Save the-undefined/9811632 to your computer and use it in GitHub Desktop.
Save the-undefined/9811632 to your computer and use it in GitHub Desktop.
Time travelling in style - got any garbage?
class Delorean
# set_coodinates
# ==============
#
# ## REQUIRES:
#
# - abbr_time => "07:30"
#
# ## RETURNS:
# An instance of Delorean.
#
def self.set_coordinates(abbr_time)
new(abbr_time)
end
# next_occurence
# ==============
#
# ## RETURNS:
# The next occurence of the time the instance of Delorean
# was instantiated with.
#
# ### FORMAT
# ActiveSupport::TimeWithZone
#
# ## EXAMPLES
#
# ```
# delorean = Delorean.new("20:30")
# delorean.next_occurance
# # => Tue, 01 Apr 2014 20:30:00 UTC +00:00
# ```
#
def next_occurence
next_lightning_strike + seconds_offset
end
private
attr_reader :hours, :minutes
def initialize(lcd_display)
h,m = lcd_display.split(':')
@hours = h.to_i
@minutes = m.to_i
end
def next_lightning_strike
Time.zone.now.tomorrow.midnight
end
def seconds_offset
hours_in_seconds + minutes_in_seconds
end
def minutes_in_seconds
60 * minutes
end
def hours_in_seconds
60 * 60 * hours
end
end
Delorean.set_coordinates("05:45").next_occurence
# => 2014-03-28 05:45:00 +0000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment