From: "Eregon (Benoit Daloze) via ruby-core" Date: 2024-10-11T12:49:44+00:00 Subject: [ruby-core:119511] [Ruby master Feature#18033] Time.new to parse a string Issue #18033 has been updated by Eregon (Benoit Daloze). matz (Yukihiro Matsumoto) wrote in #note-27: > Currently, `Time.new` only takes numeric arguments, so adding extra argument pattern add only a small complexity. But older versions like 3.0 accept year as a String: https://bugs.ruby-lang.org/issues/18033#note-10 So we get very weird behavior between versions: ``` $ ruby -ve 'p Time.new "2234-10-12T01:02:03"' ruby 3.0.6p216 (2023-03-30 revision 23a532679b) [x86_64-linux] 2234-01-01 00:00:00 +0100 $ ruby -ve 'p Time.new "2234-10-12T01:02:03"' ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-linux] :310:in `initialize': invalid value for Integer(): "2234-10-12T01:02:03" (ArgumentError) from -e:1:in `new' from -e:1:in `
' $ ruby -ve 'p Time.new "2234-10-12T01:02:03"' ruby 3.2.4 (2024-04-23 revision af471c0e01) [x86_64-linux] 2234-10-12 01:02:03 +0200 $ ruby -ve 'p Time.new "2234-10-12T01:02:03"' ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [x86_64-linux] 2234-10-12 01:02:03 +0200 ``` ---------------------------------------- Feature #18033: Time.new to parse a string https://bugs.ruby-lang.org/issues/18033#change-110126 * Author: nobu (Nobuyoshi Nakada) * Status: Closed ---------------------------------------- Make `Time.new` parse `Time#inspect` and ISO-8601 like strings. * `Time.iso8601` and `Time.parse` need an extension library, `date`. * `Time.iso8601` can't parse `Time#inspect` string. * `Time.parse` often results in unintentional/surprising results. * `Time.new` also about 1.9 times faster than `Time.iso8601`. ``` $ ./ruby -rtime -rbenchmark -e ' n = 1000 s = Time.now.iso8601 Benchmark.bm(12) do |x| x.report("Time.iso8601") {n.times{Time.iso8601(s)}} x.report("Time.parse") {n.times{Time.parse(s)}} x.report("Time.new") {n.times{Time.new(s)}} end' user system total real Time.iso8601 0.006919 0.000185 0.007104 ( 0.007091) Time.parse 0.018338 0.000207 0.018545 ( 0.018590) Time.new 0.003671 0.000069 0.003740 ( 0.003741) ``` https://github.com/ruby/ruby/pull/4825 -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/