Project

General

Profile

Actions

Bug #19293

closed

The new Time.new(String) API is nice... but we still need a stricter version of this

Added by matsuda (Akira Matsuda) over 1 year ago. Updated 9 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.3.0dev (2023-01-01T07:39:00Z master 542e984d82) +YJIT [arm64-darwin21]
[ruby-core:111565]

Description

The Ruby 3.2 style Time.new(String) API works very well so far, but since the original Time.new(Integer, Integer, Integer...) API actually accepts String objects as its arguments, there's one ambiguous case as follows:
Time.new('20230123') #=> 20230123-01-01 00:00:00 +0900

Then the problem that I'm facing is that we cannot tell if Time.new would parse the given String as ISO8601-ish or just a year, and in order to avoid this ambiguity, we still need to somehow parse the String beforehand in our application side (like we're doing this way in Ruby on Rails https://github.com/rails/rails/blob/c49b8270/activemodel/lib/active_model/type/helpers/time_value.rb#L64-L70), then dispatch to the new Time.new only when the String is validated to be conforming the ISO format. Otherwise, if we just optimistically pass in given Strings to Time.new, we'll occasionally get a Time object with an unintended buggy value.

Therefore, it unfortunately seems that my feature request on #16005 still continues... I have to keep proposing that we need either of the following:

  1. A trustworthy version of ISO8601 parser method perhaps with another name than .new that accepts strict ISO8601-ish String only (but with the T delimiter, I still don't know what the proper name of this format is).

  2. Change Time.new(Integer-ish, Integer-ish, Integer-ish...) not to accept Integer-ish Strings but to accept only Integers. But I can imagine that this direction is very unlikely acceptable, due to the incompatibility.


Files

time_benchmark.rb (1.24 KB) time_benchmark.rb nobu (Nobuyoshi Nakada), 01/02/2023 12:44 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0