module Timing::Helpers

Constants

MONTH_DAYS

Public Instance Methods

beginning_of_day(time) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/timing/helpers.rb, line 67
def years_after(time, count)
  months_after time, count * 12
end
years_ago(time, count) click to toggle source
# File lib/timing/helpers.rb, line 63
def years_ago(time, count)
  months_ago time, count * 12
end