Skip to content

Commit

Permalink
MWPW-148424 Fix chart dates (#2466)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon32 authored Jun 25, 2024
1 parent 1a8acc6 commit c05ef86
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
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));
} 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' });
}
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);
});
});
});
});

0 comments on commit c05ef86

Please sign in to comment.