Specifically: I have a Schedule model where a person cans schedule one event per day at the same time every day. Like this:
class Schedule
include Mongoid::Document
include Mongoid::Timestamps
field :hour, :type => Integer, :default => 8
field :ampm, :type => String, :default => "am"
field :time_zone, :type => String, :default => "Eastern Time (US & Canada)"
field :monday, :type => Boolean, :default => true
field :wednesday, :type => Boolean, :default => true
field :friday, :type => Boolean, :default => true
field :tuesday, :type => Boolean, :default => false
field :thursday, :type => Boolean, :default => false
field :saturday, :type => Boolean, :default => false
field :sunday, :type => Boolean, :default => false
embedded_in :user
end
I have a rake task that is run hourly and I just want to query for all schedules that require and event “now”. So for example if it is noon UTC on a Monday, then a schedule with :timezone => EST and :hour => 7 should be in the results as should a schedule with :timezone => PST and :hour => 4. This of course is a lot more tricky when it is, say, 1am UTC Tuesday because that is Monday in both PST/EST.
I thought about just normalizing the hour so everyone was on utc but this would create a mess for me with the days because someone might schedule something for 11pm wed which is actually thurs utc, so “normalization” would become a total pita where the days users select in their scheduler form wouldn’t correspond with the days we stored in the db.
I’m using mongoid in case anyone has a mongoid specific solution.
If it’s not an option to save the dates in UTC, you’ll have to query each time zone separately. Something like: