Skip to content

Instantly share code, notes, and snippets.

@quad
Last active Aug 29, 2015
Embed
What would you like to do?
Because implementing cron is always a good idea.
require 'date'
require 'minitest/autorun'
class Time
def self.normalize time
dt = DateTime.parse(time).to_time.utc
Time.utc(Time.now.year, nil, nil, dt.hour, dt.min)
end
def inbetween start_time, end_time
if start_time < end_time
(start_time..end_time).cover? self
else
start_time < self || self < end_time
end
end
end
describe :in_time do
norm = ->(ex) { ex.map(&->(t) { Time.normalize t }) }
describe "times that are in-between" do
[
["T11:00:00-05:00", "T04:00:00+10:00", "T21:00:00-05:00"], # Mia's example
["T00:00:00-00:00", "T01:00:00-00:00", "T02:00:00-00:00"], # | S ? E
["T23:00:00-00:00", "T01:00:00-00:00", "T02:00:00-00:00"], # S | ? E
["T23:00:00-00:00", "T23:59:00-00:00", "T02:00:00-00:00"], # S ? | E
["T00:00:00-00:00", "T06:00:00-00:00", "T00:00:00-00:00"], # S ? E=|
].map(&norm).each do |start_time, check_time, end_time|
it { assert check_time.inbetween start_time, end_time }
end
end
describe "times that aren't inbetween" do
[
["T00:00:00-00:00", "T01:00:00-00:00", "T02:00:00-00:00"], # | ? S E
["T23:00:00-00:00", "T01:00:00-00:00", "T02:00:00-00:00"], # ? | S E
["T22:00:00-00:00", "T23:00:00-00:00", "T02:00:00-00:00"], # ? S | E
].map(&norm).each do |check_time, start_time, end_time|
it { refute check_time.inbetween start_time, end_time }
end
[
["T00:00:00-00:00", "T06:00:00-00:00", "T12:00:00-00:00"], # | S E ?
["T23:00:00-00:00", "T06:00:00-00:00", "T12:00:00-00:00"], # S | E ?
["T18:00:00-00:00", "T23:00:00-00:00", "T12:00:00-00:00"], # S E | ?
].map(&norm).each do |start_time, end_time, check_time|
it { refute check_time.inbetween start_time, end_time }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment