Sha256: 3356b639b7baa4e8223ded51e47e6514b69a469c5155e08ca950a76283ed15d4
Contents?: true
Size: 1.28 KB
Versions: 18
Compression:
Stored size: 1.28 KB
Contents
# frozen_string_literal: true module MuchRails; end module MuchRails::Date InvalidError = Class.new(TypeError) # @example # MuchRails::Date.for(nil) # => nil # MuchRails::Date.for(" ") # => nil # MuchRails::Date.for(Time.zone.today) # => Date # MuchRails::Date.for(Time.current) # => Date # MuchRails::Date.for(DateTime.current) # => Date # MuchRails::Date.for("07/04/2020") # => Date # MuchRails::Date.for("2020.07.04") # => Date # MuchRails::Date.for("2020-07-04T08:15:00Z") # => Date def self.for(value) return if value.blank? if value.respond_to?(:to_date) && !value.is_a?(::String) value.to_date else parse(value) end rescue raise MuchRails::Date::InvalidError, "Invalid Date: #{value.inspect}." end def self.parse(value) parse_united_states(value) rescue ArgumentError parse8601(value) end def self.parse_united_states(value) formatted_value = value.to_s.gsub(/[^\w\s:]/, "-") ::Date.strptime(formatted_value, "%m-%d-%Y") rescue ArgumentError ::Date.strptime(formatted_value, "%Y-%m-%d") end def self.parse8601(value) formatted_value = value.to_s.gsub(/[^\w\s:]/, "-") ::Date.iso8601(formatted_value) rescue ArgumentError ::Time.iso8601(formatted_value).utc.to_date end end
Version data entries
18 entries across 18 versions & 1 rubygems