0

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?

Suganya Selvarajan
  • 962
  • 1
  • 11
  • 33
  • You can save the entire timestamp instead of just `05:00pm` - e.g. `"2020-05-29T17:00:00.000+05:30"`. So there's no data loss, at the time of saving and parsing later. If you want to save just time `"17:00:00.000+05:30"`. – anurag May 29 '20 at 07:29
  • I receive this time from front end. So they always return time like this – Suganya Selvarajan May 29 '20 at 07:35
  • you can preprocess it before saving - `Time.parse("05:00pm").in_time_zone("Chennai").to_s` – anurag May 29 '20 at 07:37
  • The problem with this approach is, if I parse '05:00 pm' in Time.parse("05:00pm").in_time_zone("Chennai").to_s will give me with Date. But For me the job has to run every day at 5 pm. So this will not work. Also in_time_zone("Chennai") will do +05:30 which will return 22:30. I dont want this. I want 11:30 to be stored so that the delayed job runs at 5:00 pm – Suganya Selvarajan May 29 '20 at 07:46

1 Answers1

1

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.

Rahul
  • 1,495
  • 1
  • 15
  • 25