-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
short/medium/long/full style for Intl.DateTimeFormat #108
Comments
(then we could do the same for |
E.g. let dtf = new Intl.DateTimeFormat(navigator.locales, Intl.DateTimeFormat.MEDIUM);
dtf.format(date); date.toLocaleString() == date.toLocaleString(navigator.locales, Intl.DateTimeFormat.MEDIUM) == date.toLocaleString(navigator.locales, {
day: 'numeric',
month: 'short',
year: 'numeric'
}); // In MacOS This is a bit shorter than both previous options, but requries Sebastian |
Well, the third one is really similar to (2) in that it's implicit - puts the "get the preference from host OS" on the Intl API (much like we do with timeZone now), rather than explicit (1) where we have separate API that retrieves OS preferences. |
I separated out the proposal for retrieving user defined preferences from host environment into: #109 So this issue is only about |
Mapping preferences retrieved from the OS to values for the year/month/day/hour/minute/second/etc options is not sufficient to fully support the flexibility of OS formatting patterns (if that's a desired goal). For example, in Mac OS X and Windows, users can customize the order of the components and the separators between them, as well as the format used for each individual components of the date/time. AFAICS, this is not exposed by Intl.DateTimeFormat in any way; the options can be used to control which components are present, and which format is used for each component, but not their order nor any literal separators used between them. |
That's not true for windows. See:
On Mac OS it seems that users can do all of that:
and I think we could retrieve the pattern from the OS and then operate on it the way we would on a pattern retrieved from CLDR. |
but, once again, that's more for #109. Here I'd like to talk about ability to do: date.toLocaleTimeString(locales, {
style: 'long'
};
date.toLocaleDateString(locales, {
style: 'medium'
};
date.toLocaleString(locales, {
style: 'full',
}; and initially have sane defaults for it irrelevant of the result of #109. |
Actually, I think it is; see https://bugzilla.mozilla.org/attachment.cgi?id=8802234. |
oh, cool. Didn't find it. Ok, then Windows and MacOS are on par. Still, that's #109 :) |
If we go this route, I think we'd want two
And in that case, it'd probably make sense to use the same naming for toLocaleTimeString and toLocaleDateString, rather than a non-specific |
I like introducing {date,time}style = {full, long, medium short}. CLDR has the corresponding styles. It was discussed in v1 spec, but the current way trumped. |
@jungshik What led to the current decision? I like the idea of introducing more things that have corresponding CLDR styles. |
So, CLDR has styles for date, time, and combinations of date/time: http://www.unicode.org/cldr/charts/30/summary/en.html#1816 I see two ways to expose it:
style: 'full'|'long'|'medium'|'short'
The problem with this is that not only date and time may have style, but also the pattern that joins those two. It would be awkward to have long date, short time and have to on top of that decide if the pattern that combines them into a single string is long, short or medium. On the other hand, in the RelativeTimeFormat spec discussion we noticed that the same pattern that joins date and time, can be used to join relative date and time. If we go for Any ideas? Opinions? |
First, I'm curious, why are we trying to expose these particular things, and not other things that CLDR exposes, e.g., to take a random one, Hms time format? (Not saying we shouldn't, just wondering what went into it.) If we do want to expose these combinations, I can think of a few goals in the resulting API:
If we go with your option 2, with
What do you think? If we end up permitting things like |
It seems to me that we can start with allowing only descriptive formats and that matches what most OSes expose. Adding another style like Your solution looks sane and I can see us doing it. But it doesn't answer the question on how will we work with the date+time joining pattern which can also be full|long|medium|short and potentially work with I sense that there must be a good solution to that that will allow us solve both - date+time and relativetime+time cases, but I can't see it yet. |
Well, both 'style' and 'pattern' were discussed, but in search of the largest common denominator across potential implementations, we ended up defining a rather small subset of patterns available via CLDR with a rather verbose way of specifying them ('year: numeric', 'day: ....', 'hour: ....', 'timeZoneName: short'). Adding more patterns like 'Hms', 'Hm', 'yMMM' , 'yMdjmsvvvv', and so forth was left for the future extension (IIRC). I can think of three ways to do that (now that there's been a convergence toward CLDR as the data source)
Implementation-wise, one way to handle all three ways ('pattern', current verbose option bags, 'style') is convert them all to a 'pattern' which would be looked up in a given locale data to find the corresponding locale-specific skeleton. (for option bags, that's what v8 does now. It appears that Spidermonkey and JSC do the same). A proposal put forward here is to overload 'style' with a human-friendly shortcut (full, long, etc) AND 'pattern'. ( third option above). A couple of questions to answer before deciding what to do: Which would be best in terms of making DateTimeFormat consistent (in construction and resolvedOptions)? |
@caridy , @ericf , @littledan , @rxaviers - opinions? I'm happy to champion any consensus we can come up with. I mostly care for human-friendly style shortcuts, but I can extend them to cover a list of patterns as @jungshik suggests. |
Also, we could do this gradually - get the human-friendly style - full|long|medium|short, and then talk about adding patterns. |
This proposal has received Stage 1 at today's TC39 meeting. |
I asked the committee about The only feedback was that the latter looks more like CSS which may be good, but it seems that there are no strong feelings one way or another and it's up to us to decide. |
👍 on adding these human-friendly styles. What should happen if
@zbraniecki, the combination pattern can be deduced from the date and time lengths according to CLDR (UTS#35) http://www.unicode.org/reports/tr35/tr35-dates.html#dateTimeFormat.
One benefit of overriding
@zbraniecki, in addition to these 12 options, there are the non-uniform datetimes, e.g.,
👍 too... CLDR calls them skeletons, which contains "only field information and in a canonical order". [1] I personally like the brevity of skeletons compared to the verbose options let fmt = new Intl.DateTimeFormat(locales, {style: "yMMMdjm"});
let equivalentFmt = new Intl.DateTimeFormat(locales, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
});
I agree with @jungshik's point.
I see two benefits of overriding
@SebastianZ the downside of this approach is that it makes it hard to combine it with other options such as 1: Skeleton is a more flexible formatting mechanism than the predefined list of short, medium, long, full formats.
http://www.unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems |
That's cool!
Internally at this point, we still will introduce an
I like the skeletons, I'm just a bit worried about the scope of the proposal. Adding just short/medium/long/full will allow us to get something done and we can always extend it to support a selected list of skeletons later. |
That only means we don't need to ask user to provide the length of the "gluer" format. :) I believe something like
... and by
I believe that internally it would be ok. I want to emphasized that a possible |
Ok. No strong opinion from my side on this roadmap. |
style seems like a half-baked solution compared with skeletons, we can see it as a more human friendly styling mechanism but one that is crippled since it does not allow you to do more advanced things. That might be ok, but at the same time, I think skeletons are almost ubiquitous for any internationalization platform, that we probably should lean toward that solution. |
One more thing is that human friendly styles is what host environments speak. So to enable developers to be able to say "style this date the way user wants a full date to be styles", we need this concept. |
See all the screenshots from platforms in this bug https://bugzilla.mozilla.org/show_bug.cgi?id=1308329 for reference of what users are able to manually define. |
Sorry for mixing up 'pattern' and 'skeleton' in my previous comment. From now on, I'll adhere to the CLDR terminology.
By options.pattern, which do you mean 'skeleton' or 'pattern' in the CLDR sense? It looks like you mean 'pattern' in the CLDR sense, but I think 'options.skeleton' (for skeleton in the CLDR sense) is a lot more useful than 'options.pattern'. There can be a use-case for 'options.pattern' (when a fixed format is required regardless of locale), but this use-case is rather limited than 'skeleton'. Internally, options.style, options.skeleton and current verbose option bags (for year, month, day, hour, etc) should be resolved to a 'skeleton' with a well-defined priority (and conflict-resolution recipes) among them. And, this resolved skeleton can be a part of resolvedOptions as a succinct representation of date-time format (other than calendar, numberingSystem, timeZone). Unless options.pattern is introduced as well, we can stop here in terms of what's available via resolvedOptions.
I like the idea of adding a 'human-friendly' and 'succinct/less verbose' way to specify a format, but |
Yes, I mean
I don't see how you can advance the web as a platform without closing this gap. Currently, every native platform will allow you to format date and time according to user preferences. I understand if we'll decide not to expose it to the Web, but I believe that over time there will be more platforms that will want to use JS based Intl API and will want this feature (Electron?). For that reason, I believe that having |
thanks @zbraniecki, let's transfer that repo to TC39 as well. |
Should we close this issue since we already have dateStyle and timeStyle proposal reach stage 3? |
We can close this when the corresponding proposal reaches Stage 4 and is merged. This issue has the tag "active proposal". |
The number of remaining active items is as follows:
The first one is an editorial bug that I'm working on. Assuming we can come up with a consensus on the latter two over the upcoming ECMA402 call, I plan to propose this for Stage 4 advancement soon. |
|
Why do you like to make such a big mess? I think it would be a little easier to kind of
I apologize if this doesn't go here, but I don't know where I can make a request or show my disagreement with Javascript and the dates. |
What you're suggesting is not internationalization date pattern. ISO is universal and you can format |
That's how I do it in PHP, it's simple and easy, in Javascript I don't understand how it's so complicated. https://www.php.net/manual/en/function.date.php |
That means, if your caller need to support 240 locales, the engineer need to code 240 patterns, one for each locale, in order to use your system to support that 240 locales properly. While it might be easy for you, or any engineer to figure out the first 2-5 patterns, it will be hard to find out the 6th to 240th. |
Looking at this again: why are the hyphens there? Just have "dmY" => 24/07/2020 and it's just a skeleton, as discussed earlier. (dmY=mYd etc; order does not matter) |
The php docs say "This method does not use locales. All output is in English." So as frank said, this has little to do with internationalization. |
I set the wrong example, it should be But if it can't be, then nothing will continue to use third party Javascript libraries like https://date-fns.org/v2.15.0/docs/format
Everyone has their differences, so I'm looking for something native. |
What you propose, is to let the library to allow the programmer to control exactly what the pattern is. The problem is, the pattern is different, depending on each locale, so, by providing such "low level" API, it encourage the developer to program their code based on lower level construct, and make the software much harder to extend to locales/countries, because, under such paradigm, the developer need to know the pattern for ALL the pattern of the locales they need to support, therefore, it push the responsibility of figuring out that cultural information to the developers, instead of depending on what the cultural information the library collected from many linguists. For example, could you name the date patterns of the top 80 locales your software are going to support? and how long do you think it will take for you, personally, not some magic guru you believe your boss is going to hire to help you, to find these 80 patterns? I am sure you can easily list the first 3-7 (say US, UK, Canada, AU, NZ, and maybe even France and Germany). But it will be very difficult after that, right? How about in Thailand, China, Japan, Armenia? If you think it is easy to figure out, try some of them I listed above and tell me how long it take for you to be sure and how many Unit test you need to write to verify that. |
I'm working on trying to design an alignment between how Intl API works and OS Intl preferences work.
An example of such task is how to incorporate the date/time formatting OS user preferences into DateTimeFormat API.
Currently, we expect users of our API to list the elements of the date/time format as options, or, if none are listed, we take:
Intl.DateTimeFormat
andDate.prototype.toLocaleString
: hour/minute/second/day/month/year asnumeric
Date.prototype.toLocaleTimeString
: hour/minute/second asnumeric
Date.prototype.toLocaleDateString
: day/month/year asnumeric
On the other hand, MacOS exposes four styles for date and time: short, medium, long and full.
Windows exposes short/long.
I see two solutions to bind those two APIs:
style
option and change how we handle "defaults" in Intl.DateTimeFormatIn this approach, we would use the language in spec similar to one we use for timezones: if user did not specify a timezone, we try to retrieve it from the host OS and we land on the current defaults (all as
numeric
) only if we cannot get anything from the OS.That would require adding an additional step in
12.1.2 ToDateTimeOptions
as6.a
and7.a
where the implementation could reach to host OS for the preferences and only fallback to the current6.a
and7.a
if those cannot be retrieved.In this approach,
style
option would be mutually exclusive with any of the token options.Opinions? @caridy , @rxaviers , @jungshik, @srl295 ?
The text was updated successfully, but these errors were encountered: