0

I try to save datetimes into events in MongoDB with Mongoid. The difficulty is that I want to save them for different timezones (e.g. Berlin and London). I want the user (admin) to see the actual time in the timezone so that he does not need to calculate. Afterwards I have a cron job in whenever, which looks every minute for an event to process.

I have the following parameters:

application.rb (I tried without -> standard UTC -> London is ok, Berlin wrong hour)

config.time_zone = 'Berlin'  # as the server stays in Germany

mongoid.yml (tried all combinations, need use_activesupport_time_zone to get correct times int oDB though)

use_activesupport_time_zone: true    
use_utc: false

schedule.rb (no problem here so far)

every 1.minutes do
 runner "Event.activate_due", environment: 'development'
end

event.rb (not sure for which times I am looking now)

def self.activate_due
  events_due = Event.where(:eventStart.lte => (Time.now  + 1.minute), :eventStart.gte => (Time.now))
  events_due.each do |e|
    e.activate
  end
end

I tried to change the Time.zone in events#new in order to depict the timezone's actual time in the simple_form. It seems to work, but then the controller create method seems to treat the params as standard time zone again and transforms it wrongly (+-1hour when experimenting with London/Berlin). I tried out almost every parameter combination and also Time.now/Time.zone.now. In event#index I switch through time_zones in order to show the right time.

To complicate things a bit: I save the timezone in the area model, which is connected through areaId with the event model.

How would I show the admin in the form the correct time for the event in the zone he wants to add (not his timezone) and save it correctly in Mongo to trigger it through the cronjob later?

Luis
  • 33
  • 6

1 Answers1

1

Try the following

def self.activate_due
  events_due = Event.where(
     :eventStart.to_utc.lte =>(Time.now.to_utc),
     :eventStart.to_utc.gte => (1.minute.ago.to_utc))
  events_due.each do |e|
    e.activate
  end
end

Plesse note that this will work with UTC times, so if you have an event in UTC - 7 timezone don't think it will get activated now if the server timezone is UTC + 2

a14m
  • 7,808
  • 8
  • 50
  • 67
  • That helped. Thanks The main problem was in the Time.zone switching, which happened once after the model was initiated and the timestamp already set. – Luis Dec 22 '14 at 15:16