fix chart and timezone issues, pass consistent dates to DB.
Closes #3700
This commit is contained in:
@@ -3,11 +3,13 @@ import { TIMEZONE_CONFIG, TIMEZONE_LEGACY } from '@/lib/constants';
|
||||
import { formatInTimeZone, zonedTimeToUtc, utcToZonedTime } from 'date-fns-tz';
|
||||
import { useApp, setTimezone } from '@/store/app';
|
||||
import { useLocale } from './useLocale';
|
||||
import { getTimezone } from '@/lib/date';
|
||||
|
||||
const selector = (state: { timezone: string }) => state.timezone;
|
||||
|
||||
export function useTimezone() {
|
||||
const timezone = useApp(selector);
|
||||
const localTimeZone = getTimezone();
|
||||
const { dateLocale } = useLocale();
|
||||
|
||||
const saveTimezone = (value: string) => {
|
||||
@@ -26,6 +28,38 @@ export function useTimezone() {
|
||||
);
|
||||
};
|
||||
|
||||
const formatSeriesTimezone = (data: any, column: string, timezone: string) => {
|
||||
return data.map(item => {
|
||||
const date = new Date(item[column]);
|
||||
|
||||
const format = new Intl.DateTimeFormat('en-US', {
|
||||
timeZone: timezone,
|
||||
hour12: false,
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit',
|
||||
});
|
||||
|
||||
const parts = format.formatToParts(date);
|
||||
const get = type => parts.find(p => p.type === type)?.value;
|
||||
|
||||
const year = get('year');
|
||||
const month = get('month');
|
||||
const day = get('day');
|
||||
const hour = get('hour');
|
||||
const minute = get('minute');
|
||||
const second = get('second');
|
||||
|
||||
return {
|
||||
...item,
|
||||
[column]: `${year}-${month}-${day} ${hour}:${minute}:${second}`,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const toUtc = (date: Date | string | number) => {
|
||||
return zonedTimeToUtc(date, timezone);
|
||||
};
|
||||
@@ -34,16 +68,28 @@ export function useTimezone() {
|
||||
return utcToZonedTime(date, timezone);
|
||||
};
|
||||
|
||||
const localToUtc = (date: Date | string | number) => {
|
||||
return zonedTimeToUtc(date, localTimeZone);
|
||||
};
|
||||
|
||||
const localFromUtc = (date: Date | string | number) => {
|
||||
return utcToZonedTime(date, localTimeZone);
|
||||
};
|
||||
|
||||
const canonicalizeTimezone = (timezone: string): string => {
|
||||
return TIMEZONE_LEGACY[timezone] ?? timezone;
|
||||
};
|
||||
|
||||
return {
|
||||
timezone,
|
||||
saveTimezone,
|
||||
formatTimezoneDate,
|
||||
localTimeZone,
|
||||
toUtc,
|
||||
fromUtc,
|
||||
localToUtc,
|
||||
localFromUtc,
|
||||
saveTimezone,
|
||||
formatTimezoneDate,
|
||||
formatSeriesTimezone,
|
||||
canonicalizeTimezone,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user