Ruby on Rails being an amazing framework, helps us manage the timezone of our rails application. It gives us access to a lot of helpers, to make our life easier. For example, if you want to change all the date and time of your application to the logged in users time zone, we just have to place the following code in the application_controller.
around_action :set_time_zone, if: :current_user
We assume that you have stored the user’s time_zone in your database in the time_zone column.
The application to show timezone can be set in your application.rb, if we don’t set a particular timezone then the application will just show the systems timezone.
config.time_zone = 'Central Time (US & Canada)'
If you want to know all the timezone options available in rails, run the rake -D time command in your terminal.
Even though rails would take care of the timezone, when we are using certain ruby commands, it gives us our systems timezone and not the one set by rails. So to avoid surprises, we should be aware of the timezones we are exposed to.
A rails app, would always be exposed to three timezones:
- System timezone
- Database timezone
- Rails applications own timezone
All three could be different, for example your system …