Skip to content

Commit

Permalink
feat: timezone support via gettimeoffset awslabs#2663
Browse files Browse the repository at this point in the history
  • Loading branch information
chandrashekhara.n authored and chandrashekhara.n committed Mar 14, 2024
1 parent 229e8f4 commit c7adc6a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/react-components/src/components/chart/baseChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { useChartConfiguration } from './chartOptions/useChartConfiguration';

import './chart.css';
import { useTrendCursors } from '../../echarts/extensions/trendCursors';
import { getAdjustedTime } from '../../utils/time';
import { useChartStoreDataStreamsSync } from './hooks/useChartStoreDataStreamsSync';
import useIsRefreshing from './hooks/useIsrefreshing';
import { convertViewportToMs } from './viewport/convertViewportToMs';
Expand Down Expand Up @@ -60,8 +61,8 @@ const BaseChart = ({

// Convert viewport timestamps to ms
const { initial, end } = convertViewportToMs(viewport);
const timestampStart = new Date(initial).toLocaleString();
const timestampEnd = new Date(end).toLocaleString();
const timestampStart = getAdjustedTime(new Date(initial)).toLocaleString();
const timestampEnd = getAdjustedTime(new Date(end)).toLocaleString();

// Setup instance of echarts
const { ref, chartRef } = useECharts(options?.theme);
Expand Down
21 changes: 21 additions & 0 deletions packages/react-components/src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,24 @@ export const parseDuration = (duration: number | string): number => {
// if duration is a string but we cannot parse it, we default to 10 mins.
return parsedTime != null ? parsedTime : 10 * MINUTE_IN_MS;
};

type TimeOffsetFunction = (date: Date) => number;

export const getAdjustedTime = (
date: Date,
getTimeOffset?: TimeOffsetFunction
): Date => {
let timezoneOffset: number;

if (getTimeOffset) {
timezoneOffset = getTimeOffset(date);
} else {
timezoneOffset = new Date().getTimezoneOffset();
}

return new Date(date.getTime() + timezoneOffset * 60000);
};

export const getTimeOffset = (date: Date): number => {
return date.getTimezoneOffset();
};

0 comments on commit c7adc6a

Please sign in to comment.