Skip to content

Commit 284e4c0

Browse files
committed
Merge branch '0.2.x'
2 parents 4b1d0c1 + 2b7992f commit 284e4c0

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

ext/mysql2/result.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44
static rb_encoding *binaryEncoding;
55
#endif
66

7-
#define MYSQL2_MAX_YEAR 2038
7+
#if SIZEOF_INT < SIZEOF_LONG
8+
/**
9+
* on 64bit platforms we can handle dates way outside 2038-01-19T03:14:07
10+
* because of how I'm performing the math below, this will allow a maximum
11+
* timestamp of 9846-12-12T11:5999:59
12+
*/
13+
#define MYSQL2_MAX_YEAR 9999
14+
#else
15+
/**
16+
* on 32bit platforms the maximum date the Time class can handle is 2038-01-19T03:14:07
17+
* 2082 = 2038+1+19+3+14+7
18+
*/
19+
#define MYSQL2_MAX_YEAR 2082
20+
#endif
821

922
#ifdef NEGATIVE_TIME_T
10-
/* 1901-12-13 20:45:52 UTC : The oldest time in 32-bit signed time_t. */
23+
/* 1901-12-13 20:45:52 UTC : The oldest time in 32-bit signed time_t. */
1124
#define MYSQL2_MIN_YEAR 1902
1225
#else
13-
/* 1970-01-01 00:00:00 UTC : The Unix epoch - the oldest time in portable time_t. */
26+
/* 1970-01-01 00:00:00 UTC : The Unix epoch - the oldest time in portable time_t. */
1427
#define MYSQL2_MIN_YEAR 1970
1528
#endif
1629

@@ -240,7 +253,7 @@ static VALUE rb_mysql_result_fetch_row(VALUE self, ID db_timezone, ID app_timezo
240253
rb_raise(cMysql2Error, "Invalid date: %s", row[i]);
241254
val = Qnil;
242255
} else {
243-
if (year < MYSQL2_MIN_YEAR || year+month+day > MYSQL2_MAX_YEAR) { // use DateTime instead
256+
if (year < MYSQL2_MIN_YEAR || year+month+day+hour+min+sec > MYSQL2_MAX_YEAR) { // use DateTime instead
244257
VALUE offset = INT2NUM(0);
245258
if (db_timezone == intern_local) {
246259
offset = rb_funcall(cMysql2Client, intern_local_offset, 0);

spec/mysql2/result_spec.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,18 @@
189189
r.first['test'].class.should eql(DateTime)
190190
end
191191

192-
it "should return DateTime when time > year 2038" do
193-
r = @client.query("SELECT CAST('2039-01-01 01:01:01' AS DATETIME) as test")
194-
r.first['test'].class.should eql(DateTime)
192+
if 1.size == 4 # 32bit
193+
it "should return DateTime when timestamp is > 2038-01-19T03:14:07" do
194+
# 2038-01-19T03:14:07 is the max for 32bit Ruby 1.8
195+
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
196+
r.first['test'].class.should eql(DateTime)
197+
end
198+
elsif 1.size == 8 # 64bit
199+
it "should return Time when timestamp is > 2038-01-19T03:14:07" do
200+
# 2038-01-19 03:14:07 is the max for 32bit Ruby 1.8
201+
r = @client.query("SELECT CAST('2038-01-19 03:14:08' AS DATETIME) as test")
202+
r.first['test'].class.should eql(Time)
203+
end
195204
end
196205

197206
it "should return Time for a TIMESTAMP value when within the supported range" do

0 commit comments

Comments
 (0)