Make sure we switch over to the DateTime class for DATETIME/TIMESTAMP columns that are out of the supported range for 32bit platforms

This commit is contained in:
Brian Lopez 2010-08-20 12:07:27 -07:00
parent c5f2eb7f3f
commit a19888e939
4 changed files with 35 additions and 14 deletions

View File

@ -10,7 +10,7 @@ VALUE opt_decimal_zero, opt_float_zero, opt_time_year, opt_time_month;
extern VALUE mMysql2, cMysql2Client, cMysql2Error;
static VALUE intern_encoding_from_charset;
static ID intern_new, intern_utc, intern_local, intern_encoding_from_charset_code,
intern_localtime;
intern_localtime, intern_local_offset, intern_civil;
static ID sym_symbolize_keys, sym_as, sym_array, sym_database_timezone, sym_application_timezone,
sym_local, sym_utc, sym_cast_booleans;
static ID intern_merge;
@ -189,6 +189,13 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
if (month < 1 || day < 1) {
rb_raise(cMysql2Error, "Invalid date: %s", row[i]);
val = Qnil;
} else {
if (year < 1902 || year+month+day > 2058) { // use DateTime instead
VALUE offset = INT2NUM(0);
if (db_timezone == intern_local) {
offset = rb_funcall(cMysql2Client, rb_intern("local_offset"), 0);
}
val = rb_funcall(cDateTime, intern_civil, 7, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec), offset);
} else {
val = rb_funcall(rb_cTime, db_timezone, 6, INT2NUM(year), INT2NUM(month), INT2NUM(day), INT2NUM(hour), INT2NUM(min), INT2NUM(sec));
if (!NIL_P(app_timezone)) {
@ -200,6 +207,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
}
}
}
}
break;
}
case MYSQL_TYPE_DATE: // DATE field
@ -425,6 +433,8 @@ void init_mysql2_result() {
intern_local = rb_intern("local");
intern_merge = rb_intern("merge");
intern_localtime = rb_intern("localtime");
intern_local_offset = rb_intern("local_offset");
intern_civil = rb_intern("civil");
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
sym_as = ID2SYM(rb_intern("as"));

View File

@ -1,6 +1,7 @@
# encoding: UTF-8
require 'date'
require 'bigdecimal'
require 'rational' unless RUBY_VERSION >= '1.9.2'
require 'mysql2/error'
require 'mysql2/mysql2'

View File

@ -224,5 +224,10 @@ module Mysql2
end
end
end
private
def self.local_offset
::Time.local(2010).utc_offset.to_r / 86400
end
end
end

View File

@ -143,12 +143,17 @@ describe Mysql2::Result do
@test_result['double_test'].should eql(10.3)
end
it "should return Time for a DATETIME value" do
it "should return Time for a DATETIME value when within the supported range" do
@test_result['date_time_test'].class.should eql(Time)
@test_result['date_time_test'].strftime("%F %T").should eql('2010-04-04 11:44:00')
end
it "should return Time for a TIMESTAMP value" do
it "should return DateTime for a DATETIME value when outside the supported range" do
r = @client.query("SELECT CAST('1901-1-1 01:01:01' AS DATETIME) as test")
r.first['test'].class.should eql(DateTime)
end
it "should return Time for a TIMESTAMP value when within the supported range" do
@test_result['timestamp_test'].class.should eql(Time)
@test_result['timestamp_test'].strftime("%F %T").should eql('2010-04-04 11:44:00')
end