diff --git a/app/components/formatted_time.js b/app/components/formatted_time.js index 686747e1b..45b4068ea 100644 --- a/app/components/formatted_time.js +++ b/app/components/formatted_time.js @@ -4,19 +4,17 @@ import React from 'react'; import PropTypes from 'prop-types'; import {Text} from 'react-native'; -import {injectIntl, intlShape} from 'react-intl'; import moment from 'moment-timezone'; import CustomPropTypes from 'app/constants/custom_prop_types'; -class FormattedTime extends React.PureComponent { +export default class FormattedTime extends React.PureComponent { static propTypes = { value: PropTypes.any.isRequired, timeZone: PropTypes.string, children: PropTypes.func, hour12: PropTypes.bool, style: CustomPropTypes.Style, - intl: intlShape.isRequired, }; getFormattedTime = () => { @@ -24,23 +22,14 @@ class FormattedTime extends React.PureComponent { value, timeZone, hour12, - intl, } = this.props; - const timezoneProps = timeZone ? {timeZone} : {}; - const options = { - ...timezoneProps, - hour12, - }; - const formattedTime = intl.formatTime(value, options); - - // `formatTime` returns unformatted date string on error like in the case of (react-intl) unsupported timezone. - // Therefore, use react-intl by default or moment-timezone for unsupported timezone. - if (formattedTime !== String(new Date(value))) { - return formattedTime; + let format = 'H:mm'; + if (hour12) { + const localeFormat = moment.localeData().longDateFormat('LT'); + format = localeFormat?.includes('A') ? localeFormat : 'h:mm A'; } - const format = hour12 ? 'hh:mm A' : 'HH:mm'; if (timeZone) { return moment.tz(value, timeZone).format(format); } @@ -59,5 +48,3 @@ class FormattedTime extends React.PureComponent { return {formattedTime}; } } - -export default injectIntl(FormattedTime); diff --git a/app/components/formatted_time.test.js b/app/components/formatted_time.test.js index 6af86cb0d..6f28ed1c5 100644 --- a/app/components/formatted_time.test.js +++ b/app/components/formatted_time.test.js @@ -4,9 +4,7 @@ import React from 'react'; import {render} from '@testing-library/react-native'; import {IntlProvider} from 'react-intl'; -import IntlPolyfill from 'intl'; -import 'intl/locale-data/jsonp/es'; -import 'intl/locale-data/jsonp/ko'; +import moment from 'moment-timezone'; import FormattedTime from './formatted_time'; @@ -17,8 +15,6 @@ describe('FormattedTime', () => { hour12: true, }; - setupTest(); - it('should render correctly', () => { console.error = jest.fn(); @@ -40,13 +36,15 @@ describe('FormattedTime', () => { }); it('should support localization', () => { + moment.locale('es'); let wrapper = renderWithIntl( , 'es', ); - expect(wrapper.getByText('7:02 p. m.')).toBeTruthy(); + expect(wrapper.getByText('7:02 PM')).toBeTruthy(); + moment.locale('ko'); wrapper = renderWithIntl( , 'ko', @@ -66,6 +64,7 @@ describe('FormattedTime', () => { }); it('should fallback to default short format for unsupported locale of react-intl ', () => { + moment.locale('es'); let wrapper = renderWithIntl( { 'es', ); - expect(wrapper.getByText('08:47 AM')).toBeTruthy(); + expect(wrapper.getByText('8:47 AM')).toBeTruthy(); wrapper = renderWithIntl( { timeZone='NZ-CHAT' hour12={false} />, + 'es', ); - expect(wrapper.getByText('08:47')).toBeTruthy(); + expect(wrapper.getByText('8:47')).toBeTruthy(); }); }); function renderWithIntl(component, locale = 'en') { return render({component}); } - -function setupTest() { - global.Intl = IntlPolyfill; -} diff --git a/app/components/post_header/__snapshots__/post_header.test.js.snap b/app/components/post_header/__snapshots__/post_header.test.js.snap index 9cfd2118e..715f2c956 100644 --- a/app/components/post_header/__snapshots__/post_header.test.js.snap +++ b/app/components/post_header/__snapshots__/post_header.test.js.snap @@ -51,7 +51,7 @@ exports[`PostHeader should match snapshot when just a base post 1`] = ` John Smith - - - - - - - - -