Class: Time
- Inherits:
-
Object
- Object
- Time
- Defined in:
- lib/tickle.rb,
lib/tickle.rb
Overview
:nodoc:
Class Method Summary collapse
-
.===(other) ⇒ Object
Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances.
-
.days_in_month(month, year = now.year) ⇒ Object
Return the number of days in the given month.
-
.local_time(*args) ⇒ Object
Wraps class method
time_with_datetime_fallback
withutc_or_local
set to:local
. -
.time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
Returns a new Time if requested year can be accommodated by Ruby’s Time class (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); otherwise returns a DateTime.
-
.utc_time(*args) ⇒ Object
Wraps class method
time_with_datetime_fallback
withutc_or_local
set to:utc
.
Instance Method Summary collapse
Class Method Details
.===(other) ⇒ Object
Overriding case equality method so that it returns true for ActiveSupport::TimeWithZone instances
38 39 40 |
# File 'lib/tickle.rb', line 38 def ===(other) other.is_a?(::Time) end |
.days_in_month(month, year = now.year) ⇒ Object
Return the number of days in the given month. If no year is specified, it will use the current year.
44 45 46 47 |
# File 'lib/tickle.rb', line 44 def days_in_month(month, year = now.year) return 29 if month == 2 && ::Date.gregorian_leap?(year) COMMON_YEAR_DAYS_IN_MONTH[month] end |
.local_time(*args) ⇒ Object
Wraps class method time_with_datetime_fallback
with utc_or_local
set to :local
.
66 67 68 |
# File 'lib/tickle.rb', line 66 def local_time(*args) time_with_datetime_fallback(:local, *args) end |
.time_with_datetime_fallback(utc_or_local, year, month = 1, day = 1, hour = 0, min = 0, sec = 0, usec = 0) ⇒ Object
Returns a new Time if requested year can be accommodated by Ruby’s Time class (i.e., if year is within either 1970..2038 or 1902..2038, depending on system architecture); otherwise returns a DateTime
52 53 54 55 56 57 58 |
# File 'lib/tickle.rb', line 52 def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0) time = ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec) # This check is needed because Time.utc(y) returns a time object in the 2000s for 0 <= y <= 138. time.year == year ? time : ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec) rescue ::DateTime.civil_from_format(utc_or_local, year, month, day, hour, min, sec) end |
.utc_time(*args) ⇒ Object
Wraps class method time_with_datetime_fallback
with utc_or_local
set to :utc
.
61 62 63 |
# File 'lib/tickle.rb', line 61 def utc_time(*args) time_with_datetime_fallback(:utc, *args) end |
Instance Method Details
#bump(attr, amount = nil) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/tickle.rb', line 136 def bump(attr, amount=nil) amount ||= 1 case attr when :sec then self + amount when :min then self + (amount * 60) when :hour then self + (amount * 60 * 60) when :day then self + (amount * 60 * 60 * 24) 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)) self.bump(:day, diff) when :week then self + (amount * 60 * 60 * 24 * 7) when :month then d = self.to_date >> amount Time.local(d.year, d.month, d.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 |
#to_date ⇒ Object
71 72 73 |
# File 'lib/tickle.rb', line 71 def to_date Date.new(self.year, self.month, self.day) end |
#to_time ⇒ Object
75 76 77 |
# File 'lib/tickle.rb', line 75 def to_time self end |