From c4754d0befac1a0bfb85f56419dade8378b98b32 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 9 May 2024 07:24:14 -0700 Subject: Automatic locale time formatting --- src/common/components/PositionValue.jsx | 3 +-- src/common/util/formatter.js | 12 +++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src/common') diff --git a/src/common/components/PositionValue.jsx b/src/common/components/PositionValue.jsx index b1f8f656..cedf47c9 100644 --- a/src/common/components/PositionValue.jsx +++ b/src/common/components/PositionValue.jsx @@ -42,14 +42,13 @@ const PositionValue = ({ position, property, attribute }) => { const speedUnit = useAttributePreference('speedUnit'); const volumeUnit = useAttributePreference('volumeUnit'); const coordinateFormat = usePreference('coordinateFormat'); - const hours12 = usePreference('twelveHourFormat'); const formatValue = () => { switch (key) { case 'fixTime': case 'deviceTime': case 'serverTime': - return formatTime(value, 'seconds', hours12); + return formatTime(value, 'seconds'); case 'latitude': return formatCoordinate('latitude', value, coordinateFormat); case 'longitude': diff --git a/src/common/util/formatter.js b/src/common/util/formatter.js index 0463df2b..b10d737a 100644 --- a/src/common/util/formatter.js +++ b/src/common/util/formatter.js @@ -1,6 +1,7 @@ import dayjs from 'dayjs'; import duration from 'dayjs/plugin/duration'; import relativeTime from 'dayjs/plugin/relativeTime'; +import localizedFormat from 'dayjs/plugin/localizedFormat'; import { altitudeFromMeters, @@ -16,6 +17,7 @@ import { prefixString } from './stringUtils'; dayjs.extend(duration); dayjs.extend(relativeTime); +dayjs.extend(localizedFormat); export const formatBoolean = (value, t) => (value ? t('sharedYes') : t('sharedNo')); @@ -29,18 +31,18 @@ export const formatVoltage = (value, t) => `${value.toFixed(2)} ${t('sharedVoltA export const formatConsumption = (value, t) => `${value.toFixed(2)} ${t('sharedLiterPerHourAbbreviation')}`; -export const formatTime = (value, format, hours12) => { +export const formatTime = (value, format) => { if (value) { const d = dayjs(value); switch (format) { case 'date': - return d.format('YYYY-MM-DD'); + return d.format('L'); case 'time': - return d.format(hours12 ? 'hh:mm:ss A' : 'HH:mm:ss'); + return d.format('LTS'); case 'minutes': - return d.format(hours12 ? 'YYYY-MM-DD hh:mm A' : 'YYYY-MM-DD HH:mm'); + return d.format('L LT'); default: - return d.format(hours12 ? 'YYYY-MM-DD hh:mm:ss A' : 'YYYY-MM-DD HH:mm:ss'); + return d.format('L LTS'); } } return ''; -- cgit v1.2.3