Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update timestamp generation to support ActiveSupport::TimeWithZone #34

Merged
merged 1 commit into from

2 participants

Jamie White Jeff Gardner
Jamie White

We found that the current method for generating unix timestamps in ScriptTag can produce the wrong timestamp for ActiveSupport::TimeWithZone objects.

It seems that TimeWithZone#strftime('%s') will yield a timestamp that includes daylight savings offsets. The resulting timestamp will be an hour adrift if daylight savings are in effect.

This PR switches to using to_i directly on Time, DateTime and their descendants.

Jeff Gardner erskingardner merged commit 29e8319 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 3, 2013
  1. Jamie White
This page is out of date. Refresh to see the latest.
2  Gemfile.lock
View
@@ -60,6 +60,7 @@ GEM
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)
+ tzinfo (0.3.37)
PLATFORMS
ruby
@@ -72,3 +73,4 @@ DEPENDENCIES
rspec-mocks
sinatra (~> 1.3.1)
thin (~> 1.4.1)
+ tzinfo
1  intercom-rails.gemspec
View
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'pry'
s.add_development_dependency 'sinatra', '~> 1.3.1'
s.add_development_dependency 'thin', '~> 1.4.1'
+ s.add_development_dependency 'tzinfo'
end
2  lib/intercom-rails/script_tag.rb
View
@@ -112,7 +112,7 @@ def widget_options_from_config
def convert_dates_to_unix_timestamps(object)
return Hash[object.map { |k, v| [k, convert_dates_to_unix_timestamps(v)] }] if object.is_a?(Hash)
- return object.strftime('%s').to_i if object.respond_to?(:strftime)
+ return object.to_i if object.is_a?(Time) || object.is_a?(DateTime)
object
end
7 test/intercom-rails/script_tag_test.rb
View
@@ -1,4 +1,5 @@
require 'active_support/core_ext/string/output_safety'
+require 'active_support/time'
require 'test_setup'
class ScriptTagTest < MiniTest::Unit::TestCase
@@ -23,6 +24,12 @@ def test_converts_times_to_unix_timestamps
now = Time.now
nested_time = ScriptTag.new(:user_details => {:custom_data => {"something" => now}})
assert_equal now.to_i, nested_time.intercom_settings[:custom_data]["something"]
+
+ utc_time = Time.utc(2013,04,03)
+ time_zone = ActiveSupport::TimeZone.new('London')
+ time_with_zone = ActiveSupport::TimeWithZone.new(utc_time, time_zone)
+ time_from_time_with_zone = ScriptTag.new(:user_details => {:created_at => time_with_zone})
+ assert_equal utc_time.to_i, time_from_time_with_zone.intercom_settings[:created_at]
end
def test_strips_out_nil_entries_for_standard_attributes
Something went wrong with that request. Please try again.