Search in sources :

Example 1 with CalendarDate

use of com.squarespace.cldrengine.api.CalendarDate in project template-compiler by Squarespace.

the class DateTimeFormatter method apply.

@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
    Variable var = variables.first();
    long epoch = var.node().asLong();
    String zoneId = PluginDateUtils.getTimeZoneNameFromContext(ctx);
    CLDR cldr = ctx.cldr();
    DateFormatOptions options = (DateFormatOptions) args.getOpaque();
    CalendarDate date = cldr.Calendars.toGregorianDate(epoch, zoneId);
    String result = cldr.Calendars.formatDate(date, options);
    var.set(result);
}
Also used : CalendarDate(com.squarespace.cldrengine.api.CalendarDate) Variable(com.squarespace.template.Variable) DateFormatOptions(com.squarespace.cldrengine.api.DateFormatOptions) CLDR(com.squarespace.cldrengine.CLDR)

Example 2 with CalendarDate

use of com.squarespace.cldrengine.api.CalendarDate in project template-compiler by Squarespace.

the class MessageFormats method datetime.

/**
 * Datetime message formatter.
 */
private String datetime(List<Object> args, List<String> options) {
    if (args.isEmpty()) {
        return "";
    }
    JsonNode node = (JsonNode) args.get(0);
    if (node == null) {
        return "";
    }
    long epoch = node.asLong();
    CalendarDate date = cldr.Calendars.toGregorianDate(epoch, zoneId);
    DateFormatOptions opts = OptionParsers.datetime(options);
    return cldr.Calendars.formatDate(date, opts);
}
Also used : CalendarDate(com.squarespace.cldrengine.api.CalendarDate) DateFormatOptions(com.squarespace.cldrengine.api.DateFormatOptions) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 3 with CalendarDate

use of com.squarespace.cldrengine.api.CalendarDate 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);
}
Also used : DateIntervalFormatOptions(com.squarespace.cldrengine.api.DateIntervalFormatOptions) CalendarDate(com.squarespace.cldrengine.api.CalendarDate) Variable(com.squarespace.template.Variable) CLDR(com.squarespace.cldrengine.CLDR)

Example 4 with CalendarDate

use of com.squarespace.cldrengine.api.CalendarDate 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);
}
Also used : CalendarDate(com.squarespace.cldrengine.api.CalendarDate) Variable(com.squarespace.template.Variable) CLDR(com.squarespace.cldrengine.CLDR) RelativeTimeFormatOptions(com.squarespace.cldrengine.api.RelativeTimeFormatOptions)

Example 5 with CalendarDate

use of com.squarespace.cldrengine.api.CalendarDate in project template-compiler by Squarespace.

the class MessageFormats method interval.

/**
 * Datetime interval message formatter.
 */
private String interval(List<Object> args, List<String> options) {
    if (args.size() < 2) {
        return "";
    }
    JsonNode v1 = (JsonNode) args.get(0);
    JsonNode v2 = (JsonNode) args.get(1);
    if (v1 == null || v2 == null) {
        return "";
    }
    CalendarDate start = cldr.Calendars.toGregorianDate(v1.asLong(0), zoneId);
    CalendarDate end = cldr.Calendars.toGregorianDate(v2.asLong(0), zoneId);
    DateIntervalFormatOptions opts = OptionParsers.interval(options);
    return cldr.Calendars.formatDateInterval(start, end, opts);
}
Also used : DateIntervalFormatOptions(com.squarespace.cldrengine.api.DateIntervalFormatOptions) CalendarDate(com.squarespace.cldrengine.api.CalendarDate) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

CalendarDate (com.squarespace.cldrengine.api.CalendarDate)5 CLDR (com.squarespace.cldrengine.CLDR)3 Variable (com.squarespace.template.Variable)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 DateFormatOptions (com.squarespace.cldrengine.api.DateFormatOptions)2 DateIntervalFormatOptions (com.squarespace.cldrengine.api.DateIntervalFormatOptions)2 RelativeTimeFormatOptions (com.squarespace.cldrengine.api.RelativeTimeFormatOptions)1