parse

parse(date, now=self.now)
Class Public methods

Parses date using Date._parse and converts it to a Time object.

If a block is given, the year described in date is converted by the block. For example:

Time.parse(...) {|y| 0 <= y && y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}

If the upper components of the given time are broken or missing, they are supplied with those of now. For the lower components, the minimum values (1 or 0) are assumed if broken or missing. For example:

# Suppose it is "Thu Nov 29 14:33:20 GMT 2001" now and
# your time zone is GMT:
now = Time.parse("Thu Nov 29 14:33:20 GMT 2001")
Time.parse("16:30", now)     #=> 2001-11-29 16:30:00 +0900
Time.parse("7/23", now)      #=> 2001-07-23 00:00:00 +0900
Time.parse("Aug 31", now)    #=> 2001-08-31 00:00:00 +0900
Time.parse("Aug 2000", now)  #=> 2000-08-01 00:00:00 +0900

Since there are numerous conflicts among locally defined time zone abbreviations all over the world, this method is not intended to understand all of them. For example, the abbreviation âCSTâ is used variously as:

-06:00 in America/Chicago,
-05:00 in America/Havana,
+08:00 in Asia/Harbin,
+09:30 in Australia/Darwin,
+10:30 in Australia/Adelaide,
etc.

Based on this fact, this method only understands the time zone abbreviations described in RFC 822 and the system time zone, in the order named. (i.e. a definition in RFC 822 overrides the system time zone definition.) The system time zone is taken from Time.local(year, 1, 1).zone and Time.local(year, 7, 1).zone. If the extracted time zone abbreviation does not match any of them, it is ignored and the given time is regarded as a local time.

ArgumentError is raised if Date._parse cannot extract information from date or if the Time class cannot represent specified date.

This method can be used as a fail-safe for other parsing methods as:

Time.rfc2822(date) rescue Time.parse(date)
Time.httpdate(date) rescue Time.parse(date)
Time.xmlschema(date) rescue Time.parse(date)

A failure of ::parse should be checked, though.

You must require 'time' to use this method.

doc_ruby_on_rails
2015-05-17 18:06:18
Comments
Leave a Comment

Please login to continue.