-
Notifications
You must be signed in to change notification settings - Fork 72
[LG-5532] feat(time-input): Segment state utils #3385
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
Open
shaneeza
wants to merge
3
commits into
LG-5532/segments-display-values
Choose a base branch
from
LG-5532/segments-state-utils
base: LG-5532/segments-display-values
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
87fa353
feat(date-utils): add newUTCFromTimeZone function to create UTC dates…
shaneeza bf0c1a9
fix(time-input): update minExplicitValue for hour segment to ensure c…
shaneeza fdcbd44
fix(time-input): correct typos in test descriptions for segment valid…
shaneeza 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { newUTCFromTimeZone } from './newUTCFromTimeZone'; |
54 changes: 54 additions & 0 deletions
54
packages/date-utils/src/newUTCFromTimeZone/newUTCFromTimeZone.spec.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,54 @@ | ||
| import { newUTCFromTimeZone } from './newUTCFromTimeZone'; | ||
|
|
||
| describe('packages/date-utils/newUTCFromTimeZone', () => { | ||
| describe('UTC', () => { | ||
| test('creates a new UTC date from a given time zone', () => { | ||
| const date = newUTCFromTimeZone({ | ||
| year: '2026', | ||
| month: '02', | ||
| day: '20', | ||
| hour: '23', | ||
| minute: '00', | ||
| second: '00', | ||
| timeZone: 'UTC', | ||
| }); | ||
|
|
||
| // February 20, 2026 11:00:00 PM/23:00:00 in UTC is February 20, 2026 23:00:00 UTC | ||
| expect(date).toEqual(new Date('2026-02-20T23:00:00Z')); | ||
| }); | ||
| }); | ||
|
|
||
| describe('America/New_York', () => { | ||
| test('creates a new UTC date from a given time zone', () => { | ||
| const date = newUTCFromTimeZone({ | ||
| year: '2026', | ||
| month: '02', | ||
| day: '20', | ||
| hour: '23', | ||
| minute: '00', | ||
| second: '00', | ||
| timeZone: 'America/New_York', | ||
| }); | ||
|
|
||
| // February 20, 2026 11:00:00 PM/23:00:00 in America/New_York is February 21, 2026 04:00:00 UTC (UTC-5 hours) | ||
| expect(date).toEqual(new Date('2026-02-21T04:00:00Z')); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Pacific/Kiritimati', () => { | ||
| test('creates a new UTC date from a given time zone', () => { | ||
| const date = newUTCFromTimeZone({ | ||
| year: '2026', | ||
| month: '02', | ||
| day: '20', | ||
| hour: '23', | ||
| minute: '00', | ||
| second: '00', | ||
| timeZone: 'Pacific/Kiritimati', | ||
| }); | ||
|
|
||
| // February 20, 2026 11:00:00 PM/23:00:00 in Pacific/Kiritimati is February 20, 2026 09:00:00 UTC (UTC+14 hours) | ||
| expect(date).toEqual(new Date('2026-02-20T09:00:00Z')); | ||
| }); | ||
| }); | ||
| }); |
50 changes: 50 additions & 0 deletions
50
packages/date-utils/src/newUTCFromTimeZone/newUTCFromTimeZone.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,50 @@ | ||
| import { zonedTimeToUtc } from 'date-fns-tz'; | ||
|
|
||
| /** | ||
| * Creates a new UTC date from a given time zone. | ||
| * This takes the local date created above and converts it to UTC using the `zonedTimeToUtc` helper function. | ||
| * | ||
| * @param year - The year | ||
| * @param month - The month (1-12) | ||
| * @param day - The day | ||
| * @param hour - The hour in 24 hour format | ||
| * @param minute - The minute | ||
| * @param second - The second | ||
| * @param timeZone - The time zone | ||
| * @returns The new UTC date | ||
| * | ||
| * @example | ||
| * ```js | ||
| * // February 20, 2026 11:00:00 PM/23:00:00 in America/New_York is February 21, 2026 04:00:00 UTC | ||
| * newUTCFromTimeZone({ year: '2026', month: '02', day: '20', hour: '11', minute: '00', second: '00', timeZone: 'America/New_York' }); | ||
| * // returns new Date('2026-02-21T04:00:00Z') | ||
| * ``` | ||
| */ | ||
| export const newUTCFromTimeZone = ({ | ||
| year, | ||
| month, | ||
| day, | ||
| hour, | ||
| minute, | ||
| second, | ||
| timeZone, | ||
| }: { | ||
| year: string; | ||
| month: string; | ||
| day: string; | ||
| hour: string; | ||
| minute: string; | ||
| second: string; | ||
| timeZone: string; | ||
| }) => { | ||
| const newDate = new Date( | ||
| Number(year), | ||
| Number(month) - 1, | ||
| Number(day), | ||
| Number(hour), | ||
| Number(minute), | ||
| Number(second), | ||
| ); | ||
|
|
||
| return zonedTimeToUtc(newDate, timeZone); | ||
| }; |
31 changes: 31 additions & 0 deletions
31
packages/time-input/src/utils/convert12hTo24h/convert12hTo24h.spec.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,31 @@ | ||
| import range from 'lodash/range'; | ||
|
|
||
| import { convert12hTo24h } from './convert12hTo24h'; | ||
|
|
||
| describe('convert12hTo24h', () => { | ||
| describe('AM', () => { | ||
| test('12 AM converts to 0', () => { | ||
| expect(convert12hTo24h('12', 'AM')).toEqual('0'); | ||
| }); | ||
|
|
||
| test.each(range(1, 12).map(i => [i, i]))( | ||
| '%i AM converts to %i', | ||
| (input, expected) => { | ||
| expect(convert12hTo24h(`${input}`, 'AM')).toEqual(`${expected}`); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| describe('PM', () => { | ||
| test('12 PM converts to 12', () => { | ||
| expect(convert12hTo24h('12', 'PM')).toEqual('12'); | ||
| }); | ||
|
|
||
| test.each(range(1, 12).map(i => [i, i + 12]))( | ||
| '%i PM converts to %i', | ||
| (input, expected) => { | ||
| expect(convert12hTo24h(`${input}`, 'PM')).toEqual(`${expected}`); | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
36 changes: 36 additions & 0 deletions
36
packages/time-input/src/utils/convert12hTo24h/convert12hTo24h.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,36 @@ | ||
| /** | ||
| * Converts a 12 hour format hour to a 24 hour format hour | ||
| * | ||
| * @example | ||
| * ```js | ||
| * convert12hTo24h('12', 'AM'); // '0' | ||
| * convert12hTo24h('12', 'PM'); // '12' | ||
| * convert12hTo24h('1', 'AM'); // '1' | ||
| * convert12hTo24h('1', 'PM'); // '13' | ||
| * ``` | ||
| * | ||
| * @param hour - The hour to convert | ||
| * @param dayPeriod - The day period to use for the conversion (AM or PM) | ||
| * @returns The converted hour | ||
| */ | ||
| export const convert12hTo24h = (hour: string, dayPeriod: string) => { | ||
| if (hour === '') return hour; | ||
|
|
||
| // if dayPeriod is AM and hour is 12, return 0 since 12 AM is 00:00 | ||
| if (dayPeriod === 'AM') { | ||
| if (hour === '12') { | ||
| return '0'; | ||
| } | ||
|
|
||
| // else return hour as-is | ||
| return hour; | ||
| } | ||
|
|
||
| // if dayPeriod is PM and hour is 12, return 12 since 12 PM is 12:00 | ||
| if (hour === '12') { | ||
| return '12'; | ||
| } | ||
|
|
||
| // else return hour + 12 | ||
| return `${parseInt(hour) + 12}`; | ||
| }; |
21 changes: 21 additions & 0 deletions
21
packages/time-input/src/utils/doesSomeSegmentExist/doesSomeSegmentExist.spec.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,21 @@ | ||
| import { doesSomeSegmentExist } from './doesSomeSegmentExist'; | ||
|
|
||
| describe('doesSomeSegmentExist', () => { | ||
| test('returns true if at least one segment is filled', () => { | ||
| expect(doesSomeSegmentExist({ hour: '', minute: '', second: '00' })).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| test('returns true if at all segments are filled', () => { | ||
| expect( | ||
| doesSomeSegmentExist({ hour: '12', minute: '00', second: '00' }), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| test('returns false if no segments are filled', () => { | ||
| expect(doesSomeSegmentExist({ hour: '', minute: '', second: '' })).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
| }); | ||
12 changes: 12 additions & 0 deletions
12
packages/time-input/src/utils/doesSomeSegmentExist/doesSomeSegmentExist.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,12 @@ | ||
| import { TimeSegmentsState } from '../../shared.types'; | ||
|
|
||
| /** | ||
| * Checks if some segment exists | ||
| * | ||
| * @param segments - The segments to check | ||
| * @returns Whether some segment exists | ||
| */ | ||
| export const doesSomeSegmentExist = (segments: TimeSegmentsState) => { | ||
| // check if all segments are not empty | ||
| return Object.values(segments).some(segment => segment !== ''); | ||
| }; |
18 changes: 18 additions & 0 deletions
18
packages/time-input/src/utils/findUnitOptionByDayPeriod/findUnitOptionByDayPeriod.spec.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,18 @@ | ||
| import { findUnitOptionByDayPeriod } from './findUnitOptionByDayPeriod'; | ||
|
|
||
| describe('packages/time-input/utils/findUnitOptionByDayPeriod', () => { | ||
| test('returns the unit option by day period', () => { | ||
| expect( | ||
| findUnitOptionByDayPeriod('AM', [ | ||
| { displayName: 'AM', value: 'AM' }, | ||
| { displayName: 'PM', value: 'PM' }, | ||
| ]), | ||
| ).toEqual({ displayName: 'AM', value: 'AM' }); | ||
| expect( | ||
| findUnitOptionByDayPeriod('PM', [ | ||
| { displayName: 'AM', value: 'AM' }, | ||
| { displayName: 'PM', value: 'PM' }, | ||
| ]), | ||
| ).toEqual({ displayName: 'PM', value: 'PM' }); | ||
| }); | ||
| }); |
18 changes: 18 additions & 0 deletions
18
packages/time-input/src/utils/findUnitOptionByDayPeriod/findUnitOptionByDayPeriod.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,18 @@ | ||
| import { UnitOption } from '../../TimeInputSelect/TimeInputSelect.types'; | ||
|
|
||
| /** | ||
| * Finds the select unit option based on the day period. | ||
| * | ||
| * @param dayPeriod - The day period to use for the select unit. | ||
| * @param unitOptions - The valid unit options to use for the select unit. | ||
| * @returns The select unit option. | ||
| */ | ||
| export const findUnitOptionByDayPeriod = ( | ||
| dayPeriod: string, | ||
| unitOptions: Array<UnitOption>, | ||
| ): UnitOption => { | ||
| const selectUnitOption = unitOptions.find( | ||
| option => option.displayName === dayPeriod, | ||
| ) as UnitOption; | ||
| return selectUnitOption; | ||
| }; |
42 changes: 42 additions & 0 deletions
42
packages/time-input/src/utils/getFormattedTimeSegments/getFormattedTimeSegments.spec.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,42 @@ | ||
| import { getFormattedTimeSegments } from './getFormattedTimeSegments'; | ||
|
|
||
| describe('packages/time-input/utils/getFormattedTimeSegments', () => { | ||
| test('returns the formatted time segments if all segments are 0', () => { | ||
| const formattedTimeSegments = getFormattedTimeSegments({ | ||
| hour: '0', | ||
| minute: '0', | ||
| second: '0', | ||
| }); | ||
| expect(formattedTimeSegments).toEqual({ | ||
| hour: '00', | ||
| minute: '00', | ||
| second: '00', | ||
| }); | ||
| }); | ||
|
|
||
| test('returns the formatted time segments', () => { | ||
| const formattedTimeSegments = getFormattedTimeSegments({ | ||
| hour: '2', | ||
| minute: '3', | ||
| second: '1', | ||
| }); | ||
| expect(formattedTimeSegments).toEqual({ | ||
| hour: '02', | ||
| minute: '03', | ||
| second: '01', | ||
| }); | ||
| }); | ||
|
|
||
| test('does not format segments that are already formatted', () => { | ||
| const formattedTimeSegments = getFormattedTimeSegments({ | ||
| hour: '02', | ||
| minute: '03', | ||
| second: '01', | ||
| }); | ||
| expect(formattedTimeSegments).toEqual({ | ||
| hour: '02', | ||
| minute: '03', | ||
| second: '01', | ||
| }); | ||
| }); | ||
| }); |
28 changes: 28 additions & 0 deletions
28
packages/time-input/src/utils/getFormattedTimeSegments/getFormattedTimeSegments.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,28 @@ | ||
| import { getValueFormatter } from '@leafygreen-ui/input-box'; | ||
|
|
||
| import { TimeSegmentsState } from '../../shared.types'; | ||
|
|
||
| /** | ||
| * Formats the time segments to a string with 2 digits for each segment. | ||
| * | ||
| * @param segments - The time segments to format | ||
| * @returns The formatted time segments | ||
| * | ||
| * @example | ||
| * ```js | ||
| * getFormattedTimeSegments({ hour: '2', minute: '30', second: '0' }); | ||
| * // returns: { hour: '02', minute: '30', second: '00' } | ||
| * ``` | ||
| */ | ||
| export const getFormattedTimeSegments = (segments: TimeSegmentsState) => { | ||
| const hour = getValueFormatter({ charsCount: 2, allowZero: true })( | ||
| segments.hour, | ||
| ); | ||
| const minute = getValueFormatter({ charsCount: 2, allowZero: true })( | ||
| segments.minute, | ||
| ); | ||
| const second = getValueFormatter({ charsCount: 2, allowZero: true })( | ||
| segments.second, | ||
| ); | ||
| return { hour, minute, second }; | ||
| }; |
16 changes: 16 additions & 0 deletions
16
...input/src/utils/getFormattedTimeSegmentsFromDate/getFormattedTimeSegmentsFromDate.spec.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,16 @@ | ||
| import { getFormattedTimeSegmentsFromDate } from './getFormattedTimeSegmentsFromDate'; | ||
|
|
||
| describe('packages/time-input/utils/getFormattedTimeSegmentsFromDate', () => { | ||
| test('returns the formatted time segments from a date', () => { | ||
| const formattedTimeSegments = getFormattedTimeSegmentsFromDate( | ||
| new Date('2025-01-01T01:00:00Z'), | ||
| 'en-US', | ||
| 'America/New_York', | ||
| ); | ||
| expect(formattedTimeSegments).toEqual({ | ||
| hour: '08', | ||
| minute: '00', | ||
| second: '00', | ||
| }); | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
...time-input/src/utils/getFormattedTimeSegmentsFromDate/getFormattedTimeSegmentsFromDate.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,33 @@ | ||
| import { DateType, LocaleString } from '@leafygreen-ui/date-utils'; | ||
|
|
||
| import { TimeSegmentsState } from '../../shared.types'; | ||
| import { getFormatPartsValues } from '../getFormatPartsValues/getFormatPartsValues'; | ||
| import { getFormattedTimeSegments } from '../getFormattedTimeSegments/getFormattedTimeSegments'; | ||
|
|
||
| /** | ||
| * Gets the formatted time segments from a date | ||
| * | ||
| * @param date - The date to get the formatted time segments from | ||
| * @param locale - The locale to use | ||
| * @param timeZone - The time zone to use | ||
| * @returns The formatted time segments | ||
| * | ||
| * @example | ||
| * ```js | ||
| * getFormattedTimeSegmentsFromDate(new Date('2025-01-01T12:00:00Z'), 'en-US', 'America/New_York'); | ||
| * // returns: { hour: '12', minute: '00', second: '00' } | ||
| * ``` | ||
| */ | ||
| export const getFormattedTimeSegmentsFromDate = ( | ||
| date: DateType, | ||
| locale: LocaleString, | ||
| timeZone: string, | ||
| ): TimeSegmentsState => { | ||
| const { hour, minute, second } = getFormatPartsValues({ | ||
| locale, | ||
| timeZone, | ||
| value: date, | ||
| }); | ||
|
|
||
| return getFormattedTimeSegments({ hour, minute, second }); | ||
| }; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The phrase 'at all segments' should be 'all segments' to be grammatically correct.