Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set current Time.zone in Rails?

Javascript on my page saves client UTC offset to the cookie. How do I use this cookie to create a TimeZone and assign it to Time.zone ?

I need something like:

before_filter :set_time_zone

def set_time_zone
  Time.zone = ActiveSupport::TimeZone.new('my timezone', cookies[:timezone])
end

except that the right part of this expression does not work and I'm not sure if I'm going the right way here. Can't get it.

like image 755
snitko Avatar asked Jun 03 '09 01:06

snitko


People also ask

How do you get the time zone in Ruby?

Ruby | Time zone() functionTime#zone() : zone() is a Time class method which returns the name of the time zone used for time like “UTC”, “GMT”.

How to add a Timezone attribute to a user?

Every user should have a timezone attribute, most likely in the database, but in early stages you can just create a method in the User class that returns a known value. This is one of the most crucial things, each time you need to show a date / time to a user, you should convert that time to the user's timezone.

What are the time options in the time table?

The time options ( :hour, :min, :sec, :usec, :nsec) reset cascadingly, so if only the hour is passed, then minute, sec, usec, and nsec is set to 0. If the hour and minute is passed, then sec, usec, and nsec is set to 0. The options parameter takes a hash with any of these keys: :year, :month, :day, :hour, :min, :sec, :usec, :nsec, :offset.

Do I need to include time zones in my application?

Unless you are confident that you will never need to deal with timezones, you should think about them sooner rather than later, building your application in a way that accommodates for them from the beginning. This is one of those things that can be very difficult to include later, so I don't think it is a premature optimisation.


2 Answers

Here's the working googled answer:

min = cookies[:timezone].to_i
Time.zone = ActiveSupport::TimeZone[-min.minutes]

Just to make it clear, the javascript part:

if(!($.cookie('timezone'))) {
  current_time = new Date();
  $.cookie('timezone', current_time.getTimezoneOffset(), { path: '/', expires: 10 } );
} 
like image 66
snitko Avatar answered Sep 17 '22 22:09

snitko


@snitko - you're answer worked great for me for a long time. However, as @Giovanni pointed out, it doesn't account for daylight savings time in some scenarios since it pulls the first timezone available given the minute offset. I've found a simple working answer that accounts for DST.

Head here - http://site.pageloom.com/automatic-timezone-detection-with-javascript, it's a javascript timezone detector. You simply copy the javascript code linked from the website (or right here) into your one of your app's javascript files (application.js works fine). It allows you to retrieve the timezone through an object named jstz.

Then, in application.html.erb I have

<script type="text/javascript">

  var timezone = jstz.determine();
  document.cookie = 'time_zone='+timezone.name()+';';

</script>

And in application_controller.rb, I have

before_filter :set_timezone 

private
def set_timezone
  Time.zone = cookies["time_zone"]
end

And that's all you need! Time zone is set properly for your Rails app.

EDIT: It's possible you need to place the javascript cookie setting code after your <\body> tag in application.html.erb due to the different way and order javascript files are loaded up.

like image 41
efatsi Avatar answered Sep 17 '22 22:09

efatsi