| 26 | } |
| 27 | |
| 28 | function detectLocaleFormat(locale?: string): { segments: SegmentConfig[]; separator: string } { |
| 29 | const fmt = new Intl.DateTimeFormat(locale, { |
| 30 | year: 'numeric', |
| 31 | month: '2-digit', |
| 32 | day: '2-digit', |
| 33 | }); |
| 34 | const parts = fmt.formatToParts(new Date(2000, 0, 15)); |
| 35 | const segments: SegmentConfig[] = []; |
| 36 | let separator = '/'; |
| 37 | for (const p of parts) { |
| 38 | if (p.type === 'literal') { |
| 39 | separator = p.value.trim() || p.value; |
| 40 | } else if (p.type === 'year' || p.type === 'month' || p.type === 'day') { |
| 41 | segments.push({ type: p.type, len: p.type === 'year' ? 4 : 2 }); |
| 42 | } |
| 43 | } |
| 44 | return { segments, separator }; |
| 45 | } |
| 46 | |
| 47 | /** Parse string segment values to numbers, treating blanks as 0 */ |
| 48 | function parseSegmentToNum(s: string): number { |