Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,10 @@ rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
n += rb_iseq_disasm_insn(str, code, n, iseq, child);
}

if (body->mandatory_only_iseq) {
rb_ary_push(child, (VALUE)body->mandatory_only_iseq);
}

for (l = 0; l < RARRAY_LEN(child); l++) {
VALUE isv = rb_ary_entry(child, l);
if (done_iseq && st_is_member(done_iseq, (st_data_t)isv)) continue;
Expand Down
99 changes: 74 additions & 25 deletions lib/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ def make_time(date, year, yday, mon, day, hour, min, sec, sec_fraction, zone, no
# 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.
#
# A +zone+ argument can be provided to specify the zone for the given
# +date+, if the +date+ does not include a time zone or offset.
#
# ArgumentError is raised if Date._parse cannot extract information from
# +date+ or if the Time class cannot represent specified date.
#
Expand All @@ -382,12 +385,12 @@ def make_time(date, year, yday, mon, day, hour, min, sec, sec_fraction, zone, no
#
# You must require 'time' to use this method.
#
def parse(date, now=self.now)
def parse(date, now=self.now, zone: nil)
comp = !block_given?
d = Date._parse(date, comp)
year = d[:year]
year = yield(year) if year && !comp
make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone], now)
make_time(date, year, d[:yday], d[:mon], d[:mday], d[:hour], d[:min], d[:sec], d[:sec_fraction], d[:zone] || zone, now)
end

#
Expand Down Expand Up @@ -624,39 +627,84 @@ def httpdate(date)
# You must require 'time' to use this method.
#
def xmlschema(time)
if /\A\s*
pattern = /\A\s*
(-?\d+)-(\d\d)-(\d\d)
T
(\d\d):(\d\d):(\d\d)
(\.\d+)?
(Z|[+-]\d\d(?::?\d\d)?)?
\s*\z/ix =~ time
year = $1.to_i
mon = $2.to_i
day = $3.to_i
hour = $4.to_i
min = $5.to_i
sec = $6.to_i
usec = 0
if $7
usec = Rational($7) * 1000000
\s*\z/ix
_xmlschema(pattern, time)
end
alias iso8601 xmlschema

#
# Parses +time+ as a dateTime defined by RFC3339 and converts it to
# a Time object.
#
# ArgumentError is raised if +time+ is not compliant with the format or if
# the Time class cannot represent the specified time.
#
# See #xmlschema for more information on this format.
#
# require 'time'
#
# Time.rfc3339("2011-10-05T22:26:12-04:00")
# #=> 2011-10-05 22:26:12-04:00
#
# You must require 'time' to use this method.
#
def rfc3339(time)
pattern = /\A\s*
(-?\d{4})-(\d\d)-(\d\d)
[T\s]
(\d\d):(\d\d):(\d\d)
(\.\d+)?
(Z|[+-]\d\d(?::?\d\d)?)?
\s*\z/ix
_xmlschema(pattern, time)
end

private

if RUBY_VERSION >= "3.2"
def _xmlschema(pattern, time)
if pattern.match?(time)
new(time)
else
raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
end
if $8
zone = $8
off = zone_offset(zone)
year, mon, day, hour, min, sec =
apply_offset(year, mon, day, hour, min, sec, off)
t = self.utc(year, mon, day, hour, min, sec, usec)
force_zone!(t, zone, off)
t
end
else
def _xmlschema(pattern, time)
if pattern =~ time
year = $1.to_i
mon = $2.to_i
day = $3.to_i
hour = $4.to_i
min = $5.to_i
sec = $6.to_i
usec = 0
if $7
usec = Rational($7) * 1000000
end
if $8
zone = $8
off = zone_offset(zone)
year, mon, day, hour, min, sec =
apply_offset(year, mon, day, hour, min, sec, off)
t = self.utc(year, mon, day, hour, min, sec, usec)
force_zone!(t, zone, off)
t
else
self.local(year, mon, day, hour, min, sec, usec)
end
else
self.local(year, mon, day, hour, min, sec, usec)
raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
end
else
raise ArgumentError.new("invalid xmlschema format: #{time.inspect}")
end
end
alias iso8601 xmlschema

end # class << self

#
Expand Down Expand Up @@ -731,4 +779,5 @@ def xmlschema(fraction_digits=0)
end
end
alias iso8601 xmlschema unless method_defined?(:iso8601)
alias rfc3339 xmlschema
end
6 changes: 6 additions & 0 deletions test/ruby/test_iseq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@ def test_anon_block_param_in_disasm
assert_equal [:&], param_names
end

def test_disasm_mandatory_only_overloaded
disasm = RubyVM::InstructionSequence.disasm(Time.method(:at))
assert_include disasm, "<builtin!time_s_at1/1>"
assert_include disasm, "<builtin!time_s_at/4>"
end

def strip_lineno(source)
source.gsub(/^.*?: /, "")
end
Expand Down
10 changes: 10 additions & 0 deletions test/test_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ def test_parse_offset_hour_minute_second
assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00:00:00"))
end

def test_parse_custom_offset
t = Time.at(-100000000000).utc
assert_equal(t, Time.parse("1200-02-15 BC 14:13:20", zone: "UTC"))
assert_equal(t, Time.parse("1200-02-15 BC 15:13:20", zone: "+01:00").utc)

assert_equal(t, Time.parse("1200-02-15 BC 15:13:20+01:00", zone: "UTC").utc)
assert_equal(t, Time.parse("1200-02-15 BC 15:13:20+01:00", zone: "+02:00").utc)
end

def test_parse_leap_second
t = Time.utc(1998,12,31,23,59,59)
assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998"))
Expand Down Expand Up @@ -574,6 +583,7 @@ def test_huge_precision
test = $1
define_method(test) {__send__(sub, :xmlschema)}
define_method(test.sub(/xmlschema/, 'iso8601')) {__send__(sub, :iso8601)}
define_method(test.sub(/xmlschema/, 'rfc3339')) {__send__(sub, :rfc3339)}
end

def test_parse_with_various_object
Expand Down