Skip to content

Instantly share code, notes, and snippets.

@terlar
Created December 1, 2015 11:10
Show Gist options
  • Save terlar/257a66d71a5f3a701f4d to your computer and use it in GitHub Desktop.
Save terlar/257a66d71a5f3a701f4d to your computer and use it in GitHub Desktop.
diff --git a/lib/kviberg/common/opening_hours_entry.rb b/lib/kviberg/common/opening_hours_entry.rb
index 26d7912..56c6372 100644
--- a/lib/kviberg/common/opening_hours_entry.rb
+++ b/lib/kviberg/common/opening_hours_entry.rb
@@ -18,6 +18,20 @@ module Kviberg
"#{day} #{opens} - #{closes}"
end
+ def current_day?(time = Time.now)
+ dayOfWeek == time.strftime('%u').to_i
+ end
+
+ def open?(time = Time.now)
+ return false unless current_day? time
+
+ midnight = Time.new(time.year, time.month, time.day, 0, 0, 0)
+ from_time = midnight + from
+ to_time = midnight + to
+
+ time >= from_time && time < to_time
+ end
+
private
def formatted_duration(total_seconds)
diff --git a/test/lib/kviberg/common/opening_hours_entry_test.rb b/test/lib/kviberg/common/opening_hours_entry_test.rb
index 9e2c74f..e2f6a87 100644
--- a/test/lib/kviberg/common/opening_hours_entry_test.rb
+++ b/test/lib/kviberg/common/opening_hours_entry_test.rb
@@ -15,6 +15,35 @@ module Kviberg
})
end
+ def test_current_day
+ time = Time.new(2015, 12, 03)
+ assert entry.current_day?(time)
+
+ time = Time.new(2015, 12, 01)
+ refute entry.current_day?(time)
+ end
+
+ def test_open
+ entry.from = 36_000 # 10:00
+ entry.to = 72_000 # 20:00
+ entry.dayOfWeek = Kviberg::Common::DayOfWeek::TUESDAY
+
+ time = Time.new(2015, 12, 03, 10, 0)
+ refute entry.open?(time)
+
+ time = Time.new(2015, 12, 01, 9, 59)
+ refute entry.open?(time)
+
+ time = Time.new(2015, 12, 01, 10, 0)
+ assert entry.open?(time)
+
+ time = Time.new(2015, 12, 01, 19, 59)
+ assert entry.open?(time)
+
+ time = Time.new(2015, 12, 01, 20, 0)
+ refute entry.open?(time)
+ end
+
def test_to_s
assert_equal 'Th 9:16 am - 10:05 pm', entry.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment