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

fix: Format date localized #3003

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Copy link
Member

Choose a reason for hiding this comment

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

I ran this test locally without the changes in format-date-localized and it also passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh right, because I reproduced this problem locally after changing my system timezone to pacific

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I rewrote this as an integration test because the only way to test it in a unit test environment would be to set up a separate environment with a global timezone. Moreover, the integration test I added now fully covers the original bug scenario.

// SPDX-License-Identifier: Apache-2.0

import formatDateLocalized from '../../../../../lib/components/internal/utils/date-time/format-date-localized';

describe('formatDateLocalized', () => {
test('formats date correctly when the date is not ISO formatted', () => {
expect(
formatDateLocalized({
date: 'Tue Nov 12 2024 11:03:17 GMT+0100 (Central European Standard Time)',
isDateOnly: true,
locale: 'en-US',
})
).toBe('November 12, 2024');
});

test('formats date correctly when the TZ is negative in relation to UTC', () => {
// Store original timezone offset method
const originalTimezoneOffset = Date.prototype.getTimezoneOffset;

// Mock to simulate Pacific Time (UTC-8/UTC-7)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as an alternative, it could be achieved via using https://www.npmjs.com/package/timezone-mock lib

Date.prototype.getTimezoneOffset = function () {
return 420; // 420 minutes = UTC-7 (PDT)
};

expect(
formatDateLocalized({
date: '2020-01-05',
isDateOnly: true,
locale: 'en-US',
})
).toBe('January 5, 2020');

Date.prototype.getTimezoneOffset = originalTimezoneOffset;
});
});
7 changes: 6 additions & 1 deletion src/internal/utils/date-time/format-date-localized.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { isValid, parseISO } from 'date-fns';

import { formatTimeOffsetLocalized } from './format-time-offset';

Expand All @@ -16,7 +17,11 @@ export default function formatDateLocalized({
timeOffset?: number;
locale?: string;
}) {
const date = new Date(isoDate);
let date = parseISO(isoDate);
// if the date is not ISO formatted, fallback to built-in date parsing
if (!isValid(date)) {
date = new Date(isoDate);
}

const formattedDate = new Intl.DateTimeFormat(locale, {
day: 'numeric',
Expand Down
Loading