forked from awslabs/iot-app-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: timezone support via gettimeoffset awslabs#2663
- Loading branch information
chandrashekhara.n
authored and
chandrashekhara.n
committed
May 8, 2024
1 parent
7fad147
commit 154ea32
Showing
22 changed files
with
141 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/react-components/src/components/chart/chartOptions/tooltip/time.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import React from 'react'; | ||
import { format } from 'date-fns'; | ||
import { fontWeightHeadingS } from '@cloudscape-design/design-tokens'; | ||
import { FULL_DATE } from '../../../../utils/time'; | ||
import { DateTime } from '../../../timeZone'; | ||
|
||
export type XYPlotTooltipTimeOptions = { | ||
time?: number; | ||
}; | ||
export const XYPlotTooltipTime = ({ time }: XYPlotTooltipTimeOptions) => ( | ||
<span style={{ fontWeight: fontWeightHeadingS }}> | ||
{time ? format(new Date(time), FULL_DATE) : ''} | ||
{time ? <DateTime dateTime={time} pattern={FULL_DATE} /> : ''} | ||
</span> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
...nts/src/components/chart/legend/table/columnDefinitions/trendCursor/trendCursorHeader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './timeZone'; |
44 changes: 44 additions & 0 deletions
44
packages/react-components/src/components/timeZone/timeZone.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React, { createContext, useContext } from 'react'; | ||
|
||
import { utcToZonedTime, format } from 'date-fns-tz'; | ||
import { FULL_DATE_TIME } from '../../utils/time'; | ||
|
||
// https://date-fns.org/v3.6.0/docs/Time-Zones#date-fns-tz | ||
// converts a utc date to a formatted string in a specific timeZone | ||
export const formatDate = ( | ||
dateTime: number, | ||
{ timeZone, pattern }: { timeZone: string; pattern: string } | ||
) => { | ||
const zonedDate = utcToZonedTime(new Date(dateTime).toISOString(), timeZone); | ||
const formattedDate = format(zonedDate, pattern, { timeZone: timeZone }); | ||
|
||
return formattedDate; | ||
}; | ||
|
||
// Helper components for use in a React Context | ||
export type DateTimeFormatContextOptions = { | ||
timeZone: string; | ||
}; | ||
export const DateTimeFormatContext = | ||
createContext<DateTimeFormatContextOptions>({ | ||
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone, | ||
}); | ||
|
||
export type DateTimeOptions = { | ||
dateTime: number; | ||
pattern?: string; | ||
}; | ||
export const DateTime = ({ dateTime, pattern }: DateTimeOptions) => { | ||
const formattedDate = useDateTime(dateTime, pattern); | ||
|
||
return <>{formattedDate}</>; | ||
}; | ||
|
||
export const useDateTime = (dateTime: number, pattern?: string) => { | ||
const dateTimeFormatPattern = pattern ?? FULL_DATE_TIME; | ||
const { timeZone } = useContext(DateTimeFormatContext); | ||
return formatDate(dateTime, { | ||
timeZone, | ||
pattern: dateTimeFormatPattern, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/react-components/src/echarts/extensions/trendCursors/view/utils/dateTime.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { formatDate } from '../../../../../components/timeZone'; | ||
|
||
export const getDateTime = (date: number) => { | ||
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
const formatedDate = formatDate(date, { | ||
timeZone: timeZone, | ||
pattern: 'dd/MM/yyyy HH:mm:ss', | ||
}); | ||
return formatedDate; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.