What's wrong with Rails Date
I ran into this insensitive issue this morning in the Rails 3.2 console. I'm on 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 the 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>
+3
source to share
1 answer
From the bug report here: https://rails.lighthouseapp.com/projects/8994/tickets/6410#ticket-6410-8
It's subtle - Date.yesterday uses Date.current, which will use the timezone, while Date.today will not. If you've set the timezone to where it's already tomorrow (like Europe / Berlin when I type this), 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
+5
source to share