Skip to content

Instantly share code, notes, and snippets.

@chrishomer
Created May 12, 2011 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrishomer/969361 to your computer and use it in GitHub Desktop.
Save chrishomer/969361 to your computer and use it in GitHub Desktop.
class ListingWindow
SKIP_N_PERIODS = {
"exclusive" => 1,
"pickable" => 2
}
WINDOW_TIMES = [9,18]
LISTING_TIME_ZONE = "Pacific Time (US & Canada)"
#LISTING_TIME_ZONE = "UTC"
def self.time_now
#Time.parse("April 5 2011 18:01 PDT")
Time.now.in_time_zone(LISTING_TIME_ZONE)
end
# takes hour as a 24 hour clock
def self.next_window(time = nil)
time ||= time_now
time = time.in_time_zone(LISTING_TIME_ZONE)
# Calculate next window based on time supplied
# => 1. Map Window Times array from hours to actual time values based on the day supplied
# => 2. Select the time that is greater than time supplied and take the first occurance (=next window)
windows = WINDOW_TIMES.map{|h| Time.new(time.year,time.month,time.day, h, 0, 0, time.utc_offset) }
window = windows.select{|t| t > time }.first
# => 3. If past the final window value (nil), default to the first window of the following day
time = time + 1.day
window ||= Time.new(time.year,time.month,time.day, WINDOW_TIMES[0], 0, 0, time.utc_offset)
window
end
def self.next_state_window(state,first_staged_at = nil)
n = SKIP_N_PERIODS[state.to_s]
window = next_window(first_staged_at)
n.times do |i|
window = next_window(window)
end
window
end
def self.next_exclusive_window(first_staged_at = nil)
next_state_window("exclusive",first_staged_at)
end
def self.next_pickable_window(first_staged_at = nil)
next_state_window("pickable",first_staged_at)
end
end
#
#
# def self.next_window(time = nil)
# if time
# time = time.in_time_zone(LISTING_TIME_ZONE)
# else
# time = time_now
# end
#
#
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment