Skip to content

Instantly share code, notes, and snippets.

@h3h h3h/time_utilities.rb
Last active Aug 29, 2015

Embed
What would you like to do?
Parse ISO-8601 time strings and respect the embedded time zone.
require 'active_support/core_ext/time'
require 'active_support/values/time_zone'
module TimeUtilities
# Parses an ISO-8601 time string with a time zone and keeps the time zone in
# the Time object returned, unlike Ruby Core.
#
# Examples:
#
# >> s = TimeUtilities.parse_with_time_zone('2014-08-15T15:35:00Z').utc_offset
# => 0
#
# >> s = TimeUtilities.parse_with_time_zone('2014-08-15T15:35:00-05:00').utc_offset
# => -18000
# >> ActiveSupport::TimeZone.seconds_to_utc_offset(s)
# => "-05:00"
#
# @param time_string [String] an ISO-8601 time string with a time zone
# @return [ActiveSupport::TimeWithZone]
#
def self.parse_with_time_zone(time_string)
unless time_string[4] == '-' && time_string[10] == 'T' && '-+Z'.include?(time_string[19])
raise ArgumentError, "Must be an ISO-8601-formatted date."
end
if time_string[-1] == 'Z'
ActiveSupport::TimeZone['UTC'].parse(time_string)
else # -05:00 / +09:30 / +11:00
offset = time_string[-6..-1]
sign = offset[0] == '-' ? -1 : 1
hours = offset[1] == '0' ? Integer(offset[2]) : Integer(offset[1..2])
minutes = offset[4] == '0' ? Integer(offset[5]) : Integer(offset[4..5])
offset_seconds = ((hours * 60 * 60) + (minutes * 60)) * sign
zone = ActiveSupport::TimeZone[offset_seconds]
raise "Unknown time zone with offset: #{ActiveSupport::TimeZone.seconds_to_utc_offset(offset_seconds)}" unless zone
zone.parse(time_string[0...-6])
end
end
end
require_relative '../spec_helper'
describe TimeUtilities do
describe '.parse_with_time_zone' do
let(:m) { described_class.method(:parse_with_time_zone) }
context "for a time in UTC" do
it "gives back a Time object that has its time zone as UTC" do
expect(m['2014-08-15T13:55:00Z'].utc?).to be_true
end
end
context "for a time in a negative offset time zone" do
it "gives back a Time object that has its time zone set correctly" do
time = m['2014-08-15T13:55:00-05:00']
expect(ActiveSupport::TimeZone.seconds_to_utc_offset(time.utc_offset)).to eq('-05:00')
end
end
context "for a time in a positive offset time zone" do
it "gives back a Time object that has its time zone set correctly" do
time = m['2014-08-15T13:55:00+11:00']
expect(ActiveSupport::TimeZone.seconds_to_utc_offset(time.utc_offset)).to eq('+11:00')
end
end
context "for a time in an invalid time zone" do
it "raises an exception" do
expect { m['2014-08-15T13:55:00-03:27'] }.to raise_exception
end
end
context "for a non-ISO-8601 time string" do
it "raises an exception" do
expect { m['2014-08-18 15:49:40 -0400'] }.to raise_exception
end
end
end
end
The MIT License
Copyright (c) 2014 Brad Fults, The Last Guide Company
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.