Ben Downey
Ben Downey

Reputation: 2665

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 ==)

Diff:
@@ -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?

Upvotes: 0

Views: 171

Answers (1)

Jim Van Fleet
Jim Van Fleet

Reputation: 1118

The issue here is the behavior of Time.parse. Replacing that call with Time.zone.parse 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.

Upvotes: 1

Related Questions