diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-08-28 16:23:51 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-08-28 16:23:51 +0000 |
commit | 1f4cfd612784b1101b0f30855ab38e9ba8cacb29 (patch) | |
tree | 22690776d13c4bf576d1f7688f22060c16d44373 /lib/time.rb | |
parent | 08b4cf22f89019fbaf79f4eb5056944c13387532 (diff) |
* lib/time.rb (Time.parse): extract fractional seconds using
Date._parse. [ruby-talk:153859]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@9038 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/time.rb')
-rw-r--r-- | lib/time.rb | 83 |
1 files changed, 48 insertions, 35 deletions
diff --git a/lib/time.rb b/lib/time.rb index da882f911b..3b4ee9e72a 100644 --- a/lib/time.rb +++ b/lib/time.rb @@ -7,7 +7,7 @@ # * date-time defined by RFC 2822 # * HTTP-date defined by RFC 2616 # * dateTime defined by XML Schema Part 2: Datatypes (ISO 8601) -# * various formats handled by ParseDate (string to time only) +# * various formats handled by Date._parse (string to time only) # # == Design Issues # @@ -148,8 +148,46 @@ class Time end private :apply_offset + def make_time(year, mon, day, hour, min, sec, sec_fraction, zone, now) + usec = nil + usec = (sec_fraction * 1000000).to_i if sec_fraction + if now + begin + break if year; year = now.year + break if mon; mon = now.mon + break if day; day = now.day + break if hour; hour = now.hour + break if min; min = now.min + break if sec; sec = now.sec + break if sec_fraction; usec = now.tv_usec + end until true + end + + year ||= 1970 + mon ||= 1 + day ||= 1 + hour ||= 0 + min ||= 0 + sec ||= 0 + usec ||= 0 + + off = nil + off = zone_offset(zone, year) if zone + + if off + year, mon, day, hour, min, sec = + apply_offset(year, mon, day, hour, min, sec, off) + t = Time.utc(year, mon, day, hour, min, sec, usec) + t.localtime if !zone_utc?(zone) + t + else + Time.local(year, mon, day, hour, min, sec, usec) + end + end + private :make_time + # - # Parses +date+ using ParseDate.parsedate and converts it to a Time object. + # 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: @@ -187,7 +225,7 @@ class Time # 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 ParseDate cannot extract information from + # 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: @@ -199,39 +237,10 @@ class Time # A failure for Time.parse should be checked, though. # def parse(date, now=Time.now) - year, mon, day, hour, min, sec, zone, _ = ParseDate.parsedate(date) + d = Date._parse(date, false) + year = d[:year] year = yield(year) if year && block_given? - - if now - begin - break if year; year = now.year - break if mon; mon = now.mon - break if day; day = now.day - break if hour; hour = now.hour - break if min; min = now.min - break if sec; sec = now.sec - end until true - end - - year ||= 1970 - mon ||= 1 - day ||= 1 - hour ||= 0 - min ||= 0 - sec ||= 0 - - off = nil - off = zone_offset(zone, year) if zone - - if off - year, mon, day, hour, min, sec = - apply_offset(year, mon, day, hour, min, sec, off) - t = Time.utc(year, mon, day, hour, min, sec) - t.localtime if !zone_utc?(zone) - t - else - Time.local(year, mon, day, hour, min, sec) - end + make_time(year, d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now) end MonthValue = { @@ -780,5 +789,9 @@ if __FILE__ == $0 assert_equal(31, t.day) assert_equal(8, t.mon) end + + def test_parse_fraction + assert_equal(500000, Time.parse("2000-01-01T00:00:00.5+00:00").tv_usec) + end end end |