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.
Instance Method Summary collapse
-
#call(value, options = EMPTY_HASH) ⇒ DateTime
Converts a value to a DateTime.
Instance Method Details
#call(value, options = EMPTY_HASH) ⇒ DateTime
Converts a value to a DateTime
35 36 37 38 39 40 41 42 43 |
# File 'lib/cmdx/coercions/date_time.rb', line 35 def call(value, = EMPTY_HASH) return value.to_datetime if value.respond_to?(:to_datetime) 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 |