aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/time/calculations.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/core_ext/time/calculations.rb')
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index ad260f809b..867ac86617 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -154,6 +154,13 @@ module ActiveSupport #:nodoc:
alias :monday :beginning_of_week
alias :at_beginning_of_week :beginning_of_week
+ # Returns a new Time representing the end of this week (Sunday, 23:59:59)
+ def end_of_week
+ days_to_sunday = self.wday!=0 ? 7-self.wday : 0
+ (self + days_to_sunday.days).end_of_day
+ end
+ alias :at_end_of_week :end_of_week
+
# Returns a new Time representing the start of the given day in next week (default is Monday).
def next_week(day = :monday)
days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
@@ -194,12 +201,24 @@ module ActiveSupport #:nodoc:
end
alias :at_beginning_of_quarter :beginning_of_quarter
+ # Returns a new Time representing the end of the quarter (last day of march, june, september, december, 23:59:59)
+ def end_of_quarter
+ change(:month => [3, 6, 9, 12].detect { |m| m >= self.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, :min => 0, :sec => 0, :usec => 0)
end
alias :at_beginning_of_year :beginning_of_year
+ # Returns a new Time representing the end of the year (31st of december, 23:59:59)
+ def end_of_year
+ change(:month => 12,:day => 31,:hour => 23, :min => 59, :sec => 59)
+ end
+ alias :at_end_of_year :end_of_year
+
# Convenience method which returns a new Time representing the time 1 day ago
def yesterday
self.ago(1.day)