Use Time.current / Date.current / DateTime.current on projects that have a time zone
This ancient card is no longer entirely correct for newer Railses and was rewritten.
Basically, you now need to know if your project uses a "real" time zone or
:local, and if
config.active_record.time_zone_aware_attributes is set to
false or not.
With time zones configured, always use
ActiveRecord attributes will be time-zoned, and
.currentvalues will be converted properly when written to the database.
Do not use
Time.nowand friends. Timezone-less objects will not be converted properly when written to the database.
With no/local time zone use
ActiveRecord attributes will not be time-zoned.
.currentwould hand you UTC objects whose
to_s(:db)may not convert properly.
The following was at least valid for Rails 2.3.
It's been briefly mentioned in the random list of ActiveSupport goodies, but please do remember to always use
Time.current instead of
Because of the way Rails and MySQL deal with time zones you would need to take care to use
Time.zone.now in projects which hold time zone information and
Time.now only in those that run with the server's time. If you don't, bad things can and will happen. More information can be found that card.
Time.current decides if it wants to be a
Time.zone.now or a
Time.now depending on the project settings. Using it keeps you sane and happy.
That logic is available for