Skip to content

Commit

Permalink
Upgrade @phensley/cldr to 1.7.3
Browse files Browse the repository at this point in the history
Includes interval format context fix

The low-level date formatter now has a 'first' flag, indicating if the
current format call is first in a sequence of calls, so that intervals
and other compound date formats can handle begin-sentence context
correctly.
  • Loading branch information
phensley committed Nov 28, 2023
1 parent f07e3fb commit f70a757
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 18 deletions.
6 changes: 3 additions & 3 deletions codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
},
"devDependencies": {
"@phenomnomnominal/tsquery": "^6.1.3",
"@phensley/cldr": "1.7.2",
"@phensley/cldr-compiler": "1.7.2",
"@phensley/unit-converter": "1.7.2",
"@phensley/cldr": "1.7.3",
"@phensley/cldr-compiler": "1.7.3",
"@phensley/unit-converter": "1.7.3",
"@types/jest": "^29.5.4",
"@types/node": "^20.6.0",
"@typescript-eslint/eslint-plugin": "^6.6.0",
Expand Down
7 changes: 6 additions & 1 deletion codegen/src/suite/calendars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const DIM_TIME = new Dimension<DateFormatOptions>('time', FORMAT_WIDTHS);
const DIM_SKEL = new Dimension<DateFormatOptions>('skeleton', SKELETONS);
const DIM_CONTEXT = new Dimension<DateFormatOptions>('context', [
'begin-sentence',
'middle-of-text',
]);

const INT_SKELETON = new Dimension<DateIntervalFormatOptions>('skeleton', [
Expand All @@ -102,6 +103,10 @@ const INT_SKELETON = new Dimension<DateIntervalFormatOptions>('skeleton', [
'hma',
'EEEyMMMdhms',
]);
const INT_CONTEXT = new Dimension<DateIntervalFormatOptions>('context', [
'begin-sentence',
'middle-of-text',
]);

type DateFunc<R> = <T>(cldr: CLDR, date: CalendarDate, o: T) => R;
type DateIntFunc<R> = <T>(
Expand Down Expand Up @@ -307,7 +312,7 @@ export const dateSuite = (root: string) => {
opts: DateIntervalFormatOptions,
) => c.Calendars.formatDateInterval(start, end, opts!);

intdims = [INT_SKELETON];
intdims = [INT_SKELETON, INT_CONTEXT];
buildDateIntervalFormat(
join(root, 'dateinterval.txt'),
'formatDateInterval',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class MiscData {

public static final String VERSION =
"1.7.2";
"1.7.3";


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public CalendarFormatter(Internals internals, CalendarSchema schema) {
}

public <R> void format(AbstractValue<R> val, CalendarContext<T> ctx, DateTimePattern pattern) {
format(val, ctx, pattern, true);
}

public <R> void format(AbstractValue<R> val, CalendarContext<T> ctx, DateTimePattern pattern, boolean first) {
int len = pattern.nodes.size();
for (int i = 0; i < len; i++) {
Object node = pattern.nodes.get(i);
Expand Down Expand Up @@ -299,7 +303,7 @@ public <R> void format(AbstractValue<R> val, CalendarContext<T> ctx, DateTimePat
continue;
}

if (i == 0 && ctx.context != null && field != null) {
if (first && i == 0 && ctx.context != null && field != null) {
value = this.internals.general.contextTransform(value, ctx.transform, ctx.context, field);
}
val.add(type, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ public int weekMinDays(String region) {
}

public <R> R formatDateTime(CalendarType calendar, CalendarContext<CalendarDate> ctx,
AbstractValue<R> value, DateTimePattern date, DateTimePattern time, String wrapper) {
AbstractValue<R> value, boolean first, DateTimePattern date, DateTimePattern time, String wrapper) {

CalendarFormatter<CalendarDate> formatter = this.getCalendarFormatter(calendar);
R _date = null;
R _time = null;
if (date != null) {
formatter.format(value, ctx, date);
formatter.format(value, ctx, date, first);
_date = value.render();
}
if (time != null) {
formatter.format(value, ctx, time);
formatter.format(value, ctx, time, _date != null && first);
_time = value.render();
}
if (_date != null && _time != null && wrapper != null) {
Expand All @@ -131,15 +131,15 @@ public <R> R formatDateTime(CalendarType calendar, CalendarContext<CalendarDate>
}

public <R> R formatInterval(CalendarType calendar, CalendarContext<CalendarDate> ctx,
AbstractValue<R> value, CalendarDate end, DateTimePattern pattern) {
AbstractValue<R> value, boolean first, CalendarDate end, DateTimePattern pattern) {

int i = DateTimePattern.intervalPatternBoundary(pattern);
List<Object> nodes = pattern.nodes;
DateTimePattern startPattern = new DateTimePattern(nodes.subList(0, i), "");
DateTimePattern endPattern = new DateTimePattern(nodes.subList(i, nodes.size()), "");
R s = this.formatDateTime(calendar, ctx, value, startPattern, null, null);
R s = this.formatDateTime(calendar, ctx, value, first, startPattern, null, null);
ctx.date = end;
R e = this.formatDateTime(calendar, ctx, value, endPattern, null, null);
R e = this.formatDateTime(calendar, ctx, value, false, endPattern, null, null);
return value.join(Arrays.asList(s, e));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ protected <R> R _formatDate(AbstractValue<R> value, CalendarDate date, DateForma
NumberParams params = this.privateApi.getNumberParams(options.numberSystem.get(), "default");
DateFormatRequest req = this.manager.getDateFormatRequest(date, options, params);
CalendarContext<CalendarDate> ctx = this._context(date, params, options.context.get(), options.alt);
return this.internals.calendars.formatDateTime(calendar, ctx, value, req.date, req.time, req.wrapper);
return this.internals.calendars.formatDateTime(calendar, ctx, value, true, req.date, req.time, req.wrapper);
}

protected <R> R _formatInterval(AbstractValue<R> value, CalendarDate start, CalendarDate end,
Expand All @@ -362,9 +362,9 @@ protected <R> R _formatInterval(AbstractValue<R> value, CalendarDate start, Cale
.skeleton(req.skeleton);
DateFormatRequest r = this.manager.getDateFormatRequest(start, opts, params);
CalendarContext<CalendarDate> ctx = this._context(start, params, options.context.get(), options.alt);
R _start = this.internals.calendars.formatDateTime(calendar, ctx, value, r.date, r.time, r.wrapper);
R _start = this.internals.calendars.formatDateTime(calendar, ctx, value, true, r.date, r.time, r.wrapper);
ctx.date = end;
R _end = this.internals.calendars.formatDateTime(calendar, ctx, value, r.date, r.time, r.wrapper);
R _end = this.internals.calendars.formatDateTime(calendar, ctx, value, false, r.date, r.time, r.wrapper);
WrapperPattern wrapper = this.internals.general.parseWrapper(req.wrapper);
value.wrap(wrapper, Arrays.asList(_start, _end));
return value.render();
Expand All @@ -373,12 +373,12 @@ protected <R> R _formatInterval(AbstractValue<R> value, CalendarDate start, Cale
R _date = null;
if (req.date != null) {
CalendarContext<CalendarDate> ctx = this._context(start, params, options.context.get(), options.alt);
_date = this.internals.calendars.formatDateTime(calendar, ctx, value, req.date, null, null);
_date = this.internals.calendars.formatDateTime(calendar, ctx, value, true, req.date, null, null);
}

if (req.range != null) {
CalendarContext<CalendarDate> ctx = this._context(start, params, options.context.get(), options.alt);
R _range = this.internals.calendars.formatInterval(calendar, ctx, value, end, req.range);
R _range = this.internals.calendars.formatInterval(calendar, ctx, value, _date == null, end, req.range);
if (_date == null) {
return _range;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ private <R> R _formatDateRaw(AbstractValue<R> value, CalendarDate date, DateRawF
date = convertDateTo(calendar, date);
NumberParams params = this.privateApi.getNumberParams(options.numberSystem.get(), "default");
CalendarContext<CalendarDate> ctx = this._context(date, params, options.context.get(), options.alt);
return this.internals.calendars.formatDateTime(calendar, ctx, value, pattern, null, null);
return this.internals.calendars.formatDateTime(calendar, ctx, value, true, pattern, null, null);
}

protected <T extends CalendarDate> CalendarContext<T> _context(T date, NumberParams params, ContextType context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.squarespace.cldrengine.api.CalendarDate;
import com.squarespace.cldrengine.api.ContextType;
import com.squarespace.cldrengine.api.DateIntervalFormatOptions;
import com.squarespace.cldrengine.api.Decimal;

Expand Down Expand Up @@ -97,6 +98,9 @@ private static DateIntervalFormatOptions dateIntervalOptions(JsonElement elem) {
case "skeleton":
opts.skeleton(stringValue(obj.get(key)));
break;
case "context":
opts.context(ContextType.fromString(stringValue(obj.get(key))));
break;
}

}
Expand Down

0 comments on commit f70a757

Please sign in to comment.