Module: MuchRails::Time

Defined in:
lib/much-rails/time.rb

Constant Summary collapse

InvalidError =
Class.new(TypeError)

Class Method Summary collapse

Class Method Details

.for(value) ⇒ Object

Examples:

MuchRails::Time(nil) # => nil
MuchRails::Time(" ") # => nil
MuchRails::Time(Time.current) # => Time
MuchRails::Time(DateTime.current) # => Time
MuchRails::Time(Date.zone.today) # => Time
MuchRails::Time("2020-07-04T08:15:00Z") # => Time


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/much-rails/time.rb', line 17

def self.for(value)
  return if value.blank?

  if value.respond_to?(:to_time) && !value.is_a?(::String)
    value.to_time.utc
  else
    ::Time.iso8601(value.to_s).utc
  end
rescue
  raise MuchRails::Time::InvalidError, "Invalid Time: #{value.inspect}."
end