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.
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”.
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.
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.
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.
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 } );
}
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With