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

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



57
58
59
# File 'lib/timing/helpers.rb', line 57

def months_after(time, count)
  TimeInZone.new (time.to_datetime >> count).to_time, time.utc_offset
end

#months_ago(time, count) ⇒ Object



53
54
55
# File 'lib/timing/helpers.rb', line 53

def months_ago(time, count)
  TimeInZone.new (time.to_datetime << count).to_time, time.utc_offset
end

#years_after(time, count) ⇒ Object



65
66
67
# File 'lib/timing/helpers.rb', line 65

def years_after(time, count)
  months_after time, count * 12
end

#years_ago(time, count) ⇒ Object



61
62
63
# File 'lib/timing/helpers.rb', line 61

def years_ago(time, count)
  months_ago time, count * 12
end