Saving a Rails Record with a Specific Time Zone

How to save an event in Berlin with your specific time zone and the next event in Tijuana with another?

The user is prompted to select a city for the event, as a source, for example. +02 :. 00

I would like to get a temporary code like this:

eventstart = "2011-07-22T18:00:00+02:00" 

How are you going to create this form?

UPDATE:

Implemented retention as standard UTC is excellent for many reasons. So now I am changing the time line in the view to represent * distance_to_time_in_words * for eventstart, depending on the local time of the user.

Event in Tijuana, view from Berlin time:

 old_zone = Time.zone #=> "Berlin" Time.zone = "Tijuana" t = Time.zone.parse(eventstart.to_s[0..-7]) #=> 2011-07-22 18:00:00 +08:00 Time.zone = old_zone #=> "Berlin" distance_of_time_in_words(Time.now.in_time_zone, t) #=> "9 hours" 

bulky, but it works for now. Improvement ideas are welcome!

+4
source share
1 answer

Add the time_zone column to your database and use time_zone_select in your form so that the user can select the time_zone for which he is creating an event.

And in the model, you can hide the date and time at a specific time and store utc in the database. You can use the helper element as below

 def local_time(date) Time.use_zone(self.time_zone) do Time.zone.at(date.to_i) end end 
+7
source

All Articles