Home > database >  Rails saves a Time instance to the wrong hour
Rails saves a Time instance to the wrong hour

Time:06-23

So I have the following Time as a strong:

0:00:00.000

and I use the following strptime:

Time.strptime(times[0], "%H:%M:%S.%L")

Expected Result 0:00:00.000

Actual Result 6:00:00.000

I am 6 hours behind UTC outside of daylight savings. So that might explain why it s saving six hours ahead. I've tried to wrap it in a Time.use_zone and set the zone to UTC but that still doesn't do the trick.

For now I have it subtracting six hours before saving the record. But I'd like to figure out how to force it to save the timing to the correct hour.

CodePudding user response:

Try with Time.zone

t = "0:00:00.000"
Time.zone.strptime(t, "%H:%M:%S.%L")

=> Thu, 23 Jun 2022 00:00:00.000000000 CDT -05:00
  • Related