Skip to content

Instantly share code, notes, and snippets.

@deanpcmad
Created July 20, 2013 21:46
Show Gist options
  • Save deanpcmad/6046533 to your computer and use it in GitHub Desktop.
Save deanpcmad/6046533 to your computer and use it in GitHub Desktop.
Ruby method for showing if you are open or closed
module OpenHelper
# Bank holiday dates from https://www.gov.uk/bank-holidays
if Time.now.year == 2013
DAYS = [
# "20 July 2013",
"26 August 2013",
"25 December 2013",
"26 December 2013"
]
elsif Time.now.year == 2014
DAYS = [
"1 January 2014",
"18 April 2014",
"21 April 2014",
"5 May 2014",
"26 May 2014",
"25 August 2014",
"25 December 2014",
"26 December 2014"
]
elsif Time.now.year == 2015
DAYS = [
"1 January 2015",
"3 April 2015",
"6 April 2015",
"4 May 2015",
"25 May 2015",
"31 August 2015",
"25 December 2015",
"28 December 2015"
]
end
# are you open on saturday or sunday?
SATURDAY = true
SUNDAY = false
# use 24 hour times here
TIME_OPEN = "9:00".split(":")
TIME_CLOSE = "17:00".split(":")
def open?(date = Date.today, time = Time.now)
open = Time.new(date.year, date.month, date.day, TIME_OPEN[0], TIME_OPEN[1], 0)
close = Time.new(date.year, date.month, date.day, TIME_CLOSE[0], TIME_CLOSE[1], 0)
if DAYS.include?(date.strftime("%d %B %Y"))
false # not open on bank holidays so return false
elsif date.strftime("%A") == "Saturday" && SATURDAY == false
false
elsif date.strftime("%A") == "Sunday" && SUNDAY == false
false
else
# open between the TIME_OPEN & TIME_CLOSE?
if time.between?(open, close)
true
else
false
end
end
rescue
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment