Java Code Examples for org.apache.calcite.avatica.util.DateTimeUtils#PrecisionTime

The following examples show how to use org.apache.calcite.avatica.util.DateTimeUtils#PrecisionTime . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SqlParserUtil.java    From Quicksql with MIT License 6 votes vote down vote up
public static SqlTimestampLiteral parseTimestampLiteral(String s,
    SqlParserPos pos) {
  final String dateStr = parseString(s);
  final Format format = Format.PER_THREAD.get();
  DateTimeUtils.PrecisionTime pt = null;
  // Allow timestamp literals with and without time fields (as does
  // PostgreSQL); TODO: require time fields except in Babel's lenient mode
  final DateFormat[] dateFormats = {format.timestamp, format.date};
  for (DateFormat dateFormat : dateFormats) {
    pt = DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
        dateFormat, DateTimeUtils.UTC_ZONE, -1);
    if (pt != null) {
      break;
    }
  }
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIMESTAMP", s,
            RESOURCE.badFormat(DateTimeUtils.TIMESTAMP_FORMAT_STRING).str()));
  }
  final TimestampString ts =
      TimestampString.fromCalendarFields(pt.getCalendar())
          .withFraction(pt.getFraction());
  return SqlLiteral.createTimestamp(ts, pt.getPrecision(), pos);
}
 
Example 2
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
public static SqlTimestampLiteral parseTimestampLiteral(String s,
    SqlParserPos pos) {
  final String dateStr = parseString(s);
  final Format format = Format.PER_THREAD.get();
  DateTimeUtils.PrecisionTime pt = null;
  // Allow timestamp literals with and without time fields (as does
  // PostgreSQL); TODO: require time fields except in Babel's lenient mode
  final DateFormat[] dateFormats = {format.timestamp, format.date};
  for (DateFormat dateFormat : dateFormats) {
    pt = DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
        dateFormat, DateTimeUtils.UTC_ZONE, -1);
    if (pt != null) {
      break;
    }
  }
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIMESTAMP", s,
            RESOURCE.badFormat(DateTimeUtils.TIMESTAMP_FORMAT_STRING).str()));
  }
  final TimestampString ts =
      TimestampString.fromCalendarFields(pt.getCalendar())
          .withFraction(pt.getFraction());
  return SqlLiteral.createTimestamp(ts, pt.getPrecision(), pos);
}
 
Example 3
Source File: SqlParserUtil.java    From Quicksql with MIT License 5 votes vote down vote up
public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
          Format.PER_THREAD.get().time, DateTimeUtils.UTC_ZONE, -1);
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIME", s,
            RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str()));
  }
  final TimeString t = TimeString.fromCalendarFields(pt.getCalendar())
      .withFraction(pt.getFraction());
  return SqlLiteral.createTime(t, pt.getPrecision(), pos);
}
 
Example 4
Source File: TimeWithTimeZoneString.java    From Quicksql with MIT License 5 votes vote down vote up
public TimeWithTimeZoneString withTimeZone(TimeZone timeZone) {
  if (this.timeZone.equals(timeZone)) {
    return this;
  }
  String localTimeString = localTime.toString();
  String v;
  String fraction;
  int i = localTimeString.indexOf('.');
  if (i >= 0) {
    v = localTimeString.substring(0, i);
    fraction = localTimeString.substring(i + 1);
  } else {
    v = localTimeString;
    fraction = null;
  }
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(v,
          new SimpleDateFormat(DateTimeUtils.TIME_FORMAT_STRING, Locale.ROOT),
          this.timeZone, -1);
  pt.getCalendar().setTimeZone(timeZone);
  if (fraction != null) {
    return new TimeWithTimeZoneString(
        pt.getCalendar().get(Calendar.HOUR_OF_DAY),
        pt.getCalendar().get(Calendar.MINUTE),
        pt.getCalendar().get(Calendar.SECOND),
        timeZone.getID())
            .withFraction(fraction);
  }
  return new TimeWithTimeZoneString(
      pt.getCalendar().get(Calendar.HOUR_OF_DAY),
      pt.getCalendar().get(Calendar.MINUTE),
      pt.getCalendar().get(Calendar.SECOND),
      timeZone.getID());
}
 
Example 5
Source File: TimestampWithTimeZoneString.java    From Quicksql with MIT License 5 votes vote down vote up
public TimestampWithTimeZoneString withTimeZone(TimeZone timeZone) {
  if (this.timeZone.equals(timeZone)) {
    return this;
  }
  String localDateTimeString = localDateTime.toString();
  String v;
  String fraction;
  int i = localDateTimeString.indexOf('.');
  if (i >= 0) {
    v = localDateTimeString.substring(0, i);
    fraction = localDateTimeString.substring(i + 1);
  } else {
    v = localDateTimeString;
    fraction = null;
  }
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(v,
          new SimpleDateFormat(DateTimeUtils.TIMESTAMP_FORMAT_STRING, Locale.ROOT),
          this.timeZone, -1);
  pt.getCalendar().setTimeZone(timeZone);
  if (fraction != null) {
    return new TimestampWithTimeZoneString(
        pt.getCalendar().get(Calendar.YEAR),
        pt.getCalendar().get(Calendar.MONTH) + 1,
        pt.getCalendar().get(Calendar.DAY_OF_MONTH),
        pt.getCalendar().get(Calendar.HOUR_OF_DAY),
        pt.getCalendar().get(Calendar.MINUTE),
        pt.getCalendar().get(Calendar.SECOND),
        timeZone.getID())
            .withFraction(fraction);
  }
  return new TimestampWithTimeZoneString(
      pt.getCalendar().get(Calendar.YEAR),
      pt.getCalendar().get(Calendar.MONTH) + 1,
      pt.getCalendar().get(Calendar.DAY_OF_MONTH),
      pt.getCalendar().get(Calendar.HOUR_OF_DAY),
      pt.getCalendar().get(Calendar.MINUTE),
      pt.getCalendar().get(Calendar.SECOND),
      timeZone.getID());
}
 
Example 6
Source File: SqlParserUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static SqlTimeLiteral parseTimeLiteral(String s, SqlParserPos pos) {
  final String dateStr = parseString(s);
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(dateStr,
          Format.PER_THREAD.get().time, DateTimeUtils.UTC_ZONE, -1);
  if (pt == null) {
    throw SqlUtil.newContextException(pos,
        RESOURCE.illegalLiteral("TIME", s,
            RESOURCE.badFormat(DateTimeUtils.TIME_FORMAT_STRING).str()));
  }
  final TimeString t = TimeString.fromCalendarFields(pt.getCalendar())
      .withFraction(pt.getFraction());
  return SqlLiteral.createTime(t, pt.getPrecision(), pos);
}
 
Example 7
Source File: TimeWithTimeZoneString.java    From calcite with Apache License 2.0 5 votes vote down vote up
public TimeWithTimeZoneString withTimeZone(TimeZone timeZone) {
  if (this.timeZone.equals(timeZone)) {
    return this;
  }
  String localTimeString = localTime.toString();
  String v;
  String fraction;
  int i = localTimeString.indexOf('.');
  if (i >= 0) {
    v = localTimeString.substring(0, i);
    fraction = localTimeString.substring(i + 1);
  } else {
    v = localTimeString;
    fraction = null;
  }
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(v,
          new SimpleDateFormat(DateTimeUtils.TIME_FORMAT_STRING, Locale.ROOT),
          this.timeZone, -1);
  pt.getCalendar().setTimeZone(timeZone);
  if (fraction != null) {
    return new TimeWithTimeZoneString(
        pt.getCalendar().get(Calendar.HOUR_OF_DAY),
        pt.getCalendar().get(Calendar.MINUTE),
        pt.getCalendar().get(Calendar.SECOND),
        timeZone.getID())
            .withFraction(fraction);
  }
  return new TimeWithTimeZoneString(
      pt.getCalendar().get(Calendar.HOUR_OF_DAY),
      pt.getCalendar().get(Calendar.MINUTE),
      pt.getCalendar().get(Calendar.SECOND),
      timeZone.getID());
}
 
Example 8
Source File: TimestampWithTimeZoneString.java    From calcite with Apache License 2.0 5 votes vote down vote up
public TimestampWithTimeZoneString withTimeZone(TimeZone timeZone) {
  if (this.timeZone.equals(timeZone)) {
    return this;
  }
  String localDateTimeString = localDateTime.toString();
  String v;
  String fraction;
  int i = localDateTimeString.indexOf('.');
  if (i >= 0) {
    v = localDateTimeString.substring(0, i);
    fraction = localDateTimeString.substring(i + 1);
  } else {
    v = localDateTimeString;
    fraction = null;
  }
  final DateTimeUtils.PrecisionTime pt =
      DateTimeUtils.parsePrecisionDateTimeLiteral(v,
          new SimpleDateFormat(DateTimeUtils.TIMESTAMP_FORMAT_STRING, Locale.ROOT),
          this.timeZone, -1);
  pt.getCalendar().setTimeZone(timeZone);
  if (fraction != null) {
    return new TimestampWithTimeZoneString(
        pt.getCalendar().get(Calendar.YEAR),
        pt.getCalendar().get(Calendar.MONTH) + 1,
        pt.getCalendar().get(Calendar.DAY_OF_MONTH),
        pt.getCalendar().get(Calendar.HOUR_OF_DAY),
        pt.getCalendar().get(Calendar.MINUTE),
        pt.getCalendar().get(Calendar.SECOND),
        timeZone.getID())
            .withFraction(fraction);
  }
  return new TimestampWithTimeZoneString(
      pt.getCalendar().get(Calendar.YEAR),
      pt.getCalendar().get(Calendar.MONTH) + 1,
      pt.getCalendar().get(Calendar.DAY_OF_MONTH),
      pt.getCalendar().get(Calendar.HOUR_OF_DAY),
      pt.getCalendar().get(Calendar.MINUTE),
      pt.getCalendar().get(Calendar.SECOND),
      timeZone.getID());
}