Sha256: d7939c59a815d4c6ca1edff29103e11d3d6c5e7a43b95d416c8f872298e198e1
Contents?: true
Size: 1.18 KB
Versions: 3
Compression:
Stored size: 1.18 KB
Contents
# frozen_string_literal: true module QueryFilter module Utils class DateNormalizer PG_MIN_YEAR = -4713 PG_MAX_YEAR = 294_276 attr_reader :date, :format def initialize(date, format = nil) @date = date @format = format end def parsed_value @parsed_value ||= parse end def normalize valid?(parsed_value) ? parsed_value : default_date end private def date? date.is_a?(Time) || date.is_a?(DateTime) end def valid?(value) return false unless Date.valid_date?(value.year, value.month, value.day) value.year > PG_MIN_YEAR && value.year < PG_MAX_YEAR end def parse return date if date? return default_date if date.blank? [@format].concat(QueryFilter.datetime_formats).compact.each do |format| value = safe_parse_date(date, format) return value if value end default_date end def safe_parse_date(string, format) DateTime.strptime(string, format) rescue ArgumentError => _e nil end def default_date Time.zone.today end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
query_filter-0.2.3 | lib/query_filter/utils/date_normalizer.rb |
query_filter-0.2.2 | lib/query_filter/utils/date_normalizer.rb |
query_filter-0.2.0 | lib/query_filter/utils/date_normalizer.rb |