<commit_before>module Timeliness
  module Parser
    class MissingTimezoneSupport < StandardError; end

    class << self

      def parse(value, *args)
        return value unless value.is_a?(String)

        type, options = type_and_options_from_args(args)

        time_array = _parse(value, type, options)
        return nil if time_array.nil?

        default_values_by_type(time_array, type, options) unless type == :datetime

        make_time(time_array[0..7], options[:zone])
      rescue NoMethodError => ex
        raise ex unless ex.message =~ /zone/
        raise MissingTimezoneSupport, "ActiveSupport must be loaded to use timezones other than :utc and :local."
      end

      def make_time(time_array, zone_option=nil)
        return nil unless fast_date_valid_with_fallback(*time_array[0..2])

        zone_or_offset = time_array.delete_at(7)
        zone, offset   = zone_and_offset(zone_or_offset)

        value = create_time_in_zone(time_array, zone || zone_option)
        # FIXME creates dependency that zone is set
        value = value.in_time_zone if zone

        offset ? value + (value.utc_offset - offset) : value
      rescue ArgumentError, TypeError
        nil
      end

      def _parse(string, type=nil, options={})
        if options[:strict] && type
          set = Definitions.send("#{type}_format_set")
          set.match(string, options[:format])
        else
          values = nil
          Definitions.format_sets(type, string).find {|set| values = set.match(string, options[:format]) }
          values
        end
      rescue
        nil
      end

      private

      def type_and_options_from_args(args)
        options = args.last.is_a?(Hash) ? args.pop : {}
        type_or_now = args.first
        if type_or_now.is_a?(Symbol)
          type = type_or_now
        elsif type_or_now<commit_msg>tiny speed up<commit_after>module Timeliness
  module Parser
    class MissingTimezoneSupport < StandardError; end

    class << self

      def parse(value, *args)
        return value unless value.is_a?(String)

        type, options = type_and_options_from_args(args)

        time_array = _parse(value, type, options)
        return nil if time_array.nil?

        default_values_by_type(time_array, type, options) unless type == :datetime

        make_time(time_array[0..7], options[:zone])
      rescue NoMethodError => ex
        raise ex unless ex.message =~ /zone/
        raise MissingTimezoneSupport, "ActiveSupport must be loaded to use timezones other than :utc and :local."
      end

      def make_time(time_array, zone_option=nil)
        return nil unless fast_date_valid_with_fallback(*time_array[0..2])

        zone_or_offset = time_array.delete_at(7)
        zone, offset   = zone_and_offset(zone_or_offset) if zone_or_offset

        value = create_time_in_zone(time_array, zone || zone_option)
        # FIXME creates dependency that zone is set
        value = value.in_time_zone if zone

        offset ? value + (value.utc_offset - offset) : value
      rescue ArgumentError, TypeError
        nil
      end

      def _parse(string, type=nil, options={})
        if options[:strict] && type
          set = Definitions.send("#{type}_format_set")
          set.match(string, options[:format])
        else
          values = nil
          Definitions.format_sets(type, string).find {|set| values = set.match(string, options[:format]) }
          values
        end
      rescue
        nil
      end

      private

      def type_and_options_from_args(args)
        options = args.last.is_a?(Hash) ? args.pop : {}
        type_or_now = args.first
        if type_or_now.is_a?(Symbol)
          type = type_or_now
        elsif type_or_now
{'fraction_non_alphanumeric': 0.08557588805166846, 'fraction_numerical': 0.002691065662002153, 'mean_word_length': 2.4967074317968017, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5279804', 'n_tokens_mistral': 1113, 'n_tokens_neox': 1094, 'n_words': 273}