Repository navigation
Use user timezone #93447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Use user timezone #93447
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6fd07cd
use user timezone
luacmartins 184f122
simplify parser
luacmartins dce116d
rm stripTrailingTimezone
luacmartins dbd3936
replace text at render time
luacmartins 090e519
rm usememo
luacmartins 7783cb8
rename method
luacmartins f84c8e9
rename method
luacmartins 3d297ba
rename method
luacmartins cf50483
rm unnecessary functions
luacmartins d4f5286
update tests
luacmartins d4c4b62
fix prettier
luacmartins 013b029
fix knip
luacmartins 20b4a48
fix spellcheck
luacmartins 7b4f528
update cartesian charts
luacmartins 7336805
update format
luacmartins 58b034a
simplify code
luacmartins 31e6e2b
simplify code
luacmartins baec8e9
use hook
luacmartins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
57 changes: 57 additions & 0 deletions
57
...LEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/localizeVictoryChartLabelText.ts
This file contains hidden or 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,57 @@ | ||
| import {isValid, parse} from 'date-fns'; | ||
| import DateUtils from '@libs/DateUtils'; | ||
| import CONST from '@src/CONST'; | ||
| import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails'; | ||
|
|
||
| const AS_OF_LABEL_PATTERN = /^As of:\s*(.+)$/i; | ||
| const CHART_DISPLAY_FORMAT = `MMM d, yyyy 'at' ${CONST.DATE.LOCAL_TIME_FORMAT}`; | ||
|
|
||
| /** Matches server `EXP_CHAT_COMMENT_DATETIME` (`M j, Y \a\t h:i A`) with and without a leading zero on the hour. */ | ||
| const SERVER_AS_OF_PARSE_FORMATS = ['MMM d, yyyy hh:mm aa', 'MMM d, yyyy h:mm aa'] as const; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make sure that server formatted date doesn't have timezone at the end (i.e. "As of: May 30, 2026 12:30 PM PT" |
||
|
|
||
| /** | ||
| * Parses a Victory chart "As of" datetime label text into a UTC date. | ||
| */ | ||
| function parseDateAsUTC(sourceText: string): Date | null { | ||
| const normalizedText = sourceText | ||
| .trim() | ||
| .replace(/\s+at\s+/i, ' ') | ||
| .trim(); | ||
|
|
||
| for (const formatStr of SERVER_AS_OF_PARSE_FORMATS) { | ||
| const parsed = parse(normalizedText, formatStr, new Date()); | ||
|
luacmartins marked this conversation as resolved.
Resolved
|
||
|
|
||
| if (!isValid(parsed)) { | ||
| continue; | ||
| } | ||
|
|
||
| return new Date(Date.UTC(parsed.getFullYear(), parsed.getMonth(), parsed.getDate(), parsed.getHours(), parsed.getMinutes(), parsed.getSeconds())); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Rewrites a `<victorylabel>` "As of: ..." string in the viewer's timezone. | ||
| * Returns the original text when the label does not match or cannot be parsed. | ||
| */ | ||
| function getLocalizedVictoryChartLabelText(text: string, timezone?: SelectedTimezone): string { | ||
| if (!timezone) { | ||
| return text; | ||
| } | ||
|
|
||
| const match = text.trim().match(AS_OF_LABEL_PATTERN); | ||
| if (!match) { | ||
| return text; | ||
| } | ||
|
|
||
| const utcDate = parseDateAsUTC(match[1]); | ||
| if (!utcDate) { | ||
| return text; | ||
| } | ||
|
|
||
| const localizedDate = DateUtils.formatInTimeZoneWithFallback(utcDate, timezone, CHART_DISPLAY_FORMAT); | ||
| return `As of: ${localizedDate}`; | ||
| } | ||
|
|
||
| export {getLocalizedVictoryChartLabelText, parseDateAsUTC}; | ||
This file contains hidden or 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,11 @@ | ||
| import DateUtils from '@libs/DateUtils'; | ||
| import CONST from '@src/CONST'; | ||
| import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails'; | ||
| import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; | ||
|
|
||
| function useCurrentTimezone(): SelectedTimezone { | ||
| const currentUserPersonalDetails = useCurrentUserPersonalDetails(); | ||
| return DateUtils.getCurrentTimezone(currentUserPersonalDetails?.timezone ?? CONST.DEFAULT_TIME_ZONE).selected; | ||
| } | ||
|
|
||
| export default useCurrentTimezone; |
47 changes: 47 additions & 0 deletions
47
tests/unit/components/HTMLEngineProvider/localizeVictoryChartLabelText.test.ts
This file contains hidden or 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,47 @@ | ||
| import {getLocalizedVictoryChartLabelText, parseDateAsUTC} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/localizeVictoryChartLabelText'; | ||
|
|
||
| describe('localizeVictoryChartLabelText', () => { | ||
| describe('parseDateAsUTC', () => { | ||
| it('parses server UTC chart timestamps', () => { | ||
| const utcDate = parseDateAsUTC('Jun 5, 2026 at 06:47 PM'); | ||
| expect(utcDate?.toISOString()).toBe('2026-06-05T18:47:00.000Z'); | ||
| }); | ||
|
|
||
| it('parses server UTC chart timestamps without a leading zero on the hour', () => { | ||
| const utcDate = parseDateAsUTC('Jun 12, 2026 at 8:48 AM'); | ||
| expect(utcDate?.toISOString()).toBe('2026-06-12T08:48:00.000Z'); | ||
| }); | ||
|
|
||
| it('parses server UTC chart timestamps with a leading zero on the hour', () => { | ||
| expect(parseDateAsUTC('Jun 12, 2026 at 08:48 AM')?.toISOString()).toBe('2026-06-12T08:48:00.000Z'); | ||
| expect(parseDateAsUTC('Jun 12, 2026 at 02:48 PM')?.toISOString()).toBe('2026-06-12T14:48:00.000Z'); | ||
| }); | ||
|
|
||
| it('returns null for invalid timestamps', () => { | ||
| expect(parseDateAsUTC('not a date')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getLocalizedVictoryChartLabelText', () => { | ||
| it('rewrites As of labels in the viewer timezone without a timezone label', () => { | ||
| expect(getLocalizedVictoryChartLabelText('As of: Jun 5, 2026 at 06:47 PM', 'America/Los_Angeles')).toBe('As of: Jun 5, 2026 at 11:47 AM'); | ||
| expect(getLocalizedVictoryChartLabelText('As of: Jun 5, 2026 at 06:47 PM', 'Asia/Tokyo')).toBe('As of: Jun 6, 2026 at 3:47 AM'); | ||
| expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 8:48 AM', 'America/Edmonton')).toBe('As of: Jun 12, 2026 at 2:48 AM'); | ||
| expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 8:48 AM', 'Asia/Tokyo')).toBe('As of: Jun 12, 2026 at 5:48 PM'); | ||
| expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 02:48 PM', 'America/Edmonton')).toBe('As of: Jun 12, 2026 at 8:48 AM'); | ||
| expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 02:48 PM', 'Asia/Tokyo')).toBe('As of: Jun 12, 2026 at 11:48 PM'); | ||
| }); | ||
|
|
||
| it('leaves non-As-of labels unchanged', () => { | ||
| expect(getLocalizedVictoryChartLabelText('Top employees by spend', 'America/Los_Angeles')).toBe('Top employees by spend'); | ||
| }); | ||
|
|
||
| it('returns the original text when no timezone is provided', () => { | ||
| expect(getLocalizedVictoryChartLabelText('As of: Jun 5, 2026 at 06:47 PM')).toBe('As of: Jun 5, 2026 at 06:47 PM'); | ||
| }); | ||
|
|
||
| it('returns the original text when the timestamp cannot be parsed', () => { | ||
| expect(getLocalizedVictoryChartLabelText('As of: not a date', 'America/Los_Angeles')).toBe('As of: not a date'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.