Class: RailsBrowserTimezone::Filter
- Inherits:
-
Object
- Object
- RailsBrowserTimezone::Filter
- Defined in:
- lib/rails_browser_timezone/filter.rb
Class Method Summary collapse
- .filter(controller, &block) ⇒ Object
-
.time_zone_from_utc_offset(utc_offset_summer, utc_offset_winter, last_known_tz) ⇒ Object
Returns the time zone based on parsed utc_offset_summer and utc_offset_winter Returns the default TimeZone if none is resolved.
Class Method Details
.filter(controller, &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rails_browser_timezone/filter.rb', line 5 def self.filter(controller, &block) = controller.send(:cookies) last_known_tz = [:last_known_tz] ? [:last_known_tz] : Time.zone_default.name current_time_zone = time_zone_from_utc_offset([:utc_offset_summer].to_i, [:utc_offset_winter].to_i, last_known_tz) controller.response.(:last_known_tz, {:path => "/", :value => current_time_zone.name}) Time.use_zone(current_time_zone) do yield end end |
.time_zone_from_utc_offset(utc_offset_summer, utc_offset_winter, last_known_tz) ⇒ Object
Returns the time zone based on parsed utc_offset_summer and utc_offset_winter Returns the default TimeZone if none is resolved
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rails_browser_timezone/filter.rb', line 18 def self.time_zone_from_utc_offset(utc_offset_summer, utc_offset_winter, last_known_tz) # ActiveSupport::TimeZone[offset] - will return the first time zone that matches the offset. # But, we need to get the exact time zone inorder to reflect the daylight savings. # So, we get the user's time zone exactly by matching the summer offset and winter offset both. [ActiveSupport::TimeZone[last_known_tz], ActiveSupport::TimeZone.all].flatten.compact.detect(ifnone = Time.method(:zone_default)) do |zone| Time.use_zone(zone.name) do Time.zone.parse(Setting.mid_summer_date_str).utc_offset == utc_offset_summer && Time.zone.parse(Setting.mid_winter_date_str).utc_offset == utc_offset_winter end end end |