Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing a date issue about one millisecond before midnight #54

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions source/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let _rotateBinary = (bin) => {
return bin.substr(1, bin.length - 1) + bin.substr(0, 1);
};

let _getHashForChar = (char, hash) => {
let _getHashForChar = (char, hash) => {
hash = hash ? hash : '0000';
let charCode = char.charCodeAt(0);
let hashBin = parseInt(hash, 16).toString(2);
Expand Down Expand Up @@ -79,7 +79,7 @@ let getExcelAlpha = (colNum) => {
let mod = (remaining - 1) % 26;
columnName = String.fromCharCode(aCharCode + mod) + columnName;
remaining = (remaining - 1 - mod) / 26;
}
}
return columnName;
};

Expand All @@ -101,12 +101,12 @@ let getExcelCellRef = (rowNum, colNum) => {
let mod = (remaining - 1) % 26;
columnName = String.fromCharCode(aCharCode + mod) + columnName;
remaining = (remaining - 1 - mod) / 26;
}
}
return columnName + rowNum;
};

/**
* Translates a Excel cell represenation into row and column numerical equivalents
* Translates a Excel cell represenation into row and column numerical equivalents
* @function getExcelRowCol
* @param {String} str Excel cell representation
* @returns {Object} Object keyed with row and col
Expand Down Expand Up @@ -148,14 +148,14 @@ let getExcelTS = (date) => {
const legacyLeapDate = new Date('1900-02-28T23:59:59.999Z');
if (thisDt - legacyLeapDate > 0) {
thisDt = new Date(thisDt.getTime() + 24 * 60 * 60 * 1000);
}
}

// Get milliseconds between date sent to function and epoch
// Get milliseconds between date sent to function and epoch
let diff2 = thisDt.getTime() - epoch.getTime();

let ts = diff2 / (1000 * 60 * 60 * 24);

return parseFloat(ts.toFixed(7));
return parseFloat(ts.toFixed(8));
};

let sortCellRefs = (a, b) => {
Expand Down Expand Up @@ -242,4 +242,4 @@ module.exports = {
getAllCellsInExcelRange,
getAllCellsInNumericRange,
boolToInt
};
};
27 changes: 14 additions & 13 deletions tests/library.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,34 @@ test('Test library functions', (t) => {

/**
* Tests as defined in §18.17.4.3 of ECMA-376, Second Edition, Part 1 - Fundamentals And Markup Language Reference
* The serial value 3687.4207639... represents 1910-02-03T10:05:54Z
* The serial value 1.5000000... represents 1900-01-01T12:00:00Z
* The serial value 2958465.9999884... represents 9999-12-31T23:59:59Z
* The serial value 3687.42076389... represents 1910-02-03T10:05:54Z
* The serial value 1.50000000... represents 1900-01-01T12:00:00Z
* The serial value 2958465.99998843... represents 9999-12-31T23:59:59Z
*/
t.equals(xl.getExcelTS(new Date('1910-02-03T10:05:54Z')), 3687.4207639, 'Correctly translated date 1910-02-03T10:05:54Z');
t.equals(xl.getExcelTS(new Date('1900-01-01T12:00:00Z')), 1.5000000, 'Correctly translated date 1900-01-01T12:00:00Z');
t.equals(xl.getExcelTS(new Date('9999-12-31T23:59:59Z')), 2958465.9999884, 'Correctly translated date 9999-12-31T23:59:59Z');
t.equals(xl.getExcelTS(new Date('1910-02-03T10:05:54Z')), 3687.42076389, 'Correctly translated date 1910-02-03T10:05:54Z');
t.equals(xl.getExcelTS(new Date('1900-01-01T12:00:00Z')), 1.50000000, 'Correctly translated date 1900-01-01T12:00:00Z');
t.equals(xl.getExcelTS(new Date('9999-12-31T23:59:59Z')), 2958465.99998843, 'Correctly translated date 9999-12-31T23:59:59Z');

/**
* getExcelTS should handle dates within 2 days of daylight savings change (2020-03-08 for example)
*/
t.equals(xl.getExcelTS(new Date('2020-03-05T15:38:00Z')), 43895.6513889, 'Correctly translated date 2020-03-01T15:38:00Z');
t.equals(xl.getExcelTS(new Date('2020-03-06T15:38:00Z')), 43896.6513889, 'Correctly translated date 2020-03-06T15:38:00Z');
t.equals(xl.getExcelTS(new Date('2020-03-05T15:38:00Z')), 43895.65138889, 'Correctly translated date 2020-03-01T15:38:00Z');
t.equals(xl.getExcelTS(new Date('2020-03-06T15:38:00Z')), 43896.65138889, 'Correctly translated date 2020-03-06T15:38:00Z');

/**
* Tests as defined in §18.17.4.1 of ECMA-376, Second Edition, Part 1 - Fundamentals And Markup Language Reference
* The serial value 2.0000000... represents 1900-01-01
* The serial value 3687.0000000... represents 1910-02-03
* The serial value 38749.0000000... represents 2006-02-01
* The serial value 2958465.0000000... represents 9999-12-31
* The serial value 2.00000000... represents 1900-01-01
* The serial value 3687.00000000... represents 1910-02-03
* The serial value 38749.00000000... represents 2006-02-01
* The serial value 2958465.00000000... represents 9999-12-31
*/
t.equals(xl.getExcelTS(new Date('1900-01-01T00:00:00Z')), 1, 'Correctly translated 1900-01-01');
t.equals(xl.getExcelTS(new Date('1910-02-03T00:00:00Z')), 3687, 'Correctly translated 1910-02-03');
t.equals(xl.getExcelTS(new Date('2006-02-01T00:00:00Z')), 38749, 'Correctly translated 2006-02-01');
t.equals(xl.getExcelTS(new Date('9999-12-31T00:00:00Z')), 2958465, 'Correctly translated 9999-12-31');

t.equals(xl.getExcelTS(new Date('2017-06-01T00:00:00.000Z')), 42887, 'Correctly translated 2017-06-01');
t.equals(xl.getExcelTS(new Date('2017-06-01T23:59:59.999Z')), 42887.99999999, 'Correctly translated date 2017-06-01T23:59:59Z');

t.end();
});
});