New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iso8601 and rfc3339 parsing to timezones #28272

Merged
merged 4 commits into from Mar 3, 2017

Conversation

Projects
None yet
3 participants
@pixeltrix
Member

pixeltrix commented Mar 3, 2017

Adds Time.rfc3339, ActiveSupport::TimeZone.rfc3339 and ActiveSupport::TimeZone.iso8601 plus rfc3339 aliases to xmlschema for consistency when dealing with RFC 3339 timestamps in a Rails application.

Previous to this you would have to do things like this to get RFC 3339 timestamps into a particular timezone, e.g:

"2000-01-01T00:00:00Z".in_time_zone("Hawaii")

Also this doesn't enforce strict compliance with RFC 3339 - invalid dates or missing components will return nil instead of raising and error. We already have ActiveSupport::TimeZone.parse and ActiveSupport::TimeZone.strptime so adding these methods seem a natural fit - especially when converting a large number of timestamps, e.g:

zone = Time.find_zone("Hawaii")
zone.rfc3339("2000-01-01T00:00:00Z")

Added rfc3339 aliases to xmlschema since DateTime has them and using time format names consistently within an application is better.

@pixeltrix pixeltrix force-pushed the add-iso8601-and-rfc3339-parsing branch Mar 3, 2017

@pixeltrix pixeltrix changed the title from Add iso8601 and rfc3339 parsing to Add iso8601 and rfc3339 parsing to timezones Mar 3, 2017

activesupport/lib/active_support/core_ext/time/calculations.rb Outdated
#
# Time.rfc3339('1999-12-31T14:00:00-10:00') # => 2000-01-01 00:00:00 -1000
#
# If the the time or offset components are missing then an +ArgumentError+

This comment has been minimized.

@robin850

robin850 Mar 3, 2017

Member

There is an extra "the" here. 😊

activesupport/lib/active_support/values/time_zone.rb Outdated
# Time.zone = 'Hawaii' # => "Hawaii"
# Time.zone.iso8601('1999-12-31T14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
#
# If the the time components are missing then they will be set to zero.

This comment has been minimized.

@robin850

robin850 Mar 3, 2017

Member

Ditto.

activesupport/lib/active_support/values/time_zone.rb Outdated
# Time.zone = 'Hawaii' # => "Hawaii"
# Time.zone.rfc3339('2000-01-01T00:00:00Z') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
#
# If the the time or zone components are missing then an +ArgumentError+

This comment has been minimized.

@robin850

robin850 Mar 3, 2017

Member

Ditto.

@pixeltrix

This comment has been minimized.

Member

pixeltrix commented Mar 3, 2017

@robin850 thanks - my eyes are getting old 👴

pixeltrix added some commits Mar 3, 2017

Add `ActiveSupport::TimeZone.iso8601` parsing method
Previously there was no way to get a ISO 8601 timestamp into a specific
timezone without either using `parse` or chaining methods. The new method
allows parsing directly into the timezone, e.g:

    >> Time.zone = "Hawaii"
    => "Hawaii"
    >> Time.zone.iso8601("1999-12-31T14:00:00Z")
    => Fri, 31 Dec 1999 14:00:00 HST -10:00

If the timestamp is a ISO 8601 date (YYYY-MM-DD) then the time is set
to midnight, e.g:

    >> Time.zone = "Hawaii"
    => "Hawaii"
    >> Time.zone.iso8601("1999-12-31")
    => Fri, 31 Dec 1999 00:00:00 HST -10:00

This new method has stricter semantics than the current `parse` method
and will raise an `ArgumentError` instead of returning nil, e.g:

    >> Time.zone = "Hawaii"
    => "Hawaii"
    >> Time.zone.iso8601("foobar")
    ArgumentError: invalid date
    >> Time.zone.parse("foobar")
    => nil
Add `ActiveSupport::TimeZone.rfc3339` parsing method
Previously there was no way to get a RFC 3339 timestamp
into a specific timezone without either using `parse` or
chaining methods. The new method allows parsing directly
into the timezone, e.g:

    >> Time.zone = "Hawaii"
    => "Hawaii"
    >> Time.zone.rfc3339("1999-12-31T14:00:00Z")
    => Fri, 31 Dec 1999 14:00:00 HST -10:00

This new method has stricter semantics than the current
`parse` method and will raise an `ArgumentError`
instead of returning nil, e.g:

    >> Time.zone = "Hawaii"
    => "Hawaii"
    >> Time.zone.rfc3339("foobar")
    ArgumentError: invalid date
    >> Time.zone.parse("foobar")
    => nil

It will also raise an `ArgumentError` when either the
time or offset components are missing, e.g:

    >> Time.zone = "Hawaii"
    => "Hawaii"
    >> Time.zone.rfc3339("1999-12-31")
    ArgumentError: invalid date
    >> Time.zone.rfc3339("1999-12-31T14:00:00")
    ArgumentError: invalid date
Add `Time.rfc3339` parsing method
The `Time.xmlschema` and consequently its alias `iso8601` accepts
timestamps without a offset in contravention of the RFC 3339
standard. This method enforces that constraint and raises an
`ArgumentError` if it doesn't.
Add `rfc3339` aliases to `xmlschema`
For naming consistency when using the RFC 3339 profile
of ISO 8601 in applications.

@pixeltrix pixeltrix force-pushed the add-iso8601-and-rfc3339-parsing branch to f0aeecd Mar 3, 2017

@pixeltrix pixeltrix merged commit de17d9e into master Mar 3, 2017

3 checks passed

codeclimate no new or fixed issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@pixeltrix pixeltrix deleted the add-iso8601-and-rfc3339-parsing branch Mar 3, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment