No commit activity in last 3 years
No release in over 3 years
Simple code to detect browser time zone and handle it in rails app
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

rails_browser_timezone

The purpose of this gem is to track browser's timezone and run rails controller code in the time zone detected for each request.

For background and thoughts, please refer - http://developerstream.blogspot.in/2015/09/handling-requests-in-browser-time-zone.html

Instructions to use

Installation -

  gem 'rails_browser_timezone', '0.2.0', :git => 'https://github.com/udayakiran/rails_browser_timezone'

Usage -

  1. Create a file in your initializers and set baseline year. This step is optional. This means we are worried about time zone changes that occurred till this year. My suggesstion is to keep this year same as either the year you are starting this project or the year when your rails version is released. Use "current" as the value if you want to stay on the edge. But, note that rails and your browsers need to be supporting this as well.
# Say in config/initializers/rails_browser_tz_init.rb
RailsBrowserTimezone::Setting.baseline_year = 2014 #default value is 2011. Accepted values - any valid year or string - "current"
  1. Include the around filter in every controller that needs to run code in user's timezone. If you need it for all controllers obviously add it to the application controller.
 #Rails 4.1.x or earlier (inlcuding Rails 2,3 and 4)

 class MyTimeZoneController < ApplicationController  #Or in your application_controller.rb
 
   prepend_around_filter RailsBrowserTimezone::Filter
   	.......
   	.......

 end

 #Rails 4.2.x or later

 class MyTimeZoneController < ApplicationController  #Or in your application_controller.rb

   prepend_around_action RailsBrowserTimezone::Filter 
        .......
        .......

 end
  1. Add the js code that sets the browser offsets in your js files. Code can be found in assets directory based on the js lib you use. If you use jquery, copy the js code from 'assets/set_browser_offset_cokies_jquery.js' and paste it in your js file which is inlcuded in every page. If you use prototypejs, copy the js code 'from assets/set_browser_offset_cokies_prototype.js' and paste it in your js file which is inlcuded in every page.

Saving time zone in the database -

If you like to save the last_known_timezone of any user in the database, it can be done by using "Time.zone.name" any where in your controller, once 'RailsBrowserTimezone::Filter' is done with the determining of the time zone from offsets.

Practices in your code -

  1. Use Time.zone.* not Time.* :- Most of the scenarios we need to deal with times in the current time zone not in the system time zone on which app is running. So, we should use Time.zone.now, Time.zone.parse and time_obj.in_time_zone(Time.zone) when we are dealing with time information.

  2. Use Time.use_zone :- When we need to operate in other time zones than the current system, enlose that code in Time.use_zone block. This sets back the system time zone once the code completes execution or even when exception occurs. Otherwise we should always remember to set the system's time zone back to default.

To do -

Add specs and tests.

Contributing -

Please help with your contribution by filing any issues if found. Pull requests are welcomed :)