Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Date.current is not correct #3128

Closed
mastermike14 opened this Issue Sep 26, 2011 · 3 comments

Comments

Projects
None yet
4 participants

from console Rails 3.0.7, Mac OS X Snow Leopard

ruby-1.9.2-p290 :039 > Date.current
=> Mon, 26 Sep 2011
ruby-1.9.2-p290 :040 > DateTime.now
=> Sun, 25 Sep 2011 20:47:01 -0500

Not sure why Date.current doesnt work but DateTime does. I would guess the former is Rails and the latter to be Ruby.

Contributor

dmathieu commented Sep 26, 2011

It's because of the timezone. The Date object returns the current date, apparently in UTC.
The DateTime call returns date and time, but in UTC - 5. Therefore, the day changes.

Try transforming the datetime object to utc :

DateTime.now.utc #=> Should be 26th of september
Contributor

twalpole commented Sep 26, 2011

@mastermike14 - Do you happen to have the ruby-units gem in your app? It has a version of Time#to_date that screws up everything

Owner

fxn commented Sep 26, 2011

In Rails you rarely want #now, in general you want #current to honor the tz of the application, defined in config/application.rb. Also, DateTime.yesterday and predicates like DateTime#past? and friends are defined to be relative to DateTime.current.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment