class ActiveModel::Type::DateTime
end
attribute :start, :datetime, precision: 4
include ActiveModel::Attributes
class Event
attribute:
The degree of sub-second precision can be customized when declaring an
event.start.utc # => 1999-12-31 21:07:08 UTC
event.start = “06:07:08+09:00”
time-only formats are also accepted.
String values are parsed using the ISO 8601 datetime format. Partial
event.start.zone # => “EAT”
event.start.sec # => 0
event.start.min # => 0
event.start.hour # => 3
event.start.day # => 4
event.start.month # => 9
event.start.year # => 2013
event.start.class # => Time
event.start = “Wed, 04 Sep 2013 03:00:00 EAT”
event = Event.new
end
attribute :start, :datetime
include ActiveModel::Attributes
class Event:datetime
key.
Attribute type to represent dates and times. It is registered under the
= Active Model DateTime Type
def cast_value(value)
def cast_value(value) return apply_seconds_precision(value) unless value.is_a?(::String) return if value.empty? fast_string_to_time(value) || fallback_string_to_time(value) end
def fallback_string_to_time(string)
def fallback_string_to_time(string) time_hash = begin ::Date._parse(string) rescue ArgumentError end return unless time_hash time_hash[:sec_fraction] = microseconds(time_hash) new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset)) end
def microseconds(time)
'0.123456' -> 123456
def microseconds(time) time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0 end
def type
def type :datetime end
def value_from_multiparameter_assignment(values_hash)
def value_from_multiparameter_assignment(values_hash) missing_parameters = [1, 2, 3].delete_if { |key| values_hash.key?(key) } unless missing_parameters.empty? raise ArgumentError, "Provided hash #{values_hash} doesn't contain necessary keys: #{missing_parameters}" end super end