fix timezone handling in the AR adapter
This commit is contained in:
parent
f89d2b257b
commit
c7e9ecdcbd
@ -550,7 +550,7 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
|
||||
case MYSQL_TYPE_TIME: { // TIME field
|
||||
int hour, min, sec, tokens;
|
||||
tokens = sscanf(row[i], "%2d:%2d:%2d", &hour, &min, &sec);
|
||||
val = rb_funcall(rb_cTime, intern_local, 6, INT2NUM(0), INT2NUM(1), INT2NUM(1), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
|
||||
val = rb_funcall(rb_cTime, intern_utc, 6, INT2NUM(0), INT2NUM(1), INT2NUM(1), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
|
||||
break;
|
||||
}
|
||||
case MYSQL_TYPE_TIMESTAMP: // TIMESTAMP field
|
||||
@ -564,7 +564,7 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
|
||||
rb_raise(cMysql2Error, "Invalid date: %s", row[i]);
|
||||
val = Qnil;
|
||||
} else {
|
||||
val = rb_funcall(rb_cTime, intern_local, 6, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
|
||||
val = rb_funcall(rb_cTime, intern_utc, 6, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -721,7 +721,7 @@ void Init_mysql2_ext() {
|
||||
rb_include_module(cMysql2Result, mEnumerable);
|
||||
|
||||
intern_new = rb_intern("new");
|
||||
intern_local = rb_intern("local");
|
||||
intern_utc = rb_intern("utc");
|
||||
|
||||
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
|
||||
sym_reconnect = ID2SYM(rb_intern("reconnect"));
|
||||
|
@ -25,7 +25,7 @@ static int utf8Encoding, binaryEncoding;
|
||||
#endif
|
||||
|
||||
static VALUE cBigDecimal, cDate, cDateTime;
|
||||
static ID intern_new, intern_local;
|
||||
static ID intern_new, intern_utc;
|
||||
|
||||
/* Mysql2::Error */
|
||||
static VALUE cMysql2Error;
|
||||
|
@ -57,8 +57,8 @@ module ActiveRecord
|
||||
when :integer then value.to_i rescue value ? 1 : 0
|
||||
when :float then value.to_f # returns self if it's already a Float
|
||||
when :decimal then self.class.value_to_decimal(value)
|
||||
when :datetime, :timestamp then value.class == Time ? value : self.class.string_to_time(value)
|
||||
when :time then value.class == Time ? value : self.class.string_to_dummy_time(value)
|
||||
when :datetime, :timestamp then value.class == Time ? value.in_time_zone(Base.default_timezone) : self.class.string_to_time(value)
|
||||
when :time then value.class == Time ? value.in_time_zone(Base.default_timezone) : self.class.string_to_dummy_time(value)
|
||||
when :date then value.class == Date ? value : self.class.string_to_date(value)
|
||||
when :binary then value
|
||||
when :boolean then self.class.value_to_boolean(value)
|
||||
@ -73,8 +73,8 @@ module ActiveRecord
|
||||
when :integer then "#{var_name}.to_i rescue #{var_name} ? 1 : 0"
|
||||
when :float then "#{var_name}.to_f"
|
||||
when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})"
|
||||
when :datetime, :timestamp then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_time(#{var_name})"
|
||||
when :time then "#{var_name}.class == Time ? #{var_name} : #{self.class.name}.string_to_dummy_time(#{var_name})"
|
||||
when :datetime, :timestamp then "#{var_name}.class == Time ? #{var_name}.in_time_zone(Base.default_timezone) : #{self.class.name}.string_to_time(#{var_name})"
|
||||
when :time then "#{var_name}.class == Time ? #{var_name}.in_time_zone(Base.default_timezone) : #{self.class.name}.string_to_dummy_time(#{var_name})"
|
||||
when :date then "#{var_name}.class == Date ? #{var_name} : #{self.class.name}.string_to_date(#{var_name})"
|
||||
when :binary then nil
|
||||
when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"
|
||||
|
@ -27,6 +27,7 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do
|
||||
|
||||
context "columns" do
|
||||
before(:all) do
|
||||
ActiveRecord::Base.default_timezone = 'Pacific Time (US & Canada)'
|
||||
ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test')
|
||||
Mysql2Test2.connection.execute %[
|
||||
CREATE TABLE IF NOT EXISTS mysql2_test2 (
|
||||
@ -86,9 +87,9 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do
|
||||
test.double_test.should eql('1.0000'.to_f)
|
||||
test.decimal_test.should eql(BigDecimal.new('1.0000'))
|
||||
test.date_test.should eql(Date.parse('2010-01-01'))
|
||||
test.date_time_test.should eql(Time.parse('2010-01-01 00:00:00'))
|
||||
test.date_time_test.should eql(DateTime.parse('2010-01-01 00:00:00'))
|
||||
test.timestamp_test.should be_nil
|
||||
test.time_test.class.should eql(Time)
|
||||
test.time_test.class.should eql(DateTime)
|
||||
test.year_test.should eql(2010)
|
||||
test.char_test.should eql('abcdefghij')
|
||||
test.varchar_test.should eql('abcdefghij')
|
||||
@ -122,9 +123,9 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do
|
||||
test.double_test.should eql('1.0000'.to_f)
|
||||
test.decimal_test.should eql(BigDecimal.new('1.0000'))
|
||||
test.date_test.should eql(Date.parse('2010-01-01'))
|
||||
test.date_time_test.should eql(Time.parse('2010-01-01 00:00:00'))
|
||||
test.timestamp_test.class.should eql(Time)
|
||||
test.time_test.class.should eql(Time)
|
||||
test.date_time_test.should eql(DateTime.parse('2010-01-01 00:00:00'))
|
||||
test.timestamp_test.class.should eql(ActiveSupport::TimeWithZone)
|
||||
test.time_test.class.should eql(ActiveSupport::TimeWithZone)
|
||||
test.year_test.should eql(2010)
|
||||
test.char_test.should eql('abcdefghij')
|
||||
test.varchar_test.should eql('abcdefghij')
|
||||
|
Loading…
Reference in New Issue
Block a user