From: "Eregon (Benoit Daloze)" Date: 2021-12-06T17:37:27+00:00 Subject: [ruby-core:106521] [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-22: > I wrote `Time#inspect`, but the "ISO 8601-like" format is not only used by it, e.g., `--date=iso` of git. Interesting that `git` has this too: ``` --date=iso (or --date=iso8601) shows timestamps in a ISO 8601-like format. The differences to the strict ISO 8601 format are: ��� a space instead of the T date/time delimiter ��� a space between time and time zone ��� no colon between hours and minutes of the time zone --date=iso-strict (or --date=iso8601-strict) shows timestamps in strict ISO 8601 format. ``` Maybe we really need a name for this. Like `Time#iso` and `Time.iso`. Or it could be a keyword argument: `time.iso8601(format: :git)` and `Time.iso8601(str, format: :git)`. `:git` is just an example, could be `textual`, `relaxed` or anything. Another idea would be to actually use the strict variant of iso8601 if that is well defined, like what `git log --date=iso-strict` shows. Then we'd have `Time#iso8601_strict`/`Time.iso8601_strict`, or `time.iso8601(format: :strict)` and `Time.iso8601(str, format: :strict)`. That feels proper to me because then we have a clear way to serialize and deserialize times and finding the dumping/parsing method is trivial (they have the same name, no need to guess). I feel relying on `#inspect` for efficient serializing is in general bad, because that is not the main purpose of `#inspect` (rather it's to show a clear/debug-like representation of an object). > How about `Time.at`? To me `Time.at` sounds like "give me a Time at epoch", so I think it's unexpected it does String parsing. > `Time.try_convert` feels considerable, but passing the timezone option may not fit. I think an extra optional timezone argument would be fine. ---------------------------------------- Feature #18033: Time.new to parse a string https://bugs.ruby-lang.org/issues/18033#change-95181 * 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: