Skip to content

Commit

Permalink
chore: fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Oct 24, 2024
1 parent a3b9f03 commit f5e7f1c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 0 additions & 1 deletion projects/cdk/date-time/test/time.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ describe('TuiTime', () => {
['12:00 AM', {hours: 0, minutes: 0}],
['12:34 AM', {hours: 0, minutes: 34}],
['12:59 AM', {hours: 0, minutes: 59}],
['12:59 AM', {hours: 0, minutes: 59}],
['01:00 AM', {hours: 1, minutes: 0}],
['11:00 AM', {hours: 11, minutes: 0}],
['11:59 AM', {hours: 11, minutes: 59}],
Expand Down
15 changes: 11 additions & 4 deletions projects/cdk/date-time/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,27 @@ export class TuiTime implements TuiTimeLike {
);
}

private static parseMeridiemPeriod(time: string): 'AM' | 'PM' | null {
return (
(/[AP]M/.exec(time.toUpperCase().replaceAll(/\W/g, ''))?.[0] as
| 'AM'
| 'PM') || null
);
}

private static parseHours(time: string): number {
const hours = Number(time.slice(0, 2));
const [meridiem = ''] =
/[ap]m/.exec(time.toLowerCase().replaceAll(/\W/g, '')) || [];
const meridiem = this.parseMeridiemPeriod(time);

if (!meridiem) {
return hours;
}

if (hours === 12) {
return meridiem === 'am' ? 0 : 12;
return meridiem === 'AM' ? 0 : 12;
}

return meridiem === 'pm' ? hours + 12 : hours;
return meridiem === 'PM' ? hours + 12 : hours;
}

/**
Expand Down

0 comments on commit f5e7f1c

Please sign in to comment.