Permalink
Browse files

Fixed: Date at end of a year trips an assert

When a date was near the end of a year before 1970, an assert would
trip because the day would be out of range of 0 to 30 (0 based day
within the month).

The day would be off because the time was being calculated after the
year was calculated from the day.  Moving this calculation forward
solved the problem.

Added a test to guard against regression.
  • Loading branch information...
1 parent f7d228f commit 2de4b8d8c381a4c2878cc8d5c56daeaae400009f @jkint jkint committed with Jay Kint Jan 24, 2013
Showing with 14 additions and 16 deletions.
  1. +6 −6 src/Column.cpp
  2. +8 −10 test/params.js
View
@@ -133,6 +133,12 @@ void TimestampColumn::DateFromMilliseconds( SQL_SS_TIMESTAMPOFFSET_STRUCT& date
// calculate the number of days elapsed (normalized from the beginning of supported datetime)
int64_t day = static_cast<int64_t>( milliseconds ) / MS_PER_DAY;
+ // calculate time portion of the timestamp
+ int64_t time = static_cast<int64_t>( milliseconds ) % MS_PER_DAY;
+ if( time < 0 ) {
+ time = MS_PER_DAY + time;
+ --day;
+ }
// how many leap years have passed since that time?
int64_t year = YearFromDay( day );
@@ -141,12 +147,6 @@ void TimestampColumn::DateFromMilliseconds( SQL_SS_TIMESTAMPOFFSET_STRUCT& date
start_days = leap_days_in_months;
}
- // calculate time portion of the timestamp
- int64_t time = static_cast<int64_t>( milliseconds ) % MS_PER_DAY;
- if( time < 0 ) {
- time = MS_PER_DAY + time;
- --day;
- }
int64_t month = 0;
while( day >= start_days[ month ] ) {
View
@@ -635,21 +635,19 @@ suite( 'params', function() {
});
});
- // verify fix for a bug that would return the wrong day when a datetimeoffset was inserted where the date
- // was before 1/1/1970 and the time was midnight.
- test( 'verify bug fix for dates older than 1/1/1970 with midnight time', function( test_done ) {
+ test( 'verify bug fix for last day of the year error', function( test_done ) {
sql.open( conn_str, function( err, conn ) {
assert.ifError( err );
- var midnightDate = new Date( Date.parse( "1930-08-13T00:00:00.000Z" ));
+ var eoyDate = new Date( Date.parse( "1960-12-31T11:12:13.000Z" ));
- testBoilerPlate( 'midnight_date_test', { 'midnight_date_test': 'datetimeoffset(3)' },
+ testBoilerPlate( 'eoy_date_test', { 'eoy_date_test': 'datetimeoffset(3)' },
function (done) {
- var insertQuery = "INSERT INTO midnight_date_test (midnight_date_test) VALUES (?);";
- conn.queryRaw( insertQuery, [ midnightDate ], function( e ) {
+ var insertQuery = "INSERT INTO eoy_date_test (eoy_date_test) VALUES (?);";
+ conn.queryRaw( insertQuery, [ eoyDate ], function( e ) {
assert.ifError( e );
done();
@@ -658,14 +656,14 @@ suite( 'params', function() {
// test valid dates
function( done ) {
- conn.queryRaw( "SELECT midnight_date_test FROM midnight_date_test", function( e, r ) {
+ conn.queryRaw( "SELECT eoy_date_test FROM eoy_date_test", function( e, r ) {
assert.ifError( e );
var expectedDates = [];
- var expectedDate = midnightDate
+ var expectedDate = eoyDate;
expectedDates.push( [ expectedDate ]);
- var expectedResults = { meta: [ { name: 'midnight_date_test', size: 30, nullable: true, type: 'date' } ],
+ var expectedResults = { meta: [ { name: 'eoy_date_test', size: 30, nullable: true, type: 'date' } ],
rows: expectedDates
};
assert.deepEqual( expectedResults.meta, r.meta );

0 comments on commit 2de4b8d

Please sign in to comment.