fix timezone handling in the AR adapter

This commit is contained in:
Brian Lopez 2010-06-01 11:17:59 -07:00
parent f89d2b257b
commit c7e9ecdcbd
4 changed files with 14 additions and 13 deletions

View File

@ -550,7 +550,7 @@ static VALUE rb_mysql_result_fetch_row(int argc, VALUE * argv, VALUE self) {
case MYSQL_TYPE_TIME: { // TIME field case MYSQL_TYPE_TIME: { // TIME field
int hour, min, sec, tokens; int hour, min, sec, tokens;
tokens = sscanf(row[i], "%2d:%2d:%2d", &hour, &min, &sec); 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; break;
} }
case MYSQL_TYPE_TIMESTAMP: // TIMESTAMP field 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]); rb_raise(cMysql2Error, "Invalid date: %s", row[i]);
val = Qnil; val = Qnil;
} else { } 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; break;
@ -721,7 +721,7 @@ void Init_mysql2_ext() {
rb_include_module(cMysql2Result, mEnumerable); rb_include_module(cMysql2Result, mEnumerable);
intern_new = rb_intern("new"); intern_new = rb_intern("new");
intern_local = rb_intern("local"); intern_utc = rb_intern("utc");
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys")); sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
sym_reconnect = ID2SYM(rb_intern("reconnect")); sym_reconnect = ID2SYM(rb_intern("reconnect"));

View File

@ -25,7 +25,7 @@ static int utf8Encoding, binaryEncoding;
#endif #endif
static VALUE cBigDecimal, cDate, cDateTime; static VALUE cBigDecimal, cDate, cDateTime;
static ID intern_new, intern_local; static ID intern_new, intern_utc;
/* Mysql2::Error */ /* Mysql2::Error */
static VALUE cMysql2Error; static VALUE cMysql2Error;

View File

@ -57,8 +57,8 @@ module ActiveRecord
when :integer then value.to_i rescue value ? 1 : 0 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 :float then value.to_f # returns self if it's already a Float
when :decimal then self.class.value_to_decimal(value) when :decimal then self.class.value_to_decimal(value)
when :datetime, :timestamp then value.class == Time ? value : self.class.string_to_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 : self.class.string_to_dummy_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 :date then value.class == Date ? value : self.class.string_to_date(value)
when :binary then value when :binary then value
when :boolean then self.class.value_to_boolean(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 :integer then "#{var_name}.to_i rescue #{var_name} ? 1 : 0"
when :float then "#{var_name}.to_f" when :float then "#{var_name}.to_f"
when :decimal then "#{self.class.name}.value_to_decimal(#{var_name})" 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 :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} : #{self.class.name}.string_to_dummy_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 :date then "#{var_name}.class == Date ? #{var_name} : #{self.class.name}.string_to_date(#{var_name})"
when :binary then nil when :binary then nil
when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})" when :boolean then "#{self.class.name}.value_to_boolean(#{var_name})"

View File

@ -27,6 +27,7 @@ describe ActiveRecord::ConnectionAdapters::Mysql2Adapter do
context "columns" do context "columns" do
before(:all) do before(:all) do
ActiveRecord::Base.default_timezone = 'Pacific Time (US & Canada)'
ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test') ActiveRecord::Base.establish_connection(:adapter => 'mysql2', :database => 'test')
Mysql2Test2.connection.execute %[ Mysql2Test2.connection.execute %[
CREATE TABLE IF NOT EXISTS mysql2_test2 ( 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.double_test.should eql('1.0000'.to_f)
test.decimal_test.should eql(BigDecimal.new('1.0000')) test.decimal_test.should eql(BigDecimal.new('1.0000'))
test.date_test.should eql(Date.parse('2010-01-01')) 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.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.year_test.should eql(2010)
test.char_test.should eql('abcdefghij') test.char_test.should eql('abcdefghij')
test.varchar_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.double_test.should eql('1.0000'.to_f)
test.decimal_test.should eql(BigDecimal.new('1.0000')) test.decimal_test.should eql(BigDecimal.new('1.0000'))
test.date_test.should eql(Date.parse('2010-01-01')) 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.class.should eql(Time) test.timestamp_test.class.should eql(ActiveSupport::TimeWithZone)
test.time_test.class.should eql(Time) test.time_test.class.should eql(ActiveSupport::TimeWithZone)
test.year_test.should eql(2010) test.year_test.should eql(2010)
test.char_test.should eql('abcdefghij') test.char_test.should eql('abcdefghij')
test.varchar_test.should eql('abcdefghij') test.varchar_test.should eql('abcdefghij')