Module: Timing::Helpers
- Included in:
- Timing
- Defined in:
- lib/timing/helpers.rb
Constant Summary collapse
- MONTH_DAYS =
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Instance Method Summary collapse
- #beginning_of_day(time) ⇒ Object
- #beginning_of_hour(time) ⇒ Object
- #beginning_of_month(time) ⇒ Object
- #beginning_of_week(time) ⇒ Object
- #beginning_of_year(time) ⇒ Object
- #days_in_month(month, year = nil) ⇒ Object
- #end_of_day(time) ⇒ Object
- #end_of_hour(time) ⇒ Object
- #end_of_month(time) ⇒ Object
- #end_of_week(time) ⇒ Object
- #end_of_year(time) ⇒ Object
- #months_after(time, count) ⇒ Object
- #months_ago(time, count) ⇒ Object
- #years_after(time, count) ⇒ Object
- #years_ago(time, count) ⇒ Object
Instance Method Details
#beginning_of_day(time) ⇒ Object
14 15 16 |
# File 'lib/timing/helpers.rb', line 14 def beginning_of_day(time) TimeInZone.parse time.strftime('%Y-%m-%d 00:00:00 %z') end |
#beginning_of_hour(time) ⇒ Object
6 7 8 |
# File 'lib/timing/helpers.rb', line 6 def beginning_of_hour(time) TimeInZone.parse time.strftime('%Y-%m-%d %H:00:00 %z') end |
#beginning_of_month(time) ⇒ Object
32 33 34 |
# File 'lib/timing/helpers.rb', line 32 def beginning_of_month(time) TimeInZone.parse time.strftime('%Y-%m-01 00:00:00 %z') end |
#beginning_of_week(time) ⇒ Object
22 23 24 25 |
# File 'lib/timing/helpers.rb', line 22 def beginning_of_week(time) date = beginning_of_day time date - Interval.days(date.wday) end |
#beginning_of_year(time) ⇒ Object
40 41 42 |
# File 'lib/timing/helpers.rb', line 40 def beginning_of_year(time) TimeInZone.parse time.strftime('%Y-01-01 00:00:00 %z') end |
#days_in_month(month, year = nil) ⇒ Object
48 49 50 51 |
# File 'lib/timing/helpers.rb', line 48 def days_in_month(month, year=nil) return 29 if month == 2 && Date.leap?(year || TimeInZone.now.year) MONTH_DAYS[month-1] end |
#end_of_day(time) ⇒ Object
18 19 20 |
# File 'lib/timing/helpers.rb', line 18 def end_of_day(time) TimeInZone.parse time.strftime('%Y-%m-%d 23:59:59 %z') end |
#end_of_hour(time) ⇒ Object
10 11 12 |
# File 'lib/timing/helpers.rb', line 10 def end_of_hour(time) TimeInZone.parse time.strftime('%Y-%m-%d %H:59:59 %z') end |
#end_of_month(time) ⇒ Object
36 37 38 |
# File 'lib/timing/helpers.rb', line 36 def end_of_month(time) TimeInZone.parse time.strftime("%Y-%m-#{days_in_month(time.month, time.year)} 23:59:59 %z") end |
#end_of_week(time) ⇒ Object
27 28 29 30 |
# File 'lib/timing/helpers.rb', line 27 def end_of_week(time) date = end_of_day time date + Interval.days(6 - date.wday) end |
#end_of_year(time) ⇒ Object
44 45 46 |
# File 'lib/timing/helpers.rb', line 44 def end_of_year(time) TimeInZone.parse time.strftime('%Y-12-31 23:59:59 %z') end |
#months_after(time, count) ⇒ Object
58 59 60 61 |
# File 'lib/timing/helpers.rb', line 58 def months_after(time, count) diff = ((time.to_datetime >> count) - time.to_datetime).to_f TimeInZone.new time + Interval.days(diff), time.utc_offset end |
#months_ago(time, count) ⇒ Object
53 54 55 56 |
# File 'lib/timing/helpers.rb', line 53 def months_ago(time, count) diff = (time.to_datetime - (time.to_datetime << count)).to_f TimeInZone.new time - Interval.days(diff), time.utc_offset end |
#years_after(time, count) ⇒ Object
67 68 69 |
# File 'lib/timing/helpers.rb', line 67 def years_after(time, count) months_after time, count * 12 end |
#years_ago(time, count) ⇒ Object
63 64 65 |
# File 'lib/timing/helpers.rb', line 63 def years_ago(time, count) months_ago time, count * 12 end |