diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 7fe4161fb4..ef2c98c2ae 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -3,17 +3,10 @@ require 'active_support/duration' require 'active_support/core_ext/object/acts_like' require 'active_support/core_ext/date/zones' require 'active_support/core_ext/time/zones' +require 'active_support/core_ext/date_and_time/calculations' class Date - DAYS_INTO_WEEK = { - :monday => 0, - :tuesday => 1, - :wednesday => 2, - :thursday => 3, - :friday => 4, - :saturday => 5, - :sunday => 6 - } + include DateAndTime::Calculations class << self # Returns a new Date representing the date 1 day ago (i.e. yesterday's date). @@ -32,21 +25,6 @@ class Date end end - # Returns true if the Date object's date lies in the past. Otherwise returns false. - def past? - self < ::Date.current - end - - # Returns true if the Date object's date is today. - def today? - to_date == ::Date.current # we need the to_date because of DateTime - end - - # Returns true if the Date object's date lies in the future. - def future? - 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) # and then subtracts the specified number of seconds. def ago(seconds) @@ -116,161 +94,4 @@ class Date options.fetch(:day, day) ) end - - # Returns a new Date/DateTime representing the time a number of specified weeks ago. - def weeks_ago(weeks) - advance(:weeks => -weeks) - end - - # Returns a new Date/DateTime representing the time a number of specified months ago. - def months_ago(months) - advance(:months => -months) - end - - # Returns a new Date/DateTime representing the time a number of specified months in the future. - def months_since(months) - advance(:months => months) - end - - # Returns a new Date/DateTime representing the time a number of specified years ago. - def years_ago(years) - advance(:years => -years) - end - - # Returns a new Date/DateTime representing the time a number of specified years in the future. - def years_since(years) - advance(:years => years) - end - - # Returns number of days to start of this week. Week is assumed to start on - # +start_day+, default is +:monday+. - def days_to_week_start(start_day = :monday) - start_day_number = DAYS_INTO_WEEK[start_day] - current_day_number = wday != 0 ? wday - 1 : 6 - (current_day_number - start_day_number) % 7 - end - - # Returns a new +Date+/+DateTime+ representing the start of this week. Week is - # assumed to start on +start_day+, default is +:monday+. +DateTime+ objects - # have their time set to 0:00. - def beginning_of_week(start_day = :monday) - days_to_start = days_to_week_start(start_day) - result = self - days_to_start - acts_like?(:time) ? result.midnight : result - end - alias :at_beginning_of_week :beginning_of_week - - # Returns a new +Date+/+DateTime+ representing the start of this week. Week is - # assumed to start on a Monday. +DateTime+ objects have their time set to 0:00. - def monday - beginning_of_week - end - - # Returns a new +Date+/+DateTime+ representing the end of this week. Week is - # assumed to start on +start_day+, default is +:monday+. +DateTime+ objects - # have their time set to 23:59:59. - def end_of_week(start_day = :monday) - days_to_end = 6 - days_to_week_start(start_day) - result = self + days_to_end.days - acts_like?(:time) ? result.end_of_day : result - end - alias :at_end_of_week :end_of_week - - # Returns a new +Date+/+DateTime+ representing the end of this week. Week is - # assumed to start on a Monday. +DateTime+ objects have their time set to 23:59:59. - def sunday - end_of_week - end - - # Returns a new +Date+/+DateTime+ representing the given +day+ in the previous - # week. Default is +:monday+. +DateTime+ objects have their time set to 0:00. - def prev_week(day = :monday) - result = (self - 7).beginning_of_week + DAYS_INTO_WEEK[day] - acts_like?(:time) ? result.change(:hour => 0) : result - end - alias :last_week :prev_week - - # Alias of prev_month - alias :last_month :prev_month - - # Alias of prev_year - alias :last_year :prev_year - - # Returns a new Date/DateTime representing the start of the given day in next week (default is :monday). - def next_week(day = :monday) - result = (self + 7).beginning_of_week + DAYS_INTO_WEEK[day] - acts_like?(:time) ? result.change(:hour => 0) : result - end - - # Short-hand for months_ago(3) - def prev_quarter - months_ago(3) - end - alias_method :last_quarter, :prev_quarter - - # Short-hand for months_since(3) - def next_quarter - months_since(3) - end - - # Returns a new Date/DateTime representing the start of the month (1st of the month; DateTime objects will have time set to 0:00) - def beginning_of_month - acts_like?(:time) ? change(:day => 1, :hour => 0) : change(:day => 1) - end - alias :at_beginning_of_month :beginning_of_month - - # Returns a new Date/DateTime representing the end of the month (last day of the month; DateTime objects will have time set to 0:00) - def end_of_month - last_day = ::Time.days_in_month(month, year) - if acts_like?(:time) - change(:day => last_day, :hour => 23, :min => 59, :sec => 59) - else - change(:day => last_day) - end - end - alias :at_end_of_month :end_of_month - - # Returns a new Date/DateTime representing the start of the quarter (1st of january, april, july, october; DateTime objects will have time set to 0:00) - def beginning_of_quarter - first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month } - beginning_of_month.change(:month => first_quarter_month) - end - alias :at_beginning_of_quarter :beginning_of_quarter - - # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59) - def end_of_quarter - last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month } - beginning_of_month.change(:month => last_quarter_month).end_of_month - end - alias :at_end_of_quarter :end_of_quarter - - # Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00) - def beginning_of_year - if acts_like?(:time) - change(:month => 1, :day => 1, :hour => 0) - else - change(:month => 1, :day => 1) - end - end - alias :at_beginning_of_year :beginning_of_year - - # Returns a new Time representing the end of the year (31st of december; DateTime objects will have time set to 23:59:59) - def end_of_year - if acts_like?(:time) - change(:month => 12, :day => 31, :hour => 23, :min => 59, :sec => 59) - else - change(:month => 12, :day => 31) - end - end - alias :at_end_of_year :end_of_year - - # Convenience method which returns a new Date/DateTime representing the time 1 day ago - def yesterday - self - 1 - end - - # Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time - def tomorrow - self + 1 - end end diff --git a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb new file mode 100644 index 0000000000..56c1b06608 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb @@ -0,0 +1,213 @@ +module DateAndTime + module Calculations + DAYS_INTO_WEEK = { + :monday => 0, + :tuesday => 1, + :wednesday => 2, + :thursday => 3, + :friday => 4, + :saturday => 5, + :sunday => 6 + } + + # Returns a new date/time representing yesterday. + def yesterday + advance(:days => -1) + end + + # Returns a new date/time representing tomorrow. + def tomorrow + advance(:days => 1) + end + + # Returns true if the date/time is today. + def today? + to_date == ::Date.current + end + + # Returns true if the date/time is in the past. + def past? + self < self.class.current + end + + # Returns true if the date/time is in the future. + def future? + self > self.class.current + end + + # Returns a new date/time the specified number of days ago. + def days_ago(days) + advance(:days => -days) + end + + # Returns a new date/time the specified number of days in the future. + def days_since(days) + advance(:days => days) + end + + # Returns a new date/time the specified number of weeks ago. + def weeks_ago(weeks) + advance(:weeks => -weeks) + end + + # Returns a new date/time the specified number of weeks in the future. + def weeks_since(weeks) + advance(:weeks => weeks) + end + + # Returns a new date/time the specified number of months ago. + def months_ago(months) + advance(:months => -months) + end + + # Returns a new date/time the specified number of months in the future. + def months_since(months) + advance(:months => months) + end + + # Returns a new date/time the specified number of years ago. + def years_ago(years) + advance(:years => -years) + end + + # Returns a new date/time the specified number of years in the future. + def years_since(years) + advance(:years => years) + end + + # Returns a new date/time at the start of the month. + # DateTime objects will have a time set to 0:00. + def beginning_of_month + first_hour{ change(:day => 1) } + end + alias :at_beginning_of_month :beginning_of_month + + # Returns a new date/time at the start of the quarter. + # Example: 1st January, 1st July, 1st October. + # DateTime objects will have a time set to 0:00. + def beginning_of_quarter + first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month } + beginning_of_month.change(:month => first_quarter_month) + end + alias :at_beginning_of_quarter :beginning_of_quarter + + # Returns a new date/time at the end of the quarter. + # Example: 31st March, 30th June, 30th September. + # DateTIme objects will have a time set to 23:59:59. + def end_of_quarter + last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month } + beginning_of_month.change(:month => last_quarter_month).end_of_month + end + alias :at_end_of_quarter :end_of_quarter + + # Return a new date/time at the beginning of the year. + # Example: 1st January. + # DateTime objects will have a time set to 0:00. + def beginning_of_year + change(:month => 1).beginning_of_month + end + alias :at_beginning_of_year :beginning_of_year + + # Returns a new date/time representing the given day in the next week. + # Default is :monday. + # DateTime objects have their time set to 0:00. + def next_week(day = :monday) + first_hour{ weeks_since(1).beginning_of_week.days_since(DAYS_INTO_WEEK[day]) } + end + + # Short-hand for months_since(1). + def next_month + months_since(1) + end + + # Short-hand for months_since(3) + def next_quarter + months_since(3) + end + + # Short-hand for years_since(1). + def next_year + years_since(1) + end + + # Returns a new date/time representing the given day in the previous week. + # Default is :monday. + # DateTime objects have their time set to 0:00. + def prev_week(day = :monday) + first_hour{ weeks_ago(1).beginning_of_week.days_since(DAYS_INTO_WEEK[day]) } + end + alias_method :last_week, :prev_week + + # Short-hand for months_ago(1). + def prev_month + months_ago(1) + end + alias_method :last_month, :prev_month + + # Short-hand for months_ago(3). + def prev_quarter + months_ago(3) + end + alias_method :last_quarter, :prev_quarter + + # Short-hand for years_ago(1). + def prev_year + years_ago(1) + end + alias_method :last_year, :prev_year + + # Returns the number of days to the start of the week on the given day. + # Default is :monday. + def days_to_week_start(start_day = :monday) + start_day_number = DAYS_INTO_WEEK[start_day] + current_day_number = wday != 0 ? wday - 1 : 6 + (current_day_number - start_day_number) % 7 + end + + # Returns a new date/time representing the start of this week on the given day. + # Default is :monday. + # DateTime objects have their time set to 0:00. + def beginning_of_week(start_day = :monday) + result = days_ago(days_to_week_start(start_day)) + acts_like?(:time) ? result.midnight : result + end + alias :at_beginning_of_week :beginning_of_week + alias :monday :beginning_of_week + + # Returns a new date/time representing the end of this week on the given day. + # Default is :monday (i.e end of Sunday). + # DateTime objects have their time set to 23:59:59. + def end_of_week(start_day = :monday) + last_hour{ days_since(6 - days_to_week_start(start_day)) } + end + alias :at_end_of_week :end_of_week + alias :sunday :end_of_week + + # Returns a new date/time representing the end of the month. + # DateTime objects will have a time set to 23:59:59. + def end_of_month + last_day = ::Time.days_in_month(month, year) + last_hour{ days_since(last_day - day) } + end + alias :at_end_of_month :end_of_month + + # Returns a new date/time representing the end of the year. + # DateTime objects will have a time set to 23:59:59. + def end_of_year + result = change(:month => 12).end_of_month + end + alias :at_end_of_year :end_of_year + + private + + def first_hour + result = yield + acts_like?(:time) ? result.change(:hour => 0) : result + end + + def last_hour + result = yield + acts_like?(:time) ? result.end_of_day : result + end + end +end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index d0f574f2ba..fd43849504 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -2,18 +2,12 @@ require 'active_support/duration' require 'active_support/core_ext/time/conversions' require 'active_support/time_with_zone' require 'active_support/core_ext/time/zones' +require 'active_support/core_ext/date_and_time/calculations' class Time + include DateAndTime::Calculations + COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - DAYS_INTO_WEEK = { - :monday => 0, - :tuesday => 1, - :wednesday => 2, - :thursday => 3, - :friday => 4, - :saturday => 5, - :sunday => 6 - } class << self # Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances @@ -63,21 +57,6 @@ class Time end end - # Tells whether the Time object's time lies in the past - def past? - self < ::Time.current - end - - # Tells whether the Time object's time is today - def today? - to_date == ::Date.current - end - - # Tells whether the Time object's time lies in the future - def future? - self > ::Time.current - end - # Seconds since midnight: Time.now.seconds_since_midnight def seconds_since_midnight to_i - change(:hour => 0).to_i + (usec / 1.0e+6) @@ -146,116 +125,6 @@ class Time end alias :in :since - # Returns a new Time representing the time a number of specified weeks ago. - def weeks_ago(weeks) - advance(:weeks => -weeks) - end - - # Returns a new Time representing the time a number of specified months ago - def months_ago(months) - advance(:months => -months) - end - - # Returns a new Time representing the time a number of specified months in the future - def months_since(months) - advance(:months => months) - end - - # Returns a new Time representing the time a number of specified years ago - def years_ago(years) - advance(:years => -years) - end - - # Returns a new Time representing the time a number of specified years in the future - def years_since(years) - advance(:years => years) - end - - # Short-hand for years_ago(1) - def prev_year - years_ago(1) - end - alias_method :last_year, :prev_year - - # Short-hand for years_since(1) - def next_year - years_since(1) - end - - # Short-hand for months_ago(1) - def prev_month - months_ago(1) - end - alias_method :last_month, :prev_month - - # Short-hand for months_since(1) - def next_month - months_since(1) - end - - # Short-hand for months_ago(3) - def prev_quarter - months_ago(3) - end - alias_method :last_quarter, :prev_quarter - - # Short-hand for months_since(3) - def next_quarter - months_since(3) - end - - # Returns number of days to start of this week, week starts on start_day (default is :monday). - def days_to_week_start(start_day = :monday) - start_day_number = DAYS_INTO_WEEK[start_day] - current_day_number = wday != 0 ? wday - 1 : 6 - days_span = current_day_number - start_day_number - - days_span >= 0 ? days_span : 7 + days_span - end - - # Returns a new Time representing the "start" of this week, week starts on start_day (default is :monday, i.e. Monday, 0:00). - def beginning_of_week(start_day = :monday) - days_to_start = days_to_week_start(start_day) - (self - days_to_start.days).midnight - end - alias :at_beginning_of_week :beginning_of_week - - # Returns a new +Date+/+DateTime+ representing the start of this week. Week is - # assumed to start on a Monday. +DateTime+ objects have their time set to 0:00. - def monday - beginning_of_week - end - - # Returns a new Time representing the end of this week, week starts on start_day (default is :monday, i.e. end of Sunday). - def end_of_week(start_day = :monday) - days_to_end = 6 - days_to_week_start(start_day) - (self + days_to_end.days).end_of_day - end - alias :at_end_of_week :end_of_week - - # Returns a new +Date+/+DateTime+ representing the end of this week. Week is - # assumed to start on a Monday. +DateTime+ objects have their time set to 23:59:59. - def sunday - end_of_week - end - - # Returns a new Time representing the start of the given day in the previous week (default is :monday). - def prev_week(day = :monday) - ago(1.week). - beginning_of_week. - since(DAYS_INTO_WEEK[day].day). - change(:hour => 0) - end - alias_method :last_week, :prev_week - - # Returns a new Time representing the start of the given day in next week (default is :monday). - def next_week(day = :monday) - since(1.week). - beginning_of_week. - since(DAYS_INTO_WEEK[day].day). - change(:hour => 0) - end - # Returns a new Time representing the start of the day (0:00) def beginning_of_day #(self - seconds_since_midnight).change(:usec => 0) @@ -290,70 +159,6 @@ class Time ) end - # Returns a new Time representing the start of the month (1st of the month, 0:00) - def beginning_of_month - #self - ((self.mday-1).days + self.seconds_since_midnight) - change(:day => 1, :hour => 0) - end - alias :at_beginning_of_month :beginning_of_month - - # Returns a new Time representing the end of the month (end of the last day of the month) - def end_of_month - #self - ((self.mday-1).days + self.seconds_since_midnight) - last_day = ::Time.days_in_month(month, year) - change( - :day => last_day, - :hour => 23, - :min => 59, - :sec => 59, - :usec => Rational(999999999, 1000) - ) - 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 - first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month } - beginning_of_month.change(:month => first_quarter_month) - end - alias :at_beginning_of_quarter :beginning_of_quarter - - # Returns a new Time representing the end of the quarter (end of the last day of march, june, september, december) - def end_of_quarter - last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month } - beginning_of_month.change(:month => last_quarter_month).end_of_month - end - alias :at_end_of_quarter :end_of_quarter - - # Returns a new Time representing the start of the year (1st of january, 0:00) - def beginning_of_year - change(:month => 1, :day => 1, :hour => 0) - end - alias :at_beginning_of_year :beginning_of_year - - # Returns a new Time representing the end of the year (end of the 31st of december) - def end_of_year - change( - :month => 12, - :day => 31, - :hour => 23, - :min => 59, - :sec => 59, - :usec => Rational(999999999, 1000) - ) - end - alias :at_end_of_year :end_of_year - - # Convenience method which returns a new Time representing the time 1 day ago - def yesterday - advance(:days => -1) - end - - # Convenience method which returns a new Time representing the time 1 day since the instance time - def tomorrow - advance(:days => 1) - end - # Returns a Range representing the whole day of the current time. def all_day beginning_of_day..end_of_day diff --git a/activesupport/test/core_ext/date_and_time_behavior.rb b/activesupport/test/core_ext/date_and_time_behavior.rb new file mode 100644 index 0000000000..014935b0c0 --- /dev/null +++ b/activesupport/test/core_ext/date_and_time_behavior.rb @@ -0,0 +1,186 @@ +require 'abstract_unit' + +module DateAndTimeBehavior + def test_yesterday + assert_equal date_time_init(2005,2,21,10,10,10), date_time_init(2005,2,22,10,10,10).yesterday + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,3,2,10,10,10).yesterday.yesterday + end + + def test_tomorrow + assert_equal date_time_init(2005,2,23,10,10,10), date_time_init(2005,2,22,10,10,10).tomorrow + assert_equal date_time_init(2005,3,2,10,10,10), date_time_init(2005,2,28,10,10,10).tomorrow.tomorrow + end + + def test_days_ago + assert_equal date_time_init(2005,6,4,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(1) + assert_equal date_time_init(2005,5,31,10,10,10), date_time_init(2005,6,5,10,10,10).days_ago(5) + end + + def test_days_since + assert_equal date_time_init(2005,6,6,10,10,10), date_time_init(2005,6,5,10,10,10).days_since(1) + assert_equal date_time_init(2005,1,1,10,10,10), date_time_init(2004,12,31,10,10,10).days_since(1) + end + + def test_weeks_ago + assert_equal date_time_init(2005,5,29,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(1) + assert_equal date_time_init(2005,5,1,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(5) + assert_equal date_time_init(2005,4,24,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(6) + assert_equal date_time_init(2005,2,27,10,10,10), date_time_init(2005,6,5,10,10,10).weeks_ago(14) + assert_equal date_time_init(2004,12,25,10,10,10), date_time_init(2005,1,1,10,10,10).weeks_ago(1) + end + + def test_weeks_since + assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,7,14,10,10,10), date_time_init(2005,7,7,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,7,4,10,10,10), date_time_init(2005,6,27,10,10,10).weeks_since(1) + assert_equal date_time_init(2005,1,4,10,10,10), date_time_init(2004,12,28,10,10,10).weeks_since(1) + end + + def test_months_ago + assert_equal date_time_init(2005,5,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(1) + assert_equal date_time_init(2004,11,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(7) + assert_equal date_time_init(2004,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(6) + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(12) + assert_equal date_time_init(2003,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_ago(24) + end + + def test_months_since + assert_equal date_time_init(2005,7,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(1) + assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(1) + assert_equal date_time_init(2005,12,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(6) + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,12,5,10,10,10).months_since(6) + assert_equal date_time_init(2006,1,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(7) + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(12) + assert_equal date_time_init(2007,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).months_since(24) + assert_equal date_time_init(2005,4,30,10,10,10), date_time_init(2005,3,31,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,29,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,30,10,10,10).months_since(1) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2005,1,31,10,10,10).months_since(1) + end + + def test_years_ago + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(1) + assert_equal date_time_init(1998,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_ago(7) + assert_equal date_time_init(2003,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_ago(1) # 1 year ago from leap day + end + + def test_years_since + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(1) + assert_equal date_time_init(2012,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(7) + assert_equal date_time_init(2005,2,28,10,10,10), date_time_init(2004,2,29,10,10,10).years_since(1) # 1 year since leap day + assert_equal date_time_init(2182,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).years_since(177) + end + + def test_beginning_of_month + assert_equal date_time_init(2005,2,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_month + end + + def test_beginning_of_quarter + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,15,10,10,10).beginning_of_quarter + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,1,1,0,0,0).beginning_of_quarter + assert_equal date_time_init(2005,10,1,0,0,0), date_time_init(2005,12,31,10,10,10).beginning_of_quarter + assert_equal date_time_init(2005,4,1,0,0,0), date_time_init(2005,6,30,23,59,59).beginning_of_quarter + end + + def test_end_of_quarter + assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,15,10,10,10).end_of_quarter + assert_equal date_time_init(2007,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,3,31,0,0,0).end_of_quarter + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,21,10,10,10).end_of_quarter + assert_equal date_time_init(2007,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2007,4,1,0,0,0).end_of_quarter + assert_equal date_time_init(2008,6,30,23,59,59,Rational(999999999, 1000)), date_time_init(2008,5,31,0,0,0).end_of_quarter + end + + def test_beginning_of_year + assert_equal date_time_init(2005,1,1,0,0,0), date_time_init(2005,2,22,10,10,10).beginning_of_year + end + + def test_next_week + assert_equal date_time_init(2005,2,28,0,0,0), date_time_init(2005,2,22,15,15,10).next_week + assert_equal date_time_init(2005,3,4,0,0,0), date_time_init(2005,2,22,15,15,10).next_week(:friday) + assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,10,23,0,0,0).next_week + assert_equal date_time_init(2006,11,1,0,0,0), date_time_init(2006,10,23,0,0,0).next_week(:wednesday) + end + + def test_next_month_on_31st + assert_equal date_time_init(2005,9,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_month + end + + def test_next_quarter_on_31st + assert_equal date_time_init(2005,11,30,15,15,10), date_time_init(2005,8,31,15,15,10).next_quarter + end + + def test_next_year + assert_equal date_time_init(2006,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).next_year + end + + def test_prev_week + assert_equal date_time_init(2005,2,21,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week + assert_equal date_time_init(2005,2,22,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:tuesday) + assert_equal date_time_init(2005,2,25,0,0,0), date_time_init(2005,3,1,15,15,10).prev_week(:friday) + assert_equal date_time_init(2006,10,30,0,0,0), date_time_init(2006,11,6,0,0,0).prev_week + assert_equal date_time_init(2006,11,15,0,0,0), date_time_init(2006,11,23,0,0,0).prev_week(:wednesday) + end + + def test_prev_month_on_31st + assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,3,31,10,10,10).prev_month + end + + def test_prev_quarter_on_31st + assert_equal date_time_init(2004,2,29,10,10,10), date_time_init(2004,5,31,10,10,10).prev_quarter + end + + def test_prev_year + assert_equal date_time_init(2004,6,5,10,10,10), date_time_init(2005,6,5,10,10,10).prev_year + end + + def test_days_to_week_start + assert_equal 0, date_time_init(2011,11,01,0,0,0).days_to_week_start(:tuesday) + assert_equal 1, date_time_init(2011,11,02,0,0,0).days_to_week_start(:tuesday) + assert_equal 2, date_time_init(2011,11,03,0,0,0).days_to_week_start(:tuesday) + assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday) + assert_equal 4, date_time_init(2011,11,05,0,0,0).days_to_week_start(:tuesday) + assert_equal 5, date_time_init(2011,11,06,0,0,0).days_to_week_start(:tuesday) + assert_equal 6, date_time_init(2011,11,07,0,0,0).days_to_week_start(:tuesday) + + assert_equal 3, date_time_init(2011,11,03,0,0,0).days_to_week_start(:monday) + assert_equal 3, date_time_init(2011,11,04,0,0,0).days_to_week_start(:tuesday) + assert_equal 3, date_time_init(2011,11,05,0,0,0).days_to_week_start(:wednesday) + assert_equal 3, date_time_init(2011,11,06,0,0,0).days_to_week_start(:thursday) + assert_equal 3, date_time_init(2011,11,07,0,0,0).days_to_week_start(:friday) + assert_equal 3, date_time_init(2011,11,8,0,0,0).days_to_week_start(:saturday) + assert_equal 3, date_time_init(2011,11,9,0,0,0).days_to_week_start(:sunday) + end + + def test_beginning_of_week + assert_equal date_time_init(2005,1,31,0,0,0), date_time_init(2005,2,4,10,10,10).beginning_of_week + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,28,0,0,0).beginning_of_week #monday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,29,0,0,0).beginning_of_week #tuesday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,11,30,0,0,0).beginning_of_week #wednesday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,01,0,0,0).beginning_of_week #thursday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,02,0,0,0).beginning_of_week #friday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,03,0,0,0).beginning_of_week #saturday + assert_equal date_time_init(2005,11,28,0,0,0), date_time_init(2005,12,04,0,0,0).beginning_of_week #sunday + end + + def test_end_of_week + assert_equal date_time_init(2008,1,6,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_week + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,27,0,0,0).end_of_week #monday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,28,0,0,0).end_of_week #tuesday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,29,0,0,0).end_of_week #wednesday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,30,0,0,0).end_of_week #thursday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,8,31,0,0,0).end_of_week #friday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,01,0,0,0).end_of_week #saturday + assert_equal date_time_init(2007,9,2,23,59,59,Rational(999999999, 1000)), date_time_init(2007,9,02,0,0,0).end_of_week #sunday + end + + def test_end_of_month + assert_equal date_time_init(2005,3,31,23,59,59,Rational(999999999, 1000)), date_time_init(2005,3,20,10,10,10).end_of_month + assert_equal date_time_init(2005,2,28,23,59,59,Rational(999999999, 1000)), date_time_init(2005,2,20,10,10,10).end_of_month + assert_equal date_time_init(2005,4,30,23,59,59,Rational(999999999, 1000)), date_time_init(2005,4,20,10,10,10).end_of_month + end + + def test_end_of_year + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,2,22,10,10,10).end_of_year + assert_equal date_time_init(2007,12,31,23,59,59,Rational(999999999, 1000)), date_time_init(2007,12,31,10,10,10).end_of_year + end +end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 088b74a29a..7ae1f67785 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -1,7 +1,22 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class DateExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,*args) + Date.new(year,month,day) + end + + include DateAndTimeBehavior + + def test_yesterday_in_calendar_reform + assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday + end + + def test_tomorrow_in_calendar_reform + assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow + end + def test_to_s date = Date.new(2005, 2, 21) assert_equal "2005-02-21", date.to_s @@ -46,22 +61,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,6,22), Date.new(2005,2,22).change(:month => 6) end - def test_beginning_of_week - assert_equal Date.new(2005,1,31), Date.new(2005,2,4).beginning_of_week - assert_equal Date.new(2005,11,28), Date.new(2005,11,28).beginning_of_week #monday - assert_equal Date.new(2005,11,28), Date.new(2005,11,29).beginning_of_week #tuesday - assert_equal Date.new(2005,11,28), Date.new(2005,11,30).beginning_of_week #wednesday - assert_equal Date.new(2005,11,28), Date.new(2005,12,01).beginning_of_week #thursday - assert_equal Date.new(2005,11,28), Date.new(2005,12,02).beginning_of_week #friday - assert_equal Date.new(2005,11,28), Date.new(2005,12,03).beginning_of_week #saturday - assert_equal Date.new(2005,11,28), Date.new(2005,12,04).beginning_of_week #sunday - end - - def test_monday - assert_equal Date.new(2005,11,28), Date.new(2005,11,28).monday - assert_equal Date.new(2005,11,28), Date.new(2005,12,01).monday - end - def test_sunday assert_equal Date.new(2008,3,2), Date.new(2008,3,02).sunday assert_equal Date.new(2008,3,2), Date.new(2008,2,29).sunday @@ -71,41 +70,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,1), Date.new(1582,10,15).beginning_of_week #friday end - def test_beginning_of_month - assert_equal Date.new(2005,2,1), Date.new(2005,2,22).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal Date.new(2005,1,1), Date.new(2005,2,15).beginning_of_quarter - assert_equal Date.new(2005,1,1), Date.new(2005,1,1).beginning_of_quarter - assert_equal Date.new(2005,10,1), Date.new(2005,12,31).beginning_of_quarter - assert_equal Date.new(2005,4,1), Date.new(2005,6,30).beginning_of_quarter - end - - def test_end_of_week - assert_equal Date.new(2008,2,24), Date.new(2008,2,22).end_of_week - assert_equal Date.new(2008,3,2), Date.new(2008,2,25).end_of_week #monday - assert_equal Date.new(2008,3,2), Date.new(2008,2,26).end_of_week #tuesday - assert_equal Date.new(2008,3,2), Date.new(2008,2,27).end_of_week #wednesday - assert_equal Date.new(2008,3,2), Date.new(2008,2,28).end_of_week #thursday - assert_equal Date.new(2008,3,2), Date.new(2008,2,29).end_of_week #friday - assert_equal Date.new(2008,3,2), Date.new(2008,3,01).end_of_week #saturday - assert_equal Date.new(2008,3,2), Date.new(2008,3,02).end_of_week #sunday - end - def test_end_of_week_in_calendar_reform assert_equal Date.new(1582,10,17), Date.new(1582,10,4).end_of_week #thursday end - def test_end_of_quarter - assert_equal Date.new(2008,3,31), Date.new(2008,2,15).end_of_quarter - assert_equal Date.new(2008,3,31), Date.new(2008,3,31).end_of_quarter - assert_equal Date.new(2008,12,31), Date.new(2008,10,8).end_of_quarter - assert_equal Date.new(2008,6,30), Date.new(2008,4,14).end_of_quarter - assert_equal Date.new(2008,6,30), Date.new(2008,5,31).end_of_quarter - assert_equal Date.new(2008,9,30), Date.new(2008,8,21).end_of_quarter - end - def test_end_of_year assert_equal Date.new(2008,12,31).to_s, Date.new(2008,2,22).end_of_year.to_s end @@ -116,57 +84,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2005,4,30), Date.new(2005,4,20).end_of_month end - def test_beginning_of_year - assert_equal Date.new(2005,1,1).to_s, Date.new(2005,2,22).beginning_of_year.to_s - end - - def test_weeks_ago - assert_equal Date.new(2005,5,10), Date.new(2005,5,17).weeks_ago(1) - assert_equal Date.new(2005,5,10), Date.new(2005,5,24).weeks_ago(2) - assert_equal Date.new(2005,5,10), Date.new(2005,5,31).weeks_ago(3) - assert_equal Date.new(2005,5,10), Date.new(2005,6,7).weeks_ago(4) - assert_equal Date.new(2006,12,31), Date.new(2007,2,4).weeks_ago(5) - end - - def test_months_ago - assert_equal Date.new(2005,5,5), Date.new(2005,6,5).months_ago(1) - assert_equal Date.new(2004,11,5), Date.new(2005,6,5).months_ago(7) - assert_equal Date.new(2004,12,5), Date.new(2005,6,5).months_ago(6) - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).months_ago(12) - assert_equal Date.new(2003,6,5), Date.new(2005,6,5).months_ago(24) - end - - def test_months_since - assert_equal Date.new(2005,7,5), Date.new(2005,6,5).months_since(1) - assert_equal Date.new(2006,1,5), Date.new(2005,12,5).months_since(1) - assert_equal Date.new(2005,12,5), Date.new(2005,6,5).months_since(6) - assert_equal Date.new(2006,6,5), Date.new(2005,12,5).months_since(6) - assert_equal Date.new(2006,1,5), Date.new(2005,6,5).months_since(7) - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).months_since(12) - assert_equal Date.new(2007,6,5), Date.new(2005,6,5).months_since(24) - assert_equal Date.new(2005,4,30), Date.new(2005,3,31).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,29).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,30).months_since(1) - assert_equal Date.new(2005,2,28), Date.new(2005,1,31).months_since(1) - end - - def test_years_ago - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).years_ago(1) - assert_equal Date.new(1998,6,5), Date.new(2005,6,5).years_ago(7) - assert_equal Date.new(2003,2,28), Date.new(2004,2,29).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).years_since(1) - assert_equal Date.new(2012,6,5), Date.new(2005,6,5).years_since(7) - assert_equal Date.new(2182,6,5), Date.new(2005,6,5).years_since(177) - assert_equal Date.new(2005,2,28), Date.new(2004,2,29).years_since(1) # 1 year since leap day - end - - def test_prev_year - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).prev_year - end - def test_prev_year_in_leap_years assert_equal Date.new(1999,2,28), Date.new(2000,2,29).prev_year end @@ -187,10 +104,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year end - def test_next_year - assert_equal Date.new(2006,6,5), Date.new(2005,6,5).next_year - end - def test_next_year_in_leap_years assert_equal Date.new(2001,2,28), Date.new(2000,2,29).next_year end @@ -199,24 +112,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(1582,10,4), Date.new(1581,10,10).next_year end - def test_yesterday - assert_equal Date.new(2005,2,21), Date.new(2005,2,22).yesterday - assert_equal Date.new(2005,2,28), Date.new(2005,3,2).yesterday.yesterday - end - - def test_yesterday_in_calendar_reform - assert_equal Date.new(1582,10,4), Date.new(1582,10,15).yesterday - end - - def test_tomorrow - assert_equal Date.new(2005,2,23), Date.new(2005,2,22).tomorrow - assert_equal Date.new(2005,3,2), Date.new(2005,2,28).tomorrow.tomorrow - end - - def test_tomorrow_in_calendar_reform - assert_equal Date.new(1582,10,15), Date.new(1582,10,4).tomorrow - end - def test_advance assert_equal Date.new(2006,2,28), Date.new(2005,2,28).advance(:years => 1) assert_equal Date.new(2005,6,28), Date.new(2005,2,28).advance(:months => 4) @@ -249,14 +144,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_prev_week - assert_equal Date.new(2005,5,9), Date.new(2005,5,17).prev_week - assert_equal Date.new(2006,12,25), Date.new(2007,1,7).prev_week - assert_equal Date.new(2010,2,12), Date.new(2010,2,19).prev_week(:friday) - assert_equal Date.new(2010,2,13), Date.new(2010,2,19).prev_week(:saturday) - assert_equal Date.new(2010,2,27), Date.new(2010,3,4).prev_week(:saturday) - end - def test_last_week assert_equal Date.new(2005,5,9), Date.new(2005,5,17).last_week assert_equal Date.new(2006,12,25), Date.new(2007,1,7).last_week @@ -265,38 +152,15 @@ class DateExtCalculationsTest < ActiveSupport::TestCase assert_equal Date.new(2010,2,27), Date.new(2010,3,4).last_week(:saturday) end - def test_next_week - assert_equal Date.new(2005,2,28), Date.new(2005,2,22).next_week - assert_equal Date.new(2005,3,4), Date.new(2005,2,22).next_week(:friday) - assert_equal Date.new(2006,10,30), Date.new(2006,10,23).next_week - assert_equal Date.new(2006,11,1), Date.new(2006,10,23).next_week(:wednesday) - end - def test_next_week_in_calendar_reform assert_equal Date.new(1582,10,15), Date.new(1582,9,30).next_week(:friday) assert_equal Date.new(1582,10,18), Date.new(1582,10,4).next_week end - def test_next_month_on_31st - assert_equal Date.new(2005, 9, 30), Date.new(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month end - def test_next_quarter_on_31st - assert_equal Date.new(2005, 11, 30), Date.new(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal Date.new(2004, 2, 29), Date.new(2004, 5, 31).last_quarter end @@ -420,13 +284,6 @@ class DateExtCalculationsTest < ActiveSupport::TestCase end end - def test_today - Date.stubs(:current).returns(Date.new(2000, 1, 1)) - assert_equal false, Date.new(1999, 12, 31).today? - assert_equal true, Date.new(2000,1,1).today? - assert_equal false, Date.new(2000,1,2).today? - end - def test_past Date.stubs(:current).returns(Date.new(2000, 1, 1)) assert_equal true, Date.new(1999, 12, 31).past? diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 21b7efdc73..b1d1e8ecb4 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -1,7 +1,14 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class DateTimeExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,hour,minute,second,*args) + DateTime.civil(year,month,day,hour,minute,second) + end + + include DateAndTimeBehavior + def test_to_s datetime = DateTime.new(2005, 2, 21, 14, 30, 0, 0) assert_equal "2005-02-21 14:30:00", datetime.to_s(:db) @@ -54,35 +61,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal 86399,DateTime.civil(2005,1,1,23,59,59).seconds_since_midnight end - def test_days_to_week_start - assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday) - assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday) - assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday) - assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday) - assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday) - - assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday) - assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday) - assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday) - assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday) - assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday) - end - - def test_beginning_of_week - assert_equal DateTime.civil(2005,1,31), DateTime.civil(2005,2,4,10,10,10).beginning_of_week - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,28,0,0,0).beginning_of_week #monday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,29,0,0,0).beginning_of_week #tuesday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,11,30,0,0,0).beginning_of_week #wednesday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,01,0,0,0).beginning_of_week #thursday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,02,0,0,0).beginning_of_week #friday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,03,0,0,0).beginning_of_week #saturday - assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday - end - def test_beginning_of_day assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day end @@ -99,82 +77,16 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2005,2,4,19,59,59), DateTime.civil(2005,2,4,19,30,10).end_of_hour end - def test_beginning_of_month - assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,15,10,10,10).beginning_of_quarter - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,1,1,0,0,0).beginning_of_quarter - assert_equal DateTime.civil(2005,10,1,0,0,0), DateTime.civil(2005,12,31,10,10,10).beginning_of_quarter - assert_equal DateTime.civil(2005,4,1,0,0,0), DateTime.civil(2005,6,30,23,59,59).beginning_of_quarter - end - def test_end_of_month assert_equal DateTime.civil(2005,3,31,23,59,59), DateTime.civil(2005,3,20,10,10,10).end_of_month assert_equal DateTime.civil(2005,2,28,23,59,59), DateTime.civil(2005,2,20,10,10,10).end_of_month assert_equal DateTime.civil(2005,4,30,23,59,59), DateTime.civil(2005,4,20,10,10,10).end_of_month end - def test_beginning_of_year - assert_equal DateTime.civil(2005,1,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_year - end - - def test_weeks_ago - assert_equal DateTime.civil(2005,5,29,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(1) - assert_equal DateTime.civil(2005,5,1,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(5) - assert_equal DateTime.civil(2005,4,24,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(6) - assert_equal DateTime.civil(2005,2,27,10), DateTime.civil(2005,6,5,10,0,0).weeks_ago(14) - assert_equal DateTime.civil(2004,12,25,10), DateTime.civil(2005,1,1,10,0,0).weeks_ago(1) - end - - def test_months_ago - assert_equal DateTime.civil(2005,5,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(1) - assert_equal DateTime.civil(2004,11,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(7) - assert_equal DateTime.civil(2004,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(6) - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(12) - assert_equal DateTime.civil(2003,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_ago(24) - end - - def test_months_since - assert_equal DateTime.civil(2005,7,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(1) - assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,12,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(6) - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,12,5,10,0,0).months_since(6) - assert_equal DateTime.civil(2006,1,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(7) - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(12) - assert_equal DateTime.civil(2007,6,5,10), DateTime.civil(2005,6,5,10,0,0).months_since(24) - assert_equal DateTime.civil(2005,4,30,10), DateTime.civil(2005,3,31,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,29,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,30,10,0,0).months_since(1) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2005,1,31,10,0,0).months_since(1) - end - - def test_years_ago - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(1) - assert_equal DateTime.civil(1998,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_ago(7) - assert_equal DateTime.civil(2003,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(1) - assert_equal DateTime.civil(2012,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(7) - assert_equal DateTime.civil(2182,6,5,10), DateTime.civil(2005,6,5,10,0,0).years_since(177) - assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day - end - - def test_prev_year - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year - end - def test_last_year assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year end - def test_next_year - assert_equal DateTime.civil(2006,6,5,10), DateTime.civil(2005,6,5,10,0,0).next_year - end - def test_ago assert_equal DateTime.civil(2005,2,22,10,10,9), DateTime.civil(2005,2,22,10,10,10).ago(1) assert_equal DateTime.civil(2005,2,22,9,10,10), DateTime.civil(2005,2,22,10,10,10).ago(3600) @@ -191,16 +103,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2005,2,22,10,10,12), DateTime.civil(2005,2,22,10,10,10).since(1.667) end - def test_yesterday - assert_equal DateTime.civil(2005,2,21,10,10,10), DateTime.civil(2005,2,22,10,10,10).yesterday - assert_equal DateTime.civil(2005,2,28,10,10,10), DateTime.civil(2005,3,2,10,10,10).yesterday.yesterday - end - - def test_tomorrow - assert_equal DateTime.civil(2005,2,23,10,10,10), DateTime.civil(2005,2,22,10,10,10).tomorrow - assert_equal DateTime.civil(2005,3,2,10,10,10), DateTime.civil(2005,2,28,10,10,10).tomorrow.tomorrow - end - def test_change assert_equal DateTime.civil(2006,2,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:year => 2006) assert_equal DateTime.civil(2005,6,22,15,15,10), DateTime.civil(2005,2,22,15,15,10).change(:month => 6) @@ -236,14 +138,6 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2010, 3, 29), DateTime.civil(2010, 2, 28, 22, 58, 59).advance(:months => 1, :hours => 1, :minutes => 1, :seconds => 1) end - def test_prev_week - assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).prev_week - assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).prev_week(:tuesday) - assert_equal DateTime.civil(2005,2,25), DateTime.civil(2005,3,1,15,15,10).prev_week(:friday) - assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,11,6,0,0,0).prev_week - assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).prev_week(:wednesday) - end - def test_last_week assert_equal DateTime.civil(2005,2,21), DateTime.civil(2005,3,1,15,15,10).last_week assert_equal DateTime.civil(2005,2,22), DateTime.civil(2005,3,1,15,15,10).last_week(:tuesday) @@ -252,33 +146,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase assert_equal DateTime.civil(2006,11,15), DateTime.civil(2006,11,23,0,0,0).last_week(:wednesday) end - def test_next_week - assert_equal DateTime.civil(2005,2,28), DateTime.civil(2005,2,22,15,15,10).next_week - assert_equal DateTime.civil(2005,3,4), DateTime.civil(2005,2,22,15,15,10).next_week(:friday) - assert_equal DateTime.civil(2006,10,30), DateTime.civil(2006,10,23,0,0,0).next_week - assert_equal DateTime.civil(2006,11,1), DateTime.civil(2006,10,23,0,0,0).next_week(:wednesday) - end - - def test_next_month_on_31st - assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month end - def test_next_quarter_on_31st - assert_equal DateTime.civil(2005, 11, 30), DateTime.civil(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 5, 31).last_quarter end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 412aef9301..6d6757a1b6 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -1,7 +1,14 @@ require 'abstract_unit' require 'active_support/time' +require 'core_ext/date_and_time_behavior' class TimeExtCalculationsTest < ActiveSupport::TestCase + def date_time_init(year,month,day,hour,minute,second,usec=0) + Time.local(year,month,day,hour,minute,second,usec) + end + + include DateAndTimeBehavior + def test_seconds_since_midnight assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight assert_equal 60,Time.local(2005,1,1,0,1,0).seconds_since_midnight @@ -50,37 +57,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_beginning_of_week - assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week - assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday - assert_equal Time.local(2005,11,28), Time.local(2005,11,29,0,0,0).beginning_of_week #tuesday - assert_equal Time.local(2005,11,28), Time.local(2005,11,30,0,0,0).beginning_of_week #wednesday - assert_equal Time.local(2005,11,28), Time.local(2005,12,01,0,0,0).beginning_of_week #thursday - assert_equal Time.local(2005,11,28), Time.local(2005,12,02,0,0,0).beginning_of_week #friday - assert_equal Time.local(2005,11,28), Time.local(2005,12,03,0,0,0).beginning_of_week #saturday - assert_equal Time.local(2005,11,28), Time.local(2005,12,04,0,0,0).beginning_of_week #sunday - - end - - def test_days_to_week_start - assert_equal 0, Time.local(2011,11,01,0,0,0).days_to_week_start(:tuesday) - assert_equal 1, Time.local(2011,11,02,0,0,0).days_to_week_start(:tuesday) - assert_equal 2, Time.local(2011,11,03,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 4, Time.local(2011,11,05,0,0,0).days_to_week_start(:tuesday) - assert_equal 5, Time.local(2011,11,06,0,0,0).days_to_week_start(:tuesday) - assert_equal 6, Time.local(2011,11,07,0,0,0).days_to_week_start(:tuesday) - - assert_equal 3, Time.local(2011,11,03,0,0,0).days_to_week_start(:monday) - assert_equal 3, Time.local(2011,11,04,0,0,0).days_to_week_start(:tuesday) - assert_equal 3, Time.local(2011,11,05,0,0,0).days_to_week_start(:wednesday) - assert_equal 3, Time.local(2011,11,06,0,0,0).days_to_week_start(:thursday) - assert_equal 3, Time.local(2011,11,07,0,0,0).days_to_week_start(:friday) - assert_equal 3, Time.local(2011,11,8,0,0,0).days_to_week_start(:saturday) - assert_equal 3, Time.local(2011,11,9,0,0,0).days_to_week_start(:sunday) - end - - def test_beginning_of_day assert_equal Time.local(2005,2,4,0,0,0), Time.local(2005,2,4,10,10,10).beginning_of_day with_env_tz 'US/Eastern' do @@ -97,17 +73,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour end - def test_beginning_of_month - assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month - end - - def test_beginning_of_quarter - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,15,10,10,10).beginning_of_quarter - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,1,1,0,0,0).beginning_of_quarter - assert_equal Time.local(2005,10,1,0,0,0), Time.local(2005,12,31,10,10,10).beginning_of_quarter - assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter - end - def test_end_of_day assert_equal Time.local(2007,8,12,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,12,10,10,10).end_of_day with_env_tz 'US/Eastern' do @@ -120,100 +85,14 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_end_of_week - assert_equal Time.local(2008,1,6,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_week - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,27,0,0,0).end_of_week #monday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,28,0,0,0).end_of_week #tuesday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,29,0,0,0).end_of_week #wednesday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,30,0,0,0).end_of_week #thursday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,8,31,0,0,0).end_of_week #friday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,01,0,0,0).end_of_week #saturday - assert_equal Time.local(2007,9,2,23,59,59,Rational(999999999, 1000)), Time.local(2007,9,02,0,0,0).end_of_week #sunday - end - def test_end_of_hour assert_equal Time.local(2005,2,4,19,59,59,Rational(999999999, 1000)), Time.local(2005,2,4,19,30,10).end_of_hour end - def test_end_of_month - assert_equal Time.local(2005,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2005,3,20,10,10,10).end_of_month - assert_equal Time.local(2005,2,28,23,59,59,Rational(999999999, 1000)), Time.local(2005,2,20,10,10,10).end_of_month - assert_equal Time.local(2005,4,30,23,59,59,Rational(999999999, 1000)), Time.local(2005,4,20,10,10,10).end_of_month - end - - def test_end_of_quarter - assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,15,10,10,10).end_of_quarter - assert_equal Time.local(2007,3,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,3,31,0,0,0).end_of_quarter - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,21,10,10,10).end_of_quarter - assert_equal Time.local(2007,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2007,4,1,0,0,0).end_of_quarter - assert_equal Time.local(2008,6,30,23,59,59,Rational(999999999, 1000)), Time.local(2008,5,31,0,0,0).end_of_quarter - end - - def test_end_of_year - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,2,22,10,10,10).end_of_year - assert_equal Time.local(2007,12,31,23,59,59,Rational(999999999, 1000)), Time.local(2007,12,31,10,10,10).end_of_year - end - - def test_beginning_of_year - assert_equal Time.local(2005,1,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_year - end - - def test_weeks_ago - assert_equal Time.local(2005,5,29,10), Time.local(2005,6,5,10,0,0).weeks_ago(1) - assert_equal Time.local(2005,5,1,10), Time.local(2005,6,5,10,0,0).weeks_ago(5) - assert_equal Time.local(2005,4,24,10), Time.local(2005,6,5,10,0,0).weeks_ago(6) - assert_equal Time.local(2005,2,27,10), Time.local(2005,6,5,10,0,0).weeks_ago(14) - assert_equal Time.local(2004,12,25,10), Time.local(2005,1,1,10,0,0).weeks_ago(1) - end - - def test_months_ago - assert_equal Time.local(2005,5,5,10), Time.local(2005,6,5,10,0,0).months_ago(1) - assert_equal Time.local(2004,11,5,10), Time.local(2005,6,5,10,0,0).months_ago(7) - assert_equal Time.local(2004,12,5,10), Time.local(2005,6,5,10,0,0).months_ago(6) - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(12) - assert_equal Time.local(2003,6,5,10), Time.local(2005,6,5,10,0,0).months_ago(24) - end - - def test_months_since - assert_equal Time.local(2005,7,5,10), Time.local(2005,6,5,10,0,0).months_since(1) - assert_equal Time.local(2006,1,5,10), Time.local(2005,12,5,10,0,0).months_since(1) - assert_equal Time.local(2005,12,5,10), Time.local(2005,6,5,10,0,0).months_since(6) - assert_equal Time.local(2006,6,5,10), Time.local(2005,12,5,10,0,0).months_since(6) - assert_equal Time.local(2006,1,5,10), Time.local(2005,6,5,10,0,0).months_since(7) - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).months_since(12) - assert_equal Time.local(2007,6,5,10), Time.local(2005,6,5,10,0,0).months_since(24) - assert_equal Time.local(2005,4,30,10), Time.local(2005,3,31,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,29,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,30,10,0,0).months_since(1) - assert_equal Time.local(2005,2,28,10), Time.local(2005,1,31,10,0,0).months_since(1) - end - - def test_years_ago - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(1) - assert_equal Time.local(1998,6,5,10), Time.local(2005,6,5,10,0,0).years_ago(7) - assert_equal Time.local(2003,2,28,10), Time.local(2004,2,29,10,0,0).years_ago(1) # 1 year ago from leap day - end - - def test_years_since - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).years_since(1) - assert_equal Time.local(2012,6,5,10), Time.local(2005,6,5,10,0,0).years_since(7) - assert_equal Time.local(2005,2,28,10), Time.local(2004,2,29,10,0,0).years_since(1) # 1 year since leap day - # Failure because of size limitations of numeric? - # assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177) - end - - def test_prev_year - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year - end - def test_last_year assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year end - def test_next_year - assert_equal Time.local(2006,6,5,10), Time.local(2005,6,5,10,0,0).next_year - end - def test_ago assert_equal Time.local(2005,2,22,10,10,9), Time.local(2005,2,22,10,10,10).ago(1) assert_equal Time.local(2005,2,22,9,10,10), Time.local(2005,2,22,10,10,10).ago(3600) @@ -426,16 +305,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_yesterday - assert_equal Time.local(2005,2,21,10,10,10), Time.local(2005,2,22,10,10,10).yesterday - assert_equal Time.local(2005,2,28,10,10,10), Time.local(2005,3,2,10,10,10).yesterday.yesterday - end - - def test_tomorrow - assert_equal Time.local(2005,2,23,10,10,10), Time.local(2005,2,22,10,10,10).tomorrow - assert_equal Time.local(2005,3,2,10,10,10), Time.local(2005,2,28,10,10,10).tomorrow.tomorrow - end - def test_change assert_equal Time.local(2006,2,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:year => 2006) assert_equal Time.local(2005,6,22,15,15,10), Time.local(2005,2,22,15,15,10).change(:month => 6) @@ -539,16 +408,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase assert_equal t, t.advance(:months => 0) end - def test_prev_week - with_env_tz 'US/Eastern' do - assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).prev_week - assert_equal Time.local(2005,2,22), Time.local(2005,3,1,15,15,10).prev_week(:tuesday) - assert_equal Time.local(2005,2,25), Time.local(2005,3,1,15,15,10).prev_week(:friday) - assert_equal Time.local(2006,10,30), Time.local(2006,11,6,0,0,0).prev_week - assert_equal Time.local(2006,11,15), Time.local(2006,11,23,0,0,0).prev_week(:wednesday) - end - end - def test_last_week with_env_tz 'US/Eastern' do assert_equal Time.local(2005,2,21), Time.local(2005,3,1,15,15,10).last_week @@ -559,16 +418,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_next_week - with_env_tz 'US/Eastern' do - assert_equal Time.local(2005,2,28), Time.local(2005,2,22,15,15,10).next_week - assert_equal Time.local(2005,3,1), Time.local(2005,2,22,15,15,10).next_week(:tuesday) - assert_equal Time.local(2005,3,4), Time.local(2005,2,22,15,15,10).next_week(:friday) - assert_equal Time.local(2006,10,30), Time.local(2006,10,23,0,0,0).next_week - assert_equal Time.local(2006,11,1), Time.local(2006,10,23,0,0,0).next_week(:wednesday) - end - end - def test_next_week_near_daylight_start with_env_tz 'US/Eastern' do assert_equal Time.local(2006,4,3), Time.local(2006,4,2,23,1,0).next_week, 'just crossed standard => daylight' @@ -709,14 +558,6 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase end end - def test_next_month_on_31st - assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month - end - - def test_prev_month_on_31st - assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month - end - def test_last_month_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month end @@ -938,15 +779,6 @@ class TimeExtMarshalingTest < ActiveSupport::TestCase assert_equal t, unmarshaled end - - def test_next_quarter_on_31st - assert_equal Time.local(2005, 11, 30), Time.local(2005, 8, 31).next_quarter - end - - def test_prev_quarter_on_31st - assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).prev_quarter - end - def test_last_quarter_on_31st assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter end