Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/active_record/connection_adapters/sqlserver/type/datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ def sqlserver_type
end

def serialize(value)
return super unless value.acts_like?(:time)
datetime = super.to_s(:_sqlserver_datetime).tap do |v|
value = super
return value unless value.acts_like?(:time)
datetime = value.to_s(:_sqlserver_datetime).tap do |v|
fraction = quote_fractional(value)
v << ".#{fraction}"
end
Expand All @@ -34,9 +35,7 @@ def quoted(value)
private

def cast_value(value)
value = value.acts_like?(:time) ? value : super
return unless value
apply_seconds_precision(value)
super.try(:in_time_zone)
end

def fast_string_to_time(string)
Expand All @@ -53,6 +52,20 @@ def fast_string_to_time_zone
::Time.zone || ActiveSupport::TimeZone.new('UTC')
end

# Copy of module ActiveModel::Type::Helpers::TimeValue with
# else condition using a zone for local parsing.
def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
return if year.nil? || (year == 0 && mon == 0 && mday == 0)
if offset
time = ::Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
return unless time
time -= offset
is_utc? ? time : time.getlocal
else
fast_string_to_time_zone.local(year, mon, mday, hour, min, sec, microsec) rescue nil
end
end

end
end
end
Expand Down