( fieldOptions: FieldOptions, options: FormatterOptions )
| 182 | }; |
| 183 | |
| 184 | export function getFormatOptions( |
| 185 | fieldOptions: FieldOptions, |
| 186 | options: FormatterOptions |
| 187 | ): Intl.DateTimeFormatOptions { |
| 188 | let defaultFieldOptions = options.shouldForceLeadingZeros |
| 189 | ? TWO_DIGIT_FIELD_OPTIONS |
| 190 | : DEFAULT_FIELD_OPTIONS; |
| 191 | fieldOptions = {...defaultFieldOptions, ...fieldOptions}; |
| 192 | let granularity = options.granularity || 'minute'; |
| 193 | let keys = Object.keys(fieldOptions); |
| 194 | let startIdx = keys.indexOf(options.maxGranularity ?? 'year'); |
| 195 | if (startIdx < 0) { |
| 196 | startIdx = 0; |
| 197 | } |
| 198 | |
| 199 | let endIdx = keys.indexOf(granularity); |
| 200 | if (endIdx < 0) { |
| 201 | endIdx = 2; |
| 202 | } |
| 203 | |
| 204 | if (startIdx > endIdx) { |
| 205 | throw new Error('maxGranularity must be greater than granularity'); |
| 206 | } |
| 207 | |
| 208 | let opts: Intl.DateTimeFormatOptions = keys.slice(startIdx, endIdx + 1).reduce((opts, key) => { |
| 209 | opts[key] = fieldOptions[key]; |
| 210 | return opts; |
| 211 | }, {}); |
| 212 | |
| 213 | if (options.hourCycle != null) { |
| 214 | opts.hour12 = options.hourCycle === 12; |
| 215 | } |
| 216 | |
| 217 | opts.timeZone = options.timeZone || 'UTC'; |
| 218 | |
| 219 | let hasTime = granularity === 'hour' || granularity === 'minute' || granularity === 'second'; |
| 220 | if (hasTime && options.timeZone && !options.hideTimeZone) { |
| 221 | opts.timeZoneName = 'short'; |
| 222 | } |
| 223 | |
| 224 | if (options.showEra && startIdx === 0) { |
| 225 | opts.era = 'short'; |
| 226 | } |
| 227 | |
| 228 | return opts; |
| 229 | } |
| 230 | |
| 231 | export function getPlaceholderTime(placeholderValue: DateValue | null | undefined): TimeValue { |
| 232 | if (placeholderValue && 'hour' in placeholderValue) { |
no test coverage detected