Skip to content

Commit

Permalink
fix(@formatjs/intl-datetimeformat): fix hourCycle setting, fix #2291
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Nov 9, 2020
1 parent b5cbe34 commit 1915595
Show file tree
Hide file tree
Showing 6 changed files with 651 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
shortLessPenalty,
longLessPenalty,
} from './utils';
import {parseDateTimeSkeleton} from './skeleton';
import {processDateTimePattern} from './skeleton';

function isNumericType(
t: 'numeric' | '2-digit' | 'narrow' | 'short' | 'long'
Expand Down Expand Up @@ -94,11 +94,12 @@ export function BestFitFormatMatcher(
}

const skeletonFormat = {...bestFormat};
const patternFormat = parseDateTimeSkeleton(bestFormat.rawPattern);
const patternFormat = {rawPattern: bestFormat.rawPattern} as Formats;
processDateTimePattern(bestFormat.rawPattern, patternFormat);

// Kinda following https://github.com/unicode-org/icu/blob/dd50e38f459d84e9bf1b0c618be8483d318458ad/icu4j/main/classes/core/src/com/ibm/icu/text/DateTimePatternGenerator.java
// Method adjustFieldTypes
for (const prop in patternFormat) {
for (const prop in skeletonFormat) {
const skeletonValue = skeletonFormat[prop as TABLE_6];
const patternValue = patternFormat[prop as TABLE_6];
const requestedValue = options[prop as TABLE_6];
Expand Down Expand Up @@ -127,6 +128,9 @@ export function BestFitFormatMatcher(
patternFormat[prop as TABLE_6] = requestedValue;
}
// Copy those over
patternFormat.pattern = skeletonFormat.pattern;
patternFormat.pattern12 = skeletonFormat.pattern12;
patternFormat.skeleton = skeletonFormat.skeleton;
patternFormat.rangePatterns = skeletonFormat.rangePatterns;
patternFormat.rangePatterns12 = skeletonFormat.rangePatterns12;
return patternFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ function isTimeRelated(opt: Opt) {
return false;
}

function resolveHourCycle(hc: string, hcDefault: string, hour12?: boolean) {
if (hc == null) {
hc = hcDefault;
}
if (hour12 !== undefined) {
if (hour12) {
if (hcDefault === 'h11' || hcDefault === 'h23') {
hc = 'h11';
} else {
hc = 'h12';
}
} else {
invariant(!hour12, 'hour12 must not be set');
if (hcDefault === 'h11' || hcDefault === 'h23') {
hc = 'h23';
} else {
hc = 'h24';
}
}
}
return hc;
}

interface Opt extends Omit<Formats, 'pattern' | 'pattern12'> {
localeMatcher: string;
ca: DateTimeFormatOptions['calendar'];
Expand Down Expand Up @@ -239,11 +262,16 @@ export function InitializeDateTimeFormat(
if (matcher === 'basic') {
bestFormat = BasicFormatMatcher(opt, formats);
} else {
// IMPL DETAILS START
if (isTimeRelated(opt)) {
opt.hour12 =
internalSlots.hourCycle === 'h11' ||
internalSlots.hourCycle === 'h12';
const hc = resolveHourCycle(
internalSlots.hourCycle,
dataLocaleData.hourCycle,
hour12
);
opt.hour12 = hc === 'h11' || hc === 'h12';
}
// IMPL DETAILS END
bestFormat = BestFitFormatMatcher(opt, formats);
}
} else {
Expand All @@ -259,6 +287,10 @@ export function InitializeDateTimeFormat(
}
bestFormat = DateTimeStyleFormat(dateStyle, timeStyle, dataLocaleData);
}
// IMPL DETAIL START
// For debugging
internalSlots.format = bestFormat;
// IMPL DETAIL END
for (const prop in opt) {
const p = bestFormat[prop as 'era'];
if (p !== undefined) {
Expand All @@ -268,27 +300,11 @@ export function InitializeDateTimeFormat(
let pattern;
let rangePatterns;
if (internalSlots.hour !== undefined) {
const hcDefault = dataLocaleData.hourCycle;
let hc = internalSlots.hourCycle;
if (hc == null) {
hc = hcDefault;
}
if (hour12 !== undefined) {
if (hour12) {
if (hcDefault === 'h11' || hcDefault === 'h23') {
hc = 'h11';
} else {
hc = 'h12';
}
} else {
invariant(!hour12, 'hour12 must not be set');
if (hcDefault === 'h11' || hcDefault === 'h23') {
hc = 'h23';
} else {
hc = 'h24';
}
}
}
const hc = resolveHourCycle(
internalSlots.hourCycle,
dataLocaleData.hourCycle,
hour12
);
internalSlots.hourCycle = hc;

if (hc === 'h11' || hc === 'h12') {
Expand Down

0 comments on commit 1915595

Please sign in to comment.