wakproductions / stock_market_days

A Ruby gem for determining which days the US stock market is open
4 stars 3 forks source link

Stock market hours? #4

Open jch opened 6 months ago

jch commented 6 months ago

I'm currently using this method to calculate whether market is open. The problem is it doesn't handle EST vs EDT. Thoughts on adding something more robust to your gem?

class MarketHours
  def self.open?(time = nil)
    Time.use_zone('Eastern Time (US & Canada)') do
      time ||= Time.current
      return false if time.on_weekend?

      market_open = Time.zone.local(time.year, time.month, time.day, 9, 30)
      market_close = Time.zone.local(time.year, time.month, time.day, 16, 0)

      time.between?(market_open, market_close)
    end
  end
end
wakproductions commented 1 month ago

Time zones are tricky. One problem with this is that Time.current is from Rails. I think a gem like this should not need to have Rails as a dependency.