CARVIEW |
Select Language
HTTP/2 200
x-amz-id-2: R2xkLZkwm8qsWlQDrDdR0a3V1CSEDHB/cjsRWHoQwp0r4e4+gYhRLtd6LhjZcxWW2OZbX7FuAjrIvVoEHA/1pF77KsqYFbS1yAGo0uDIiFk=
x-amz-request-id: E72SRN252SNE5CD4
last-modified: Sat, 03 May 2025 18:21:05 GMT
etag: "c8583b767170e0db4366a0fc37bdfcff"
x-amz-server-side-encryption: AES256
server: AmazonS3
content-encoding: gzip
via: 1.1 varnish, 1.1 varnish
content-type: text/plain; charset=utf-8
accept-ranges: bytes
age: 0
date: Sat, 26 Jul 2025 19:37:58 GMT
x-served-by: cache-tyo11947-TYO, cache-bom-vanm7210076-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753558678.991211,VS0,VE294
vary: Accept-Encoding
content-length: 942
From: "mame (Yusuke Endoh) via ruby-core"
Date: 2025-05-03T18:17:38+00:00
Subject: [ruby-core:121815] [Ruby Feature#21307] A way to strictly validate time input
Issue #21307 has been reported by mame (Yusuke Endoh).
----------------------------------------
Feature #21307: A way to strictly validate time input
https://bugs.ruby-lang.org/issues/21307
* Author: mame (Yusuke Endoh)
* Status: Open
----------------------------------------
Currently, `Time.new` sometimes silently rolls over invalid date/time values:
```ruby
Time.new(2025, 2, 29, 0, 0, 0, "+00:00") #=> 2025-03-01 00:00:00
Time.new(2025, 1, 1, 24, 0, 0, "+00:00") #=> 2025-01-02 00:00:00
Time.new(2025, 1, 1, 0, 0, 60, "+00:00") #=> 2025-01-01 00:01:00
```
But in other cases, it raises `ArgumentError`:
```ruby
Time.new(2025, 1, 32, 0, 0, 0, "+00:00") #=> argument out of range (ArgumentError)
Time.new(2025, 1, 1, 24, 0, 1, "+00:00") #=> sec out of range (ArgumentError)
Time.new(2025, 1, 1, 25, 0, 0, "+00:00") #=> hour out of range (ArgumentError)
Time.new(2025, 1, 1, 0, 60, 0, "+00:00") #=> min out of range (ArgumentError)
Time.new(2025, 1, 1, 0, 0, 61, "+00:00") #=> sec out of range (ArgumentError)
```
Is this inconsistency intentional? While automatic rollover can be useful, the current behavior feels unpredictable.
---
If this is by design, I would like to propose a strict version, e.g. `Time.strict_new`, that raises an error for any invalid value:
```ruby
Time.strict_new(2025, 2, 29, 0, 0, 0, "+00:00") #=> ArgumentError
```
Right now, the only workaround is to compare the input with the resulting object, which is clunky:
```ruby
def time_strict_new(year, mon, day, hour, min, sec, zone)
r = Time.strict_new(year, mon, day, hour, min, sec, zone)
raise ArgumentError if r.year != year
raise ArgumentError if r.mon != mon
raise ArgumentError if r.day != day
raise ArgumentError if r.hour != hour
raise ArgumentError if r.min != min
raise ArgumentError if r.sec != sec
r
end
```
In my case, I'm writing a TOML parser and need to reject invalid datetimes like `2100-02-29T15:15:15Z`. The rollover behavior gets in the way.
--
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/