Skip to content

Instantly share code, notes, and snippets.

@iamvery
Last active August 29, 2015 14:06
Show Gist options
  • Save iamvery/eab7edeff434d2dfc89d to your computer and use it in GitHub Desktop.
Save iamvery/eab7edeff434d2dfc89d to your computer and use it in GitHub Desktop.
Buggy date behavior in Rails 3. Appears to be fixed in Rails 4. When zoned time is a different date from UTC, it is persisted incorrectly.
Loading development environment (Rails 3.2.19)
irb(main):001:0> Time.zone
=> #<ActiveSupport::TimeZone:0x007fa2e4533d68 @name="Osaka", @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Asia/Tokyo>, @current_period=nil>
irb(main):002:0> p = Plan.new
=> #<Plan id: nil, decided_on: nil>
irb(main):003:0> time = 1.day.ago
=> Fri, 26 Sep 2014 06:31:54 JST +09:00
irb(main):004:0> time.to_date
###
=> Fri, 26 Sep 2014
###
irb(main):005:0> p.decided_on = time
=> Fri, 26 Sep 2014 06:31:54 JST +09:00
irb(main):006:0> p.decided_on
=> Fri, 26 Sep 2014 06:31:54 JST +09:00
irb(main):007:0> p.save!
(0.1ms) begin transaction
SQL (1.9ms) INSERT INTO "plans" ("decided_on") VALUES (?) [["decided_on", Fri, 26 Sep 2014 06:31:54 JST +09:00]]
(2.3ms) commit transaction
=> true
irb(main):008:0> p.decided_on
###
=> Fri, 26 Sep 2014 06:31:54 JST +09:00
###
irb(main):009:0> p.reload
Plan Load (0.1ms) SELECT "plans".* FROM "plans" WHERE "plans"."id" = ? LIMIT 1 [["id", 2]]
=> #<Plan id: 2, decided_on: "2014-09-25">
irb(main):010:0> p.decided_on
###
=> Thu, 25 Sep 2014
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment