Skip to content

Commit

Permalink
fix(@formatjs/intl-datetimeformat): fix h/K pattern, fix #2236
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Oct 25, 2020
1 parent f9f2c3e commit 23257c6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/ecma402-abstract/src/DateTimeFormat/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function matchSkeletonPattern(
// Hour
case 'h':
result.hour = ['numeric', '2-digit'][len - 1];
result.hour12 = true;
return '{hour}';

case 'H':
Expand All @@ -103,6 +104,7 @@ function matchSkeletonPattern(

case 'K':
result.hour = ['numeric', '2-digit'][len - 1];
result.hour12 = true;
return '{hour}';

case 'k':
Expand Down
67 changes: 66 additions & 1 deletion packages/ecma402-abstract/tests/BestFitFormatMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {bestFitFormatMatcherScore} from '../src/DateTimeFormat/BestFitFormatMatcher';
import {
bestFitFormatMatcherScore,
BestFitFormatMatcher,
} from '../src/DateTimeFormat/BestFitFormatMatcher';
import {parseDateTimeSkeleton} from '../src/DateTimeFormat/skeleton';

test('bestFitFormatMatcherScore', function () {
Expand All @@ -21,6 +24,68 @@ test('bestFitFormatMatcherScore', function () {
bestFitFormatMatcherScore(opts, parseDateTimeSkeleton('HH:mm:ss v'))
);
});
test('BestFitFormatMatcher second tz', function () {
const availableFormats = {
'h:mm:ss a zzzz': 'h:mm:ss a zzzz',
'h:mm:ss a z': 'h:mm:ss a z',
h: 'h a',
H: 'HH',
hm: 'h:mm a',
Hm: 'HH:mm',
hms: 'h:mm:ss a',
Hms: 'HH:mm:ss',
hmsv: 'h:mm:ss a v',
Hmsv: 'HH:mm:ss v',
hmv: 'h:mm a v',
Hmv: 'HH:mm v',
};
expect(
BestFitFormatMatcher(
{
year: undefined,
month: undefined,
day: undefined,
hour: '2-digit',
minute: '2-digit',
second: undefined,
timeZoneName: 'short',
hour12: true,
},
Object.keys(availableFormats).map(k =>
parseDateTimeSkeleton(k, availableFormats[k as 'h'])
)
)
).toEqual({
hour: '2-digit',
hour12: true,
minute: '2-digit',
pattern: '{hour}:{minute} {timeZoneName}',
pattern12: '{hour}:{minute} {ampm} {timeZoneName}',
rawPattern: 'h:mm a v',
skeleton: 'h:mm a v',
timeZoneName: 'short',
});
});
test('bestFitFormatMatcherScore second tz', function () {
const opts = {
year: undefined,
month: undefined,
day: undefined,
hour: '2-digit',
minute: '2-digit',
second: undefined,
timeZoneName: 'short',
hour12: true,
};
expect(
bestFitFormatMatcherScore(opts, parseDateTimeSkeleton('hmv', 'h:mm a v'))
).toBeGreaterThan(
bestFitFormatMatcherScore(
opts,
parseDateTimeSkeleton('h:mm:ss a z', 'h:mm:ss a z')
)
);
});
test('bestFitFormatMatcherScore long weekday (ko)', function () {
const opts = {
weekday: 'long',
Expand Down
26 changes: 20 additions & 6 deletions packages/intl-datetimeformat/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,26 @@ describe('Intl.DateTimeFormat', function () {
hourCycle: 'h23',
timeZone: 'Europe/Berlin',
});
expect(formatter.format(new Date('2019-03-31T00:59:59.999Z'))).toBe('1');
expect(formatter.format(new Date('2019-03-31T01:00:00.000Z'))).toBe('3');
expect(formatter.format(new Date('2019-03-31T01:00:00.001Z'))).toBe('3');
expect(formatter.format(new Date('2019-03-31T00:59:59.999Z'))).toBe('01');
expect(formatter.format(new Date('2019-03-31T01:00:00.000Z'))).toBe('03');
expect(formatter.format(new Date('2019-03-31T01:00:00.001Z'))).toBe('03');

expect(formatter.format(new Date('2019-10-27T00:59:59.999Z'))).toBe('2');
expect(formatter.format(new Date('2019-10-27T01:00:00.000Z'))).toBe('2');
expect(formatter.format(new Date('2019-10-27T01:00:00.001Z'))).toBe('2');
expect(formatter.format(new Date('2019-10-27T00:59:59.999Z'))).toBe('02');
expect(formatter.format(new Date('2019-10-27T01:00:00.000Z'))).toBe('02');
expect(formatter.format(new Date('2019-10-27T01:00:00.001Z'))).toBe('02');
});
it('test #2236', function () {
const date = new Date('2020-09-16T11:55:32.491+02:00');
const formatter = new DateTimeFormat('en-US', {
year: undefined,
month: undefined,
day: undefined,
hour: '2-digit',
minute: '2-digit',
second: undefined,
timeZoneName: 'short',
timeZone: 'Europe/Amsterdam',
});
expect(formatter.format(date)).toBe('11:55 AM GMT+2');
});
});

0 comments on commit 23257c6

Please sign in to comment.