Sha256: aeb2ae6b2b932e27cce7bcae75b4c6d98a99e294a740698b00f39370e4735d7a

Contents?: true

Size: 1.95 KB

Versions: 42

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/object/blank"

module ActiveSupport
  class Duration
    # Serializes duration to string according to ISO 8601 Duration format.
    class ISO8601Serializer # :nodoc:
      DATE_COMPONENTS = %i(years months days)

      def initialize(duration, precision: nil)
        @duration = duration
        @precision = precision
      end

      # Builds and returns output string.
      def serialize
        parts = normalize
        return "PT0S" if parts.empty?

        output = +"P"
        output << "#{parts[:years]}Y"   if parts.key?(:years)
        output << "#{parts[:months]}M"  if parts.key?(:months)
        output << "#{parts[:days]}D"    if parts.key?(:days)
        output << "#{parts[:weeks]}W"   if parts.key?(:weeks)
        time = +""
        time << "#{parts[:hours]}H"     if parts.key?(:hours)
        time << "#{parts[:minutes]}M"   if parts.key?(:minutes)
        if parts.key?(:seconds)
          time << "#{sprintf(@precision ? "%0.0#{@precision}f" : '%g', parts[:seconds])}S"
        end
        output << "T#{time}" unless time.empty?
        output
      end

      private
        # Return pair of duration's parts and whole duration sign.
        # Parts are summarized (as they can become repetitive due to addition, etc).
        # Zero parts are removed as not significant.
        # If all parts are negative it will negate all of them and return minus as a sign.
        def normalize
          parts = @duration.parts.each_with_object(Hash.new(0)) do |(k, v), p|
            p[k] += v  unless v.zero?
          end

          # Convert weeks to days and remove weeks if mixed with date parts
          if week_mixed_with_date?(parts)
            parts[:days] += parts.delete(:weeks) * SECONDS_PER_WEEK / SECONDS_PER_DAY
          end

          parts
        end

        def week_mixed_with_date?(parts)
          parts.key?(:weeks) && (parts.keys & DATE_COMPONENTS).any?
        end
    end
  end
end

Version data entries

42 entries across 42 versions & 7 rubygems

Version Path
activesupport-6.1.7.10 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.9 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.8 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.7 lib/active_support/duration/iso8601_serializer.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.6 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.5 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.4 lib/active_support/duration/iso8601_serializer.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.3 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.2 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7.1 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.7 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.6.1 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.6 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.5.1 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.5 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.4.7 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.4.6 lib/active_support/duration/iso8601_serializer.rb
activesupport-6.1.4.5 lib/active_support/duration/iso8601_serializer.rb