-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Inclusion of DateTime in a Date range and vice versa #36194
Closed
vishaltelangre
wants to merge
3
commits into
rails:master
from
vishaltelangre:date-and-date-time-range-include-fix
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
activesupport/lib/active_support/core_ext/range/include_date_time.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveSupport | ||
module IncludeDateTime #:nodoc: | ||
# Extends the default Range#include? to support | ||
# Date and DateTime range comparisons. | ||
# | ||
# (Date.today.beginning_of_month..Date.today.end_of_month).include?(Date.today) # => true | ||
# (Date.today.beginning_of_month..Date.today.end_of_month).include?(DateTime.now) # => true | ||
# (DateTime.now.beginning_of_month..DateTime.now.end_of_month).include?(Date.today) # => true | ||
# (DateTime.now.beginning_of_month..DateTime.now.end_of_month).include?(DateTime.now) # => true | ||
# | ||
def include?(value) | ||
if first.is_a?(Date) || first.is_a?(DateTime) | ||
cover?(value) | ||
elsif last.is_a?(Date) || last.is_a?(DateTime) | ||
cover?(value) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
|
||
Range.prepend(ActiveSupport::IncludeDateTime) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,59 @@ def test_date_range | |
assert_instance_of Range, DateTime.new..DateTime::Infinity.new | ||
end | ||
|
||
def test_date_range_includes_date | ||
range = Date.today.beginning_of_month..Date.today.end_of_month | ||
assert(range.include?(Date.today)) | ||
end | ||
|
||
def test_date_range_includes_date_time | ||
range = Date.today.beginning_of_month..Date.today.end_of_month | ||
assert(range.include?(DateTime.now)) | ||
end | ||
|
||
def test_date_time_range_includes_date | ||
range = DateTime.now.beginning_of_month..DateTime.now.end_of_month | ||
assert(range.include?(Date.today)) | ||
end | ||
|
||
def test_date_time_range_includes_date_time | ||
range = DateTime.now.beginning_of_month..DateTime.now.end_of_month | ||
assert(range.include?(DateTime.now)) | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should also add test cases for (Date.yesterday..Date.tomorrow).include?(Time.now)
(Date.yesterday..Date.tomorrow).include?(Time.zone.now) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added tests for these scenarios, too. |
||
def test_date_range_includes_time | ||
range = Date.yesterday..Date.tomorrow | ||
|
||
assert(range.include?(Time.now)) | ||
end | ||
|
||
def test_date_range_includes_time_with_zone | ||
time_with_zone = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["UTC"], Time.now.utc) | ||
range = Date.yesterday..Date.tomorrow | ||
|
||
assert(range.include?(time_with_zone)) | ||
end | ||
|
||
def test_date_time_range_includes_time | ||
range = DateTime.yesterday..DateTime.tomorrow | ||
|
||
assert(range.include?(Time.now)) | ||
end | ||
|
||
def test_time_with_zone_range_includes_time_with_zone | ||
yesterday = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["UTC"], 1.day.ago) | ||
time_with_zone = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["UTC"], Time.now.utc) | ||
range = (yesterday..1.minute.from_now) | ||
|
||
assert(range.include?(time_with_zone)) | ||
end | ||
|
||
def test_time_with_zone_range_includes_date | ||
range = (1.week.ago..2.days.from_now) | ||
|
||
assert(range.include?(Date.today)) | ||
end | ||
|
||
def test_overlaps_last_inclusive | ||
assert((1..5).overlaps?(5..10)) | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafaelfranca That's what this PR does. We rely on
#cover?
here. This fix is inspired by this extension https://github.com/rails/rails/blob/870f087825/activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb which is already present in the upstream.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm more inclined to remove that extension.
include?
should not work for values thatRange.to_a
would not return.Can we deprecate that extension and tell people to use
cover?
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafaelfranca Sure. I will make another PR to deprecate the
activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb
extension.