未验证 提交 9a9ef963 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #32185 from nholden/human_readable_date_time_comparisons

Add `before?` and `after?` methods to date and time classes
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
*Ashe Connor*, *Aaron Patterson* *Ashe Connor*, *Aaron Patterson*
* Add `before?` and `after?` methods to `Date`, `DateTime`,
`Time`, and `TimeWithZone`.
*Nick Holden*
* Add `:private` option to ActiveSupport's `Module#delegate` * Add `:private` option to ActiveSupport's `Module#delegate`
in order to delegate methods as private: in order to delegate methods as private:
......
...@@ -142,4 +142,6 @@ def compare_with_coercion(other) ...@@ -142,4 +142,6 @@ def compare_with_coercion(other)
end end
alias_method :compare_without_coercion, :<=> alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion alias_method :<=>, :compare_with_coercion
alias_method :before?, :<
alias_method :after?, :>
end end
...@@ -208,4 +208,6 @@ def <=>(other) ...@@ -208,4 +208,6 @@ def <=>(other)
super super
end end
end end
alias_method :before?, :<
alias_method :after?, :>
end end
...@@ -302,6 +302,8 @@ def compare_with_coercion(other) ...@@ -302,6 +302,8 @@ def compare_with_coercion(other)
end end
alias_method :compare_without_coercion, :<=> alias_method :compare_without_coercion, :<=>
alias_method :<=>, :compare_with_coercion alias_method :<=>, :compare_with_coercion
alias_method :before?, :<
alias_method :after?, :>
# Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances # Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances
# can be eql? to an equivalent Time # can be eql? to an equivalent Time
......
...@@ -225,6 +225,8 @@ def strftime(format) ...@@ -225,6 +225,8 @@ def strftime(format)
def <=>(other) def <=>(other)
utc <=> other utc <=> other
end end
alias_method :before?, :<
alias_method :after?, :>
# Returns true if the current object's time is within the specified # Returns true if the current object's time is within the specified
# +min+ and +max+ time. # +min+ and +max+ time.
......
...@@ -360,6 +360,18 @@ def test_future ...@@ -360,6 +360,18 @@ def test_future
end end
end end
def test_before
assert_equal false, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 5))
assert_equal false, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 6))
assert_equal true, Date.new(2017, 3, 6).before?(Date.new(2017, 3, 7))
end
def test_after
assert_equal true, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 5))
assert_equal false, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 6))
assert_equal false, Date.new(2017, 3, 6).after?(Date.new(2017, 3, 7))
end
def test_current_returns_date_today_when_zone_not_set def test_current_returns_date_today_when_zone_not_set
with_env_tz "US/Central" do with_env_tz "US/Central" do
Time.stub(:now, Time.local(1999, 12, 31, 23)) do Time.stub(:now, Time.local(1999, 12, 31, 23)) do
......
...@@ -285,6 +285,18 @@ def test_future_without_offset ...@@ -285,6 +285,18 @@ def test_future_without_offset
end end
end end
def test_before
assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 11, 59, 59))
assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 12, 0, 0))
assert_equal true, DateTime.civil(2017, 3, 6, 12, 0, 0).before?(DateTime.civil(2017, 3, 6, 12, 00, 1))
end
def test_after
assert_equal true, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 11, 59, 59))
assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 12, 0, 0))
assert_equal false, DateTime.civil(2017, 3, 6, 12, 0, 0).after?(DateTime.civil(2017, 3, 6, 12, 00, 1))
end
def test_current_returns_date_today_when_zone_is_not_set def test_current_returns_date_today_when_zone_is_not_set
with_env_tz "US/Eastern" do with_env_tz "US/Eastern" do
Time.stub(:now, Time.local(1999, 12, 31, 23, 59, 59)) do Time.stub(:now, Time.local(1999, 12, 31, 23, 59, 59)) do
......
...@@ -736,6 +736,18 @@ def test_future_with_time_current_as_time_with_zone ...@@ -736,6 +736,18 @@ def test_future_with_time_current_as_time_with_zone
end end
end end
def test_before
assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 11, 59, 59))
assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 12, 0, 0))
assert_equal true, Time.utc(2017, 3, 6, 12, 0, 0).before?(Time.utc(2017, 3, 6, 12, 00, 1))
end
def test_after
assert_equal true, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 11, 59, 59))
assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 12, 0, 0))
assert_equal false, Time.utc(2017, 3, 6, 12, 0, 0).after?(Time.utc(2017, 3, 6, 12, 00, 1))
end
def test_acts_like_time def test_acts_like_time
assert_predicate Time.new, :acts_like_time? assert_predicate Time.new, :acts_like_time?
end end
......
...@@ -289,6 +289,20 @@ def test_future_with_time_current_as_time_with_zone ...@@ -289,6 +289,20 @@ def test_future_with_time_current_as_time_with_zone
end end
end end
def test_before
twz = ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone)
assert_equal false, twz.before?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 11, 59, 59), @time_zone))
assert_equal false, twz.before?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone))
assert_equal true, twz.before?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 00, 1), @time_zone))
end
def test_after
twz = ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone)
assert_equal true, twz.after?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 11, 59, 59), @time_zone))
assert_equal false, twz.after?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 0, 0), @time_zone))
assert_equal false, twz.after?(ActiveSupport::TimeWithZone.new(Time.utc(2017, 3, 6, 12, 00, 1), @time_zone))
end
def test_eql? def test_eql?
assert_equal true, @twz.eql?(@twz.dup) assert_equal true, @twz.eql?(@twz.dup)
assert_equal true, @twz.eql?(Time.utc(2000)) assert_equal true, @twz.eql?(Time.utc(2000))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册