Set timezone from user in ApplicationController

It may not matter most of the time, but sometimes corner cases are relevant.

There is always some time zone set. UTC is the default although you can configure a different default if you want. Ideally your default is just a fallback. Ideally you are setting the time zone for the duration of the request based on what is most useful to the person making the request. This is what either your around_action or before_action do. Both will successfully do that but only one will put it back to the default when the request is done.

Most of the time this lack of cleanup won’t matter as the very next request will also assign the request-relevant timezone overwriting whatever was there before. But what if some controller doesn’t inherit from ApplicationController? Or what if a request is not handled by a controller at all but instead Rack middleware?

Whatever the scenario, corner cases like this may assume the time zone is on the default not realizing the last request didn’t cleanup itself. Cleaning up like this just keeps things more predictable. You don’t end up with odd scenarios that occur once in a while only when a series of certain actions happen.

1 Like