Sha256: 68dcd2267fe91b70060cb6da8cf2214be527ada511e9ea78bb5c3029179ac861

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require 'date'

module RASN1
  module Types
    # ASN.1 UTCTime
    #
    # +{#value} of a +UtcTime+ should be a ruby Time.
    #
    # ===Notes
    # When encoding, resulting string is always a UTC time, appended with +Z+.
    # Seconds are always generated.
    #
    # On parsing, UTC times (ending with +Z+) and local times (ending with
    # +sHHMM+, where +s+ is +++ or +-+, and +HHMM+ is the time differential
    # betwen UTC and local time) are both supported. Seconds may be present or
    # not.
    # @author Sylvain Daubert
    class UtcTime < Primitive
      # UtcTime id value
      ID = 23

      # Get ASN.1 type
      # @return [String]
      def self.type
        'UTCTime'
      end

      private

      def value_to_der
        @value.getutc.strftime('%y%m%d%H%M%SZ')
      end

      def der_to_value(der, ber: false) # rubocop:disable Lint/UnusedMethodArgument
        format = case der.size
                 when 11
                   '%Y%m%d%H%MZ'
                 when 13
                   '%Y%m%d%H%M%SZ'
                 when 15
                   '%Y%m%d%H%M%z'
                 when 17
                   '%Y%m%d%H%M%S%z'
                 else
                   prefix = @name.nil? ? type : "tag #{@name}"
                   raise ASN1Error, "#{prefix}: unrecognized format: #{der}"
                 end
        century = (Time.now.year / 100).to_s
        @value = DateTime.strptime(century + der, format).to_time
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rasn1-0.11.0 lib/rasn1/types/utc_time.rb
rasn1-0.10.0 lib/rasn1/types/utc_time.rb
rasn1-0.9.0 lib/rasn1/types/utc_time.rb