Parent

Namespace

Class/Module Index [+]

Quicksearch

Date

Public Class Methods

[ruby 1-9 only]
click to toggle source
_httpdate(str) → Hash or nil

Attempt to parse string using the 3 HTTP formats specified in RFC 2616. Returns a Hash of values if the string was parsed, and nil if not.

# File lib/date/format.rb, line 872
def self._httpdate(str)
  if /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+
      \d{2}\s+
      (#{Format::ABBR_MONTHS.keys.join('|')})\s+
      -?\d{4}\s+ # allow minus, anyway
      \d{2}:\d{2}:\d{2}\s+
      gmt\s*\z/ox =~ str
    _rfc2822(str)
  elsif /\A\s*(#{Format::DAYS.keys.join('|')})\s*,\s+
      \d{2}\s*-\s*
      (#{Format::ABBR_MONTHS.keys.join('|')})\s*-\s*
      \d{2}\s+
      \d{2}:\d{2}:\d{2}\s+
      gmt\s*\z/ox =~ str
    _parse(str)
  elsif /\A\s*(#{Format::ABBR_DAYS.keys.join('|')})\s+
      (#{Format::ABBR_MONTHS.keys.join('|')})\s+
      \d{1,2}\s+
      \d{2}:\d{2}:\d{2}\s+
      \d{4}\s*\z/ox =~ str
    _parse(str)
  end
end
[ruby 1-9 only]
click to toggle source
_iso8601(str) → Hash or nil

Attempt to parse string using a wide variety of ISO 8601 based formats, including the civil, commercial, and ordinal formats. Returns a Hash of values if the string was parsed, and nil if not.

# File lib/date/format.rb, line 739
def self._iso8601(str)
  if /\A\s*(([-+]?\d{2,}|-)-\d{2}-\d{2}|
            ([-+]?\d{2,})?-\d{3}|
            (\d{2}|\d{4})?-w\d{2}-\d|
            -w-\d)
      (t
      \d{2}:\d{2}(:\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/x =~ str
    _parse(str)
  elsif /\A\s*(([-+]?(\d{2}|\d{4})|--)\d{2}\d{2}|
            ([-+]?(\d{2}|\d{4}))?\d{3}|-\d{3}|
            (\d{2}|\d{4})?w\d{2}\d)
      (t?
      \d{2}\d{2}(\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(\d{2})?)?)?\s*\z/x =~ str
    _parse(str)
  elsif /\A\s*(\d{2}:\d{2}(:\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(:?\d{2})?)?)?\s*\z/x =~ str
    _parse(str)
  elsif /\A\s*(\d{2}\d{2}(\d{2}([,.]\d+)?)?
      (z|[-+]\d{2}(\d{2})?)?)?\s*\z/x =~ str
    _parse(str)
  end
end
[ruby 1-9 only]
click to toggle source
_jisx0301(str) → Hash or nil

Attempt to parse string using the JIS X 0301 date format or ISO8601 format. Returns a Hash of values if the string was parsed, and nil if not.

# File lib/date/format.rb, line 903
def self._jisx0301(str)
  if /\A\s*[mtsh]?\d{2}\.\d{2}\.\d{2}
      (t
      (\d{2}:\d{2}(:\d{2}([,.]\d*)?)?
      (z|[-+]\d{2}(:?\d{2})?)?)?)?\s*\z/x =~ str
    if /\A\s*\d/ =~ str
      _parse(str.sub(/\A\s*(\d)/, 'h\1'))
    else
      _parse(str)
    end
  else
    _iso8601(str)
  end
end
_parse(str, comp=true) → Hash click to toggle source

Attempt to parse the string by trying a wide variety of date formats sequentially (unless a match is found by the fast Ragel-based parser). The comp argument determines whether to convert 2-digit years to 4-digit years. If the str is not in a supported format, an empty hash will be returned.

This method searches for a match anywhere in the string, unlike most of the other ruby 1.9-only parsing methods which require that an exact match for the entire string.

# File lib/date/format.rb, line 662
def self._parse(str, comp=true)
  if v = _ragel_parse(str)
    return v
  end

  str = str.dup

  e = {:_ => {:comp => comp}}
  str.gsub!(/[^-+',.\/:@[:alnum:]\[\]]+/, ' ')

  _parse_time(str, e)
  _parse_day(str, e)

  _parse_eu(str, e)     ||
  _parse_us(str, e)     ||
  _parse_iso(str, e)    ||
  _parse_jis(str, e)    ||
  _parse_vms(str, e)    ||
  _parse_sla(str, e)    ||
  _parse_dot(str, e)    ||
  _parse_iso2(str, e)   ||
  _parse_year(str, e)   ||
  _parse_mon(str, e)    ||
  _parse_mday(str, e)   ||
  _parse_ddd(str, e)

  if str.sub!(/\b(bc\b|bce\b|b\.c\.|b\.c\.e\.)/, ' ')
    if e[:year]
      e[:year] = -e[:year] + 1
    end
  end

  if str.sub!(/\A\s*(\d{1,2})\s*\z/, ' ')
    if e[:hour] && !e[:mday]
      v = $1.to_i
      if (1..31) === v
        e[:mday] = v
      end
    end
    if e[:mday] && !e[:hour]
      v = $1.to_i
      if (0..24) === v
        e[:hour] = v
      end
    end
  end

  if e[:_][:comp]
    if e[:cwyear]
      if e[:cwyear] >= 0 && e[:cwyear] <= 99
        e[:cwyear] += if e[:cwyear] >= 69
                    then 1900 else 2000 end
      end
    end
    if e[:year]
      if e[:year] >= 0 && e[:year] <= 99
        e[:year] += if e[:year] >= 69
                  then 1900 else 2000 end
      end
    end
  end

  e[:offset] ||= zone_to_diff(e[:zone]) if e[:zone]

  e.delete(:_)
  e
end
[ruby 1-9 only]
click to toggle source
_rfc2822(str) → Hash or nil

Attempt to parse string using the RFC 2822 format used in email. It's similar to the preferred HTTP format except specific offsets can be used. Returns a Hash of values if the string was parsed, and nil if not.

# File lib/date/format.rb, line 841
def self._rfc2822(str)
  if /\A\s*(?:(?:#{Format::ABBR_DAYS.keys.join('|')})\s*,\s+)?
      \d{1,2}\s+
      (?:#{Format::ABBR_MONTHS.keys.join('|')})\s+
      -?(\d{2,})\s+ # allow minus, anyway
      \d{2}:\d{2}(:\d{2})?\s*
      (?:[-+]\d{4}|ut|gmt|e[sd]t|c[sd]t|m[sd]t|p[sd]t|[a-ik-z])\s*\z/ox =~ str
    e = _parse(str, false)
    if $1.size < 4
      if e[:year] < 50
        e[:year] += 2000
      elsif e[:year] < 1000
        e[:year] += 1900
      end
    end
    e
  end
end
Also aliased as: _rfc822
[ruby 1-9 only]
click to toggle source
_rfc3339(str) → Hash or nil

Attempt to parse string using the RFC 3339 format, which is the same as the ISO8601 civil format requiring a time and time zone. Returns a Hash of values if the string was parsed, and nil if not.

# File lib/date/format.rb, line 772
def self._rfc3339(str)
  if /\A\s*-?\d{4}-\d{2}-\d{2} # allow minus, anyway
      (t|\s)
      \d{2}:\d{2}:\d{2}(\.\d+)?
      (z|[-+]\d{2}:\d{2})\s*\z/x =~ str
    _parse(str)
  end
end
_rfc822(str) click to toggle source
ruby 1.9 only
Alias for: _rfc2822
[ruby 1-9 only]
click to toggle source
_xmlschema(str) → Hash or nil

Attempt to parse string using the XML schema format, which is similar to the ISO8601 civil format, with most parts being optional. Returns a Hash of values if the string was parsed, and nil if not.

# File lib/date/format.rb, line 789
def self._xmlschema(str)
  if /\A\s*(-?\d{4,})(?:-(\d{2})(?:-(\d{2}))?)?
      (?:t
        (\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?)?
      (z|[-+]\d{2}:\d{2})?\s*\z/x =~ str
    e = {}
    e[:year] = $1.to_i
    e[:mon] = $2.to_i if $2
    e[:mday] = $3.to_i if $3
    e[:hour] = $4.to_i if $4
    e[:min] = $5.to_i if $5
    e[:sec] = $6.to_i if $6
    e[:sec_fraction] = $7.to_i/10.0**$7.size if $7
    if $8
      e[:zone] = $8
      e[:offset] = zone_to_diff($8)
    end
    e
  elsif /\A\s*(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?
      (z|[-+]\d{2}:\d{2})?\s*\z/x =~ str
    e = {}
    e[:hour] = $1.to_i if $1
    e[:min] = $2.to_i if $2
    e[:sec] = $3.to_i if $3
    e[:sec_fraction] = $4.to_i/10.0**$4.size if $4
    if $5
      e[:zone] = $5
      e[:offset] = zone_to_diff($5)
    end
    e
  elsif /\A\s*(?:--(\d{2})(?:-(\d{2}))?|---(\d{2}))
      (z|[-+]\d{2}:\d{2})?\s*\z/x =~ str
    e = {}
    e[:mon] = $1.to_i if $1
    e[:mday] = $2.to_i if $2
    e[:mday] = $3.to_i if $3
    if $4
      e[:zone] = $4
      e[:offset] = zone_to_diff($4)
    end
    e
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.