From 9b345cee493a90e58597bbf79ae2af4e340cc64f Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Tue, 29 Jul 2008 16:46:25 +0200 Subject: [PATCH] Added past? and future? methods to Date/DateTime/Time and today? method for Date. --- .../active_support/core_ext/date/calculations.rb | 31 ++++++++++++++----- .../core_ext/date_time/calculations.rb | 20 +++++++++--- .../active_support/core_ext/time/calculations.rb | 26 +++++++++++----- activesupport/test/core_ext/date_ext_test.rb | 8 +++++ activesupport/test/core_ext/date_time_ext_test.rb | 7 ++++ activesupport/test/core_ext/time_ext_test.rb | 7 ++++ 6 files changed, 78 insertions(+), 21 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index b5180c9..9df4714 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -20,18 +20,33 @@ module ActiveSupport #:nodoc: def yesterday ::Date.today.yesterday end - + # Returns a new Date representing the date 1 day after today (i.e. tomorrow's date). def tomorrow ::Date.today.tomorrow end - + # Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today. def current ::Time.zone_default ? ::Time.zone.today : ::Date.today end end - + + # Tells whether the Date object's date lies in the past + def past? + self.to_date < ::Date.today + end + + # Tells whether the Date object's date is today + def today? + self.to_date == ::Date.today + end + + # Tells whether the Date object's date lies in the future + def future? + self.to_date > ::Date.today + end + # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) # and then subtracts the specified number of seconds def ago(seconds) @@ -57,7 +72,7 @@ module ActiveSupport #:nodoc: def end_of_day to_time.end_of_day end - + def plus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other other.since(self) @@ -65,7 +80,7 @@ module ActiveSupport #:nodoc: plus_without_duration(other) end end - + def minus_with_duration(other) #:nodoc: if ActiveSupport::Duration === other plus_with_duration(-other) @@ -73,7 +88,7 @@ module ActiveSupport #:nodoc: minus_without_duration(other) end end - + # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with # any of these keys: :years, :months, :weeks, :days. def advance(options) @@ -98,7 +113,7 @@ module ActiveSupport #:nodoc: options[:day] || self.day ) end - + # Returns a new Date/DateTime representing the time a number of specified months ago def months_ago(months) advance(:months => -months) @@ -161,7 +176,7 @@ module ActiveSupport #:nodoc: days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6} result = (self + 7).beginning_of_week + days_into_week[day] self.acts_like?(:time) ? result.change(:hour => 0) : result - end + end # Returns a new ; DateTime objects will have time set to 0:00DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00) def beginning_of_month diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index 155c961..93bbc4f 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -7,7 +7,7 @@ module ActiveSupport #:nodoc: module Calculations def self.included(base) #:nodoc: base.extend ClassMethods - + base.class_eval do alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion @@ -21,6 +21,16 @@ module ActiveSupport #:nodoc: end end + # Tells whether the DateTime object's datetime lies in the past + def past? + self.to_datetime < ::Time.now.to_datetime + end + + # Tells whether the DateTime object's datetime lies in the future + def future? + self.to_datetime > ::Time.now.to_datetime + end + # Seconds since midnight: DateTime.now.seconds_since_midnight def seconds_since_midnight self.sec + (self.min * 60) + (self.hour * 3600) @@ -78,7 +88,7 @@ module ActiveSupport #:nodoc: def end_of_day change(:hour => 23, :min => 59, :sec => 59) end - + # Adjusts DateTime to UTC by adding its offset value; offset is set to 0 # # Example: @@ -89,17 +99,17 @@ module ActiveSupport #:nodoc: new_offset(0) end alias_method :getutc, :utc - + # Returns true if offset == 0 def utc? offset == 0 end - + # Returns the offset value in seconds def utc_offset (offset * 86400).to_i end - + # Layers additional behavior on DateTime#<=> so that Time and ActiveSupport::TimeWithZone instances can be compared with a DateTime def compare_with_coercion(other) other = other.comparable_time if other.respond_to?(:comparable_time) diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index cd234c9..9b7055f 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -9,13 +9,13 @@ module ActiveSupport #:nodoc: base.class_eval do alias_method :plus_without_duration, :+ alias_method :+, :plus_with_duration - + alias_method :minus_without_duration, :- alias_method :-, :minus_with_duration - + alias_method :minus_without_coercion, :- alias_method :-, :minus_with_coercion - + alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion end @@ -28,7 +28,7 @@ module ActiveSupport #:nodoc: def ===(other) other.is_a?(::Time) end - + # Return the number of days in the given month. # If no year is specified, it will use the current year. def days_in_month(month, year = now.year) @@ -57,6 +57,16 @@ module ActiveSupport #:nodoc: end end + # Tells whether the Time object's time lies in the past + def past? + self.to_time < ::Time.now + end + + # Tells whether the Time object's time lies in the future + def future? + self.to_time > ::Time.now + end + # Seconds since midnight: Time.now.seconds_since_midnight def seconds_since_midnight self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6) @@ -106,7 +116,7 @@ module ActiveSupport #:nodoc: (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f end rescue - self.to_datetime.since(seconds) + self.to_datetime.since(seconds) end alias :in :since @@ -199,7 +209,7 @@ module ActiveSupport #:nodoc: change(:day => last_day, :hour => 23, :min => 59, :sec => 59, :usec => 0) end alias :at_end_of_month :end_of_month - + # Returns a new Time representing the start of the quarter (1st of january, april, july, october, 0:00) def beginning_of_quarter beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month }) @@ -249,7 +259,7 @@ module ActiveSupport #:nodoc: minus_without_duration(other) end end - + # Time#- can also be used to determine the number of seconds between two Time instances. # We're layering on additional behavior so that ActiveSupport::TimeWithZone instances # are coerced into values that Time#- will recognize @@ -257,7 +267,7 @@ module ActiveSupport #:nodoc: other = other.comparable_time if other.respond_to?(:comparable_time) minus_without_coercion(other) end - + # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances # can be chronologically compared with a Time def compare_with_coercion(other) diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index ddfe1f9..3e20a90 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -214,6 +214,14 @@ class DateExtCalculationsTest < Test::Unit::TestCase end end + def test_today_past_future + t1, t2, t3 = Date.yesterday, Date.today, Date.tomorrow + + assert t1.past? + assert t2.today? + assert t3.future? + end + uses_mocha 'TestDateCurrent' do def test_current_returns_date_today_when_zone_default_not_set with_env_tz 'US/Central' do diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 854a3a0..a61865d 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -207,6 +207,13 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema) end + def test_past_future + t1, t2 = 2.minutes.ago.to_datetime, 2.minutes.from_now.to_datetime + + assert t1.past? + assert t2.future? + end + def test_acts_like_time assert DateTime.new.acts_like_time? end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 8740497..dbe6114 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -561,6 +561,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_nothing_raised { Time.now.xmlschema } end + def test_past_future + t1, t2 = 2.minutes.ago, 2.minutes.from_now + + assert t1.past? + assert t2.future? + end + def test_acts_like_time assert Time.new.acts_like_time? end -- 1.5.2.4 From fa314d289fd0785713ac65f1da12ecba6d364d4c Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Tue, 29 Jul 2008 16:54:18 +0200 Subject: [PATCH] Added today? method to Time to be more consistent. Improved test coverage. --- .../active_support/core_ext/time/calculations.rb | 5 +++++ activesupport/test/core_ext/date_ext_test.rb | 4 +++- activesupport/test/core_ext/date_time_ext_test.rb | 2 ++ 3 files changed, 10 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 9b7055f..599000f 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -62,6 +62,11 @@ module ActiveSupport #:nodoc: self.to_time < ::Time.now end + # Tells whether the Time object's time is today + def today? + self.to_date == ::Date.today + end + # Tells whether the Time object's time lies in the future def future? self.to_time > ::Time.now diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 3e20a90..5603d07 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -215,11 +215,13 @@ class DateExtCalculationsTest < Test::Unit::TestCase end def test_today_past_future - t1, t2, t3 = Date.yesterday, Date.today, Date.tomorrow + t1, t2, t3, t4, t5 = Date.yesterday, Date.today, Date.tomorrow, 2.minutes.ago, 2.minutes.from_now assert t1.past? assert t2.today? assert t3.future? + assert t4.today? + assert t5.today? end uses_mocha 'TestDateCurrent' do diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index a61865d..13f5746 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -212,6 +212,8 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert t1.past? assert t2.future? + assert t1.today? + assert t2.today? end def test_acts_like_time -- 1.5.2.4 From 62379001821bbfcf582cb9488c7083c0cfe9ae44 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Tue, 29 Jul 2008 17:08:05 +0200 Subject: [PATCH] Added missing test for Time#today? and renamed tests accordingly. --- activesupport/test/core_ext/date_time_ext_test.rb | 2 +- activesupport/test/core_ext/time_ext_test.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 13f5746..cbca8dd 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -207,7 +207,7 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_match(/^2080-02-28T15:15:10-06:?00$/, DateTime.civil(2080, 2, 28, 15, 15, 10, -0.25).xmlschema) end - def test_past_future + def test_past_today_future t1, t2 = 2.minutes.ago.to_datetime, 2.minutes.from_now.to_datetime assert t1.past? diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index dbe6114..069392c 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -561,11 +561,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_nothing_raised { Time.now.xmlschema } end - def test_past_future + def test_past_today_future t1, t2 = 2.minutes.ago, 2.minutes.from_now assert t1.past? assert t2.future? + assert t1.today? + assert t2.today? end def test_acts_like_time -- 1.5.2.4