Class: Time

Inherits:
Object
  • Object
show all
Defined in:
lib/tickle.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#bump(attr, amount = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tickle.rb', line 71

def bump(attr, amount=nil)
  amount ||= 1
  case attr
  when :sec then
    Time.local(self.year, self.month, self.day, self.hour, self.min, self.sec + amount)
  when :min then
    Time.local(self.year, self.month, self.day, self.hour, self.min + amount, self.sec)
  when :hour then
    Time.local(self.year, self.month, self.day, self.hour + amount, self.min, self.sec)
  when :day then
    Time.local(self.year, self.month, self.day + amount, self.hour, self.min, self.sec)
  when :wday then
    amount = Time::RFC2822_DAY_NAME.index(amount) if amount.is_a?(String)
    raise Exception, "specified day of week invalid.  Use #{Time::RFC2822_DAY_NAME}" unless amount
    diff = (amount > self.wday) ? (amount - self.wday) : (7 - (self.wday - amount))
    Time.local(self.year, self.month, self.day + diff, self.hour, self.min, self.sec)
  when :week then
    Time.local(self.year, self.month, self.day + (amount * 7), self.hour, self.min, self.sec)
  when :month then
    Time.local(self.year, self.month + amount, self.day, self.hour, self.min, self.sec)
  when :year then
    Time.local(self.year + amount, self.month, self.day, self.hour, self.min, self.sec)
  else
    raise Exception, "type \"#{attr}\" not supported."
  end
end