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
- .ical_duration(duration) ⇒ Object
- .ical_format(time) ⇒ 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 |
.ical_duration(duration) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/ice_cube/time_util.rb', line 39 def self.ical_duration(duration) hours = duration / 3600; duration %= 3600 minutes = duration / 60; duration %= 60 repr = '' repr << "#{hours}H" if hours > 0 repr << "#{minutes}M" if minutes > 0 repr << "#{duration}S" if duration > 0 "PT#{repr}" end |
.ical_format(time) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/ice_cube/time_util.rb', line 31 def self.ical_format(time) if time.utc? ":#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time else ";TZID=#{time.strftime('%Z:%Y%m%dT%H%M%S')}" # local time specified end 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 |