Example usage for org.joda.time DateTimeZone getDefault

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

Introduction

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

Prototype

public static DateTimeZone getDefault() 

Source Link

Document

Gets the default time zone.

Usage

From source file:org.apache.hadoop.hive.ql.optimizer.calcite.translator.ASTBuilder.java

License:Apache License

public static ASTNode literal(RexLiteral literal, boolean useTypeQualInLiteral) {
    Object val = null;
    int type = 0;
    SqlTypeName sqlType = literal.getType().getSqlTypeName();

    switch (sqlType) {
    case BINARY:/* w  ww.java  2s  .c  o  m*/
    case DATE:
    case TIME:
    case TIMESTAMP:
    case INTERVAL_DAY:
    case INTERVAL_DAY_HOUR:
    case INTERVAL_DAY_MINUTE:
    case INTERVAL_DAY_SECOND:
    case INTERVAL_HOUR:
    case INTERVAL_HOUR_MINUTE:
    case INTERVAL_HOUR_SECOND:
    case INTERVAL_MINUTE:
    case INTERVAL_MINUTE_SECOND:
    case INTERVAL_MONTH:
    case INTERVAL_SECOND:
    case INTERVAL_YEAR:
    case INTERVAL_YEAR_MONTH:
        if (literal.getValue() == null) {
            return ASTBuilder.construct(HiveParser.TOK_NULL, "TOK_NULL").node();
        }
        break;
    case TINYINT:
    case SMALLINT:
    case INTEGER:
    case BIGINT:
    case DOUBLE:
    case DECIMAL:
    case FLOAT:
    case REAL:
    case VARCHAR:
    case CHAR:
    case BOOLEAN:
        if (literal.getValue3() == null) {
            return ASTBuilder.construct(HiveParser.TOK_NULL, "TOK_NULL").node();
        }
    }

    switch (sqlType) {
    case TINYINT:
        if (useTypeQualInLiteral) {
            val = literal.getValue3() + "Y";
        } else {
            val = literal.getValue3();
        }
        type = HiveParser.IntegralLiteral;
        break;
    case SMALLINT:
        if (useTypeQualInLiteral) {
            val = literal.getValue3() + "S";
        } else {
            val = literal.getValue3();
        }
        type = HiveParser.IntegralLiteral;
        break;
    case INTEGER:
        val = literal.getValue3();
        type = HiveParser.IntegralLiteral;
        break;
    case BIGINT:
        if (useTypeQualInLiteral) {
            val = literal.getValue3() + "L";
        } else {
            val = literal.getValue3();
        }
        type = HiveParser.IntegralLiteral;
        break;
    case DOUBLE:
        val = literal.getValue3() + "D";
        type = HiveParser.NumberLiteral;
        break;
    case DECIMAL:
        val = literal.getValue3() + "BD";
        type = HiveParser.NumberLiteral;
        break;
    case FLOAT:
    case REAL:
        val = literal.getValue3();
        type = HiveParser.Number;
        break;
    case VARCHAR:
    case CHAR:
        val = literal.getValue3();
        String escapedVal = BaseSemanticAnalyzer.escapeSQLString(String.valueOf(val));
        type = HiveParser.StringLiteral;
        val = "'" + escapedVal + "'";
        break;
    case BOOLEAN:
        val = literal.getValue3();
        type = ((Boolean) val).booleanValue() ? HiveParser.KW_TRUE : HiveParser.KW_FALSE;
        break;
    case DATE: {
        //Calcite Calendar is always GMT, Hive atm uses JVM local
        final Calendar c = (Calendar) literal.getValue();
        final DateTime dt = new DateTime(c.getTimeInMillis(), DateTimeZone.forTimeZone(c.getTimeZone()));
        type = HiveParser.TOK_DATELITERAL;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        val = df.format(dt.toDateTime(DateTimeZone.getDefault()).toDate());
        val = "'" + val + "'";
    }
        break;
    case TIME:
    case TIMESTAMP: {
        //Calcite Calendar is always GMT, Hive atm uses JVM local
        final Calendar c = (Calendar) literal.getValue();
        final DateTime dt = new DateTime(c.getTimeInMillis(), DateTimeZone.forTimeZone(c.getTimeZone()));
        type = HiveParser.TOK_TIMESTAMPLITERAL;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        val = df.format(dt.toDateTime(DateTimeZone.getDefault()).toDate());
        val = "'" + val + "'";
    }
        break;
    case INTERVAL_YEAR:
    case INTERVAL_MONTH:
    case INTERVAL_YEAR_MONTH: {
        type = HiveParser.TOK_INTERVAL_YEAR_MONTH_LITERAL;
        BigDecimal monthsBd = (BigDecimal) literal.getValue();
        HiveIntervalYearMonth intervalYearMonth = new HiveIntervalYearMonth(monthsBd.intValue());
        val = "'" + intervalYearMonth.toString() + "'";
    }
        break;
    case INTERVAL_DAY:
    case INTERVAL_DAY_HOUR:
    case INTERVAL_DAY_MINUTE:
    case INTERVAL_DAY_SECOND:
    case INTERVAL_HOUR:
    case INTERVAL_HOUR_MINUTE:
    case INTERVAL_HOUR_SECOND:
    case INTERVAL_MINUTE:
    case INTERVAL_MINUTE_SECOND:
    case INTERVAL_SECOND: {
        type = HiveParser.TOK_INTERVAL_DAY_TIME_LITERAL;
        BigDecimal millisBd = (BigDecimal) literal.getValue();

        // Calcite literal is in millis, convert to seconds
        BigDecimal secsBd = millisBd.divide(BigDecimal.valueOf(1000));
        HiveIntervalDayTime intervalDayTime = new HiveIntervalDayTime(secsBd);
        val = "'" + intervalDayTime.toString() + "'";
    }
        break;
    case NULL:
        type = HiveParser.TOK_NULL;
        break;

    //binary type should not be seen.
    case BINARY:
    default:
        throw new RuntimeException("Unsupported Type: " + sqlType);
    }

    return (ASTNode) ParseDriver.adaptor.create(type, String.valueOf(val));
}

From source file:org.apache.hadoop.hive.ql.udf.UDFDateFloor.java

License:Apache License

public TimestampWritable evaluate(TimestampWritable t) {
    if (t == null) {
        return null;
    }/* w  ww .j a v  a 2  s  .c  om*/
    final long originalTimestamp = t.getTimestamp().getTime(); // default
    final long originalTimestampUTC = new DateTime(originalTimestamp).withZoneRetainFields(DateTimeZone.UTC)
            .getMillis(); // default -> utc
    final long newTimestampUTC = granularity.truncate(originalTimestampUTC); // utc
    final long newTimestamp = new DateTime(newTimestampUTC, DateTimeZone.UTC)
            .withZoneRetainFields(DateTimeZone.getDefault()).getMillis(); // utc -> default
    result.setTime(newTimestamp);
    return result;
}

From source file:org.apache.isis.applib.clock.Clock.java

License:Apache License

public static LocalDate getTimeAsLocalDate() {
    final DateTimeZone timeZone = DateTimeZone.forTimeZone(TimeZone.getDefault());
    return new LocalDate(getTime(), timeZone);
}

From source file:org.apache.isis.objectstore.sql.jdbc.JdbcConnector.java

License:Apache License

private void addPreparedValues(final PreparedStatement statement) throws SQLException {
    if (queryValues.size() > 0) {
        int i = 1;
        try {/* w  w w .j a  va2  s  .  c o m*/
            for (final Object value : queryValues) {
                if (value instanceof LocalDate) {
                    try {
                        statement.setObject(i, value, java.sql.Types.DATE);
                    } catch (final SQLException e) {
                        // TODO This daft catch is required my MySQL, which
                        // also requires the TimeZone offset to be
                        // "undone"
                        final LocalDate localDate = (LocalDate) value;
                        final int millisOffset = -DateTimeZone.getDefault().getOffset(null);
                        final java.util.Date javaDate = localDate
                                .toDateTimeAtStartOfDay(DateTimeZone.forOffsetMillis(millisOffset)).toDate();

                        statement.setObject(i, javaDate, java.sql.Types.DATE);
                    }
                } else if (value instanceof InputStream) {
                    statement.setBlob(i, (InputStream) value);
                } else {
                    statement.setObject(i, value);
                }
                i++;
            }
        } catch (final SQLException e) {
            LOG.error("Error adding prepared value " + i + " of type "
                    + queryValues.get(i - 1).getClass().getSimpleName(), e);
            throw e;
        }
    }
}

From source file:org.apache.isis.runtimes.dflt.objectstores.sql.jdbc.JdbcConnector.java

License:Apache License

private void addPreparedValues(final PreparedStatement statement) throws SQLException {
    if (queryValues.size() > 0) {
        int i = 1;
        try {//www  .ja  v a  2 s.  co m
            for (final Object value : queryValues) {
                if (value instanceof LocalDate) {
                    try {
                        statement.setObject(i, value, java.sql.Types.DATE);
                    } catch (final SQLException e) {
                        // TODO This daft catch is required my MySQL, which
                        // also requires the TimeZone offset to be
                        // "undone"
                        final LocalDate localDate = (LocalDate) value;
                        final int millisOffset = -DateTimeZone.getDefault().getOffset(null);
                        final java.util.Date javaDate = localDate
                                .toDateTimeAtStartOfDay(DateTimeZone.forOffsetMillis(millisOffset)).toDate();

                        statement.setObject(i, javaDate, java.sql.Types.DATE);
                    }
                } else {
                    statement.setObject(i, value);
                }
                i++;
            }
        } catch (final SQLException e) {
            LOG.error("Error adding prepared value " + i + " of type "
                    + queryValues.get(i - 1).getClass().getSimpleName(), e);
            throw e;
        }
    }
}

From source file:org.apache.pig.piggybank.evaluation.datetime.truncate.ISOHelper.java

License:Apache License

/**
* @param input a non-null, non-empty Tuple,
*  whose first element is a ISO 8601 string representation of a date, time, or dateTime;
*  with optional time zone.// w  w  w  .ja va 2s .c o m
* @return a DateTime representing the date, 
*  with DateTimeZone set to the time zone parsed from the string,
*  or else DateTimeZone.defaultTimeZone() if one is set,
*  or else UTC.
* @throws ExecException if input is a malformed or empty tuple.
* This method is public so that it can be tested in TestTruncateDateTime. 
* Otherwise, it would have "package" visibility.
*/
public static DateTime parseDateTime(Tuple input) throws ExecException {

    // Save previous default time zone for restore later.
    DateTimeZone previousDefaultTimeZone = DateTimeZone.getDefault();

    // Temporarily set default time zone to UTC, for this parse.
    DateTimeZone.setDefault(DEFAULT_DATE_TIME_ZONE);

    String isoDateString = input.get(0).toString();
    DateTime dt = ISODateTimeFormat.dateTimeParser().withOffsetParsed().parseDateTime(isoDateString);

    // restore previous default TimeZone.
    DateTimeZone.setDefault(previousDefaultTimeZone);

    return dt;
}

From source file:org.apache.tajo.storage.orc.ORCScanner.java

License:Apache License

@Override
public void init() throws IOException {
    OrcReader orcReader;/*from w w  w. j a  va2s . c  om*/

    if (targets == null) {
        targets = schema.toArray();
    }

    super.init();

    outTuple = new VTuple(targets.length);

    Path path = fragment.getPath();

    if (fs == null) {
        fs = FileScanner.getFileSystem((TajoConf) conf, path);
    }

    if (fis == null) {
        fis = fs.open(path);
    }

    OrcDataSource orcDataSource = new HdfsOrcDataSource(this.fragment.getPath().toString(), fis,
            fs.getFileStatus(path).getLen(),
            Integer.parseInt(meta.getOption(StorageConstants.ORC_MAX_MERGE_DISTANCE,
                    StorageConstants.DEFAULT_ORC_MAX_MERGE_DISTANCE)));

    targetColInfo = new ColumnInfo[targets.length];
    for (int i = 0; i < targets.length; i++) {
        ColumnInfo cinfo = new ColumnInfo();
        cinfo.type = targets[i].getDataType();
        cinfo.id = schema.getColumnId(targets[i].getQualifiedName());
        targetColInfo[i] = cinfo;
    }

    // creating vectors for buffering
    vectors = new Vector[targetColInfo.length];
    for (int i = 0; i < targetColInfo.length; i++) {
        vectors[i] = createOrcVector(targetColInfo[i].type);
    }

    Set<Integer> columnSet = new HashSet<Integer>();
    for (ColumnInfo colInfo : targetColInfo) {
        columnSet.add(colInfo.id);
    }

    orcReader = new OrcReader(orcDataSource, new OrcMetadataReader());

    // TODO: make OrcPredicate useful
    // TODO: TimeZone should be from conf
    // TODO: it might be splittable
    recordReader = orcReader.createRecordReader(columnSet, OrcPredicate.TRUE, fragment.getStartKey(),
            fragment.getLength(), DateTimeZone.getDefault());

    LOG.debug("file fragment { path: " + fragment.getPath() + ", start offset: " + fragment.getStartKey()
            + ", length: " + fragment.getLength() + "}");

    getNextBatch();
}

From source file:org.apache.wicket.datetime.DateConverter.java

License:Apache License

/**
 * Gets the server time zone. Override this method if you want to fix to a certain time zone,
 * regardless of what actual time zone the server is in.
 * /*w ww  .  j a  va 2s.  c o m*/
 * @return The server time zone
 */
protected DateTimeZone getTimeZone() {
    return DateTimeZone.getDefault();
}

From source file:org.apereo.portal.test.TimeZoneTestUtils.java

License:Apache License

public void beforeTest() {
    defaultTimeZone = TimeZone.getDefault();
    defaultDateTimeZone = DateTimeZone.getDefault();
    defaultUserTimezone = System.getProperty("user.timezone");

    TimeZone.setDefault(testTimeZone.toTimeZone());
    DateTimeZone.setDefault(testTimeZone);
    System.setProperty("user.timezone", testTimeZone.getID());
}

From source file:org.basepom.mojo.propertyhelper.DateField.java

License:Apache License

@Override
public Optional<String> getPropertyValue() {
    final DateTimeZone timeZone = dateDefinition.getTimezone().isPresent()
            ? DateTimeZone.forID(dateDefinition.getTimezone().get())
            : DateTimeZone.getDefault();

    final Optional<String> format = dateDefinition.getFormat();
    final DateTimeFormatter formatter;
    if (format.isPresent()) {
        formatter = DateTimeFormat.forPattern(format.get());
    } else {/*from w  w  w  .j  a  v a2s .  c o  m*/
        formatter = null;
    }

    DateTime date = getDateTime(valueProvider.getValue(), formatter, timeZone);

    if (date == null && dateDefinition.getValue().isPresent()) {
        date = new DateTime(dateDefinition.getValue().get(), timeZone);
    }

    if (date == null) {
        date = new DateTime(timeZone);
    }

    String result;
    if (formatter != null) {
        result = formatter.print(date);
        valueProvider.setValue(result);
    } else {
        result = date.toString();
        valueProvider.setValue(Long.toString(date.getMillis()));
    }

    if (dateDefinition.getTransformers().isPresent()) {
        result = TransformerRegistry.applyTransformers(dateDefinition.getTransformers().get(), result);
    }
    return Optional.fromNullable(result);
}