# SqlserverDatetimeFix module ActiveRecord module ConnectionAdapters class SQLServerColumn def cast_to_time(value) return value if value.is_a?(Time) or value.is_a?(DateTime) time_array = ParseDate.parsedate(value) time_array[0] ||= 2000 time_array[1] ||= 1 time_array[2] ||= 1 Time.send(Base.default_timezone, *time_array) rescue DateTime.new(*time_array[0..5]) rescue nil end def cast_to_datetime(value) if value.is_a?(Time) or value.is_a?(DateTime) if value.year != 0 and value.month != 0 and value.day != 0 return value else return Time.mktime(2000, 1, 1, value.hour, value.min, value.sec) rescue nil end end return cast_to_time(value) if value.is_a?(Date) or value.is_a?(String) rescue nil value end end end end module ActiveRecord module ConnectionAdapters #:nodoc: # An abstract definition of a column in a table. class Column def self.string_to_time(string) return string unless string.is_a?(String) date_array = ParseDate.parsedate(string) return nil if (date_array.compact.empty?) # treat 0000-00-00 as nil DateTime.new(*date_array.compact) rescue nil end end end end module ActiveRecord module ConnectionAdapters class SQLServerAdapter < AbstractAdapter def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) execute(sql, name) id_value || select_one("SELECT SCOPE_IDENTITY() AS Ident")["Ident"] end end end end