class Aliyun::Log::Record::TypeCasting::DateTimeType

Public Instance Methods

cast(value) click to toggle source
# File lib/aliyun/log/record/type_casting.rb, line 87
def cast(value)
  return nil unless value.respond_to?(:to_datetime)

  dt = begin
         ::DateTime.parse(value)
       rescue StandardError
         nil
       end
  if dt
    seconds = string_utc_offset(value) || 0
    offset = seconds_to_offset(seconds)
    ::DateTime.new(dt.year, dt.mon, dt.mday, dt.hour, dt.min, dt.sec, offset)
  else
    value.to_datetime
  end
end
dump(value) click to toggle source
# File lib/aliyun/log/record/type_casting.rb, line 104
def dump(value)
  if value.respond_to?(:to_datetime)
    value.to_datetime.iso8601
  else
    value.to_s
  end
end

Private Instance Methods

seconds_to_offset(seconds) click to toggle source
# File lib/aliyun/log/record/type_casting.rb, line 118
def seconds_to_offset(seconds)
  ActiveSupport::TimeZone.seconds_to_utc_offset(seconds)
end
string_utc_offset(string) click to toggle source
# File lib/aliyun/log/record/type_casting.rb, line 114
def string_utc_offset(string)
  Date._parse(string)[:offset]
end