From: "Eregon (Benoit Daloze)" <noreply@...>
Date: 2021-12-06T13:18:28+00:00
Subject: [ruby-core:106512] [Ruby master Feature#18033] Time.new to parse a string

Issue #18033 has been updated by Eregon (Benoit Daloze).


nobu (Nobuyoshi Nakada) wrote in #note-19:
> First, the primary target is the result of `Time#inspect`, and it is not fully compliant with ISO-8601.

Thanks for making that clear, I wasn't sure why not just improve `Time.iso8601`.

Right, they differ:
```
irb(main):006:0> t.inspect
=> "2021-12-06 14:10:15.334531885 +0100"
irb(main):007:0> t.iso8601
=> "2021-12-06T14:10:15+01:00"
```

Why we do we need to parse `Time#inspect` output?
It seems in general bad to rely on `inspect` for serialization, since it only works for some classes.
Maybe to preserve subseconds?

`#inspect` does not feel like a proper way to serialize a Time instance, so if we want to add that I think we should have a new method for it too (maybe `#dump`?).

In any case, `Time.new` accepts individual time components, and I think making it parse strings is just confusing.
A new method `Time.try_convert` (or `Time.undump`) feels more appropriate for such parsing.

> Second, ISO-8601 allows many variants, but I'm not going to implement them all.

Is that what makes it faster?
The PR still uses a Regexp so I guess the main difference is the Regexp is a little bit simpler?
They look fairly similar:
* https://github.com/ruby/ruby/pull/4639/files
* https://github.com/ruby/ruby/blob/715a51a0d6963f9d727191d4e1ad0690fd28c4dd/lib/time.rb#L617-L650

----------------------------------------
Feature #18033: Time.new to parse a string
https://bugs.ruby-lang.org/issues/18033#change-95171

* Author: nobu (Nobuyoshi Nakada)
* Status: Open
* Priority: Normal
----------------------------------------
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/4639



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>