mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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
This commit is contained in:
parent
08b4cf22f8
commit
1f4cfd6127
2 changed files with 53 additions and 35 deletions
|
@ -1,3 +1,8 @@
|
|||
Mon Aug 29 01:19:57 2005 Tanaka Akira <akr@m17n.org>
|
||||
|
||||
* lib/time.rb (Time.parse): extract fractional seconds using
|
||||
Date._parse. [ruby-talk:153859]
|
||||
|
||||
Sat Aug 27 20:20:01 2005 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
|
||||
|
||||
* ext/curses/curses.c ({curses,window}_clrtoeol): added. suggested
|
||||
|
|
83
lib/time.rb
83
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue