Example usage for org.joda.time DateTimeZone forID

List of usage examples for org.joda.time DateTimeZone forID

Introduction

In this page you can find the example usage for org.joda.time DateTimeZone forID.

Prototype

@FromString
public static DateTimeZone forID(String id) 

Source Link

Document

Gets a time zone instance for the specified time zone id.

Usage

From source file:io.crate.expression.scalar.TimeZoneParser.java

License:Apache License

public static DateTimeZone parseTimeZone(BytesRef timezone) throws IllegalArgumentException {
    if (timezone == null) {
        throw new IllegalArgumentException("invalid time zone value NULL");
    }/*from ww  w. j  a  va  2 s  .  c  om*/
    if (timezone.equals(DEFAULT_TZ_BYTES_REF)) {
        return DEFAULT_TZ;
    }

    DateTimeZone tz = TIME_ZONE_MAP.get(timezone);
    if (tz == null) {
        try {
            String text = timezone.utf8ToString();
            int index = text.indexOf(':');
            if (index != -1) {
                int beginIndex = text.charAt(0) == '+' ? 1 : 0;
                // format like -02:30
                tz = DateTimeZone.forOffsetHoursMinutes(Integer.parseInt(text.substring(beginIndex, index)),
                        Integer.parseInt(text.substring(index + 1)));
            } else {
                // id, listed here: http://joda-time.sourceforge.net/timezones.html
                // or here: http://www.joda.org/joda-time/timezones.html
                tz = DateTimeZone.forID(text);
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(
                    String.format(Locale.ENGLISH, "invalid time zone value '%s'", timezone.utf8ToString()));
        }
        TIME_ZONE_MAP.putIfAbsent(timezone, tz);
    }
    return tz;
}

From source file:io.crate.operation.scalar.DateTruncTimeZoneAwareFunction.java

License:Apache License

private DateTimeZone parseZone(BytesRef zone) throws IllegalArgumentException {
    String text = zone.utf8ToString();
    int index = text.indexOf(':');
    if (index != -1) {
        int beginIndex = text.charAt(0) == '+' ? 1 : 0;
        // format like -02:30
        return DateTimeZone.forOffsetHoursMinutes(Integer.parseInt(text.substring(beginIndex, index)),
                Integer.parseInt(text.substring(index + 1)));
    } else {/*from  www  .ja v  a 2  s . c  o  m*/
        // id, listed here: http://joda-time.sourceforge.net/timezones.html
        // or here: http://www.joda.org/joda-time/timezones.html
        return DateTimeZone.forID(text);
    }
}

From source file:io.druid.jackson.DruidDefaultSerializersModule.java

License:Apache License

public DruidDefaultSerializersModule() {
    super("Druid default serializers");

    JodaStuff.register(this);
    addDeserializer(Granularity.class, new JsonDeserializer<Granularity>() {
        @Override/*w  ww . j a v  a 2  s  . c  o m*/
        public Granularity deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            return Granularity.valueOf(jp.getText().toUpperCase());
        }
    });
    addDeserializer(DateTimeZone.class, new JsonDeserializer<DateTimeZone>() {
        @Override
        public DateTimeZone deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
            String tzId = jp.getText();
            try {
                return DateTimeZone.forID(tzId);
            } catch (IllegalArgumentException e) {
                // also support Java timezone strings
                return DateTimeZone.forTimeZone(TimeZone.getTimeZone(tzId));
            }
        }
    });
    addSerializer(DateTimeZone.class, new JsonSerializer<DateTimeZone>() {
        @Override
        public void serialize(DateTimeZone dateTimeZone, JsonGenerator jsonGenerator,
                SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
            jsonGenerator.writeString(dateTimeZone.getID());
        }
    });
    addSerializer(Sequence.class, new JsonSerializer<Sequence>() {
        @Override
        public void serialize(Sequence value, final JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            jgen.writeStartArray();
            value.accumulate(null, new Accumulator<Object, Object>() {
                @Override
                public Object accumulate(Object o, Object o1) {
                    try {
                        jgen.writeObject(o1);
                    } catch (IOException e) {
                        throw Throwables.propagate(e);
                    }
                    return null;
                }
            });
            jgen.writeEndArray();
        }
    });
    addSerializer(Yielder.class, new JsonSerializer<Yielder>() {
        @Override
        public void serialize(Yielder yielder, final JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            try {
                jgen.writeStartArray();
                while (!yielder.isDone()) {
                    final Object o = yielder.get();
                    jgen.writeObject(o);
                    yielder = yielder.next(null);
                }
                jgen.writeEndArray();
            } finally {
                yielder.close();
            }
        }
    });
    addSerializer(ByteOrder.class, ToStringSerializer.instance);
    addDeserializer(ByteOrder.class, new JsonDeserializer<ByteOrder>() {
        @Override
        public ByteOrder deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
            if (ByteOrder.BIG_ENDIAN.toString().equals(jp.getText())) {
                return ByteOrder.BIG_ENDIAN;
            }
            return ByteOrder.LITTLE_ENDIAN;
        }
    });
}

From source file:io.druid.java.util.common.DateTimes.java

License:Apache License

public static DateTimeZone inferTzfromString(String tzId) {
    try {//from ww w. jav a2  s.  co m
        return DateTimeZone.forID(tzId);
    } catch (IllegalArgumentException e) {
        // also support Java timezone strings
        return DateTimeZone.forTimeZone(TimeZone.getTimeZone(tzId));
    }
}

From source file:io.druid.query.expression.ExprUtils.java

License:Apache License

public static DateTimeZone toTimeZone(final Expr timeZoneArg) {
    if (!timeZoneArg.isLiteral()) {
        throw new IAE("Time zone must be a literal");
    }//from   w w w. ja v  a2 s  .c  o m

    final Object literalValue = timeZoneArg.getLiteralValue();
    return literalValue == null ? DateTimeZone.UTC : DateTimeZone.forID((String) literalValue);
}

From source file:io.druid.query.expression.ExprUtils.java

License:Apache License

public static PeriodGranularity toPeriodGranularity(final Expr periodArg, final Expr originArg,
        final Expr timeZoneArg, final Expr.ObjectBinding bindings) {
    final Period period = new Period(periodArg.eval(bindings).asString());
    final DateTime origin;
    final DateTimeZone timeZone;

    if (originArg == null) {
        origin = null;/*w  ww.j av a 2  s . c om*/
    } else {
        final Object value = originArg.eval(bindings).value();
        origin = value != null ? new DateTime(value) : null;
    }

    if (timeZoneArg == null) {
        timeZone = null;
    } else {
        final String value = timeZoneArg.eval(bindings).asString();
        timeZone = value != null ? DateTimeZone.forID(value) : null;
    }

    return new PeriodGranularity(period, origin, timeZone);
}

From source file:io.druid.query.expression.TimestampParseExprMacro.java

License:Apache License

@Override
public Expr apply(final List<Expr> args) {
    if (args.size() < 1 || args.size() > 3) {
        throw new IAE("Function[%s] must have 1 to 3 arguments", name());
    }/* w  ww.  j a  v a  2  s .c o m*/

    final Expr arg = args.get(0);
    final String formatString = args.size() > 1 ? (String) args.get(1).getLiteralValue() : null;
    final DateTimeZone timeZone;

    if (args.size() > 2 && args.get(2).getLiteralValue() != null) {
        timeZone = DateTimeZone.forID((String) args.get(2).getLiteralValue());
    } else {
        timeZone = DateTimeZone.UTC;
    }

    final DateTimeFormatter formatter = formatString == null ? ISODateTimeFormat.dateTimeParser()
            : DateTimeFormat.forPattern(formatString).withZone(timeZone);

    class TimestampParseExpr implements Expr {
        @Nonnull
        @Override
        public ExprEval eval(final ObjectBinding bindings) {
            try {
                return ExprEval.of(formatter.parseDateTime(arg.eval(bindings).asString()).getMillis());
            } catch (IllegalArgumentException e) {
                // Catch exceptions potentially thrown by formatter.parseDateTime. Our docs say that unparseable timestamps
                // are returned as nulls.
                return ExprEval.of(null);
            }
        }

        @Override
        public void visit(final Visitor visitor) {
            arg.visit(visitor);
            visitor.visit(this);
        }
    }

    return new TimestampParseExpr();
}

From source file:io.druid.sql.calcite.expression.TimeExtractOperatorConversion.java

License:Apache License

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature,
        final RexNode rexNode) {
    final RexCall call = (RexCall) rexNode;
    final RexNode timeArg = call.getOperands().get(0);
    final DruidExpression timeExpression = Expressions.toDruidExpression(plannerContext, rowSignature, timeArg);
    if (timeExpression == null) {
        return null;
    }/*from  ww  w  .j a va2s.  c o  m*/

    final TimestampExtractExprMacro.Unit unit = TimestampExtractExprMacro.Unit
            .valueOf(RexLiteral.stringValue(call.getOperands().get(1)).toUpperCase());

    final DateTimeZone timeZone = call.getOperands().size() > 2
            && !RexLiteral.isNullLiteral(call.getOperands().get(2))
                    ? DateTimeZone.forID(RexLiteral.stringValue(call.getOperands().get(2)))
                    : plannerContext.getTimeZone();

    return applyTimeExtract(timeExpression, unit, timeZone);
}

From source file:io.druid.sql.calcite.expression.TimeFloorOperatorConversion.java

License:Apache License

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature,
        final RexNode rexNode) {
    final RexCall call = (RexCall) rexNode;
    final List<RexNode> operands = call.getOperands();
    final List<DruidExpression> druidExpressions = Expressions.toDruidExpressions(plannerContext, rowSignature,
            operands);//w w w  .j a  va  2 s . co  m

    if (druidExpressions == null) {
        return null;
    } else if (operands.get(1).isA(SqlKind.LITERAL)
            && (operands.size() <= 2 || operands.get(2).isA(SqlKind.LITERAL))
            && (operands.size() <= 3 || operands.get(3).isA(SqlKind.LITERAL))) {
        // Granularity is a literal. Special case since we can use an extractionFn here.
        final Period period = new Period(RexLiteral.stringValue(operands.get(1)));
        final DateTime origin = operands.size() > 2 && !RexLiteral.isNullLiteral(operands.get(2))
                ? Calcites.calciteDateTimeLiteralToJoda(operands.get(2), plannerContext.getTimeZone())
                : null;
        final DateTimeZone timeZone = operands.size() > 3 && !RexLiteral.isNullLiteral(operands.get(3))
                ? DateTimeZone.forID(RexLiteral.stringValue(operands.get(3)))
                : plannerContext.getTimeZone();
        final PeriodGranularity granularity = new PeriodGranularity(period, origin, timeZone);
        return applyTimestampFloor(druidExpressions.get(0), granularity);
    } else {
        // Granularity is dynamic
        return DruidExpression.fromFunctionCall("timestamp_floor", druidExpressions);
    }
}

From source file:io.druid.sql.calcite.expression.TimeFormatOperatorConversion.java

License:Apache License

@Override
public DruidExpression toDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature,
        final RexNode rexNode) {
    final RexCall call = (RexCall) rexNode;
    final RexNode timeArg = call.getOperands().get(0);
    final DruidExpression timeExpression = Expressions.toDruidExpression(plannerContext, rowSignature, timeArg);
    if (timeExpression == null) {
        return null;
    }//  w  w  w .ja v  a  2 s  . com

    final String pattern = call.getOperands().size() > 1 && !RexLiteral.isNullLiteral(call.getOperands().get(1))
            ? RexLiteral.stringValue(call.getOperands().get(1))
            : "yyyy-MM-dd'T'HH:mm:ss.SSSZZ";
    final DateTimeZone timeZone = call.getOperands().size() > 2
            && !RexLiteral.isNullLiteral(call.getOperands().get(2))
                    ? DateTimeZone.forID(RexLiteral.stringValue(call.getOperands().get(2)))
                    : plannerContext.getTimeZone();

    return timeExpression.map(simpleExtraction -> applyTimestampFormat(simpleExtraction, pattern, timeZone),
            expression -> DruidExpression.functionCall("timestamp_format",
                    ImmutableList
                            .of(expression, DruidExpression.stringLiteral(pattern),
                                    DruidExpression.stringLiteral(timeZone.getID()))
                            .stream().map(DruidExpression::fromExpression).collect(Collectors.toList())));
}