Skip to content

Instantly share code, notes, and snippets.

@jbranchaud
Created April 2, 2016 19:22
Show Gist options
  • Save jbranchaud/2fc89a93c04bf048d06228956e3d47c5 to your computer and use it in GitHub Desktop.
Save jbranchaud/2fc89a93c04bf048d06228956e3d47c5 to your computer and use it in GitHub Desktop.
Error in timezone offset calculation
require 'date'
require 'minitest/autorun'
class TestMeme < MiniTest::Unit::TestCase
def time_to_timestamp(now:)
hours_in_seconds = now.strftime('%H').to_i * 3600
minutes_in_seconds = now.strftime('%M').to_i * 60
seconds = now.strftime('%S').to_i
local_now = hours_in_seconds + minutes_in_seconds + seconds
offset_now = local_now - now.to_i % (24 * 3600)
if offset_now <= -12 * 60 * 60
offset_now += (24 * 3600)
# elsif offset_now >= 12 * 60 * 60
# offset_now -= (24 * 3600)
end
Time.at(now.to_i - offset_now).to_datetime.strftime('%Y%m%d%H%M%S')
end
# passes
def test_630pm_central
time = Time.new(2016, 04, 01, 18, 30, 0, "-05:00")
assert_equal time_to_timestamp(now: time), '20160401233000'
end
# fails without lines 16 and 17
def test_730pm_central
time = Time.new(2016, 04, 01, 19, 30, 0, "-05:00")
assert_equal time_to_timestamp(now: time), '20160402003000'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment