Reputation: 6728
I am building a Rails application where users can post the interview details they know.The form has a input field interview time which is date time selector.I want to give universal support to this application.
Actual problem:
Let us say I have configured the IST time zone in my production server.Then if a user from Japan selects an interview time which will be parsed in IST time zone.The result will be an incorrect interview time.
To display the correct interview time.
1.Do I need to get the time zone from the user?(probably a select box)
2.Is there is any other option to automatically detect the time zone?
Thank you folks :)
If something is unclear please add a comment below the question.
Upvotes: 2
Views: 249
Reputation: 49114
Baldrick's answer is right -- you should store the times in UTC and store each user's timezone in the user's record.
In addition, what users often want to see is what the local time was at the location.
To do that, when you store the UTC time, also store the timezone of the user who created the event.
The tricky part of all this is that MySQL and many other databases do NOT store the timezone with a date/time. So the timezone info is lost unless you store it explicitly in a different field.
Thus you need two fields in your database:
event_time_utc: Datetime, # UTC timezone for when the event happened
event_timezone: string # the timezone of where the event happened
Upvotes: 1
Reputation: 24340
I've got a similar issue. I kept the default rails config so all times are stored in UTC in the DB, and I used the following methods from ActiveSupport::TimeZone :
when I need to save a date from a form, I used local_to_utc
to convert the date from local to UTC:
ActiveSupport::TimeZone[my_timezone].local_to_utc(date)
when I need to display a date from the DB, I used
ActiveSupport::TimeZone[my_timezone].utc_to_local(date)
This way all dates saved in the DB are in UTC, but are handled by the users in local time, that can be different for each user. You can store the timezone at User
level (a string
field in the DB), and/or use a select helper to generate a select field.
Upvotes: 3