Convert ruby time to server timezone
Sep 29, 2014

In web application have to handle about 4 different timezones:

  • application timezone
  • user timezone
  • server time timezone
  • database timezone

Application timezone

Usually same with timezone of your business location, also usually a default timezone.

Rails.configuration.time_zone # => "Jakarta"
# or

User timezone

Timezone of current user, can be saved in user profile, or defected from javascript, eg

Usually set as:

Server timezone

It's a system time of current server, mostly UTC but sometimes can be a time of datacenter location. In development it's developer's machine timezone. You may need it if you want to make a cron task. Cron works in server's timezone.

To calculate it I have handy function what I use with gem whenever:

# Your business timezone = "Asia/Jakarta"

def to_server_time(time_str)
  time =
  server_offset = / 1.hour

to_server_time('00:30') # => Sun, 28 Sep 2014 17:30:00 WET +00:00
to_server_time('00:30').strftime("%H:%M") # => "17:30"

Database timezone

Mysql don't store timezone in datetime format, but Postgresql does. Anyway it better if all dates in database have same timezone. Usually it's UTC, or can be custom:

# or
comments powered by Disqus