instance method iso8601

Ruby on Rails 6.0.6

Since v5.2.8.1

Available in: v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

iso8601(str)

Method for creating new ActiveSupport::TimeWithZone instance in time zone of self from an ISO 8601 string.

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

If the time components are missing then they will be set to zero.

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

If the string is invalid then an ArgumentError will be raised unlike parse which usually returns nil when given an invalid date string.

Parameters

str req
Source
# File activesupport/lib/active_support/values/time_zone.rb, line 380
    def iso8601(str)
      parts = Date._iso8601(str)

      raise ArgumentError, "invalid date" if parts.empty?

      time = Time.new(
        parts.fetch(:year),
        parts.fetch(:mon),
        parts.fetch(:mday),
        parts.fetch(:hour, 0),
        parts.fetch(:min, 0),
        parts.fetch(:sec, 0) + parts.fetch(:sec_fraction, 0),
        parts.fetch(:offset, 0)
      )

      if parts[:offset]
        TimeWithZone.new(time.utc, self)
      else
        TimeWithZone.new(nil, self, time)
      end
    end

Defined in activesupport/lib/active_support/values/time_zone.rb line 380 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveSupport::TimeZone

Type at least 2 characters to search.

↑↓ navigate · open · esc close