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

MWPW-148424 Fix chart dates #2466

Merged
merged 1 commit into from
Jun 25, 2024
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: 4 additions & 12 deletions libs/blocks/chart/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,12 @@ export function formatExcelDate(date) {
let newDate;

if (!Number.isNaN(+date)) {
const hours = Math.floor((+date % 1) * 24);
const minutes = Math.floor((((+date % 1) * 24) - hours) * 60);
const offsetUTC = 24 - (new Date().getTimezoneOffset() / 60);

newDate = new Date(Date.UTC(0, 0, +date, hours - offsetUTC, minutes));
newDate = +date > 99999
? new Date(+date * 1000)
: new Date(Math.round((+date - (1 + 25567 + 1)) * 86400 * 1000));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't mind some var names for these numbers, if it makes sense to do so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the best I found for an explanation https://gist.github.com/christopherscott/2782634

} else {
newDate = new Date(date);
}

const localDateFormat = new Date(
newDate.getFullYear(),
newDate.getMonth(),
newDate.getDate(),
);

return localDateFormat.toLocaleString([], { dateStyle: 'short' });
return newDate.toLocaleString([], { dateStyle: 'short', timeZone: 'GMT' });
Comment on lines 36 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Brandon32 does this really fix the issue at hand? Locally this fails in GMT+2 (CEST/Zurich Time)

It converts the Date (8/5/2022) to newDate: '2022-08-04T22:00:00.000Z' and hence appears a day earlier. I would imagine on a real chart it would have the same issue

test/blocks/chart/chart.test.js:

 🚧 Browser logs:
      { date: '8/5/2022', newDate: '2022-08-04T22:00:00.000Z' }
      { date: '8/6/2022', newDate: '2022-08-05T22:00:00.000Z' }

 ❌ chart > chart mark series data
      AssertionError: expected [ Array(1) ] to deeply equal [ Array(1) ]
      + expected - actual
      
       [
         [
           {
             "name": "Weekend Sale"
      -      "xAxis": "8/4/22"
      +      "xAxis": "8/5/22"
           }
           {
      -      "xAxis": "8/5/22"
      +      "xAxis": "8/6/22"
           }
         ]
       ]
      
      at n.<anonymous> (test/blocks/chart/chart.test.js:255:39)

Chromium: |██████████████████████████████| 1/1 test files | 0 passed, 1 failed

}
25 changes: 24 additions & 1 deletion test/blocks/chart/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import sinon from 'sinon';
import { expect } from '@esm-bundle/chai';

const { throttle, parseValue } = await import('../../../libs/blocks/chart/utils.js');
const {
throttle,
parseValue,
formatExcelDate,
} = await import('../../../libs/blocks/chart/utils.js');

describe('chart utils', () => {
describe('throttle', () => {
Expand Down Expand Up @@ -46,4 +50,23 @@ describe('chart utils', () => {
expect(parseValue('foo')).to.equal('foo');
});
});

describe('formatExcelDate', () => {
const dates = [
{ excel: 45200, expected: '10/1/23' },
{ excel: 45231, expected: '11/1/23' },
{ excel: 45261, expected: '12/1/23' },
{ excel: 45292, expected: '1/1/24' },
{ excel: 45323, expected: '2/1/24' },
{ excel: 45352, expected: '3/1/24' },
{ excel: 45383, expected: '4/1/24' },
{ excel: 45413, expected: '5/1/24' },
];

dates.forEach((date) => {
it(`formats excel date ${date.excel} to ${date.expected}`, () => {
expect(formatExcelDate(date.excel)).to.equal(date.expected);
});
});
});
});
Loading