class Object

Public Instance Methods

timeUntil(time=Time.new) click to toggle source
# File lib/420.rb, line 20
def timeUntil(time=Time.new)

    # This truncates the seconds off the time, to prevent rounding
    time = Time.parse(time.strftime("%H:%M:00"), time)
    time = Time.local(time.year, time.month, time.day, time.strftime("%I"), time.strftime("%M"))

    bowl_times = [Time.local(time.year, time.month, time.day, 4, 20),
        Time.local(time.year, time.month, time.day, 16, 20)]

    # This means it's exactly 420!
    if time.strftime("%I:%M") == "04:20"
        local = I18n.t :happy, :scope => 'returns'
        return local

    # This means it's before 4:20am.
    elsif time < bowl_times[0]
        time_until = Time.diff(time, bowl_times[0])
        if time_until[:hour] == 0
            local = I18n.t :time_minutes, :scope => 'returns'
            return local % [time_until[:minute]]
        else
            local = I18n.t :time, :scope => 'returns'
            return local % [time_until[:hour], time_until[:minute]]
        end

    # This means it's passed 4:20am.
    elsif time > bowl_times[0] and time < bowl_times[1]
        time_until = Time.diff(time, bowl_times[1])
        if time_until[:hour] == 0
            local = I18n.t :time_minutes, :scope => 'returns'
            return local % [time_until[:minute]]
        else
            local = I18n.t :time, :scope => 'returns'
            return local % [time_until[:hour], time_until[:minute]]
        end
    end

end