Module: TimeUtil
- Included in:
- IceCube
- Defined in:
- lib/ice_cube/time_util.rb
Constant Summary collapse
- LeapYearMonthDays =
[31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- CommonYearMonthDays =
[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
Class Method Summary collapse
- .days_in_month(date) ⇒ Object
- .days_in_year(date) ⇒ Object
- .is_leap?(date) ⇒ Boolean
-
.serializable_time(time) ⇒ Object
this method exists because ActiveSupport will serialize TimeWithZone’s in collections in UTC time instead of their local time.
Class Method Details
.days_in_month(date) ⇒ Object
27 28 29 |
# File 'lib/ice_cube/time_util.rb', line 27 def self.days_in_month(date) is_leap?(date) ? LeapYearMonthDays[date.month - 1] : CommonYearMonthDays[date.month - 1] end |
.days_in_year(date) ⇒ Object
23 24 25 |
# File 'lib/ice_cube/time_util.rb', line 23 def self.days_in_year(date) is_leap?(date) ? 366 : 365 end |
.is_leap?(date) ⇒ Boolean
19 20 21 |
# File 'lib/ice_cube/time_util.rb', line 19 def self.is_leap?(date) (date.year % 4 == 0 && date.year % 100 != 0) || (date.year % 400 == 0) end |
.serializable_time(time) ⇒ Object
this method exists because ActiveSupport will serialize TimeWithZone’s in collections in UTC time instead of their local time. if time
is a TimeWithZone, we move it to a DateTime Note: When converting to datetime, you microseconds get set to 0
11 12 13 14 15 16 17 |
# File 'lib/ice_cube/time_util.rb', line 11 def self.serializable_time(time) if time.respond_to?(:to_datetime) time.to_datetime else time end end |