Method: Time.parse
- Defined in:
-
lib/framework/time.rb,
lib/framework/autocomplete/Time.rb
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 timezone 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 timezone abbreviations all over the world, this method is not made 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 the fact, this method only understands the timezone abbreviations described in RFC 822 and the system timezone, in the order named. (i.e. a definition in RFC 822 overrides the system timezone definition.) The system timezone is taken from Time.local(year, 1, 1).zone
and Time.local(year, 7, 1).zone
. If the extracted timezone 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 Time class cannot represent specified date.
This method can be used as 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 for Time.parse should be checked, though.
time library should be required to use this method as follows.
require 'time'
266 267 268 269 270 271 272 273 274 275 |
# File 'lib/framework/time.rb', line 266 def parse(date, now=self.now) comp = !block_given? d = Date._parse(date, comp) if !d[:year] && !d[:mon] && !d[:mday] && !d[:hour] && !d[:min] && !d[:sec] && !d[:sec_fraction] raise ArgumentError, "no time information in #{date.inspect}" end year = d[:year] year = yield(year) if year && !comp make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) end |