Sha256: 3522a80fb8029c2dbc87c0d1ab805905cfb5e07a922378587d2c26da66f1eb7c

Contents?: true

Size: 847 Bytes

Versions: 3

Compression:

Stored size: 847 Bytes

Contents

# frozen_string_literal: true

require "date"

module InferModel
  class Parsers::DateTime
    extend Callable
    extend Dry::Initializer

    ACCEPTABLE_DATETIME_FORMATS = [
      "%Y-%m-%dT%T%z",
      "%Y-%m-%dT%T%Z",
      "%Y-%m-%dT%TZ",
      "%d.%m.%Y %T%z",
      "%d.%m.%Y %T%Z",
      "%d.%m.%Y %T",
      "%d.%m.%Y %H:%M",
      "%Y-%m-%dT",
      "%Y-%m-%d",
      "%d.%m.%Y",
    ].freeze

    param :value
    option :allow_blank, default: -> { true }

    def call
      raise Parsers::Error, "value was blank which is not allowed" if value.nil? && !allow_blank
      return if value.nil? || value.empty?

      ACCEPTABLE_DATETIME_FORMATS.each do |format|
        return DateTime.strptime(value, format)
      rescue Date::Error
        next
      end

      raise Parsers::Error, "'#{value}' is not a DateTime"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
infer_model-0.1.2 lib/infer_model/parsers/date_time.rb
infer_model-0.1.1 lib/infer_model/parsers/date_time.rb
infer_model-0.1.0 lib/infer_model/parsers/date_time.rb