[ruby-core:96378] [Ruby master Bug#16440] Date range inclusion behaviors are inconsistent
From:
wishdev@...
Date:
2019-12-20 17:28:26 UTC
List:
ruby-core #96378
Issue #16440 has been updated by wishdev (John Higgins).
Nothing strange with your example - but that doesn't mean it totally works right.
First your example is a DATE range - so adding this line
`(may1..may31).each { |x| puts x }`
That shows that your set is each day within the range at Midnight - therefore any other time is not included (and in fact on my system - the to_time option returns false instead of true).
BUT, on the other hand - one might imagine that something like
`may1 = DateTime.parse("2019-05-01")
may31 = DateTime.parse("2019-05-31")
noon_of_may3 = DateTime.parse("2019-05-03 12:00")
(may1..may31).include? noon_of_may3`
Should get a true for the include
It appears though, that DateTime ranges only use the exact Time each day of the range
`(may1..may31).each { |x| puts x }`
Shows this with the DateTime range.
So I don't believe there is an issue with the code as you have it - but there might be a conversation as to why a DateTime range does not appear to work for your example.
John
----------------------------------------
Bug #16440: Date range inclusion behaviors are inconsistent
https://bugs.ruby-lang.org/issues/16440#change-83299
* Author: st0012 (Stan Lo)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
It's weird that a Date range can include Time and DateTime objects that were converted from a Date object. But it can't include a newly generated DateTime object. For example:
```
may1 = Date.parse("2019-05-01")
may3 = Date.parse("2019-05-03")
noon_of_may3 = DateTime.parse("2019-05-03 12:00")
may31 = Date.parse("2019-05-31")
(may1..may31).include? may3 # => True
(may1..may31).include? may3.to_time # => True
(may1..may31).include? may3.to_datetime # => True
(may1..may31).include? noon_of_may3 # => False
```
Shouldn't the last case return `true` as well?
Related Rails issue: https://github.com/rails/rails/issues/36175
--
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>