Sha256: 9c00216b4494e1d6b8dd6e6d54d0cfb8ca0fcc96ef6c0106cc43194f77ca47f9

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

# typed: strict

require 'time'

module Kuby
  module Docker
    class TimestampTag
      # extend T::Sig

      FORMAT = '%Y%m%d%H%M%S'.freeze

      # T::Sig::WithoutRuntime.sig { params(str: T.nilable(String)).returns(T.nilable(Kuby::Docker::TimestampTag)) }
      def self.try_parse(str)
        return nil unless str

        # The strptime function stops scanning after the pattern has been matched, so
        # we check for all numbers here to prevent things like 20210424165405-assets
        # from being treated as a timestamp tag.
        return nil unless str =~ /\A\d+\z/

        time = begin
          Time.strptime(str, FORMAT)
        rescue ArgumentError
          return nil
        end

        new(time)
      end

      # T::Sig::WithoutRuntime.sig { returns(Kuby::Docker::TimestampTag) }
      def self.now
        new(Time.now.utc)
      end

      # T::Sig::WithoutRuntime.sig { returns(Time) }
      attr_reader :time

      # T::Sig::WithoutRuntime.sig { params(time: Time).void }
      def initialize(time)
        @time = time
      end

      # T::Sig::WithoutRuntime.sig { returns(String) }
      def to_s
        time.strftime(FORMAT)
      end

      # T::Sig::WithoutRuntime.sig { params(other: Kuby::Docker::TimestampTag).returns(T.nilable(Integer)) }
      def <=>(other)
        time <=> other.time
      end

      # T::Sig::WithoutRuntime.sig { params(other: Kuby::Docker::TimestampTag).returns(T::Boolean) }
      def ==(other)
        time == other.time
      end

      # T::Sig::WithoutRuntime.sig { returns(Integer) }
      def hash
        time.hash
      end

      # T::Sig::WithoutRuntime.sig { params(other: Kuby::Docker::TimestampTag).returns(T::Boolean) }
      def eql?(other)
        time == other.time
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kuby-core-0.20.2 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.20.1 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.20.0 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.19.0 lib/kuby/docker/timestamp_tag.rb
kuby-core-0.18.0 lib/kuby/docker/timestamp_tag.rb