Time.zone in a Rails application changes time zone globally.
Time.now is used in any other request,
it uses whatever Time.zone is set to earlier.
To avoid Time.zone issues, do not set
in_time_zone method to get time in desired zone.
The other day when going through code,
found out a method with an argument
time_zone argument was used to set
The code below Time zone set, was calculating some times,
which required the zone to be used which was passed in as an argument.
Time.zone sets the time zone,
is changed at application level.
This causes time zone issues at other places.
As other places do not want
Time.zone to be changed dynamically.
Solution: Avoid Setting Time.zone
To resolve, the problem at all other plcaes, we started using in_time_zone method.
Listing down the source code of the
TimeWithZone object in time zone passed to it as an argument.
in_time_zone method is available on
- Practice using
in_time_zonezone to convert Time in the desired zone.
Time.nowif you prefer everything in UTC.
DateTime.nowif you prefer everything in UTC.