Created
April 2, 2016 19:22
-
-
Save jbranchaud/2fc89a93c04bf048d06228956e3d47c5 to your computer and use it in GitHub Desktop.
Error in timezone offset calculation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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