Skip to content
This repository was archived by the owner on Jul 22, 2022. It is now read-only.

Fix getExceTS daylight savings bug #333

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions source/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,14 @@ let getExcelRowCol = (str) => {
let getExcelTS = (date) => {

let thisDt = new Date(date);
thisDt.setDate(thisDt.getDate() + 1);
thisDt = new Date(thisDt.getTime() + 24 * 60 * 60 * 1000);

let epoch = new Date('1900-01-01T00:00:00.0000Z');

// Handle legacy leap year offset as described in §18.17.4.1
const legacyLeapDate = new Date('1900-02-28T23:59:59.999Z');
if (thisDt - legacyLeapDate > 0) {
thisDt.setDate(thisDt.getDate() + 1);
thisDt = new Date(thisDt.getTime() + 24 * 60 * 60 * 1000);
}

// Get milliseconds between date sent to function and epoch
Expand Down
6 changes: 6 additions & 0 deletions tests/library.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ test('Test library functions', (t) => {
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');

/**
* 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');

/**
* 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
Expand Down