instance method parse

Ruby on Rails 4.2.9

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 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

parse(str, now=now())

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

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

If upper components are missing from the string, they are supplied from TimeZone#now:

Time.zone.now               # => Fri, 31 Dec 1999 14:00:00 HST -10:00
Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00

However, if the date component is not provided, but any other upper components are supplied, then the day of the month defaults to 1:

Time.zone.parse('Mar 2000') # => Wed, 01 Mar 2000 00:00:00 HST -10:00

Parameters

str req
now opt = now()
Source
# File activesupport/lib/active_support/values/time_zone.rb, line 351
    def parse(str, now=now())
      parts = Date._parse(str, false)
      return if parts.empty?

      time = Time.new(
        parts.fetch(:year, now.year),
        parts.fetch(:mon, now.month),
        parts.fetch(:mday, parts[:year] || parts[:mon] ? 1 : now.day),
        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 351 · 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