From 2661c3ecec8f2e4907f9a63f165df14024208fb9 Mon Sep 17 00:00:00 2001 From: Artem Makushov Date: Fri, 11 Aug 2023 19:06:45 +0200 Subject: [PATCH 1/9] refactored DateUtils, timezone fixes, additional tests, removed all usage of moment --- src/CONST.js | 5 + src/components/AutoUpdateTime.js | 14 +- .../ReportActionItem/ChronosOOOListActions.js | 10 +- src/libs/DateUtils.js | 143 ++++++++++++++---- src/pages/home/report/ParticipantLocalTime.js | 13 +- tests/unit/DateUtilsTest.js | 86 ++++++----- 6 files changed, 185 insertions(+), 86 deletions(-) diff --git a/src/CONST.js b/src/CONST.js index 56102c7641f7..bfb57a5feaed 100755 --- a/src/CONST.js +++ b/src/CONST.js @@ -181,6 +181,11 @@ const CONST = { MOMENT_FORMAT_STRING: 'YYYY-MM-DD', SQL_DATE_TIME: 'YYYY-MM-DD HH:mm:ss', FNS_FORMAT_STRING: 'yyyy-MM-dd', + LOCAL_TIME_FORMAT: 'h:mm a', + WEEKDAY_TIME_FORMAT: 'eeee', + FNS_TIMEZONE_FORMAT_STRING: "yyyy-MM-dd'T'HH:mm:ssXXX", + FNS_DB_FORMAT_STRING: 'yyyy-MM-dd HH:mm:ss.SSS', + LONG_DATE_FORMAT_WITH_WEEKDAY: 'eeee, MMMM d, yyyy', UNIX_EPOCH: '1970-01-01 00:00:00.000', MAX_DATE: '9999-12-31', MIN_DATE: '0001-01-01', diff --git a/src/components/AutoUpdateTime.js b/src/components/AutoUpdateTime.js index a522a3e6dcdc..cb15cb20b4ea 100644 --- a/src/components/AutoUpdateTime.js +++ b/src/components/AutoUpdateTime.js @@ -27,21 +27,13 @@ function AutoUpdateTime(props) { * @returns {moment} Returns the locale moment object */ const getCurrentUserLocalTime = useCallback( - () => DateUtils.getLocalMomentFromDatetime(props.preferredLocale, null, props.timezone.selected), + () => DateUtils.getLocalDateFromDatetime(props.preferredLocale, null, props.timezone.selected), [props.preferredLocale, props.timezone.selected], ); const [currentUserLocalTime, setCurrentUserLocalTime] = useState(getCurrentUserLocalTime); const minuteRef = useRef(new Date().getMinutes()); - const timezoneName = useMemo(() => { - // With non-GMT timezone, moment.zoneAbbr() will return the name of that timezone, so we can use it directly. - if (Number.isNaN(Number(currentUserLocalTime.zoneAbbr()))) { - return currentUserLocalTime.zoneAbbr(); - } - - // With GMT timezone, moment.zoneAbbr() will return a number, so we need to display it as GMT {abbreviations} format, e.g.: GMT +07 - return `GMT ${currentUserLocalTime.zoneAbbr()}`; - }, [currentUserLocalTime]); + const timezoneName = useMemo(() => DateUtils.getZoneAbbreviation(currentUserLocalTime, props.timezone.selected), [currentUserLocalTime, props.timezone.selected]); useEffect(() => { // If the any of the props that getCurrentUserLocalTime depends on change, we want to update the displayed time immediately @@ -68,7 +60,7 @@ function AutoUpdateTime(props) { {props.translate('detailsPage.localTime')} - {currentUserLocalTime.format('LT')} {timezoneName} + {DateUtils.formatToLocalTime(currentUserLocalTime)} {timezoneName} ); diff --git a/src/components/ReportActionItem/ChronosOOOListActions.js b/src/components/ReportActionItem/ChronosOOOListActions.js index 3c9c65d8f254..61c504d122ff 100644 --- a/src/components/ReportActionItem/ChronosOOOListActions.js +++ b/src/components/ReportActionItem/ChronosOOOListActions.js @@ -37,8 +37,8 @@ function ChronosOOOListActions(props) { {_.map(events, (event) => { - const start = DateUtils.getLocalMomentFromDatetime(props.preferredLocale, lodashGet(event, 'start.date', '')); - const end = DateUtils.getLocalMomentFromDatetime(props.preferredLocale, lodashGet(event, 'end.date', '')); + const start = DateUtils.getLocalDateFromDatetime(props.preferredLocale, lodashGet(event, 'start.date', '')); + const end = DateUtils.getLocalDateFromDatetime(props.preferredLocale, lodashGet(event, 'end.date', '')); return (