Skip to content

Commit

Permalink
Date: Filter am/pm data for "a" date pattern
Browse files Browse the repository at this point in the history
CLDR introduced additional dayPeriods (e.g., morning1, afternoon1,
evening1, etc) along with am and pm. The new dayPeriods should be handle
by "b" and "B" date patterns, while the "a" pattern should still handle
am/pm.

For Chinese, the corresponding value for either pm or afternoon1 is
exactly the same, therefore it causes problem on the reverse lookup when
parsing.

This change fixes the above parsing issue by filtering am/pm only in the
parser properties. It also filters it out on formatting as a simple
optimization (to avoid unnecessary properties).

Fixes globalizejs#509
Closes globalizejs#508
  • Loading branch information
rxaviers committed Mar 2, 2017
1 parent d093121 commit 2c14222
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/date/format-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,14 @@ return function( pattern, cldr ) {

// Period (AM or PM)
case "a":
properties.dayPeriods = cldr.main(
"dates/calendars/gregorian/dayPeriods/format/wide"
);
properties.dayPeriods = {
am: cldr.main(
"dates/calendars/gregorian/dayPeriods/format/wide/am"
),
pm: cldr.main(
"dates/calendars/gregorian/dayPeriods/format/wide/pm"
)
};
break;

// Hour
Expand Down
13 changes: 9 additions & 4 deletions src/date/tokenizer-properties.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
define([
"./pattern-re",
"../common/create-error/unsupported-feature",
"../number/symbol"
], function( datePatternRe, createErrorUnsupportedFeature, numberSymbol ) {
"../number/symbol",
"../util/object/filter"
], function( datePatternRe, createErrorUnsupportedFeature, numberSymbol, objectFilter ) {

/**
* tokenizerProperties( pattern, cldr )
Expand Down Expand Up @@ -126,9 +127,13 @@ return function( pattern, cldr ) {

// Period (AM or PM)
case "a":
cldr.main([
cldr.main(
"dates/calendars/gregorian/dayPeriods/format/wide"
]);
);
properties[ "gregorian/dayPeriods/format/wide" ] = objectFilter(
properties[ "gregorian/dayPeriods/format/wide" ],
/^am|^pm/
);
break;

// Zone
Expand Down
1 change: 1 addition & 0 deletions test/unit/date/format-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ QUnit.test( "should return days properties for day of week (eee..eeeeee|ccc..ccc

QUnit.test( "should return dayPeriods property for period (a)", function( assert ) {
assert.ok( "dayPeriods" in properties( "a", cldr ) );
assert.equal( Object.keys(properties( "a", cldr ).dayPeriods).length, 2 );
});

});
11 changes: 9 additions & 2 deletions test/unit/date/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ define([
"src/date/tokenizer-properties",
"json!cldr-data/main/en/ca-gregorian.json",
"json!cldr-data/main/en/numbers.json",
"json!cldr-data/main/zh/ca-gregorian.json",
"json!cldr-data/main/zh/numbers.json",
"json!cldr-data/supplemental/likelySubtags.json",
"json!cldr-data/supplemental/timeData.json",
"json!cldr-data/supplemental/weekData.json",
Expand All @@ -15,9 +17,9 @@ define([
"cldr/event",
"cldr/supplemental"
], function( Cldr, parse, parseProperties, startOf, tokenizer, numberTokenizerProperties,
enCaGregorian, enNumbers, likelySubtags, timeData, weekData, util ) {
enCaGregorian, enNumbers, zhCaGregorian, zhNumbers, likelySubtags, timeData, weekData, util ) {

var cldr, date1, date2, midnight;
var cldr, date1, date2, midnight, zh;

function assertParse( assert, stringDate, pattern, cldr, date ) {
var tokenizerProperties, tokens;
Expand Down Expand Up @@ -50,12 +52,15 @@ function simpleNumberParser( value ) {
Cldr.load(
enCaGregorian,
enNumbers,
zhCaGregorian,
zhNumbers,
likelySubtags,
timeData,
weekData
);

cldr = new Cldr( "en" );
zh = new Cldr( "zh" );

midnight = new Date();
midnight = startOf( midnight, "day" );
Expand Down Expand Up @@ -269,6 +274,8 @@ QUnit.test( "should parse period (a)", function( assert ) {
date2 = startOf( date2, "hour" );
assertParse( assert, "5 AM", "h a", cldr, date1 );
assertParse( assert, "5 PM", "h a", cldr, date2 );
assertParse( assert, "上午5", "ah", zh, date1 );
assertParse( assert, "下午5", "ah", zh, date2 );
});

/**
Expand Down

0 comments on commit 2c14222

Please sign in to comment.