js-temporal / temporal-polyfill

Polyfill for Temporal (under construction)
ISC License
529 stars 28 forks source link

Unable to handle valid ISO 8601 week format #145

Open zzal opened 2 years ago

zzal commented 2 years ago

Version "@js-temporal/polyfill": "0.4.0"

Just tried: Temporal.PlainDate.from('2022-W12'), where 2022-W12 is ISO week-numbering format. Receiving: Uncaught RangeError: invalid ISO 8601 string: 2022-W12

ISO 8601 source: https://en.wikipedia.org/wiki/ISO_8601#Week_dates

Don't know how important this issue is, or performance hit would result implementing it, but error could have message like unsupported ISO 8601 string?

ptomato commented 2 years ago

Thanks! See https://github.com/js-temporal/proposal-temporal-v2/issues/11 for potential future support of this. Until such a proposal is adopted, I like the suggestion to detect the format and issue a more informative error message.

(Note that you'd have to add a day number to the string to be able to construct a PlainDate out of it, e.g. 2022-W12-5)

khawarizmus commented 1 year ago

@ptomato What is the alternative if we want to use this format in our code?

ptomato commented 1 year ago

For YYYY-WWW-D you would have to write a parser for the format, and then calculate the year-month-day to construct a PlainDate. The latter you can find many algorithms online for it, it's basically the reverse of https://tc39.es/proposal-temporal/#sec-temporal-toisoweekofyear. For the parser, you'd have to decide what variations of the format would be needed for your application. It could be as simple as /^([0-9]{4})-W([0-9]{2})-([0-9])$/ or you might need to accept YYYYWWWD without dashes, or ±YYYYYY-WWW-D with extended years, or YYYY-WWW-D[u-ca=CCCC] with a calendar annotation, etc.

For YYYY-WWW, I guess you'd need to decide what kind of calculations you want to do with it, and potentially create your own PlainYearWeek type.