Example usage for org.joda.time DateTimeZone UTC

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

Introduction

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

Prototype

DateTimeZone UTC

To view the source code for org.joda.time DateTimeZone UTC.

Click Source Link

Document

The time zone for Universal Coordinated Time

Usage

From source file:com.facebook.presto.hive.OrcHiveRecordCursor.java

License:Apache License

public OrcHiveRecordCursor(RecordReader recordReader, long totalBytes, Properties splitSchema,
        List<HivePartitionKey> partitionKeys, List<HiveColumnHandle> columns, DateTimeZone hiveStorageTimeZone,
        DateTimeZone sessionTimeZone, TypeManager typeManager) {
    checkNotNull(recordReader, "recordReader is null");
    checkArgument(totalBytes >= 0, "totalBytes is negative");
    checkNotNull(splitSchema, "splitSchema is null");
    checkNotNull(partitionKeys, "partitionKeys is null");
    checkNotNull(columns, "columns is null");
    checkNotNull(hiveStorageTimeZone, "hiveStorageTimeZone is null");
    checkNotNull(sessionTimeZone, "sessionTimeZone is null");

    this.recordReader = recordReader;
    this.totalBytes = totalBytes;
    this.sessionTimeZone = sessionTimeZone;

    int size = columns.size();

    this.names = new String[size];
    this.types = new Type[size];
    this.hiveTypes = new HiveType[size];

    this.fieldInspectors = new ObjectInspector[size];

    this.hiveColumnIndexes = new int[size];

    this.isPartitionColumn = new boolean[size];

    this.loaded = new boolean[size];
    this.booleans = new boolean[size];
    this.longs = new long[size];
    this.doubles = new double[size];
    this.slices = new Slice[size];
    this.nulls = new boolean[size];

    // ORC stores timestamps relative to 2015-01-01 00:00:00 but in the timezone of the writer
    // When reading back a timestamp the Hive ORC reader will an epoch in this machine's timezone
    // We must correct for the difference between the writer's timezone and this machine's
    // timezone (on 2015-01-01)
    long hiveStorageCorrection = new DateTime(2015, 1, 1, 0, 0, hiveStorageTimeZone).getMillis()
            - new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).getMillis();
    long jvmCorrection = new DateTime(2015, 1, 1, 0, 0).getMillis()
            - new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).getMillis();
    timeZoneCorrection = hiveStorageCorrection - jvmCorrection;

    // initialize data columns
    StructObjectInspector rowInspector = getTableObjectInspector(splitSchema);

    for (int i = 0; i < columns.size(); i++) {
        HiveColumnHandle column = columns.get(i);

        names[i] = column.getName();/*from w w  w .  j  a  v  a  2 s  .  co  m*/
        types[i] = typeManager.getType(column.getTypeName());
        hiveTypes[i] = column.getHiveType();

        if (!column.isPartitionKey()) {
            fieldInspectors[i] = rowInspector.getStructFieldRef(column.getName()).getFieldObjectInspector();
        }

        hiveColumnIndexes[i] = column.getHiveColumnIndex();
        isPartitionColumn[i] = column.isPartitionKey();
    }

    // parse requested partition columns
    Map<String, HivePartitionKey> partitionKeysByName = uniqueIndex(partitionKeys,
            HivePartitionKey.nameGetter());
    for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
        HiveColumnHandle column = columns.get(columnIndex);
        if (column.isPartitionKey()) {
            HivePartitionKey partitionKey = partitionKeysByName.get(column.getName());
            checkArgument(partitionKey != null, "Unknown partition key %s", column.getName());

            byte[] bytes = partitionKey.getValue().getBytes(Charsets.UTF_8);

            if (HiveUtil.isHiveNull(bytes)) {
                nulls[columnIndex] = true;
            } else if (types[columnIndex].equals(BOOLEAN)) {
                if (isTrue(bytes, 0, bytes.length)) {
                    booleans[columnIndex] = true;
                } else if (isFalse(bytes, 0, bytes.length)) {
                    booleans[columnIndex] = false;
                } else {
                    String valueString = new String(bytes, Charsets.UTF_8);
                    throw new IllegalArgumentException(
                            String.format("Invalid partition value '%s' for BOOLEAN partition key %s",
                                    valueString, names[columnIndex]));
                }
            } else if (types[columnIndex].equals(BIGINT)) {
                if (bytes.length == 0) {
                    throw new IllegalArgumentException(String.format(
                            "Invalid partition value '' for BIGINT partition key %s", names[columnIndex]));
                }
                longs[columnIndex] = parseLong(bytes, 0, bytes.length);
            } else if (types[columnIndex].equals(DOUBLE)) {
                if (bytes.length == 0) {
                    throw new IllegalArgumentException(String.format(
                            "Invalid partition value '' for DOUBLE partition key %s", names[columnIndex]));
                }
                doubles[columnIndex] = parseDouble(bytes, 0, bytes.length);
            } else if (types[columnIndex].equals(VARCHAR)) {
                slices[columnIndex] = Slices.wrappedBuffer(bytes);
            } else if (types[columnIndex].equals(DATE)) {
                longs[columnIndex] = ISODateTimeFormat.date().withZone(DateTimeZone.UTC)
                        .parseMillis(partitionKey.getValue());
            } else if (types[columnIndex].equals(TIMESTAMP)) {
                longs[columnIndex] = parseHiveTimestamp(partitionKey.getValue(), hiveStorageTimeZone);
            } else {
                throw new UnsupportedOperationException("Unsupported column type: " + types[columnIndex]);
            }
        }
    }
}

From source file:com.facebook.presto.kudu.properties.KuduTableProperties.java

License:Apache License

private static Object toValue(Schema schema, PartialRow bound, Integer idx) {
    Type type = schema.getColumnByIndex(idx).getType();
    switch (type) {
    case UNIXTIME_MICROS:
        long millis = bound.getLong(idx) / 1000;
        return ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).print(millis);
    case STRING:/*from   ww w  .  ja v  a  2s . c o m*/
        return bound.getString(idx);
    case INT64:
        return bound.getLong(idx);
    case INT32:
        return bound.getInt(idx);
    case INT16:
        return bound.getShort(idx);
    case INT8:
        return (short) bound.getByte(idx);
    case BOOL:
        return bound.getBoolean(idx);
    case BINARY:
        return bound.getBinaryCopy(idx);
    default:
        throw new IllegalStateException("Unhandled type " + type + " for range partition");
    }
}

From source file:com.facebook.presto.kudu.properties.KuduTableProperties.java

License:Apache License

private static long toUnixTimeMicros(Object obj, Type type, String name) {
    if (Number.class.isAssignableFrom(obj.getClass())) {
        return ((Number) obj).longValue();
    } else if (obj instanceof String) {
        String s = (String) obj;
        s = s.trim().replace(' ', 'T');
        long millis = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC).parseMillis(s);
        return millis * 1000;
    } else {//from  w w w.java  2s . c  o  m
        handleInvalidValue(name, type, obj);
        return 0;
    }
}

From source file:com.facebook.presto.raptor.storage.OrcTestingUtil.java

License:Apache License

public static OrcRecordReader createRecordReader(OrcReader orcReader, Map<Integer, Type> includedColumns)
        throws IOException {
    return orcReader.createRecordReader(includedColumns, OrcPredicate.TRUE, DateTimeZone.UTC,
            new AggregatedMemoryContext());
}

From source file:com.facebook.presto.tests.H2QueryRunner.java

License:Apache License

private static void insertRows(ConnectorTableMetadata tableMetadata, Handle handle, RecordSet data) {
    List<ColumnMetadata> columns = tableMetadata.getColumns().stream()
            .filter(columnMetadata -> !columnMetadata.isHidden()).collect(toImmutableList());

    String vars = Joiner.on(',').join(nCopies(columns.size(), "?"));
    String sql = format("INSERT INTO %s VALUES (%s)", tableMetadata.getTable().getTableName(), vars);

    RecordCursor cursor = data.cursor();
    while (true) {
        // insert 1000 rows at a time
        PreparedBatch batch = handle.prepareBatch(sql);
        for (int row = 0; row < 1000; row++) {
            if (!cursor.advanceNextPosition()) {
                batch.execute();/*from w w w  .  ja  v a  2  s  .  c  o  m*/
                return;
            }
            PreparedBatchPart part = batch.add();
            for (int column = 0; column < columns.size(); column++) {
                Type type = columns.get(column).getType();
                if (BOOLEAN.equals(type)) {
                    part.bind(column, cursor.getBoolean(column));
                } else if (BIGINT.equals(type)) {
                    part.bind(column, cursor.getLong(column));
                } else if (INTEGER.equals(type)) {
                    part.bind(column, (int) cursor.getLong(column));
                } else if (DOUBLE.equals(type)) {
                    part.bind(column, cursor.getDouble(column));
                } else if (type instanceof VarcharType) {
                    part.bind(column, cursor.getSlice(column).toStringUtf8());
                } else if (DATE.equals(type)) {
                    long millisUtc = TimeUnit.DAYS.toMillis(cursor.getLong(column));
                    // H2 expects dates in to be millis at midnight in the JVM timezone
                    long localMillis = DateTimeZone.UTC.getMillisKeepLocal(DateTimeZone.getDefault(),
                            millisUtc);
                    part.bind(column, new Date(localMillis));
                } else {
                    throw new IllegalArgumentException("Unsupported type " + type);
                }
            }
        }
        batch.execute();
    }
}

From source file:com.facebook.util.TimeIntervalType.java

License:Apache License

/**
 * Returns the number of milliseconds per unit of this interval type.
 * //w ww  . ja v  a2s  . c om
 * @return the number of milliseconds per unit of this interval type.
 */
public long toDurationMillis() {
    return fieldType.getDurationType().getField(
            // Assume that durations are always in UTC
            ISOChronology.getInstance(DateTimeZone.UTC)).getUnitMillis();
}

From source file:com.facebook.util.TimeUtil.java

License:Apache License

public static DateTimeZone getDateTimeZone(String dateTimeZoneStr) {
    if ((dateTimeZoneStr == null) || dateTimeZoneStr.isEmpty()) {
        return DateTimeZone.UTC;
    }//from   w w  w . j  a va 2 s.  com
    return TIME_ZONE_MAP.get(dateTimeZoneStr);
}

From source file:com.facebook.util.TimeUtil.java

License:Apache License

public static ISOChronology getChronology(String dateTimeZoneStr) {
    if ((dateTimeZoneStr == null) || dateTimeZoneStr.isEmpty()) {
        dateTimeZoneStr = DateTimeZone.UTC.getID();
    }// w  ww.j  a va  2 s.  com
    return CHRONOLOGY_MAP.get(dateTimeZoneStr);
}

From source file:com.fota.comm.util.DateTimeTypeHandlerCallback.java

License:Open Source License

public Object getResult(ResultGetter getter) throws SQLException {
    Timestamp ts = getter.getTimestamp();
    if (ts != null) {
        return new DateTime(ts, DateTimeZone.UTC);
    } else {/*  www. j a v a2s  . com*/
        return null;
    }
}

From source file:com.foundationdb.server.types.mcompat.mfuncs.MConvertTZ.java

License:Open Source License

@Override
protected void doEvaluate(TExecutionContext context, LazyList<? extends ValueSource> inputs,
        ValueTarget output) {//  w w w  .j  av a2 s .  c  om
    long original = inputs.get(0).getInt64();
    long[] ymd = MDateAndTime.decodeDateTime(original);
    if (!MDateAndTime.isValidDateTime(ymd, ZeroFlag.YEAR)) {
        output.putNull();
    } else {
        try {
            DateTimeZone fromTz = MDateAndTime.parseTimeZone(inputs.get(1).getString());
            DateTimeZone toTz = MDateAndTime.parseTimeZone(inputs.get(2).getString());
            MutableDateTime date = MDateAndTime.toJodaDateTime(ymd, fromTz);
            // If the value falls out of the supported range of the TIMESTAMP
            // when converted from fromTz to UTC, no conversion occurs.
            date.setZone(DateTimeZone.UTC);
            final long converted;
            if (MDateAndTime.isValidTimestamp(date)) {
                date.setZone(toTz);
                converted = MDateAndTime.encodeDateTime(date);
            } else {
                converted = original;
            }
            output.putInt64(converted);
        } catch (InvalidDateFormatException e) {
            context.warnClient(e);
            output.putNull();
        }
    }
}