js-temporal / proposal-temporal-v2

Future additions to Temporal
MIT License
24 stars 1 forks source link

Add resolver option(s) to support ID parsing for custom calendars and time zones #17

Open ptomato opened 3 years ago

ptomato commented 3 years ago

This is a niche use case, but if you create a custom calendar or time zone, there is currently no way to deserialize it, or Temporal objects including it, from a string.

A proposal is described in the closed issue https://github.com/tc39/proposal-temporal/issues/1423#issue-823754198. To summarize that proposal in some code examples:

class CustomCalendar {
  toString() { return 'custom-calendar'; }
  // ... implementation of some custom calendar ...
}
const isoString = '2021-04-28[u-ca=custom-calendar]';

function calendarResolver(id) {
  if (id === 'custom-calendar') return new CustomCalendar();
  return Temporal.Calendar.from(id);
}

// Without calendarResolver, this would throw RangeError: invalid calendar 'custom-calendar'
Temporal.PlainDate.from(isoString, { calendarResolver })

// Same here
time = Temporal.PlainTime.from('12:00');
time.withPlainDate(isoString, { calendarResolver })

Examples for custom time zones not given here, but analogous to the above.

Advantages:

Concerns:

Prior art:

None so far.

Constraints / corner cases: