($time: string)
| 1 | export function parseTime($time: string): [number, number] { |
| 2 | const parts = $time.split(':').slice(0, 2); |
| 3 | const lowercased = $time.trim().toLowerCase(); |
| 4 | const isAM = lowercased.endsWith('am') || lowercased.endsWith('a.m.'); |
| 5 | const isPM = lowercased.endsWith('pm') || lowercased.endsWith('p.m.'); |
| 6 | |
| 7 | let hours = parts.length > 0 ? parseInt(parts[0]) : 0; |
| 8 | if (isNaN(hours) || hours > 23) { |
| 9 | hours = 0; |
| 10 | } |
| 11 | if (isAM && hours === 12) { |
| 12 | hours = 0; |
| 13 | } |
| 14 | if (isPM && hours < 12) { |
| 15 | hours += 12; |
| 16 | } |
| 17 | |
| 18 | let minutes = parts.length > 1 ? parseInt(parts[1]) : 0; |
| 19 | if (isNaN(minutes) || minutes > 59) { |
| 20 | minutes = 0; |
| 21 | } |
| 22 | |
| 23 | return [hours, minutes]; |
| 24 | } |
| 25 | |
| 26 | function parse24HTime(time: string): number[] { |
| 27 | return time.split(':').map((x) => parseInt(x)); |
no outgoing calls
no test coverage detected