use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class DateTimeIntervalFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
int count = variables.count();
if (count < 2) {
return;
}
Variable v1 = variables.get(0);
Variable v2 = variables.get(1);
CLDR cldr = ctx.cldr();
String zoneId = PluginDateUtils.getTimeZoneNameFromContext(ctx);
CalendarDate start = cldr.Calendars.toGregorianDate(v1.node().asLong(0), zoneId);
CalendarDate end = cldr.Calendars.toGregorianDate(v2.node().asLong(0), zoneId);
DateIntervalFormatOptions options = (DateIntervalFormatOptions) args.getOpaque();
String result = cldr.Calendars.formatDateInterval(start, end, options);
v1.set(result);
}
use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class RelativeTimeFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
int count = variables.count();
Variable v1 = variables.get(0);
Long now = ctx.now();
long s = now == null ? System.currentTimeMillis() : now.longValue();
long e = v1.node().asLong();
if (count > 1) {
s = e;
e = variables.get(1).node().asLong();
}
CLDR cldr = ctx.cldr();
CalendarDate start = cldr.Calendars.toGregorianDate(s, "UTC");
CalendarDate end = cldr.Calendars.toGregorianDate(e, "UTC");
RelativeTimeFormatOptions opts = OptionParsers.relativetime(args);
String res = cldr.Calendars.formatRelativeTime(start, end, opts);
v1.set(res);
}
use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class MoneyFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
JsonNode node = var.node();
JsonNode decimalValue = node.path("decimalValue");
JsonNode currencyNode = node.path("currencyCode");
if (decimalValue.isMissingNode() || currencyNode.isMissingNode()) {
// Check if we have a new money node and CLDR formatting is enabled.
if (CommerceUtils.useCLDRMode(ctx)) {
decimalValue = node.path("value");
currencyNode = node.path("currency");
}
// No valid money node found.
if (decimalValue.isMissingNode() || currencyNode.isMissingNode()) {
var.setMissing();
return;
}
}
CLDR cldr = ctx.cldr();
String code = currencyNode.asText();
Decimal decimal = GeneralUtils.nodeToDecimal(decimalValue);
CurrencyType currency = CurrencyType.fromString(code);
CurrencyFormatOptions opts = (CurrencyFormatOptions) args.getOpaque();
String result = cldr.Numbers.formatCurrency(decimal, currency, opts);
var.set(result);
}
Aggregations