Module: TimeExt::Calculations
- Included in:
- Time
- Defined in:
- lib/time_ext/calculations.rb
Overview
Adds an even greater extent of calculation methods on top of those already provided by ActiveSupport.
Instance Method Summary collapse
-
#beginning_of_hour ⇒ Object
(also: #at_beginning_of_hour)
Returns a new Time representing the start of the hour (XX:00:00).
-
#beginning_of_minute ⇒ Object
(also: #beginning_of_min, #at_beginning_of_min, #at_beginning_of_minute)
Returns a new Time representing the start of the minute (XX:XX:00).
-
#beginning_of_second ⇒ Object
(also: #beginning_of_sec, #at_beginning_of_sec, #at_beginning_of_second)
Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9).
-
#ceil(unit = :sec) ⇒ Object
(also: #beginning_of_next, #at_beginning_of_next)
Returns a new Time representing the start of the next unit specified (second by default).
-
#days_ago(days) ⇒ Object
Returns a new Time representing the time a number of specified days ago.
-
#days_since(days) ⇒ Object
Returns a new Time representing the time a number of specified days in the future.
-
#end_of(unit = :sec) ⇒ Object
Returns a new Time representing the end of the unit specified (defaults to second).
-
#end_of_hour ⇒ Object
Returns a new Time representing the end of the hour, XX:59:59.999999 (.999999999 in ruby1.9).
-
#end_of_minute ⇒ Object
(also: #end_of_min)
Returns a new Time representing the end of the hour, XX:XX:59.999999 (.999999999 in ruby1.9).
-
#end_of_second ⇒ Object
(also: #end_of_sec)
Returns a new Time representing the end of the hour, XX:XX:XX.999999 (.999999999 in ruby1.9).
-
#floor(unit = :sec) ⇒ Object
(also: #beginning_of, #at_beginning_of)
Returns a new Time representing the start of the unit specified (second by default).
-
#hours_ago(hours) ⇒ Object
Returns a new Time representing the time a number of specified hours ago.
-
#hours_since(hours) ⇒ Object
Returns a new Time representing the time a number of specified hours in the future.
-
#minutes_ago(minutes) ⇒ Object
(also: #mins_ago)
Returns a new Time representing the time a number of specified minutes ago.
-
#minutes_since(minutes) ⇒ Object
(also: #mins_since)
Returns a new Time representing the time a number of specified minutes in the future.
-
#next(unit = :sec) ⇒ Object
Returns a new Time representing the next unit specified (defaults to second).
-
#next_day ⇒ Object
Short-hand for days_since(1).
-
#next_hour ⇒ Object
Short-hand for hours_since(1).
-
#next_minute ⇒ Object
(also: #next_min)
Short-hand for minutes_since(1).
-
#next_quarter ⇒ Object
Short-hand for quarters_since(1).beginning_of_quarter.
-
#next_second ⇒ Object
(also: #next_sec)
Short-hand for seconds_since(1).
-
#prev(unit = :sec) ⇒ Object
Returns a new Time representing the previoius unit specified (defaults to second).
-
#prev_day ⇒ Object
Short-hand for days_ago(1).
-
#prev_hour ⇒ Object
Short-hand for hours_ago(1).
-
#prev_minute ⇒ Object
(also: #prev_min)
Short-hand for minutes_ago(1).
-
#prev_quarter ⇒ Object
Short-hand for quarters_ago(1).beginning_of_quarter.
-
#prev_second ⇒ Object
(also: #prev_sec)
Short-hand for seconds_ago(1).
-
#prev_week(day = :monday) ⇒ Object
Returns a new Time representing the start of the given day in the previous week (default is Monday).
-
#quarters_ago(quarters) ⇒ Object
Returns a new Time representing the time a number of specified quarters (3 months) ago.
-
#quarters_since(quarters) ⇒ Object
Returns a new Time representing the time a number of specified quarters (3 months) in the future.
-
#round(unit = :sec) ⇒ Object
(also: #beginning_of_closest)
Returns a new Time representing the start of the current or next unit specified (second by default) depending which is closest.
-
#weeks_ago(weeks) ⇒ Object
Returns a new Time representing the time a number of specified weeks ago.
-
#weeks_since(weeks) ⇒ Object
Returns a new Time representing the time a number of specified weeks in the future.
Instance Method Details
#beginning_of_hour ⇒ Object Also known as: at_beginning_of_hour
Returns a new Time representing the start of the hour (XX:00:00).
182 183 184 |
# File 'lib/time_ext/calculations.rb', line 182 def beginning_of_hour change(:min => 0, :sec => 0, :usec => 0) end |
#beginning_of_minute ⇒ Object Also known as: beginning_of_min, at_beginning_of_min, at_beginning_of_minute
Returns a new Time representing the start of the minute (XX:XX:00).
168 169 170 |
# File 'lib/time_ext/calculations.rb', line 168 def beginning_of_minute change(:sec => 0, :usec => 0) end |
#beginning_of_second ⇒ Object Also known as: beginning_of_sec, at_beginning_of_sec, at_beginning_of_second
Returns a new Time representing the start of the second, XX:XX:XX.000000 (.000000000 in ruby1.9).
154 155 156 |
# File 'lib/time_ext/calculations.rb', line 154 def beginning_of_second change(:usec => 0) end |
#ceil(unit = :sec) ⇒ Object Also known as: beginning_of_next, at_beginning_of_next
Returns a new Time representing the start of the next unit specified (second by default).
13 14 15 |
# File 'lib/time_ext/calculations.rb', line 13 def ceil(unit = :sec) self.send("next_#{unit}").send("beginning_of_#{unit}") end |
#days_ago(days) ⇒ Object
Returns a new Time representing the time a number of specified days ago.
119 120 121 |
# File 'lib/time_ext/calculations.rb', line 119 def days_ago(days) ago(days.days) end |
#days_since(days) ⇒ Object
Returns a new Time representing the time a number of specified days in the future.
124 125 126 |
# File 'lib/time_ext/calculations.rb', line 124 def days_since(days) since(days.days) end |
#end_of(unit = :sec) ⇒ Object
Returns a new Time representing the end of the unit specified (defaults to second).
149 150 151 |
# File 'lib/time_ext/calculations.rb', line 149 def end_of(unit = :sec) send("end_of_#{unit}") end |
#end_of_hour ⇒ Object
Returns a new Time representing the end of the hour, XX:59:59.999999 (.999999999 in ruby1.9).
188 189 190 |
# File 'lib/time_ext/calculations.rb', line 188 def end_of_hour change(:min => 59, :sec => 59, :usec => 999999.999) end |
#end_of_minute ⇒ Object Also known as: end_of_min
Returns a new Time representing the end of the hour, XX:XX:59.999999 (.999999999 in ruby1.9).
176 177 178 |
# File 'lib/time_ext/calculations.rb', line 176 def end_of_minute change(:sec => 59, :usec => 999999.999) end |
#end_of_second ⇒ Object Also known as: end_of_sec
Returns a new Time representing the end of the hour, XX:XX:XX.999999 (.999999999 in ruby1.9).
162 163 164 |
# File 'lib/time_ext/calculations.rb', line 162 def end_of_second change(:usec => 999999.999) end |
#floor(unit = :sec) ⇒ Object Also known as: beginning_of, at_beginning_of
Returns a new Time representing the start of the unit specified (second by default).
6 7 8 |
# File 'lib/time_ext/calculations.rb', line 6 def floor(unit = :sec) self.send("beginning_of_#{unit}") end |
#hours_ago(hours) ⇒ Object
Returns a new Time representing the time a number of specified hours ago.
109 110 111 |
# File 'lib/time_ext/calculations.rb', line 109 def hours_ago(hours) ago(hours.hours) end |
#hours_since(hours) ⇒ Object
Returns a new Time representing the time a number of specified hours in the future.
114 115 116 |
# File 'lib/time_ext/calculations.rb', line 114 def hours_since(hours) since(hours.hours) end |
#minutes_ago(minutes) ⇒ Object Also known as: mins_ago
Returns a new Time representing the time a number of specified minutes ago.
97 98 99 |
# File 'lib/time_ext/calculations.rb', line 97 def minutes_ago(minutes) ago(minutes.minutes) end |
#minutes_since(minutes) ⇒ Object Also known as: mins_since
Returns a new Time representing the time a number of specified minutes in the future.
103 104 105 |
# File 'lib/time_ext/calculations.rb', line 103 def minutes_since(minutes) since(minutes.minutes) end |
#next(unit = :sec) ⇒ Object
Returns a new Time representing the next unit specified (defaults to second).
33 34 35 |
# File 'lib/time_ext/calculations.rb', line 33 def next(unit = :sec) send("next_#{unit}") end |
#next_day ⇒ Object
Short-hand for days_since(1).
77 78 79 |
# File 'lib/time_ext/calculations.rb', line 77 def next_day days_since(1) end |
#next_hour ⇒ Object
Short-hand for hours_since(1).
67 68 69 |
# File 'lib/time_ext/calculations.rb', line 67 def next_hour hours_since(1) end |
#next_minute ⇒ Object Also known as: next_min
Short-hand for minutes_since(1).
56 57 58 |
# File 'lib/time_ext/calculations.rb', line 56 def next_minute minutes_since(1) end |
#next_quarter ⇒ Object
Short-hand for quarters_since(1).beginning_of_quarter.
87 88 89 |
# File 'lib/time_ext/calculations.rb', line 87 def next_quarter quarters_since(1).beginning_of_quarter end |
#next_second ⇒ Object Also known as: next_sec
Short-hand for seconds_since(1).
44 45 46 |
# File 'lib/time_ext/calculations.rb', line 44 def next_second seconds_since(1) end |
#prev(unit = :sec) ⇒ Object
Returns a new Time representing the previoius unit specified (defaults to second).
28 29 30 |
# File 'lib/time_ext/calculations.rb', line 28 def prev(unit = :sec) send("prev_#{unit}") end |
#prev_day ⇒ Object
Short-hand for days_ago(1).
72 73 74 |
# File 'lib/time_ext/calculations.rb', line 72 def prev_day days_ago(1) end |
#prev_hour ⇒ Object
Short-hand for hours_ago(1).
62 63 64 |
# File 'lib/time_ext/calculations.rb', line 62 def prev_hour hours_ago(1) end |
#prev_minute ⇒ Object Also known as: prev_min
Short-hand for minutes_ago(1).
50 51 52 |
# File 'lib/time_ext/calculations.rb', line 50 def prev_minute minutes_ago(1) end |
#prev_quarter ⇒ Object
Short-hand for quarters_ago(1).beginning_of_quarter.
92 93 94 |
# File 'lib/time_ext/calculations.rb', line 92 def prev_quarter quarters_ago(1).beginning_of_quarter end |
#prev_second ⇒ Object Also known as: prev_sec
Short-hand for seconds_ago(1).
38 39 40 |
# File 'lib/time_ext/calculations.rb', line 38 def prev_second seconds_ago(1) end |
#prev_week(day = :monday) ⇒ Object
Returns a new Time representing the start of the given day in the previous week (default is Monday).
82 83 84 |
# File 'lib/time_ext/calculations.rb', line 82 def prev_week(day = :monday) weeks_ago(1).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) end |
#quarters_ago(quarters) ⇒ Object
Returns a new Time representing the time a number of specified quarters (3 months) ago.
139 140 141 |
# File 'lib/time_ext/calculations.rb', line 139 def quarters_ago(quarters) ago((quarters * 3).months) end |
#quarters_since(quarters) ⇒ Object
Returns a new Time representing the time a number of specified quarters (3 months) in the future.
144 145 146 |
# File 'lib/time_ext/calculations.rb', line 144 def quarters_since(quarters) since((quarters * 3).months) end |
#round(unit = :sec) ⇒ Object Also known as: beginning_of_closest
Returns a new Time representing the start of the current or next unit specified (second by default) depending which is closest
20 21 22 23 24 |
# File 'lib/time_ext/calculations.rb', line 20 def round(unit = :sec) next_unit = self.ceil(unit) this_unit = self.floor(unit) (self - this_unit) < (next_unit - self) ? this_unit : next_unit end |
#weeks_ago(weeks) ⇒ Object
Returns a new Time representing the time a number of specified weeks ago.
129 130 131 |
# File 'lib/time_ext/calculations.rb', line 129 def weeks_ago(weeks) ago(weeks.weeks) end |
#weeks_since(weeks) ⇒ Object
Returns a new Time representing the time a number of specified weeks in the future.
134 135 136 |
# File 'lib/time_ext/calculations.rb', line 134 def weeks_since(weeks) since(weeks.weeks) end |