class ActiveModel::Type::Time
end
attribute :start, :time, precision: 4
include ActiveModel::Attributes
class Event
attribute:
The degree of sub-second precision can be customized when declaring an
event.start # => 1999-12-31 21:01:02 UTC
event.start = “00:01:02+03:00”
Partial time-only formats are also accepted.
event.start # => 2000-01-01 07:23:45 UTC
event.start.class # => Time
event.start = “2004-10-25T01:23:45-06:00”
event = Event.new
normalized to have a date of 2000-01-01 and be in the UTC time zone.
String values are parsed using the ISO 8601 datetime format, but are
end
attribute :start, :time
include ActiveModel::Attributes
class Event:time
key.
Attribute type for time of day representation. It is registered under the
= Active Model Time Type
def cast_value(value)
def cast_value(value) return apply_seconds_precision(value) unless value.is_a?(::String) return if value.blank? dummy_time_value = value.sub(/\A\d{4}-\d\d-\d\d(?:T|\s)|/, "2000-01-01 ") fast_string_to_time(dummy_time_value) || begin time_hash = begin ::Date._parse(dummy_time_value) rescue ArgumentError end return if time_hash.nil? || time_hash[:hour].nil? new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset)) end end
def type
def type :time end
def user_input_in_time_zone(value)
def user_input_in_time_zone(value) return unless value.present? case value when ::String value = "2000-01-01 #{value}" time_hash = begin ::Date._parse(value) rescue ArgumentError end return if time_hash.nil? || time_hash[:hour].nil? when ::Time value = value.change(year: 2000, day: 1, month: 1) end super(value) end