Skip to content

Instantly share code, notes, and snippets.

@shterrett
Created March 10, 2016 15:39
Show Gist options
  • Save shterrett/b81589d5220e8e402dcc to your computer and use it in GitHub Desktop.
Save shterrett/b81589d5220e8e402dcc to your computer and use it in GitHub Desktop.
require "minitest/autorun"
require "timecop"
describe "Parsing time in a time zone" do
describe "on current day" do
it "returns the proper offset" do
Timecop.travel Time.new(2016, 3, 9) do
time_string = "03/09/2016 10:00 AM America/Chicago"
format_string = "%m/%d/%Y %I:%M %p %Z"
parsed_time = Time.strptime(time_string, format_string)
expected_time = Time.new(2016, 3, 9, 10, 0, 0, "-06:00")
# fails - parsed time is at "-05:00" offset, which is my current, local
# timezone (Eastern Standard)
parsed_time.must_equal expected_time
end
end
it "returns the proper offset when explicitly passing offset" do
Timecop.travel Time.new(2016, 3, 9) do
time_string = "03/09/2016 10:00 AM -0600"
format_string = "%m/%d/%Y %I:%M %p %z"
parsed_time = Time.strptime(time_string, format_string)
expected_time = Time.new(2016, 3, 9, 10, 0, 0, "-06:00")
parsed_time.must_equal expected_time
end
end
it "returns the proper offset when explicitly passing abbreviation with
daylight savings time indication" do
Timecop.travel Time.new(2016, 3, 9) do
time_string = "03/09/2016 10:00 AM CST"
format_string = "%m/%d/%Y %I:%M %p %Z"
parsed_time = Time.strptime(time_string, format_string)
expected_time = Time.new(2016, 3, 9, 10, 0, 0, "-06:00")
parsed_time.must_equal expected_time
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment