Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Date.tomorrow > Time.now return false in Ruby on Rails?

See the RoR pry console result:

[1] pry(main)> Date.tomorrow > Time.now
=> false
[2] pry(main)> Date.tomorrow.to_time > Time.now
=> true
[3] pry(main)> Date.tomorrow
=> Tue, 07 Oct 2014
[4] pry(main)> Time.now
=> 2014-10-06 22:52:40 -0400

Added timestamp result to let you know the rough values.

like image 340
Guoqiang Huang Avatar asked Feb 11 '23 20:02

Guoqiang Huang


1 Answers

Because Date.tomorrow is tomorrow and Time.now in UTC is also tomorrow.

You'll notice you are doing this at 10:52pm (e.g. EST)
but that is actually 2:52am tomorrow (given the -0400 difference)

Thus as tomorrow is not "greater than" tomorrow, that is false.

If you changed it to >= it might then pass.

As you've found though, a workaround is use tack on .to_time

like image 180
Michael Durrant Avatar answered Feb 15 '23 10:02

Michael Durrant