Bug #5988

Time class interprets "2012-02-30" as "2012-03-01"

Added by Dylan Markow about 2 years ago. Updated about 1 year ago.

[ruby-core:42448]
Status:Rejected
Priority:Normal
Assignee:Akira Tanaka
Category:-
Target version:-
ruby -v:ruby 2.0.0dev (2012-02-09 trunk 34499) [x86_64-darwin11.3.0] Backport:

Description

=begin
The time class seems to accept any number from 1-31 as a valid day for any month of the year, even if the month has less than 31 days. If the day exceeds the number of days, it just spills over into the following month; "2012-02-30" become "2012-03-01", "2012-04-31" becomes "2012-05-01", and so on. It's not until you try 32 as a day number that you get the expected Argument Error. This happens with Time.new, Time.parse, Time.local, Time.utc, etc.

irb(main):002:0> RUBYDESCRIPTION
=> "ruby 2.0.0dev (2012-02-09 trunk 34499) [x86
64-darwin11.3.0]"
irb(main):003:0> Time.parse "2012-02-30"
=> 2012-03-01 00:00:00 -0800
irb(main):004:0> Time.new 2012, 2, 30
=> 2012-03-01 00:00:00 -0800
irb(main):005:0> Time.local 2012, 2, 30
=> 2012-03-01 00:00:00 -0800
irb(main):006:0> Time.utc 2012, 2, 30
=> 2012-03-01 00:00:00 UTC
irb(main):007:0> Time.new 2012, 4, 31
=> 2012-05-01 00:00:00 -0700
irb(main):008:0> Time.new 2012, 2, 32
ArgumentError: argument out of range
from (irb):8:in initialize'
from (irb):8:in
new'
from (irb):8
from /Users/dylan/.rbenv/versions/2.0.0-dev/bin/irb:12:in `'
irb(main):009:0>

=end

History

#1 Updated by Dylan Markow about 2 years ago

=begin
By comparison, the Date class handles this properly:

irb(main):004:0> Date.new 2012, 2, 29
=> #
irb(main):005:0> Date.new 2012, 2, 30
ArgumentError: invalid date
from (irb):5:in new'
from (irb):5
from /Users/dylan/.rbenv/versions/2.0.0-dev/bin/irb:12:in
'
irb(main):006:0>
=end

#2 Updated by Yui NARUSE about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Akira Tanaka

#3 Updated by Akira Tanaka about 2 years ago

2012/2/10 Yui NARUSE naruse@airemix.jp:

The time class seems to accept any number from 1-31 as a valid day for any month of the year, even if the month has less than 31 days. If the day exceeds the number of days, it just spills over into the following month; "2012-02-30" become "2012-03-01", "2012-04-31" becomes "2012-05-01", and so on. It's not until you try 32 as a day number that you get the expected Argument Error. This happens with Time.new, Time.parse, Time.local, Time.utc, etc.

Once, we tried to check time validity by testing round tripness.

But it was too strict. So we abondon the test.

If people really need strict Time constructors,
new methods (Time.strictlocal, Time.strictutc, etc.) should be considerd.
--
Tanaka Akira

#4 Updated by Akira Tanaka about 1 year ago

  • Status changed from Assigned to Rejected

No reply after explanation long time.

Also available in: Atom PDF