Last Updated: March 06, 2021
·
4.252K
· coutermarsh

Schedule rails jobs in your users timezone

Struggled with this for a while today. Thought I'd share it!

Was trying to schedule sidekiq jobs to run at a specific time... based on our users timezone. Here's what I ended up going with.

This schedules a sidekiq job for 9:25am in the users timezone.

Time.zone = user.time_zone

user_time_to_utc = Time.zone.parse('2012-10-22 09:25:00').utc
puts "Scheduling for #{user.login} at #{user_time_to_utc}"

SidekiqJob.perform_at(user_time_to_utc,user.id)

Also on gist: https://gist.github.com/3927973

1 Response
Add your response

I believe you'd want to set Time.zone back to whatever it was before in the example above, otherwsie the Time class will be set to the last users zone until restart (or something else changes it).

An alternate might be:

ActiveSupport::TimeZone.new(user.time_zone).parse('2012-10-22 09:25:00')

over 1 year ago ·