Skip to content

Commit

Permalink
fix timezone handling in the AR adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmario committed Jun 1, 2010
1 parent f89d2b2 commit c7e9ecd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
6 changes: 3 additions & 3 deletions ext/mysql2_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion ext/mysql2_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions lib/active_record/connection_adapters/mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def type_cast(value)
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)
Expand All @@ -73,8 +73,8 @@ def type_cast_code(var_name)
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})"
Expand Down
11 changes: 6 additions & 5 deletions spec/active_record/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Mysql2Test2 < ActiveRecord::Base

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 (
Expand Down Expand Up @@ -86,9 +87,9 @@ class Mysql2Test2 < ActiveRecord::Base
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')
Expand Down Expand Up @@ -122,9 +123,9 @@ class Mysql2Test2 < ActiveRecord::Base
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')
Expand Down

0 comments on commit c7e9ecd

Please sign in to comment.