Search in sources :

Example 91 with TimeZone

use of java.util.TimeZone in project hive by apache.

the class TestDateTimeMath method testTimestampIntervalDayTimeArithmetic.

@Test
public void testTimestampIntervalDayTimeArithmetic() throws Exception {
    char plus = '+';
    char minus = '-';
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03", plus, "1 1:1:1", "2001-01-02 02:03:04");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.456", plus, "1 1:1:1", "2001-01-02 02:03:04.456");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.456", plus, "1 1:1:1.555", "2001-01-02 02:03:05.011");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03", plus, "1 1:1:1.555555555", "2001-01-02 02:03:04.555555555");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.456", plus, "1 1:1:1.555555555", "2001-01-02 02:03:05.011555555");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.500", plus, "1 1:1:1.499", "2001-01-02 02:03:04.999");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.500", plus, "1 1:1:1.500", "2001-01-02 02:03:05.0");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.500", plus, "1 1:1:1.501", "2001-01-02 02:03:05.001");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.500000000", plus, "1 1:1:1.4999999999", "2001-01-02 02:03:04.999999999");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.500000000", plus, "1 1:1:1.500", "2001-01-02 02:03:05.0");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03.500000000", plus, "1 1:1:1.500000001", "2001-01-02 02:03:05.000000001");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03", minus, "0 01:02:03", "2001-01-01 00:00:00");
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03", minus, "0 0:0:0", "2001-01-01 01:02:03");
    checkTsIntervalDayTimeArithmetic(null, plus, "1 1:1:1.555555555", null);
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03", plus, null, null);
    checkTsIntervalDayTimeArithmetic(null, minus, "1 1:1:1.555555555", null);
    checkTsIntervalDayTimeArithmetic("2001-01-01 01:02:03", minus, null, null);
    // Try some time zone boundaries
    TimeZone originalTz = TimeZone.getDefault();
    try {
        // America/Los_Angeles DST dates - 2015-03-08 02:00:00/2015-11-01 02:00:00
        TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
        checkTsIntervalDayTimeArithmetic("2015-03-08 01:59:58", plus, "0 0:0:01", "2015-03-08 01:59:59");
        checkTsIntervalDayTimeArithmetic("2015-03-08 01:59:59", plus, "0 0:0:01", "2015-03-08 03:00:00");
        checkTsIntervalDayTimeArithmetic("2015-03-08 03:00:00", minus, "0 0:0:01", "2015-03-08 01:59:59");
        checkTsIntervalDayTimeArithmetic("2015-03-08 01:59:59.995", plus, "0 0:0:0.005", "2015-03-08 03:00:00");
        checkTsIntervalDayTimeArithmetic("2015-03-08 01:59:59.995", plus, "0 0:0:0.0051", "2015-03-08 03:00:00.0001");
        checkTsIntervalDayTimeArithmetic("2015-03-08 03:00:00", minus, "0 0:0:0.005", "2015-03-08 01:59:59.995");
        checkTsIntervalDayTimeArithmetic("2015-11-01 01:59:58", plus, "0 0:0:01", "2015-11-01 01:59:59");
        checkTsIntervalDayTimeArithmetic("2015-11-01 01:59:59", plus, "0 0:0:01", "2015-11-01 02:00:00");
        // UTC has no such adjustment
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
        checkTsIntervalDayTimeArithmetic("2015-03-08 01:59:58", plus, "0 0:0:01", "2015-03-08 01:59:59");
        checkTsIntervalDayTimeArithmetic("2015-03-08 01:59:59", plus, "0 0:0:01", "2015-03-08 02:00:00");
    } finally {
        TimeZone.setDefault(originalTz);
    }
}
Also used : TimeZone(java.util.TimeZone)

Example 92 with TimeZone

use of java.util.TimeZone in project hive by apache.

the class TestDateWritable method testDaylightSavingsTime.

@Test
public void testDaylightSavingsTime() throws Exception {
    LinkedList<DtMismatch> bad = new LinkedList<>();
    for (String timeZone : TimeZone.getAvailableIDs()) {
        TimeZone previousDefault = TimeZone.getDefault();
        TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
        assertEquals("Default timezone should now be " + timeZone, timeZone, TimeZone.getDefault().getID());
        ExecutorService threadPool = Executors.newFixedThreadPool(1);
        try {
            // TODO: pointless
            threadPool.submit(new DateTestCallable(bad, timeZone)).get();
        } finally {
            threadPool.shutdown();
            TimeZone.setDefault(previousDefault);
        }
    }
    StringBuilder errors = new StringBuilder("\nDATE MISMATCH:\n");
    for (DtMismatch dm : bad) {
        errors.append("E ").append(dm.tz).append(": ").append(dm.expected).append(" != ").append(dm.found).append("\n");
    }
    LOG.error(errors.toString());
    if (!bad.isEmpty())
        throw new Exception(bad.size() + " mismatches, see logs");
}
Also used : TimeZone(java.util.TimeZone) ExecutorService(java.util.concurrent.ExecutorService) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException)

Example 93 with TimeZone

use of java.util.TimeZone in project hibernate-orm by hibernate.

the class LocalDateCustomSessionLevelTimeZoneTest method testTimeZone.

@Test
@TestForIssue(jiraKey = "HHH-11396")
public void testTimeZone() {
    TimeZone old = TimeZone.getDefault();
    try {
        // The producer (MySQL) Berlin and returns 1980-01-01
        TimeZone jdbcTimeZone = TimeZone.getTimeZone("Europe/Berlin");
        TimeZone.setDefault(jdbcTimeZone);
        //hibernate.connection.url jdbc:mysql://localhost/hibernate_orm_test
        doInHibernateSessionBuilder(() -> sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE), s -> {
            Person person = new Person();
            person.id = 1L;
            s.persist(person);
        });
        doInHibernateSessionBuilder(() -> sessionFactory().withOptions().jdbcTimeZone(TIME_ZONE), s -> {
            Person person = s.find(Person.class, 1L);
            assertEquals(LocalDate.of(2017, 3, 7), person.createdOn);
        });
    } finally {
        TimeZone.setDefault(old);
    }
}
Also used : TimeZone(java.util.TimeZone) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 94 with TimeZone

use of java.util.TimeZone in project pinot by linkedin.

the class SegmentMetadataImpl method toJson.

/**
   * Converts segment metadata to json
   * @param columnFilter list only  the columns in the set. Lists all the columns if
   *                     the parameter value is null
   * @return json representation of segment metadata
   */
public JSONObject toJson(@Nullable Set<String> columnFilter) throws JSONException {
    JSONObject rootMeta = new JSONObject();
    try {
        rootMeta.put("segmentName", _segmentName);
        rootMeta.put("schemaName", _schema != null ? _schema.getSchemaName() : JSONObject.NULL);
        rootMeta.put("crc", _crc);
        rootMeta.put("creationTimeMillis", _creationTime);
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS' UTC'");
        dateFormat.setTimeZone(timeZone);
        String creationTimeStr = _creationTime != Long.MIN_VALUE ? dateFormat.format(new Date(_creationTime)) : "";
        rootMeta.put("creationTimeReadable", creationTimeStr);
        rootMeta.put("timeGranularitySec", _timeGranularity != null ? _timeGranularity.getStandardSeconds() : null);
        if (_timeInterval == null) {
            rootMeta.put("startTimeMillis", (String) null);
            rootMeta.put("startTimeReadable", "null");
            rootMeta.put("endTimeMillis", (String) null);
            rootMeta.put("endTimeReadable", "null");
        } else {
            rootMeta.put("startTimeMillis", _timeInterval.getStartMillis());
            rootMeta.put("startTimeReadable", _timeInterval.getStart().toString());
            rootMeta.put("endTimeMillis", _timeInterval.getEndMillis());
            rootMeta.put("endTimeReadable", _timeInterval.getEnd().toString());
        }
        rootMeta.put("pushTimeMillis", _pushTime);
        String pushTimeStr = _pushTime != Long.MIN_VALUE ? dateFormat.format(new Date(_pushTime)) : "";
        rootMeta.put("pushTimeReadable", pushTimeStr);
        rootMeta.put("refreshTimeMillis", _refreshTime);
        String refreshTimeStr = _refreshTime != Long.MIN_VALUE ? dateFormat.format(new Date(_refreshTime)) : "";
        rootMeta.put("refreshTimeReadable", refreshTimeStr);
        rootMeta.put("segmentVersion", _segmentVersion.toString());
        rootMeta.put("hasStarTree", hasStarTree());
        rootMeta.put("creatorName", _creatorName == null ? JSONObject.NULL : _creatorName);
        rootMeta.put("paddingCharacter", String.valueOf(_paddingCharacter));
        rootMeta.put("hllLog2m", _hllLog2m);
        JSONArray columnsJson = new JSONArray();
        ObjectMapper mapper = new ObjectMapper();
        for (String column : _allColumns) {
            if (columnFilter != null && !columnFilter.contains(column)) {
                continue;
            }
            ColumnMetadata columnMetadata = _columnMetadataMap.get(column);
            JSONObject columnJson = new JSONObject(mapper.writeValueAsString(columnMetadata));
            columnsJson.put(columnJson);
        }
        rootMeta.put("columns", columnsJson);
        return rootMeta;
    } catch (Exception e) {
        LOGGER.error("Failed to convert field to json for segment: {}", _segmentName, e);
        throw new RuntimeException("Failed to convert segment metadata to json", e);
    }
}
Also used : TimeZone(java.util.TimeZone) JSONObject(org.json.JSONObject) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) JSONArray(org.json.JSONArray) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) JSONException(org.json.JSONException) IOException(java.io.IOException) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Example 95 with TimeZone

use of java.util.TimeZone in project morphia by mongodb.

the class CalendarConverter method decode.

@Override
public Object decode(final Class type, final Object o, final MappedField mf) {
    if (o == null) {
        return null;
    }
    final List values = (List) o;
    if (values.size() < 2) {
        return null;
    }
    //-- date --//
    final Date utcDate = (Date) values.get(0);
    final long millis = utcDate.getTime();
    //-- TimeZone --//
    final String timeZoneId = (String) values.get(1);
    final TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
    //-- GregorianCalendar construction --//
    final GregorianCalendar calendar = new GregorianCalendar(timeZone);
    calendar.setTimeInMillis(millis);
    return calendar;
}
Also used : TimeZone(java.util.TimeZone) GregorianCalendar(java.util.GregorianCalendar) List(java.util.List) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

TimeZone (java.util.TimeZone)577 Date (java.util.Date)140 Calendar (java.util.Calendar)137 SimpleTimeZone (java.util.SimpleTimeZone)89 Test (org.junit.Test)85 SimpleDateFormat (java.text.SimpleDateFormat)69 Locale (java.util.Locale)69 GregorianCalendar (java.util.GregorianCalendar)56 ArrayList (java.util.ArrayList)32 DateFormat (java.text.DateFormat)24 ParseException (java.text.ParseException)23 IOException (java.io.IOException)20 Map (java.util.Map)15 HashSet (java.util.HashSet)13 PreparedStatement (java.sql.PreparedStatement)11 HashMap (java.util.HashMap)11 DateTimeZone (org.joda.time.DateTimeZone)11 Support_TimeZone (tests.support.Support_TimeZone)10 Resources (android.content.res.Resources)9 SQLException (java.sql.SQLException)9