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
Related
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.
I have a background job that runs every 15 minutes and generates reminder emails. I would like to create a query that returns all users who have a specific time saved and respect how their timezone setting effects that time.
So I have a User model that stores:
:time: a users reminder time, eg 17:00:00
:string: their timezone, eg EST
So if the job runs at 17:00:00 EST, it will return users whose settings are:
reminder_time: 17:00:00, time_zone: EST
reminder_time: 13:00:00, time_zone: PST
What is the best way to build that query? Can it be done in one pass, relying on Postgres to handle the work? Do I have to stagger it, group by each time zone and doing the math for each on in Ruby?
I currently have this setup as an ActiveRecord scope that doesn't consider timezones, and I am trying to add that consideration now.
scope :receives_reminder_at, -> (time) do
ready.where(reminder_time: time)
end
When dealing with users in multiple timezones, it is normally easiest to standardize on UTC.
So store the reminder_time in UTC, this way you don't have to worry about the TZ when querying, since they will all be normalized to UTC. (assuming you are running your servers UTC. it will just work as expected). Then you just use their TZ offset in order adjust the time for their viewing.
You could use a select on User model. Something like:
User.select{|user| user.time == time_the_job_runs.in_time_zone(user.string)}
You should replace the "time_the_job_runs". It didn't got clear for me how to get it. But the in_time_zone method should be the one you're looking for to convert time based on a timezone string. Hope it helps, thanks!
Just met same problem. And found article how to make it, just how to think solve it myself. But with ready code. https://robots.thoughtbot.com/a-case-study-in-multiple-time-zones
Main idea is to:
At first you find timezones which have specific time now.
module ActiveSupport
class TimeZone
def self.current_zones(hour)
all.select { |zone|
t = Time.current.in_time_zone(zone)
t.hour == hour
}.map(&:tzinfo).map(&:name)
end
end
end
You find users with this timezone.
User.where(zone: ActiveSupport::TimeZone.current_zones(hour)).where(options)
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"
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.
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"