class ActiveSupport::Duration
def build(value)
ActiveSupport::Duration.build(2716146).parts # => {:months=>1, :days=>1}
ActiveSupport::Duration.build(31556952).parts # => {:years=>1}
to the individual parts:
Creates a new Duration from a seconds value that is converted
def build(value) unless value.is_a?(::Numeric) raise TypeError, "can't build an #{self.name} from a #{value.class.name}" end parts = {} remainder_sign = value <=> 0 remainder = value.round(9).abs variable = false PARTS.each do |part| unless part == :seconds part_in_seconds = PARTS_IN_SECONDS[part] parts[part] = remainder.div(part_in_seconds) * remainder_sign remainder %= part_in_seconds unless parts[part].zero? variable ||= VARIABLE_PARTS.include?(part) end end end unless value == 0 parts[:seconds] = remainder * remainder_sign new(value, parts, variable) end