From 2829076660b04a96706779aeff0415ea6cd93448 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 0f3cf4c..63a9bfd 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -210,6 +210,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 c1bdee4..62e200d 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -563,6 +563,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 e3c5cb9f6ce69945a266384eb9a5b0930ed0a6cd 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 63a9bfd..bf67570 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -211,11 +211,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 cb8a33f84a8311d66b88286f9b336e6c8520bd0f 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 62e200d..fc1beca 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -563,11 +563,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 From f427faaecc9e3bf2b7a0e287af43aacdd24b33a5 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Tue, 29 Jul 2008 17:59:12 +0200 Subject: [PATCH] Use DateTime#now instead of Time#now.to_datetime. --- .../core_ext/date_time/calculations.rb | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) 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 93bbc4f..b4b8594 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -23,12 +23,12 @@ module ActiveSupport #:nodoc: # Tells whether the DateTime object's datetime lies in the past def past? - self.to_datetime < ::Time.now.to_datetime + self.to_datetime < ::DateTime.now end # Tells whether the DateTime object's datetime lies in the future def future? - self.to_datetime > ::Time.now.to_datetime + self.to_datetime > ::DateTime.now end # Seconds since midnight: DateTime.now.seconds_since_midnight -- 1.5.2.4 From 9aac8e522e7b6c2a763580583cfb11e218373d4a Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 11:16:50 +0200 Subject: [PATCH] Added support for time zones. --- .../active_support/core_ext/date/calculations.rb | 6 +++--- .../core_ext/date_time/calculations.rb | 8 ++++++-- .../active_support/core_ext/time/calculations.rb | 6 +++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 9df4714..2bea167 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -34,17 +34,17 @@ module ActiveSupport #:nodoc: # Tells whether the Date object's date lies in the past def past? - self.to_date < ::Date.today + self.to_date < ::Date.current end # Tells whether the Date object's date is today def today? - self.to_date == ::Date.today + self.to_date == ::Date.current end # Tells whether the Date object's date lies in the future def future? - self.to_date > ::Date.today + self.to_date > ::Date.current end # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) 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 b4b8594..86e7dee 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -19,16 +19,20 @@ module ActiveSupport #:nodoc: def local_offset ::Time.local(2007).utc_offset.to_r / 86400 end + + def current + ::Time.zone_default ? ::Time.zone.now.to_datetime : ::Time.now.to_datetime + end end # Tells whether the DateTime object's datetime lies in the past def past? - self.to_datetime < ::DateTime.now + self.to_datetime < ::DateTime.current end # Tells whether the DateTime object's datetime lies in the future def future? - self.to_datetime > ::DateTime.now + self.to_datetime > ::DateTime.current end # Seconds since midnight: DateTime.now.seconds_since_midnight diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 599000f..3d8d78c 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -59,17 +59,17 @@ module ActiveSupport #:nodoc: # Tells whether the Time object's time lies in the past def past? - self.to_time < ::Time.now + self.to_time < ::Time.current end # Tells whether the Time object's time is today def today? - self.to_date == ::Date.today + self.to_date == ::Date.current end # Tells whether the Time object's time lies in the future def future? - self.to_time > ::Time.now + self.to_time > ::Time.current end # Seconds since midnight: Time.now.seconds_since_midnight -- 1.5.2.4 From 89ed3ac9adfca0d6b61f8c996cc4fdea54d0edd7 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 11:50:12 +0200 Subject: [PATCH] Stubbed out tests. --- activesupport/test/core_ext/date_ext_test.rb | 21 +++++++++++++-------- activesupport/test/core_ext/date_time_ext_test.rb | 17 ++++++++++------- activesupport/test/core_ext/time_ext_test.rb | 15 +++++++++------ 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index bf67570..52f32c9 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -210,14 +210,19 @@ class DateExtCalculationsTest < Test::Unit::TestCase end end - def test_today_past_future - 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? + uses_mocha 'past?, today? and future?' do + def test_today_past_future + Date.stubs(:current).returns(Date.civil(2008, 9, 10)) + t2 = Date.current + t1, t3 = t2.yesterday, t2.tomorrow + t4, t5 = t2 - 1.second, t2 + 1.second + + assert t1.past? + assert t2.today? + assert t3.future? + assert t4.past? + assert t5.today? + end 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 cbca8dd..b186d5e 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -207,13 +207,16 @@ 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_today_future - t1, t2 = 2.minutes.ago.to_datetime, 2.minutes.from_now.to_datetime - - assert t1.past? - assert t2.future? - assert t1.today? - assert t2.today? + uses_mocha 'past?, today? and future?' do + def test_past_today_future + DateTime.stubs(:current).returns(DateTime.civil(2008, 9, 10, 11, 0, 1)) + t1, t2 = DateTime.civil(2008, 9, 10, 11, 0, 0), DateTime.civil(2008, 9, 10, 11, 0, 2) + + assert t1.past? + assert t2.future? + assert t1.today? + assert t2.today? + end end def test_acts_like_time diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index fc1beca..c676df7 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -563,13 +563,16 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_nothing_raised { Time.now.xmlschema } end - def test_past_today_future - t1, t2 = 2.minutes.ago, 2.minutes.from_now + uses_mocha 'past?, today? and future?' do + def test_past_today_future + Time.stubs(:current).returns(Time.local(2008, 9, 10, 11, 0, 1)) + t1, t2 = Time.local(2008, 9, 10, 11, 0, 0), Time.local(2008, 9, 10, 11, 0, 2) - assert t1.past? - assert t2.future? - assert t1.today? - assert t2.today? + assert t1.past? + assert t2.future? + assert t1.today? + assert t2.today? + end end def test_acts_like_time -- 1.5.2.4 From 05e018c25ceedab8a7ab5d43b2ad4482e6849215 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 11:52:32 +0200 Subject: [PATCH] Added tests for DateTime#current. --- activesupport/test/core_ext/date_time_ext_test.rb | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-) diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index b186d5e..eaee6a1 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -219,6 +219,16 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase end end + def test_current_without_time_zone + assert DateTime.current.is_a?(DateTime) + end + + def test_current_with_time_zone + with_env_tz 'US/Eastern' do + assert DateTime.current.is_a?(DateTime) + end + end + def test_acts_like_time assert DateTime.new.acts_like_time? end -- 1.5.2.4 From 7773434797a7bbc46b0e1e21d5de78d448b13feb Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 11:53:43 +0200 Subject: [PATCH] Added tests for DateTime#current. --- activesupport/test/core_ext/date_time_ext_test.rb | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-) diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index eaee6a1..8196c69 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -217,6 +217,16 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert t1.today? assert t2.today? end + + def test_past_today_future_with_time + DateTime.stubs(:current).returns(DateTime.civil(2008, 9, 10, 11, 0, 1)) + t1, t2 = Time.local(2008, 9, 10, 11, 0, 0), Time.local(2008, 9, 10, 11, 0, 2) + + assert t1.past? + assert t2.future? + assert t1.today? + assert t2.today? + end end def test_current_without_time_zone -- 1.5.2.4 From 5e75810f04c9cb42e022f914beef9e538e5c31c3 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 11:53:52 +0200 Subject: [PATCH] Revert "Added tests for DateTime#current." This reverts commit 9838eed857785bbaf2ec10cd9f2e83d6eb58a606. --- activesupport/test/core_ext/date_time_ext_test.rb | 10 ---------- 1 files changed, 0 insertions(+), 10 deletions(-) diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 8196c69..eaee6a1 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -217,16 +217,6 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert t1.today? assert t2.today? end - - def test_past_today_future_with_time - DateTime.stubs(:current).returns(DateTime.civil(2008, 9, 10, 11, 0, 1)) - t1, t2 = Time.local(2008, 9, 10, 11, 0, 0), Time.local(2008, 9, 10, 11, 0, 2) - - assert t1.past? - assert t2.future? - assert t1.today? - assert t2.today? - end end def test_current_without_time_zone -- 1.5.2.4 From d88af01f9ed7034be5682254ecee3eb375168f9b Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 12:40:08 +0200 Subject: [PATCH] Added support for TimeWithZone. --- .../active_support/core_ext/date/calculations.rb | 6 +- .../core_ext/date_time/calculations.rb | 4 +- .../active_support/core_ext/time/calculations.rb | 4 +- activesupport/lib/active_support/time_with_zone.rb | 92 +++++++++++--------- activesupport/test/core_ext/time_with_zone_test.rb | 13 +++ 5 files changed, 72 insertions(+), 47 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 2bea167..6cdcccf 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -34,17 +34,17 @@ module ActiveSupport #:nodoc: # Tells whether the Date object's date lies in the past def past? - self.to_date < ::Date.current + self < ::Date.current end # Tells whether the Date object's date is today def today? - self.to_date == ::Date.current + self.to_date == ::Date.current # we need the to_date because of DateTime end # Tells whether the Date object's date lies in the future def future? - self.to_date > ::Date.current + self > ::Date.current end # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) 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 86e7dee..0099431 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -27,12 +27,12 @@ module ActiveSupport #:nodoc: # Tells whether the DateTime object's datetime lies in the past def past? - self.to_datetime < ::DateTime.current + self < ::DateTime.current end # Tells whether the DateTime object's datetime lies in the future def future? - self.to_datetime > ::DateTime.current + self > ::DateTime.current end # Seconds since midnight: DateTime.now.seconds_since_midnight diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 3d8d78c..dbd89b2 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -59,7 +59,7 @@ module ActiveSupport #:nodoc: # Tells whether the Time object's time lies in the past def past? - self.to_time < ::Time.current + self < ::Time.current end # Tells whether the Time object's time is today @@ -69,7 +69,7 @@ module ActiveSupport #:nodoc: # Tells whether the Time object's time lies in the future def future? - self.to_time > ::Time.current + self > ::Time.current end # Seconds since midnight: Time.now.seconds_since_midnight diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 75591b7..44088f4 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -1,6 +1,6 @@ require 'tzinfo' module ActiveSupport - # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are + # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are # limited to UTC and the system's ENV['TZ'] zone. # # You shouldn't ever need to create a TimeWithZone instance directly via new -- instead, Rails provides the methods @@ -32,12 +32,12 @@ module ActiveSupport class TimeWithZone include Comparable attr_reader :time_zone - + def initialize(utc_time, time_zone, local_time = nil, period = nil) @utc, @time_zone, @time = utc_time, time_zone, local_time @period = @utc ? period : get_period_and_ensure_valid_local_time end - + # Returns a Time or DateTime instance that represents the time in +time_zone+. def time @time ||= period.to_local(@utc) @@ -51,7 +51,7 @@ module ActiveSupport alias_method :getgm, :utc alias_method :getutc, :utc alias_method :gmtime, :utc - + # Returns the underlying TZInfo::TimezonePeriod. def period @period ||= time_zone.period_for_utc(@utc) @@ -62,38 +62,38 @@ module ActiveSupport return self if time_zone == new_zone utc.in_time_zone(new_zone) end - + # Returns a Time.local() instance of the simultaneous time in your system's ENV['TZ'] zone def localtime utc.getlocal end alias_method :getlocal, :localtime - + def dst? period.dst? end alias_method :isdst, :dst? - + def utc? time_zone.name == 'UTC' end alias_method :gmt?, :utc? - + def utc_offset period.utc_total_offset end alias_method :gmt_offset, :utc_offset alias_method :gmtoff, :utc_offset - + def formatted_offset(colon = true, alternate_utc_string = nil) utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon) end - + # Time uses +zone+ to display the time zone abbreviation, so we're duck-typing it. def zone period.zone_identifier.to_s end - + def inspect "#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}" end @@ -122,7 +122,7 @@ module ActiveSupport %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") end end - + def to_yaml(options = {}) if options.kind_of?(YAML::Emitter) utc.to_yaml(options) @@ -130,19 +130,19 @@ module ActiveSupport time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z')) end end - + def httpdate utc.httpdate end - + def rfc2822 to_s(:rfc822) end alias_method :rfc822, :rfc2822 - + # :db format outputs time in UTC; all others output time in local. # Uses TimeWithZone's +strftime+, so %Z and %z work correctly. - def to_s(format = :default) + def to_s(format = :default) return utc.to_s(format) if format == :db if formatter = ::Time::DATE_FORMATS[format] formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter) @@ -150,27 +150,39 @@ module ActiveSupport "#{time.strftime("%Y-%m-%d %H:%M:%S")} #{formatted_offset(false, 'UTC')}" # mimicking Ruby 1.9 Time#to_s format end end - + # Replaces %Z and %z directives with +zone+ and +formatted_offset+, respectively, before passing to # Time#strftime, so that zone information is correct def strftime(format) format = format.gsub('%Z', zone).gsub('%z', formatted_offset(false)) time.strftime(format) end - + # Use the time in UTC for comparisons. def <=>(other) utc <=> other end - + def between?(min, max) utc.between?(min, max) end - + + def past? + utc.past? + end + + def today? + utc.today? + end + + def future? + utc.future? + end + def eql?(other) utc == other end - + def +(other) # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time, # otherwise move forward from #utc, for accuracy when moving across DST boundaries @@ -194,7 +206,7 @@ module ActiveSupport result.in_time_zone(time_zone) end end - + def since(other) # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time, # otherwise move forward from #utc, for accuracy when moving across DST boundaries @@ -204,7 +216,7 @@ module ActiveSupport utc.since(other).in_time_zone(time_zone) end end - + def ago(other) since(-other) end @@ -218,7 +230,7 @@ module ActiveSupport utc.advance(options).in_time_zone(time_zone) end end - + %w(year mon month day mday hour min sec).each do |method_name| class_eval <<-EOV def #{method_name} @@ -226,45 +238,45 @@ module ActiveSupport end EOV end - + def usec time.respond_to?(:usec) ? time.usec : 0 end - + def to_a [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone] end - + def to_f utc.to_f - end - + end + def to_i utc.to_i end alias_method :hash, :to_i alias_method :tv_sec, :to_i - + # A TimeWithZone acts like a Time, so just return +self+. def to_time self end - + def to_datetime utc.to_datetime.new_offset(Rational(utc_offset, 86_400)) end - + # So that +self+ acts_like?(:time). def acts_like_time? true end - + # Say we're a Time to thwart type checking. def is_a?(klass) klass == ::Time || super end alias_method :kind_of?, :is_a? - + # Neuter freeze because freezing can cause problems with lazy loading of attributes. def freeze self @@ -273,7 +285,7 @@ module ActiveSupport def marshal_dump [utc, time_zone.name, time] end - + def marshal_load(variables) initialize(variables[0].utc, ::Time.__send__(:get_zone, variables[1]), variables[2].utc) end @@ -290,10 +302,10 @@ module ActiveSupport result = time.__send__(sym, *args, &block) result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result end - - private + + private def get_period_and_ensure_valid_local_time - # we don't want a Time.local instance enforcing its own DST rules as well, + # we don't want a Time.local instance enforcing its own DST rules as well, # so transfer time values to a utc constructor if necessary @time = transfer_time_values_to_utc_constructor(@time) unless @time.utc? begin @@ -304,11 +316,11 @@ module ActiveSupport retry end end - + def transfer_time_values_to_utc_constructor(time) ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:usec) ? time.usec : 0) end - + def duration_of_variable_length?(obj) ActiveSupport::Duration === obj && obj.parts.flatten.detect {|p| [:years, :months, :days].include? p } end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index dfe0448..1cf2186 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -152,6 +152,19 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2)) end + uses_mocha 'past?, today? and future?' do + def test_past_today_future + Time.stubs(:current).returns(@twz.utc) + Date.stubs(:current).returns(@twz.utc.to_date) + t1, t2 = @twz - 1.second, @twz + 1.second + + assert t1.past? + assert t2.future? + assert !t1.today? + assert t2.today? + end + end + def test_eql? assert @twz.eql?(Time.utc(2000)) assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) ) -- 1.5.2.4 From e880918a6eb50e614cde881b732e0c945f1c7747 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 12:43:15 +0200 Subject: [PATCH] Changed tests that were bound to break. --- activesupport/test/core_ext/date_ext_test.rb | 4 ++-- activesupport/test/core_ext/date_time_ext_test.rb | 5 +++-- activesupport/test/core_ext/time_ext_test.rb | 5 +++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 52f32c9..49737ef 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -212,8 +212,8 @@ class DateExtCalculationsTest < Test::Unit::TestCase uses_mocha 'past?, today? and future?' do def test_today_past_future - Date.stubs(:current).returns(Date.civil(2008, 9, 10)) - t2 = Date.current + Date.stubs(:current).returns(Date.civil(2000, 1, 1)) + t2 = Date.civil(2000, 1, 1) t1, t3 = t2.yesterday, t2.tomorrow t4, t5 = t2 - 1.second, t2 + 1.second diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index eaee6a1..8a76010 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -209,8 +209,9 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase uses_mocha 'past?, today? and future?' do def test_past_today_future - DateTime.stubs(:current).returns(DateTime.civil(2008, 9, 10, 11, 0, 1)) - t1, t2 = DateTime.civil(2008, 9, 10, 11, 0, 0), DateTime.civil(2008, 9, 10, 11, 0, 2) + Date.stubs(:current).returns(Date.civil(2000, 1, 1)) + DateTime.stubs(:current).returns(DateTime.civil(2000, 1, 1, 1, 0, 1)) + t1, t2 = DateTime.civil(2000, 1, 1, 1, 0, 0), DateTime.civil(2000, 1, 1, 1, 0, 2) assert t1.past? assert t2.future? diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index c676df7..cacf313 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -565,8 +565,9 @@ class TimeExtCalculationsTest < Test::Unit::TestCase uses_mocha 'past?, today? and future?' do def test_past_today_future - Time.stubs(:current).returns(Time.local(2008, 9, 10, 11, 0, 1)) - t1, t2 = Time.local(2008, 9, 10, 11, 0, 0), Time.local(2008, 9, 10, 11, 0, 2) + Date.stubs(:current).returns(Date.civil(2000, 1, 1)) + Time.stubs(:current).returns(Time.local(2000, 1, 1, 1, 0, 1)) + t1, t2 = Time.local(2000, 1, 1, 1, 0, 0), Time.local(2000, 1, 1, 1, 0, 2) assert t1.past? assert t2.future? -- 1.5.2.4 From 5062f24d279bf1ad0006f8936aa3af98f3919a33 Mon Sep 17 00:00:00 2001 From: Clemens Kofler Date: Wed, 10 Sep 2008 15:55:42 +0200 Subject: [PATCH] Whitespace. --- .../active_support/core_ext/date/calculations.rb | 2 +- .../active_support/core_ext/time/calculations.rb | 4 ++-- activesupport/test/core_ext/time_ext_test.rb | 12 ++++++------ activesupport/test/core_ext/time_with_zone_test.rb | 18 +++++++++--------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 6cdcccf..43d70c7 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -89,7 +89,7 @@ module ActiveSupport #:nodoc: end end - # Provides precise Date calculations for years, months, and days. The +options+ parameter takes a hash with + # 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) d = self diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index dbd89b2..070f72c 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -29,8 +29,8 @@ module ActiveSupport #:nodoc: 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. + # 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) return 29 if month == 2 && ::Date.gregorian_leap?(year) COMMON_YEAR_DAYS_IN_MONTH[month] diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index cacf313..c6ebccc 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -653,24 +653,24 @@ class TimeExtMarshalingTest < Test::Unit::TestCase assert_equal t, unmarshaled assert_equal t.zone, unmarshaled.zone end - - def test_marshaling_with_local_instance + + def test_marshaling_with_local_instance t = Time.local(2000) marshaled = Marshal.dump t unmarshaled = Marshal.load marshaled assert_equal t, unmarshaled assert_equal t.zone, unmarshaled.zone end - - def test_marshaling_with_frozen_utc_instance + + def test_marshaling_with_frozen_utc_instance t = Time.utc(2000).freeze marshaled = Marshal.dump t unmarshaled = Marshal.load marshaled assert_equal t, unmarshaled assert_equal t.zone, unmarshaled.zone end - - def test_marshaling_with_frozen_local_instance + + def test_marshaling_with_frozen_local_instance t = Time.local(2000).freeze marshaled = Marshal.dump t unmarshaled = Marshal.load marshaled diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 1cf2186..f04d8fc 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -551,7 +551,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", twz.since(1.days + 1.second).inspect assert_equal "Sun, 02 Apr 2006 10:30:01 EDT -04:00", (twz + 1.days + 1.second).inspect end - + def test_advance_1_day_across_spring_dst_transition_backwards twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,10,30)) # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long @@ -561,7 +561,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", (twz - 1.days).inspect assert_equal "Sat, 01 Apr 2006 10:30:01 EST -05:00", twz.ago(1.days - 1.second).inspect end - + def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,1,10,30)) # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long @@ -578,7 +578,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.since(24.hours).inspect assert_equal "Sun, 02 Apr 2006 11:30:00 EDT -04:00", twz.advance(:hours => 24).inspect end - + def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_spring_dst_transition_backwards twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,11,30)) # In 2006, spring DST transition occurred Apr 2 at 2AM; this day was only 23 hours long @@ -595,7 +595,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.ago(24.hours).inspect assert_equal "Sat, 01 Apr 2006 10:30:00 EST -05:00", twz.advance(:hours => -24).inspect end - + def test_advance_1_day_across_fall_dst_transition twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30)) # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long @@ -606,7 +606,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", twz.since(1.days + 1.second).inspect assert_equal "Sun, 29 Oct 2006 10:30:01 EST -05:00", (twz + 1.days + 1.second).inspect end - + def test_advance_1_day_across_fall_dst_transition_backwards twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,10,30)) # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long @@ -616,7 +616,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.days).inspect assert_equal "Sat, 28 Oct 2006 10:30:01 EDT -04:00", twz.ago(1.days - 1.second).inspect end - + def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,28,10,30)) # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long @@ -633,7 +633,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.since(24.hours).inspect assert_equal "Sun, 29 Oct 2006 09:30:00 EST -05:00", twz.advance(:hours => 24).inspect end - + def test_advance_1_day_expressed_as_number_of_seconds_minutes_or_hours_across_fall_dst_transition_backwards twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,9,30)) # In 2006, fall DST transition occurred Oct 29 at 2AM; this day was 25 hours long @@ -682,7 +682,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", twz.ago(1.month).inspect assert_equal "Sat, 28 Oct 2006 10:30:00 EDT -04:00", (twz - 1.month).inspect end - + def test_advance_1_year twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,2,15,10,30)) assert_equal "Sun, 15 Feb 2009 10:30:00 EST -05:00", twz.advance(:years => 1).inspect @@ -692,7 +692,7 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", twz.years_ago(1).inspect assert_equal "Thu, 15 Feb 2007 10:30:00 EST -05:00", (twz - 1.year).inspect end - + def test_advance_1_year_during_dst twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2008,7,15,10,30)) assert_equal "Wed, 15 Jul 2009 10:30:00 EDT -04:00", twz.advance(:years => 1).inspect -- 1.5.2.4