Intl.js icon indicating copy to clipboard operation
Intl.js copied to clipboard

hour12 should be taken into consideration when searching for best match format

Open caridy opened this issue 10 years ago • 1 comments

this is related to issue https://github.com/andyearnshaw/Intl.js/issues/109

@andyearnshaw I have done some debugging on the issue described in issue #109, and here are some preliminary thoughts:

When trying to format a time in french using this code:

new IntlPolyfill.DateTimeFormat('fr-FR', {
    hour: 'numeric',
    minute: 'numeric'
}).format(new Date('2000/12/12 09:09'));

It will resolve to one of the formats under availableFormats, specifically, the pattern "h:mm a" from this line: https://github.com/andyearnshaw/Intl.js/blob/41e1bb2aa40c713e5c393e84080af320f9b0b747/locale-data/json/fr-FR.json#L49

As a result, the internal representation of the format is:

{ 
    hour: 'numeric',
    minute: '2-digit',
    pattern: '{hour}:{minute}',
    hour12: true,
    pattern12: '{hour}:{minute} {ampm}' 
}

Interestingly, the output is what we have in pattern, instead of pattern12, which conflicts with the default value of hour12 for that locale, and the entry in the resolved format which is set to true as well. I will expect this to use pattern12 instead. But even if we fix that, the main problem remains, which is, hour should have been set to 2-digit instead of numeric.

More investigation on this shows that if we modify the options to set hour to 2-digit explicitly, as in the following code:

new IntlPolyfill.DateTimeFormat('fr-FR', {
    hour: '2-digit',
    minute: 'numeric'
}).format(new Date('2000/12/12 09:09'));

It resolves to another format, this time the correct one, which correspond to the short entry in timeFormats, which is equal to: "HH:mm", link here: https://github.com/andyearnshaw/Intl.js/blob/41e1bb2aa40c713e5c393e84080af320f9b0b747/locale-data/json/fr-FR.json#L75

As a result, it produces this:

{ 
    hour: '2-digit',
    minute: '2-digit',
    pattern: '{hour}:{minute}'
}

And everything looks correct.

When analyzing the difference in weight from the two patterns for the first example, I found something weird when debugging the best match for the following structure:

{ 
  '[[weekday]]': undefined,
  '[[era]]': undefined,
  '[[year]]': undefined,
  '[[month]]': undefined,
  '[[day]]': undefined,
  '[[hour]]': 'numeric',
  '[[minute]]': 'numeric',
  '[[second]]': undefined,
  '[[timeZoneName]]': undefined
}

[[hour12]] is not taken in consideration for the weighting, as a result, the following format weight less

{ 
  hour: '2-digit',
  minute: '2-digit',
  hour12: false,
  pattern: '{hour}:{minute}'
}

than

{
  hour: 'numeric',
  minute: '2-digit',
  pattern: '{hour}:{minute}',
  hour12: true,
  pattern12: '{hour}:{minute} {ampm}'
}

which makes sense, but if hour12 were considered, they should have similar weight, and the first one matches the hour12 value. I wonder if we are missing something here.

caridy avatar Jul 07 '15 01:07 caridy

This is now taken into consideration for bestFit algo in https://github.com/andyearnshaw/Intl.js/issues/171

caridy avatar May 12 '16 17:05 caridy