Testing in Seoul

by ewout

When dealing with time in a rails application, there are three time zones to be concerned with.

  1. UTC
  2. Time.zone
  3. Local time zone

By convention, all times are stored in the database as UTC. ActiveRecord abstracts this by converting dates from UTC to Time.zone when a timestamp is read from the database and doing the opposite when serializing times. That is, if you don’t forget to annotate each Time object that comes into the system with the correct zone.

The local time zone is the one you currently happen to live in, it should not matter at all in the application. Yet, by default, Time objects in ruby use the local time zone. Time.now returns the current local time, which is completely useless and even harmful in development.

I live in Belgium (CET), most of our clients do too. For years, I have been testing with Time.zone = ‘Brussels’. From now on, my testing Time.zone becomes ‘Seoul’.

Why Seoul?

The choice of a testing time zone is somewhat arbitrary, but it should not be equal to the local time zone. When UTC, Time.zone and the local time zone are different, it is easier to spot a bug where local time is used instead of the current Time.zone.

I also think it is important the time zone offset is far away from my local time zone, it makes me think more about time zones instead of just assuming that ActiveRecord will do its job.

Fork me on GitHub