vutran
vutran

Reputation: 2175

What's wrong with Rails's Date

I ran into this non-sense problem this morning in Rails 3.2 console. I'm under MacOS 10.10, my timezone is +7.

Loading development environment (Rails 3.2.12)
irb(main):001:0> Date.today
=> Sun, 16 Nov 2014
irb(main):002:0> Date.yesterday
=> Fri, 14 Nov 2014
irb(main):003:0>

Everything is fine with original Ruby Date:

irb(main):006:0> Date.today
=> #<Date: 2014-11-16 ((2456978j,0s,0n),+0s,2299161j)>
irb(main):007:0> Date.today.prev_day
=> #<Date: 2014-11-15 ((2456977j,0s,0n),+0s,2299161j)>
irb(main):008:0>

Upvotes: 3

Views: 702

Answers (1)

Alec Sanger
Alec Sanger

Reputation: 4562

From the bug report here: https://rails.lighthouseapp.com/projects/8994/tickets/6410#ticket-6410-8

This is a subtle one - Date.yesterday uses Date.current which will use the time zone whereas Date.today doesn't. If you set your time zone to one where it's tomorrow already (e.g. Europe/Berlin as I type this) then you can get Date.today == Date.yesterday:

Time.zone = "Europe/London" => "Europe/London" Date.today == Date.yesterday => false

Time.zone = "Europe/Berlin" => "Europe/Berlin" Date.today == Date.yesterday => true

Upvotes: 5

Related Questions