Supporting Timezones in Rails - ruby-on-rails

I've got an application that allows users within the same company to create job records, view, edit, and search these jobs. The users are spread out all over the United States. A user in California may need to update a job for a user in New York and visa versa.
I read through an article that suggested setting Time.zone in the controller with a before action, but if I do this, I assume that Rails would then save that record in the current time zone for like the California user. Then, if the New York user updates the same record then updated_at time would then be in a different time zone than the created_at attribute. Ideally, I see all of my records having a UTC time and then when a given user accesses/creates records, the time is displayed to them in their set timezone but saved in the database as UTC. I'm not sure how to do this or if it's even the correct approach. Can anyone provide some guidance?
Thanks in advance.

#Cannon Moyer if you see table structure of any table. updated_at and created_at columns store timestamp without timezone. When displaying, These values are automatically converted with timezone as application's timezone(UTC or IST) is set. so don't need to worry about time when a given user accesses/creates records.
For your case as #iGian suggested this gem is useful for you. It sets the Rails timezone to the browser's configured timezone for each request.

Related

Finding records across time zones using active record

I'm developing an app where a user can request that an email be sent to them at a specific time every day in their timezone.
For example User A lives in London and schedules an email at 2pm every day London time and User B lives in New York and schedules an email at 2pm New York time.
I'm wondering how I should store my schedule timestamps in my database and what supporting information I need to store along side this to support the fact that all times depend on a specific timezone.
I'm thinking that storing the timestamps as utc with a separate column for the timezone is the way to go but I'm unsure of how to query the database to return all scheduled emails at a specific time without having to perform one query per timezone.
Yes, store the data in UTC - rails will do this by default; put a time_zone column on your User model
Here is a post I wrote for timezones in rails which you might find helpful: http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/
This might be overly complicated, but I am thinking if you had a background job running (delayed_job, sidekiq, etc) it would check each timezone/email schedule - something like this (You definitely want time_zone on your User, maybe on the schedule as well?)
# loop through all timezones
ActiveSupport::TimeZone.zones_map(&:name).each do |time_zone|
# get a list of any saved schedules in this timezone
scheduled_in_zone = EmailSchedule.where(time_zone: time_zone)
# execute block in that timezone
Time.use_zone(time_zone) do
# Time.current is timezone aware (same as Time.zone.now)
now = Time.current
scheduled_in_zone.each do |scheduled|
if scheduled.deliver_at <= now
scheduled.deliver_it!
end
end
end
end
I also recommend checking out: http://railscasts.com/episodes/106-time-zones-revised

Ruby time zone conversions and always showing local time to the user

I have a system of cities and events that happen in those cities. When a user creates an event, it is associated with a city, and therefore a time zone. I'm confused about Ruby's various implementations (Date, Time, DateTime) and how to handle the saving and displaying of event times relative to a city's time zone.
I have a datetime column in my database. My HTML form has a field for city (and therefore that city's time zone) and I use datetime_select() to give the user fields to enter the event's date and time. The idea is that if the user has selected "Seattle", and then a time of 7pm on April 1, the user should not have to worry about time zones at all, and the assumption is that they are setting it for 7pm Seattle time. I want to either save this datetime as UTC (after making the adjustment from Seattle time to UTC) or apply the Seattle time zone to it without altering the specified hour. Since I am using mass assignment when I save my form data to the database, I'm guessing I need to alter the data (adjusting for time zone) before saving, otherwise, it will assume that this 7pm time is already UTC, which of course it isn't.
When I display this information from the database, it will be in the context of a time zone, so when displaying this event, the system knows it is in Seattle and will therefore show 7pm (as the original user who created the event intended).
How should I handle this? What implementation should I use (Date, Time, DateTime), and what conversions should be done on saving and loading the data from the database so users always see local times?

Rails seems to be creating objects in wrong time zone for created_at, updated_at values

I just created a new object using a form and its updated_at and created_at values are
created_at: "2013-04-25 03:22:19", updated_at: "2013-04-25 03:22:19",
However that is ~7 hours ahead of where I am (PST).
Time.now.to_s
=> "2013-04-24 20:23:12 -0700"
How can I make sure the time zone is consistent with wherever a user is creating it from?
Your created_at and updated_at values are written from your server to the database, so they will always be in your server's or application's time zone. By the time the server processes the form data and saves to your database, it has no knowledge of the browser's time zone.
You can set a Rails time zone using config.time_zone, as #lulalala suggested.
It sounds like you're interested in displaying times to your users in their native time zones. You have two options for this:
Ask your user for a time zone on signup (or assign a default one and allow users to edit it)
Detect time zone using javascript and report back to your server (http://josephscott.org/archives/2009/08/detecting-client-side-time-zone-offset-via-javascript/) - not guaranteed to be correct
Once you have the time zone saved as a user attribute, you can display your times to your users like this:
Model.created_at.in_time_zone(#current_user.time_zone)
I check irb timezone with the command > Time.now.to_s and the time was correct, but the app save it wrong by 3 hours. So..
The solution for me was add config.time_zone = "Santiago" to config/environments/development.rbfile. The time zone list is here, and my entry was
"Santiago" => "America/Santiago"

Managing multiple users time zones in ruby on rails

I want to save an event in a default time zone i.e. Whenever an user submits an event it will get convert to the default time zone and will be save in the database.
Whenever user requests for an event, the system will find that users time zone and convert the date (from the default format) in that user time zone and display it to the user.
I'm having difficult time where to start from i saw many notes and documents but couldn't figure it out the complete process of doing it.
I saw this code but couldn't understand how to use it :
before_filter :set_time_zone
def set_time_zone
Time.zone = current_user.time_zone if current_user
end
The default time zone in the database is UTC.
There are 2 ways you can do it:
Allow their user to store their time zone in the database
Grab their time zone from their IP
For option 1, Rails has very good support for time zones. The #all method will allow you to create a dropdown for them to choose from. Then save it in the database with the user record.
Option 2 is less work for the user, but is also less accurate. There are a few services that convert IP's to Time Zones.
To display the time in a given time zone, use Time#in_time_zone or you can set the Time.zone as above and it should display properly.

Time.now.midnight's susceptibility to time zones

I have some daily analytics-style records that track usage on my site, and they work as follows:
When an 'event' occurs, the site looks for a record that was created at Time.now.midnight.
If such a record is not found, a new one is created, and created_at is set to Time.now.midnight.
Here's the question - does Time.now.midnight get recorded differently depending upon the client's time zone? If so, am I correct in assuming that the above very simple system will break down?
How can I fix it so all analytics records, irrespective of the time zone of the user who triggered their creation, get pegged to one time?
Note: I imagine that setting the created_at field to a Date instead of Datetime might have been better in this scenario. Let's assume we're stuck with datetime for this question.
Time.now does not get recorded differently based on the clients timezone.
Time.now returns a new time using the system timezone (aka the server)
To use a client specific timezone you have to have a user select their zone and use Time.current or Time.zone.now (which do the same thing)
created_at is usually pinned to UTC, so you shouldn't have any issues their either.
(to change this you need to change Rails.root/config/application.rb)
config.time_zone = "whatever you want the ActiveRecord default to be"

Resources