ruby-on-railstimezonedelayed-jobenqueue

Rails DelayedJob run_at Time Zone discrepancy


I am using Rails Delayed Job to en queue jobs to run at a specific time. I am in IST TimeZone. Delayed Job run_at is also configured to use IST TimeZone.

Now I want to trigger a job at 5:00 pm IST. I am storing '05:00 pm' as a string in my table and parsing it before scheduling the job.

So,

Time.parse('05:00 pm')

returns

2020-05-29 17:00:00 UTC

If I parse using IST

 Time.parse('05:00 pm').in_time_zone('Chennai')
 => Fri, 29 May 2020 22:30:00 IST +05:30

But I want 05:00 pm to be parsed as

  2020-05-29 11:30:00 UTC

so that my delayed job understands to run this job at 5:00 pm IST.

I can manually reduce 5:30 hours and enqueue but thats not the correct / optimized way I feel. Please suggest how can I do this?


Solution

  • You can do something like ActiveSupport::TimeZone.new('Chennai').parse("5 pm").utc

    Or ideally you could store your time as "17:00:00 +0530", so it's clear you mean the time in IST. Then simply Time.parse("17:00:00 +0530").utc would work too.