Skip to content

Clarifying the expected behavior for non-gregorian calendars in DateTimeFormat.formatToParts #225

Open
@jackhorton

Description

@jackhorton

I ran into an implementation issue a few days ago in Chakra where calling formatToParts on a date from a non-Gregorian calendar can cause some confusion. Right now, V8 and Chakra agree that new Intl.DateTimeFormat("en-u-ca-chinese", { year: "numeric" }).format() produces the string "2018(wu-xu)", while SpiderMonkey returns the string "35". As a result, when formatting to parts, SpiderMonkey returns a single part, { type: "year", value: "35" }... however, what the expected result should be for V8 and Chakra is unclear to me. "2018(wu-xu)" is created at least on our end by passing "y" to udatpg_getBestPattern, which will return "r(U)" for the Chinese calendar, where "r" is the related Gregorian year and "U" is the year name.

Before continuing, if "2018(wu-xu)" is not the correct string to be producing here, then this may not be an issue. With that being said, it looks like all of the engines that I have access to on Windows (cant test JSC+Intl right now) generally are using udatpg_getBestPattern or something vaguely similar, so even if this string is wrong, this issue may come up again in the future. Additionally, if the ability to ask for short or long date is added, then we will definitely run into issues where basically any LDML specifier could show up in the users preferred short/long date format, so formatToParts would need to be able to map every LDML specifier to a part type. As such, with the current part types, is it expected that there will be two "year" parts coming from this string? That goes against an unwritten rule that I have always assumed about this API where there would only ever be 0 or 1 of each non-literal part, such that you could do ...formatToParts().filter((part) => part.type === "year")[0] to get the correct year. Perhaps that was a bad assumption on my part, but if there are multiple year fields, how will an application know which is the preferable one for when they just want to display "the year," whatever that may mean? I don't know enough about the Chinese calendar to say if 2018 or wu-xu (or neither) is the best answer, but I don't think the API is currently set up to handle this situation.

If we agree that 2018 and wu-xu should not both be type = year, I would propose adding a new part type, "relatedYear," to line up with the LDML's "r" field.

/cc @zbraniecki, @jefgen

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: datetimeComponent: dates, times, timezoness: in progressStatus: the issue has an active proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions