Module ActiveSupport::CoreExtensions::Date::Calculations
In: vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb

Enables the use of time calculations within Time itself

Methods

Classes and Modules

Module ActiveSupport::CoreExtensions::Date::Calculations::ClassMethods

Public Instance methods

Provides precise Date calculations for years, months, and days. The options parameter takes a hash with any of these keys: :years, :months, :weeks, :days.

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 72
72:         def advance(options)
73:           d = self
74:           d = d >> options.delete(:years) * 12 if options[:years]
75:           d = d >> options.delete(:months)     if options[:months]
76:           d = d +  options.delete(:weeks) * 7  if options[:weeks]
77:           d = d +  options.delete(:days)       if options[:days]
78:           d
79:         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

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 30
30:         def ago(seconds)
31:           to_time.since(-seconds)
32:         end
at_beginning_of_day()

Alias for beginning_of_day

at_beginning_of_month()

Alias for beginning_of_month

at_beginning_of_quarter()
at_beginning_of_week()

Alias for beginning_of_week

at_beginning_of_year()

Alias for beginning_of_year

at_end_of_month()

Alias for end_of_month

at_midnight()

Alias for beginning_of_day

Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00)

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 42
42:         def beginning_of_day
43:           to_time
44:         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)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 152
152:         def beginning_of_month
153:           self.acts_like?(:time) ? change(:day => 1,:hour => 0, :min => 0, :sec => 0) : change(:day => 1)
154:         end

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)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 165
165:         def beginning_of_quarter
166:           beginning_of_month.change(:month => [10, 7, 4, 1].detect { |m| m <= self.month })
167:         end

Returns a new Date/DateTime representing the "start" of this week (i.e, Monday; DateTime objects will have time set to 0:00)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 136
136:         def beginning_of_week
137:           days_to_monday = self.wday!=0 ? self.wday-1 : 6
138:           result = self - days_to_monday
139:           self.acts_like?(:time) ? result.midnight : result
140:         end

Returns a new Date/DateTime representing the start of the year (1st of january; DateTime objects will have time set to 0:00)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 171
171:         def beginning_of_year
172:           self.acts_like?(:time) ? change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0) : change(:month => 1, :day => 1)
173:         end

Returns a new Date where one or more of the elements have been changed according to the options parameter.

Examples:

  Date.new(2007, 5, 12).change(:day => 1)                  # => Date.new(2007, 5, 1)
  Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12)

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 87
87:         def change(options)
88:           ::Date.new(
89:             options[:year]  || self.year,
90:             options[:month] || self.month,
91:             options[:day]   || self.day
92:           )
93:         end

Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 50
50:         def end_of_day
51:           to_time.end_of_day
52:         end

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)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 158
158:         def end_of_month
159:           last_day = ::Time.days_in_month( self.month, self.year )
160:           self.acts_like?(:time) ? change(:day => last_day, :hour => 23, :min => 59, :sec => 59) : change(:day => last_day)
161:         end
in(seconds)

Alias for since

Short-hand for months_ago(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 126
126:         def last_month
127:           months_ago(1)
128:         end

Short-hand for years_ago(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 116
116:         def last_year
117:           years_ago(1)
118:         end
midnight()

Alias for beginning_of_day

monday()

Alias for beginning_of_week

Returns a new Date/DateTime representing the time a number of specified months ago

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 96
96:         def months_ago(months)
97:           advance(:months => -months)
98:         end

Returns a new Date/DateTime representing the time a number of specified months in the future

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 101
101:         def months_since(months)
102:           advance(:months => months)
103:         end

Short-hand for months_since(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 131
131:         def next_month
132:           months_since(1)
133:         end

Returns a new Date/DateTime representing the start of the given day in next week (default is Monday).

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 145
145:         def next_week(day = :monday)
146:           days_into_week = { :monday => 0, :tuesday => 1, :wednesday => 2, :thursday => 3, :friday => 4, :saturday => 5, :sunday => 6}
147:           result = (self + 7).beginning_of_week + days_into_week[day]
148:           self.acts_like?(:time) ? result.change(:hour => 0) : result
149:         end

Short-hand for years_since(1)

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 121
121:         def next_year
122:           years_since(1)
123:         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 adds the specified number of seconds

[Source]

    # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 36
36:         def since(seconds)
37:           to_time.since(seconds)
38:         end

Convenience method which returns a new Date/DateTime representing the time 1 day since the instance time

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 182
182:         def tomorrow
183:           self + 1
184:         end

Returns a new Date/DateTime representing the time a number of specified years ago

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 106
106:         def years_ago(years)
107:           advance(:years => -years)
108:         end

Returns a new Date/DateTime representing the time a number of specified years in the future

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 111
111:         def years_since(years)
112:           advance(:years => years)
113:         end

Convenience method which returns a new Date/DateTime representing the time 1 day ago

[Source]

     # File vendor/rails/activesupport/lib/active_support/core_ext/date/calculations.rb, line 177
177:         def yesterday
178:           self - 1
179:         end

[Validate]