5

Rails stores all DateTimes in the UTC timezone. I need to display that time now to the user in the timezone that their browser reports they're in. Is there an easy way to do this?

Using latest Rails (3.2.13).

at.
  • 50,922
  • 104
  • 292
  • 461

2 Answers2

7

Someone has made the solution into a gem:

https://github.com/basecamp/local_time

Pencilcheck
  • 2,664
  • 3
  • 25
  • 14
3

in client (js):

function set_time_zone_offset() {
    var current_time = new Date();
    $.cookie('time_zone', current_time.getTimezoneOffset());
}

in Application Controller:

before_filter :set_timezone 

def set_timezone  
 min = request.cookies["time_zone"].to_i
 Time.zone = ActiveSupport::TimeZone[-min.minutes]
end 

credit: https://stackoverflow.com/a/5930596/643500

Community
  • 1
  • 1
Sully
  • 14,672
  • 5
  • 54
  • 79