Skip to content

Rodrigo Urubatan – About Code

Helping ruby developers to use the best tools for each job so they can solve hard problems, with less bugs and have more free time.

Menu
  • Home
  • My last presentations
  • About
  • Privacy Policy
Menu

Beware of Date.yesterday on your code (A timezone history)

Posted on 2019-07-15 by Rodrigo Urubatan

A few minutes ago I had a problem in production in a scheduled job that was working for more than a year now, the problem was that a new user scheduled it for 22:00 GMT -3, what hadn’t happened before.

The code had tests, but we usually do not work around this time and the tests automated tests never were executed this late at night.

That scheduled job used to define the contents of a message Date.yesterday, and that being executed at 22:00 GMT-3, was really being executed at 01:00 GMT.

And this caused Date.yesterday to actually be today at 00:00.

The solution was actually simple, but really hard to test, since the problem only happens if the scheduled time in the user time zine was tomorrow GMT.

So every time you are using any type of date and time in your code, remember to surround it with this simple snippet that will save your life!

Time.use_zone(current_user.timezone) do
# Here it is safe to use Date.yesterday, and any relative date and time codes
end

If you are using Rails, and you can configure a timezone for the application (what is not my situation, because each user has a different timezone), you can simply change ” config.time_zone ” in your environment or config/application.rb

For one timezone for each user, you can for example add an around filter in the ApplicationController like this:

around_action :user_timezone
 
def user_timezone
  Time.use_zone(current_user.timezone) do
    yield
  end
end

And if I did that before (not exactly that, but the idea is the same) I wouldn’t have had the production problem from today…

So I hope this post can help prevent the same problem in your application.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

  • Why Embrace Nesting in Ruby Modules?
  • An easy way to have a local “Github Copilot” for free
  • SPA without touching Javascript – The magic of Ruby on rails and Hotwire
  • I see Dead Jobs everywhere (sidekiq DeadSet)
  • Quick tips that help: rails notes

Arquives

  • May 2024
  • April 2024
  • February 2023
  • January 2023
  • December 2022
  • June 2021
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • May 2019
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • August 2015
  • August 2014
  • July 2014
  • August 2007

Categories

  • AI
  • articles
  • cfp
  • firebase
  • gems
  • git
  • opinion
  • presentations
  • projects
  • rails6
  • ruby
  • Sem categoria
  • server-api
  • tutorials
  • Uncategorized
© 2025 Rodrigo Urubatan – About Code | Powered by Minimalist Blog WordPress Theme