Skip to content

Commit a443c5f

Browse files
committed
Merge pull request rails#20839 from TheBlasfem/added_examples_dateandtime_calculations
Added examples to DateAndTime::Calculations [ci skip]
2 parents 3f1c5d3 + 2849f69 commit a443c5f

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

activesupport/lib/active_support/core_ext/date_and_time/calculations.rb

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,33 +92,58 @@ def years_since(years)
9292
end
9393

9494
# Returns a new date/time at the start of the month.
95-
# DateTime objects will have a time set to 0:00.
95+
#
96+
# today = Date.today # => Thu, 18 Jun 2015
97+
# today.beginning_of_month # => Mon, 01 Jun 2015
98+
#
99+
# +DateTime+ objects will have a time set to 0:00.
100+
#
101+
# now = DateTime.current # => Thu, 18 Jun 2015 15:23:13 +0000
102+
# now.beginning_of_month # => Mon, 01 Jun 2015 00:00:00 +0000
96103
def beginning_of_month
97104
first_hour(change(:day => 1))
98105
end
99106
alias :at_beginning_of_month :beginning_of_month
100107

101108
# Returns a new date/time at the start of the quarter.
102-
# Example: 1st January, 1st July, 1st October.
103-
# DateTime objects will have a time set to 0:00.
109+
#
110+
# today = Date.today # => Fri, 10 Jul 2015
111+
# today.beginning_of_quarter # => Wed, 01 Jul 2015
112+
#
113+
# +DateTime+ objects will have a time set to 0:00.
114+
#
115+
# now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
116+
# now.beginning_of_quarter # => Wed, 01 Jul 2015 00:00:00 +0000
104117
def beginning_of_quarter
105118
first_quarter_month = [10, 7, 4, 1].detect { |m| m <= month }
106119
beginning_of_month.change(:month => first_quarter_month)
107120
end
108121
alias :at_beginning_of_quarter :beginning_of_quarter
109122

110123
# Returns a new date/time at the end of the quarter.
111-
# Example: 31st March, 30th June, 30th September.
112-
# DateTime objects will have a time set to 23:59:59.
124+
#
125+
# today = Date.today # => Fri, 10 Jul 2015
126+
# today.end_of_quarter # => Wed, 30 Sep 2015
127+
#
128+
# +DateTime+ objects will have a time set to 23:59:59.
129+
#
130+
# now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
131+
# now.end_of_quarter # => Wed, 30 Sep 2015 23:59:59 +0000
113132
def end_of_quarter
114133
last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month }
115134
beginning_of_month.change(:month => last_quarter_month).end_of_month
116135
end
117136
alias :at_end_of_quarter :end_of_quarter
118137

119138
# Return a new date/time at the beginning of the year.
120-
# Example: 1st January.
121-
# DateTime objects will have a time set to 0:00.
139+
#
140+
# today = Date.today # => Fri, 10 Jul 2015
141+
# today.beginning_of_year # => Thu, 01 Jan 2015
142+
#
143+
# +DateTime+ objects will have a time set to 0:00.
144+
#
145+
# now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
146+
# now.beginning_of_year # => Thu, 01 Jan 2015 00:00:00 +0000
122147
def beginning_of_year
123148
change(:month => 1).beginning_of_month
124149
end
@@ -138,8 +163,8 @@ def beginning_of_year
138163
#
139164
# +DateTime+ objects have their time set to 0:00 unless +same_time+ is true.
140165
#
141-
# now = Time.current # => Thu, 07 May 2015 13:31:16 UTC +00:00
142-
# now.next_week # => Mon, 11 May 2015 00:00:00 UTC +00:00
166+
# now = DateTime.current # => Thu, 07 May 2015 13:31:16 +0000
167+
# now.next_week # => Mon, 11 May 2015 00:00:00 +0000
143168
def next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false)
144169
result = first_hour(weeks_since(1).beginning_of_week.days_since(days_span(given_day_in_next_week)))
145170
same_time ? copy_time_to(result) : result

0 commit comments

Comments
 (0)