Ben Downey Ben Downey - 1 year ago 113
Ruby Question

Rails Timestamps UTC vs

I'm getting a failing rpsec test and it seems like it's a difference between the system clock and how Rails evaluates the time.


expected: 2015-01-01 15:00:00.000000000 -0800
got: 2015-01-01 15:00:00.000000000 +0000

(compared using ==)

@@ -1,2 +1,2 @@
-2015-01-01 15:00:00 -0800
+2015-01-01 15:00:00 UTC


For some reason
Time.parse('01/01/2015 3:00pm')
coerces the string into the timezone on the system clock. But the code is under test is outputting in UTC.

Does anyone know why or where this happens?

Answer Source

The issue here is the behavior of Time.parse. Replacing that call with allows an ActiveSupport::TimeWithZone

Though the particulars of the implementation may vary, you can get the sense of the design goals of Time.parse from this documentation:

Since there are numerous conflicts among locally defined time zone abbreviations all over the world, this method is not intended to understand all of them.

GMT is considered the default time, and matches the output you report above. Replacing it with an ActiveSupport::TimeWithZone allows Rails configuration to amend that default expectation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download