public static TimestampTZ parse(String s) { return parse(s, null); }
/** * Initialize current timestamp, other necessary query initialization. */ public void setupQueryCurrentTimestamp() { queryCurrentTimestamp = Instant.now(); // Provide a facility to set current timestamp during tests if (sessionConf.getBoolVar(ConfVars.HIVE_IN_TEST)) { String overrideTimestampString = HiveConf.getVar(sessionConf, HiveConf.ConfVars.HIVETESTCURRENTTIMESTAMP, (String)null); if (overrideTimestampString != null && overrideTimestampString.length() > 0) { TimestampTZ zonedDateTime = TimestampTZUtil.convert( Timestamp.valueOf(overrideTimestampString), sessionConf.getLocalTimeZone()); queryCurrentTimestamp = zonedDateTime.getZonedDateTime().toInstant(); } } }
public TimestampLocalTZTypeInfo(String timeZoneStr) { super(serdeConstants.TIMESTAMPLOCALTZ_TYPE_NAME); this.timeZone = TimestampTZUtil.parseTimeZone(timeZoneStr); }
StringObjectInspector soi = (StringObjectInspector) oi; String s = soi.getPrimitiveJavaObject(o).trim(); return TimestampTZUtil.parseOrNull(trimNanoTimestamp(s), timeZone); return TimestampTZUtil.parseOrNull(trimNanoTimestamp(s), timeZone); return TimestampTZUtil.convert(date, timeZone); return TimestampTZUtil.convert(ts, timeZone);
public static TimestampTZ parse(String s, ZoneId defaultTimeZone) { s = handleSingleDigitHourOffset(s); ZonedDateTime zonedDateTime; try {
StringObjectInspector soi = (StringObjectInspector) oi; String s = soi.getPrimitiveJavaObject(o).trim(); return TimestampTZUtil.parseOrNull(trimNanoTimestamp(s), timeZone); return TimestampTZUtil.parseOrNull(trimNanoTimestamp(s), timeZone); return TimestampTZUtil.convert(date, timeZone); return TimestampTZUtil.convert(ts, timeZone);
public static TimestampTZ parse(String s, ZoneId defaultTimeZone) { s = handleSingleDigitHourOffset(s); ZonedDateTime zonedDateTime; try {
public static TimestampTZ parseOrNull(String s, ZoneId defaultTimeZone) { try { return parse(s, defaultTimeZone); } catch (DateTimeParseException e) { if (LOG.isDebugEnabled()) { LOG.debug("Invalid string " + s + " for TIMESTAMP WITH TIME ZONE", e); } return null; } }
@Test public void testConvertFromTimestamp() { TimeZone defaultZone = TimeZone.getDefault(); try { // Use system zone when converting from timestamp to timestamptz String s = "2017-06-12 23:12:56.34"; TimestampTZ tstz1 = TimestampTZUtil.convert( Timestamp.valueOf(s), TimeZone.getTimeZone("Europe/London").toZoneId()); TimestampTZ tstz2 = TimestampTZUtil.convert( Timestamp.valueOf(s), TimeZone.getTimeZone("America/Los_Angeles").toZoneId()); Assert.assertTrue(tstz1.compareTo(tstz2) < 0); } finally { TimeZone.setDefault(defaultZone); } }
/** * Obtains the local time-zone ID. */ public ZoneId getLocalTimeZone() { String timeZoneStr = getVar(ConfVars.HIVE_LOCAL_TIME_ZONE); return TimestampTZUtil.parseTimeZone(timeZoneStr); }
public static TimestampTZ convert(Timestamp ts, ZoneId defaultTimeZone) { return parse(ts.toString(), defaultTimeZone); }
public TimestampLocalTZTypeInfo(String timeZoneStr) { super(serdeConstants.TIMESTAMPLOCALTZ_TYPE_NAME); this.timeZone = TimestampTZUtil.parseTimeZone(timeZoneStr); }
public static TimestampTZ convert(Date date, ZoneId defaultTimeZone) { return parse(date.toString(), defaultTimeZone); }
/** * Obtains the local time-zone ID. */ public ZoneId getLocalTimeZone() { String timeZoneStr = getVar(ConfVars.HIVE_LOCAL_TIME_ZONE); return TimestampTZUtil.parseTimeZone(timeZoneStr); }
@Test public void testVariations() { // Omitting zone or time part is allowed TimestampTZUtil.parse("2017-01-01 13:33:00", ZoneId.of("UTC")); TimestampTZUtil.parse("2017-11-08 Europe/London"); TimestampTZUtil.parse("2017-05-20", ZoneId.of("UTC")); TimestampTZUtil.parse("2017-11-08GMT"); TimestampTZUtil.parse("2017-10-11 GMT+8:00"); TimestampTZUtil.parse("2017-05-08 07:45:00-3:00"); }
return Timestamp.valueOf((String) value); case TIMESTAMPLOCALTZ_TYPE: return TimestampTZUtil.parse((String) value); case DECIMAL_TYPE: return new BigDecimal((String)value);
@Override public void init(ByteArrayRef bytes, int start, int length) { String s = null; if (!LazyUtils.isDateMaybe(bytes.getData(), start, length)) { isNull = true; return; } TimestampTZ t = null; try { s = new String(bytes.getData(), start, length, StandardCharsets.US_ASCII); if (s.equals("NULL")) { isNull = true; logExceptionMessage(bytes, start, length, serdeConstants.TIMESTAMPLOCALTZ_TYPE_NAME.toUpperCase()); } else { t = TimestampTZUtil.parse(s, timeZone); isNull = false; } } catch (DateTimeParseException e) { isNull = true; logExceptionMessage(bytes, start, length, serdeConstants.TIMESTAMPLOCALTZ_TYPE_NAME.toUpperCase()); } data.set(t); }
@Test public void testConvertFromTimestamp2() { TimeZone defaultZone = TimeZone.getDefault(); try { // Use system zone when converting from timestamp to timestamptz String s = "2017-06-12 23:12:56.34"; TimestampTZ tstz1 = TimestampTZUtil.parse(s + " " + TimeZone.getTimeZone("Europe/London").getID()); TimestampTZ tstz2 = TimestampTZUtil.parse(s + " " + TimeZone.getTimeZone("America/Los_Angeles").getID()); Assert.assertTrue(tstz1.compareTo(tstz2) < 0); } finally { TimeZone.setDefault(defaultZone); } } }
@Test public void testInvalidStrings() { // invalid zone try { TimestampTZUtil.parse("2017-01-01 13:33:00 foo"); Assert.fail("Invalid timezone ID should cause exception"); } catch (DateTimeParseException e) { // expected } // invalid time part try { TimestampTZUtil.parse("2017-01-01 13:33:61"); Assert.fail("Invalid time should cause exception"); } catch (DateTimeParseException e) { // expected } }