Skip to content

Commit b42fb2c

Browse files
committed
Date: Filter am/pm data for "a" date pattern
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 #509 Closes #508
1 parent 72fd1ce commit b42fb2c

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

src/date/format-properties.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,14 @@ return function( pattern, cldr ) {
185185

186186
// Period (AM or PM)
187187
case "a":
188-
properties.dayPeriods = cldr.main(
189-
"dates/calendars/gregorian/dayPeriods/format/wide"
190-
);
188+
properties.dayPeriods = {
189+
am: cldr.main(
190+
"dates/calendars/gregorian/dayPeriods/format/wide/am"
191+
),
192+
pm: cldr.main(
193+
"dates/calendars/gregorian/dayPeriods/format/wide/pm"
194+
)
195+
};
191196
break;
192197

193198
// Hour

src/date/tokenizer-properties.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
define([
22
"./pattern-re",
33
"../common/create-error/unsupported-feature",
4-
"../number/symbol"
5-
], function( datePatternRe, createErrorUnsupportedFeature, numberSymbol ) {
4+
"../number/symbol",
5+
"../util/object/filter"
6+
], function( datePatternRe, createErrorUnsupportedFeature, numberSymbol, objectFilter ) {
67

78
/**
89
* tokenizerProperties( pattern, cldr )
@@ -126,9 +127,13 @@ return function( pattern, cldr ) {
126127

127128
// Period (AM or PM)
128129
case "a":
129-
cldr.main([
130+
cldr.main(
130131
"dates/calendars/gregorian/dayPeriods/format/wide"
131-
]);
132+
);
133+
properties[ "gregorian/dayPeriods/format/wide" ] = objectFilter(
134+
properties[ "gregorian/dayPeriods/format/wide" ],
135+
/^am|^pm/
136+
);
132137
break;
133138

134139
// Zone

test/functional/date/date-formatter.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,8 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
9393
pad2NumberFormatter( runtimeArgs[ 0 ][ 2 ] );
9494
assert.deepEqual( runtimeArgs[ 1 ], {
9595
"dayPeriods": {
96-
"afternoon1": "in the afternoon",
9796
"am": "AM",
98-
"am-alt-variant": "am",
99-
"evening1": "in the evening",
100-
"midnight": "midnight",
101-
"morning1": "in the morning",
102-
"night1": "at night",
103-
"noon": "noon",
104-
"pm": "PM",
105-
"pm-alt-variant": "pm"
97+
"pm": "PM"
10698
},
10799
"pattern": "h:mm:ss a",
108100
"timeSeparator": ":"
@@ -165,16 +157,8 @@ QUnit.test( "should allow for runtime compilation", function( assert ) {
165157
pad2NumberFormatter( runtimeArgs[ 0 ][ 2 ] );
166158
assert.deepEqual( runtimeArgs[ 1 ], {
167159
"dayPeriods": {
168-
"afternoon1": "in the afternoon",
169160
"am": "AM",
170-
"am-alt-variant": "am",
171-
"evening1": "in the evening",
172-
"midnight": "midnight",
173-
"morning1": "in the morning",
174-
"night1": "at night",
175-
"noon": "noon",
176-
"pm": "PM",
177-
"pm-alt-variant": "pm"
161+
"pm": "PM"
178162
},
179163
"days": {
180164
"E": {

test/unit/date/format-properties.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ QUnit.test( "should return days properties for day of week (eee..eeeeee|ccc..ccc
115115

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

120121
});

test/unit/date/parse.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ define([
77
"src/date/tokenizer-properties",
88
"json!cldr-data/main/en/ca-gregorian.json",
99
"json!cldr-data/main/en/numbers.json",
10+
"json!cldr-data/main/zh/ca-gregorian.json",
11+
"json!cldr-data/main/zh/numbers.json",
1012
"json!cldr-data/supplemental/likelySubtags.json",
1113
"json!cldr-data/supplemental/timeData.json",
1214
"json!cldr-data/supplemental/weekData.json",
@@ -15,9 +17,9 @@ define([
1517
"cldr/event",
1618
"cldr/supplemental"
1719
], function( Cldr, parse, parseProperties, startOf, tokenizer, numberTokenizerProperties,
18-
enCaGregorian, enNumbers, likelySubtags, timeData, weekData, util ) {
20+
enCaGregorian, enNumbers, zhCaGregorian, zhNumbers, likelySubtags, timeData, weekData, util ) {
1921

20-
var cldr, date1, date2, midnight;
22+
var cldr, date1, date2, midnight, zh;
2123

2224
function assertParse( assert, stringDate, pattern, cldr, date ) {
2325
var tokenizerProperties, tokens;
@@ -50,12 +52,15 @@ function simpleNumberParser( value ) {
5052
Cldr.load(
5153
enCaGregorian,
5254
enNumbers,
55+
zhCaGregorian,
56+
zhNumbers,
5357
likelySubtags,
5458
timeData,
5559
weekData
5660
);
5761

5862
cldr = new Cldr( "en" );
63+
zh = new Cldr( "zh" );
5964

6065
midnight = new Date();
6166
midnight = startOf( midnight, "day" );
@@ -269,6 +274,8 @@ QUnit.test( "should parse period (a)", function( assert ) {
269274
date2 = startOf( date2, "hour" );
270275
assertParse( assert, "5 AM", "h a", cldr, date1 );
271276
assertParse( assert, "5 PM", "h a", cldr, date2 );
277+
assertParse( assert, "上午5", "ah", zh, date1 );
278+
assertParse( assert, "下午5", "ah", zh, date2 );
272279
});
273280

274281
/**

0 commit comments

Comments
 (0)