Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert arguments to time of day #2

Merged
merged 5 commits into from
Aug 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/tod/shift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ def initialize(beginning, ending, exclude_end=false)
raise ArgumentError, "exclude_end must be true or false"
end

@beginning = beginning.is_a?(TimeOfDay) ? beginning : TimeOfDay(beginning)
@ending = ending.is_a?(TimeOfDay) ? ending : TimeOfDay(ending)
@beginning = beginning.is_a?(::Tod::TimeOfDay) ? beginning : ::Tod::TimeOfDay(beginning)
@ending = ending.is_a?(::Tod::TimeOfDay) ? ending : ::Tod::TimeOfDay(ending)
@exclude_end = exclude_end

normalized_ending = ending.to_i
normalized_ending += TimeOfDay::NUM_SECONDS_IN_DAY if normalized_ending < beginning.to_i
normalized_ending = @ending.to_i
normalized_ending += TimeOfDay::NUM_SECONDS_IN_DAY if normalized_ending < @beginning.to_i

@range = Range.new(beginning.to_i, normalized_ending, @exclude_end)
@range = Range.new(@beginning.to_i, normalized_ending, @exclude_end)

freeze # Shift instances are value objects
end

# Returns true if the time of day is inside the shift, false otherwise.
def include?(tod)
second = tod.to_i
second = tod.is_a?(::Tod::TimeOfDay) ? tod.to_i : ::Tod::TimeOfDay(tod).to_i
second += TimeOfDay::NUM_SECONDS_IN_DAY if second < @range.first
@range.cover?(second)
end
Expand Down