/** * Parse a string (without a zone) as a value of TIME type. * <p> * For example: {@code "01:23:00"} is parsed to TIME {@code 01:23:00} * and {@code "01:23:00 +01:23"} is rejected. * * @return stack representation of TIME type */ public static long parseTimeWithoutTimeZone(String value) { return TIME_FORMATTER.parseMillis(value); }
/** * Parses an offset from the string. * * @param str the string * @return the offset millis */ private static int parseOffset(String str) { return -(int) LazyInit.OFFSET_FORMATTER.parseMillis(str); }
/** * Parses an offset from the string. * * @param str the string * @return the offset millis */ private static int parseOffset(String str) { return -(int) LazyInit.OFFSET_FORMATTER.parseMillis(str); }
public static long parseHiveDate(String value) { long millis = HIVE_DATE_PARSER.parseMillis(value); return TimeUnit.MILLISECONDS.toDays(millis); }
public static long parseHiveTimestamp(String value, DateTimeZone timeZone) { return HIVE_TIMESTAMP_PARSER.withZone(timeZone).parseMillis(value); }
public static int parseDate(String value) { return (int) TimeUnit.MILLISECONDS.toDays(DATE_FORMATTER.parseMillis(value)); }
public static boolean timestampHasTimeZone(String value) { try { try { TIMESTAMP_WITH_TIME_ZONE_FORMATTER.parseMillis(value); return true; } catch (RuntimeException e) { // `.withZoneUTC()` makes `timestampHasTimeZone` return value independent of JVM zone TIMESTAMP_WITHOUT_TIME_ZONE_FORMATTER.withZoneUTC().parseMillis(value); return false; } } catch (RuntimeException e) { throw new IllegalArgumentException(format("Invalid timestamp '%s'", value)); } }
private long parseTimestamp(Slice slice, int offset, int length) { //noinspection deprecation return dateTimeFormatter.parseMillis(new String(slice.getBytes(offset, length), 0)); } }
/** * Parse a string (without a zone) as a value of TIME type, interpreted in {@code timeZoneKey} zone. * * @return stack representation of legacy TIME type */ @Deprecated public static long parseTimeWithoutTimeZone(TimeZoneKey timeZoneKey, String value) { return TIME_FORMATTER.withZone(getDateTimeZone(timeZoneKey)).parseMillis(value); }
private static int parseDate(Slice slice, int offset, int length) { long millis = HIVE_DATE_PARSER.parseMillis(slice.toStringAscii(offset, length)); return toIntExact(MILLISECONDS.toDays(millis)); } }
/** * Parse a string (optionally containing a zone) as a value of either TIMESTAMP or TIMESTAMP WITH TIME ZONE type. * <p> * For example: {@code "2000-01-01 01:23:00"} is parsed to TIMESTAMP {@code 2000-01-01T01:23:00} * and {@code "2000-01-01 01:23:00 +01:23"} is parsed to TIMESTAMP WITH TIME ZONE * {@code 2000-01-01T01:23:00.000+01:23}. * * @return stack representation of TIMESTAMP or TIMESTAMP WITH TIME ZONE type, depending on input */ public static long parseTimestampLiteral(String value) { try { DateTime dateTime = TIMESTAMP_WITH_TIME_ZONE_FORMATTER.parseDateTime(value); return packDateTimeWithZone(dateTime); } catch (Exception e) { return TIMESTAMP_WITHOUT_TIME_ZONE_FORMATTER.parseMillis(value); } }
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 { handleInvalidValue(name, type, obj); return 0; } }
/** * Parse a string (optionally containing a zone) as a value of TIMESTAMP type. * If the string doesn't specify a zone, it is interpreted in {@code timeZoneKey} zone. * * @return stack representation of legacy TIMESTAMP type */ @Deprecated public static long parseTimestampWithoutTimeZone(TimeZoneKey timeZoneKey, String value) { return TIMESTAMP_WITH_OR_WITHOUT_TIME_ZONE_FORMATTER.withChronology(getChronology(timeZoneKey)).parseMillis(value); }
@Override public long getLong() { long millis = FORMATTER.parseMillis(getSlice().toStringAscii()); Type type = columnHandle.getType(); if (type.equals(DATE)) { return MILLISECONDS.toDays(millis); } if (type.equals(TIMESTAMP) || type.equals(TIME)) { return millis; } if (type.equals(TIMESTAMP_WITH_TIME_ZONE) || type.equals(TIME_WITH_TIME_ZONE)) { return packDateTimeWithZone(millis, 0); } return millis; } }
/** * Gets the millis, which is the ISO parsed string value. * * @param object the String to convert, must not be null * @param chrono the chronology to use, non-null result of getChronology * @return the millisecond value * @throws IllegalArgumentException if the value if invalid */ public long getInstantMillis(Object object, Chronology chrono) { String str = (String) object; DateTimeFormatter p = ISODateTimeFormat.dateTimeParser(); return p.withChronology(chrono).parseMillis(str); }
/** * Parse a string (optionally containing a zone) as a value of either TIMESTAMP or TIMESTAMP WITH TIME ZONE type. * If the string doesn't specify a zone, it is interpreted in {@code timeZoneKey} zone. * * @return stack representation of legacy TIMESTAMP or TIMESTAMP WITH TIME ZONE type, depending on input */ @Deprecated public static long parseTimestampLiteral(TimeZoneKey timeZoneKey, String value) { try { DateTime dateTime = TIMESTAMP_WITH_TIME_ZONE_FORMATTER.parseDateTime(value); return packDateTimeWithZone(dateTime); } catch (RuntimeException e) { return LEGACY_TIMESTAMP_WITHOUT_TIME_ZONE_FORMATTER.withChronology(getChronology(timeZoneKey)).parseMillis(value); } }
/** * Gets the millis, which is the ISO parsed string value. * * @param object the String to convert, must not be null * @param chrono the chronology to use, non-null result of getChronology * @return the millisecond value * @throws IllegalArgumentException if the value if invalid */ public long getInstantMillis(Object object, Chronology chrono) { String str = (String) object; DateTimeFormatter p = ISODateTimeFormat.dateTimeParser(); return p.withChronology(chrono).parseMillis(str); }
@Override protected long getMillis() { if (value.isValueNode()) { try { return FORMATTER.parseMillis(value.asText()); } catch (IllegalArgumentException e) { throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse value '%s' as '%s' for column '%s'", value.asText(), columnHandle.getType(), columnHandle.getName())); } } throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse non-value node as '%s' for column '%s'", columnHandle.getType(), columnHandle.getName())); } }
@Override protected long getMillis() { if (!value.isValueNode()) { throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse non-value node as '%s' for column '%s'", columnHandle.getType(), columnHandle.getName())); } try { return formatter.parseMillis(value.asText()); } catch (IllegalArgumentException e) { throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse value '%s' as '%s' for column '%s'", value.asText(), columnHandle.getType(), columnHandle.getName())); } } }
@ScalarFunction @LiteralParameters({"x", "y"}) @SqlType(StandardTypes.TIMESTAMP) public static long dateParse(ConnectorSession session, @SqlType("varchar(x)") Slice dateTime, @SqlType("varchar(y)") Slice formatString) { DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString) .withChronology(session.isLegacyTimestamp() ? getChronology(session.getTimeZoneKey()) : UTC_CHRONOLOGY) .withLocale(session.getLocale()); try { return formatter.parseMillis(dateTime.toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } }