Class: ActiveRecord::Type::DateTime

Inherits:
Value
  • Object
show all
Includes:
TimeValue
Defined in:
lib/active_record/type/date_time.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods included from TimeValue

#klass, #type_cast_for_schema

Methods inherited from Value

#==, #binary?, #changed?, #changed_in_place?, #initialize, #klass, #number?, #text?, #type_cast_for_schema, #type_cast_from_database, #type_cast_from_user

Constructor Details

This class inherits a constructor from ActiveRecord::Type::Value

Instance Method Details

#typeObject



6
7
8
# File 'lib/active_record/type/date_time.rb', line 6

def type
  :datetime
end

#type_cast_for_database(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/active_record/type/date_time.rb', line 10

def type_cast_for_database(value)
  return super unless value.acts_like?(:time)

  zone_conversion_method = ActiveRecord::Base.default_timezone == :utc ? :getutc : :getlocal

  if value.respond_to?(zone_conversion_method)
    value = value.send(zone_conversion_method)
  end

  return value unless has_precision?

  result = value.to_s(:db)
  if value.respond_to?(:usec) && (1..6).cover?(precision)
    "#{result}.#{sprintf("%0#{precision}d", value.usec / 10 ** (6 - precision))}"
  else
    result
  end
end