Module: CMDx::Coercions::DateTime
Overview
Converts various input types to DateTime format
Handles conversion from date strings, Date objects, Time objects, and other values that can be converted to DateTime using Ruby’s DateTime.parse method or custom strptime formats.
Constant Summary collapse
- ANALOG_TYPES =
Types that are already date-time-like and don’t need conversion
%w[Date DateTime Time].freeze
Instance Method Summary collapse
-
#call(value, options = {}) ⇒ DateTime
Converts a value to a DateTime.
Instance Method Details
#call(value, options = {}) ⇒ DateTime
Converts a value to a DateTime
40 41 42 43 44 45 46 47 48 |
# File 'lib/cmdx/coercions/date_time.rb', line 40 def call(value, = {}) return value if ANALOG_TYPES.include?(value.class.name) return ::DateTime.strptime(value, [:strptime]) if [:strptime] ::DateTime.parse(value) rescue TypeError, ::Date::Error type = Locale.t("cmdx.types.date_time") raise CoercionError, Locale.t("cmdx.coercions.into_a", type:) end |