Sha256: 2217b4a6dccd6cb2aae5664c3c09aa1962b8cfceb73d243dd3c20b5359cec78e

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true
module Shipit
  class Duration < ActiveSupport::Duration
    ParseError = Class.new(ArgumentError)

    FORMAT = %r{
      \A
      (?<days>\d+d)?
      (?<hours>\d+h)?
      (?<minutes>\d+m)?
      (?<seconds>\d+s?)?
      \z
    }x
    UNITS = {
      's' => :seconds,
      'm' => :minutes,
      'h' => :hours,
      'd' => :days,
    }.freeze

    class << self
      def parse(value)
        return new(-1) if value.to_s == "-1"

        unless match = FORMAT.match(value.to_s)
          raise ParseError, "not a duration: #{value.inspect}"
        end
        parts = []
        UNITS.each_value do |unit|
          if value = match[unit]
            parts << [unit, value.to_i]
          end
        end

        time = ::Time.current
        new(time.advance(parts.to_h) - time, parts)
      end
    end

    def initialize(value, parts = [[:seconds, value]])
      super
    end

    def to_s
      days, seconds_left = value.divmod(1.day.to_i)
      if days > 0
        "#{days}d#{Time.at(seconds_left).utc.strftime('%Hh%Mm%Ss')}"
      else
        Time.at(value).utc.strftime('%Hh%Mm%Ss')[/[^0a-z]\w+/] || '0s'
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shipit-engine-0.39.0 app/models/shipit/duration.rb
shipit-engine-0.38.0 app/models/shipit/duration.rb
shipit-engine-0.37.0 app/models/shipit/duration.rb
shipit-engine-0.36.1 app/models/shipit/duration.rb
shipit-engine-0.36.0 app/models/shipit/duration.rb
shipit-engine-0.35.1 app/models/shipit/duration.rb
shipit-engine-0.35.0 app/models/shipit/duration.rb
shipit-engine-0.34.0 app/models/shipit/duration.rb