org.apache.calcite.util.TimestampWithTimeZoneString Java Examples

The following examples show how to use org.apache.calcite.util.TimestampWithTimeZoneString. 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: DateRangeRules.java    From Bats with Apache License 2.0 6 votes vote down vote up
private RexLiteral dateTimeLiteral(RexBuilder rexBuilder, Calendar calendar, RexNode operand) {
    final TimestampString ts;
    final int p;
    switch (operand.getType().getSqlTypeName()) {
    case TIMESTAMP:
        ts = TimestampString.fromCalendarFields(calendar);
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampLiteral(ts, p);
    case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
        ts = TimestampString.fromCalendarFields(calendar);
        final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
        final TimestampString localTs = new TimestampWithTimeZoneString(ts, tz)
                .withTimeZone(DateTimeUtils.UTC_ZONE).getLocalTimestampString();
        p = operand.getType().getPrecision();
        return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(localTs, p);
    case DATE:
        final DateString d = DateString.fromCalendarFields(calendar);
        return rexBuilder.makeDateLiteral(d);
    default:
        throw Util.unexpected(operand.getType().getSqlTypeName());
    }
}
 
Example #2
Source File: DateRangeRules.java    From calcite with Apache License 2.0 6 votes vote down vote up
private RexLiteral dateTimeLiteral(RexBuilder rexBuilder, Calendar calendar,
    RexNode operand) {
  final TimestampString ts;
  final int p;
  switch (operand.getType().getSqlTypeName()) {
  case TIMESTAMP:
    ts = TimestampString.fromCalendarFields(calendar);
    p = operand.getType().getPrecision();
    return rexBuilder.makeTimestampLiteral(ts, p);
  case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
    ts = TimestampString.fromCalendarFields(calendar);
    final TimeZone tz = TimeZone.getTimeZone(this.timeZone);
    final TimestampString localTs =
        new TimestampWithTimeZoneString(ts, tz)
            .withTimeZone(DateTimeUtils.UTC_ZONE)
            .getLocalTimestampString();
    p = operand.getType().getPrecision();
    return rexBuilder.makeTimestampWithLocalTimeZoneLiteral(localTs, p);
  case DATE:
    final DateString d = DateString.fromCalendarFields(calendar);
    return rexBuilder.makeDateLiteral(d);
  default:
    throw Util.unexpected(operand.getType().getSqlTypeName());
  }
}
 
Example #3
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static long timeWithLocalTimeZoneToTimestamp(String date, int v, TimeZone timeZone) {
  final TimeWithTimeZoneString tTZ = TimeWithTimeZoneString.fromMillisOfDay(v)
      .withTimeZone(DateTimeUtils.UTC_ZONE);
  return new TimestampWithTimeZoneString(date + " " + tTZ.toString())
      .withTimeZone(timeZone)
      .getLocalTimestampString()
      .getMillisSinceEpoch();
}
 
Example #4
Source File: SqlFunctions.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static long timeWithLocalTimeZoneToTimestampWithLocalTimeZone(String date, int v) {
  final TimeWithTimeZoneString tTZ = TimeWithTimeZoneString.fromMillisOfDay(v)
      .withTimeZone(DateTimeUtils.UTC_ZONE);
  return new TimestampWithTimeZoneString(date + " " + tTZ.toString())
      .getLocalTimestampString()
      .getMillisSinceEpoch();
}
 
Example #5
Source File: RexProgramTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
@Test void testCompareTimestampWithTimeZone() {
  final TimestampWithTimeZoneString timestampLTZChar1 =
      new TimestampWithTimeZoneString("2011-07-20 10:34:56 America/Los_Angeles");
  final TimestampWithTimeZoneString timestampLTZChar2 =
      new TimestampWithTimeZoneString("2011-07-20 19:34:56 Europe/Rome");
  final TimestampWithTimeZoneString timestampLTZChar3 =
      new TimestampWithTimeZoneString("2011-07-20 01:34:56 Asia/Tokyo");
  final TimestampWithTimeZoneString timestampLTZChar4 =
      new TimestampWithTimeZoneString("2011-07-20 10:34:56 America/Los_Angeles");

  assertThat(timestampLTZChar1.equals(timestampLTZChar2), is(false));
  assertThat(timestampLTZChar1.equals(timestampLTZChar3), is(false));
  assertThat(timestampLTZChar1.equals(timestampLTZChar4), is(true));
}
 
Example #6
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static int timestampWithLocalTimeZoneToDate(long v, TimeZone timeZone) {
  return TimestampWithTimeZoneString.fromMillisSinceEpoch(v)
      .withTimeZone(timeZone)
      .getLocalDateString()
      .getDaysSinceEpoch();
}
 
Example #7
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static int timestampWithLocalTimeZoneToTime(long v, TimeZone timeZone) {
  return TimestampWithTimeZoneString.fromMillisSinceEpoch(v)
      .withTimeZone(timeZone)
      .getLocalTimeString()
      .getMillisOfDay();
}
 
Example #8
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static long timestampWithLocalTimeZoneToTimestamp(long v, TimeZone timeZone) {
  return TimestampWithTimeZoneString.fromMillisSinceEpoch(v)
      .withTimeZone(timeZone)
      .getLocalTimestampString()
      .getMillisSinceEpoch();
}
 
Example #9
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static String timestampWithLocalTimeZoneToString(long v, TimeZone timeZone) {
  return TimestampWithTimeZoneString.fromMillisSinceEpoch(v)
      .withTimeZone(timeZone)
      .toString();
}
 
Example #10
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static int timestampWithLocalTimeZoneToTimeWithLocalTimeZone(long v) {
  return TimestampWithTimeZoneString.fromMillisSinceEpoch(v)
      .getLocalTimeString()
      .getMillisOfDay();
}
 
Example #11
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static Long toTimestampWithLocalTimeZone(String v) {
  return v == null ? null : new TimestampWithTimeZoneString(v)
      .withTimeZone(DateTimeUtils.UTC_ZONE)
      .getLocalTimestampString()
      .getMillisSinceEpoch();
}
 
Example #12
Source File: SqlFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static Long toTimestampWithLocalTimeZone(String v, TimeZone timeZone) {
  return v == null ? null : new TimestampWithTimeZoneString(v + " " + timeZone.getID())
      .withTimeZone(DateTimeUtils.UTC_ZONE)
      .getLocalTimestampString()
      .getMillisSinceEpoch();
}
 
Example #13
Source File: RexBuilderTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Tests
 * {@link RexBuilder#makeTimestampWithLocalTimeZoneLiteral(TimestampString, int)}. */
@Test void testTimestampWithLocalTimeZoneLiteral() {
  final RelDataTypeFactory typeFactory =
      new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
  final RelDataType timestampType =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE);
  final RelDataType timestampType3 =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 3);
  final RelDataType timestampType9 =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 9);
  final RelDataType timestampType18 =
      typeFactory.createSqlType(SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE, 18);
  final RexBuilder builder = new RexBuilder(typeFactory);

  // The new way
  final TimestampWithTimeZoneString ts = new TimestampWithTimeZoneString(
      1969, 7, 21, 2, 56, 15, TimeZone.getTimeZone("PST").getID());
  checkTimestampWithLocalTimeZone(
      builder.makeLiteral(ts.getLocalTimestampString(), timestampType, false));

  // Now with milliseconds
  final TimestampWithTimeZoneString ts2 = ts.withMillis(56);
  assertThat(ts2.toString(), is("1969-07-21 02:56:15.056 PST"));
  final RexNode literal2 = builder.makeLiteral(
      ts2.getLocalTimestampString(), timestampType3, false);
  assertThat(((RexLiteral) literal2).getValue().toString(), is("1969-07-21 02:56:15.056"));

  // Now with nanoseconds
  final TimestampWithTimeZoneString ts3 = ts.withNanos(56);
  final RexNode literal3 = builder.makeLiteral(
      ts3.getLocalTimestampString(), timestampType9, false);
  assertThat(((RexLiteral) literal3).getValueAs(TimestampString.class)
          .toString(), is("1969-07-21 02:56:15"));
  final TimestampWithTimeZoneString ts3b = ts.withNanos(2345678);
  final RexNode literal3b = builder.makeLiteral(
      ts3b.getLocalTimestampString(), timestampType9, false);
  assertThat(((RexLiteral) literal3b).getValueAs(TimestampString.class)
          .toString(), is("1969-07-21 02:56:15.002"));

  // Now with a very long fraction
  final TimestampWithTimeZoneString ts4 = ts.withFraction("102030405060708090102");
  final RexNode literal4 = builder.makeLiteral(
      ts4.getLocalTimestampString(), timestampType18, false);
  assertThat(((RexLiteral) literal4).getValueAs(TimestampString.class)
          .toString(), is("1969-07-21 02:56:15.102"));

  // toString
  assertThat(ts2.round(1).toString(), is("1969-07-21 02:56:15 PST"));
  assertThat(ts2.round(2).toString(), is("1969-07-21 02:56:15.05 PST"));
  assertThat(ts2.round(3).toString(), is("1969-07-21 02:56:15.056 PST"));
  assertThat(ts2.round(4).toString(), is("1969-07-21 02:56:15.056 PST"));

  assertThat(ts2.toString(6), is("1969-07-21 02:56:15.056000 PST"));
  assertThat(ts2.toString(1), is("1969-07-21 02:56:15.0 PST"));
  assertThat(ts2.toString(0), is("1969-07-21 02:56:15 PST"));

  assertThat(ts2.round(0).toString(), is("1969-07-21 02:56:15 PST"));
  assertThat(ts2.round(0).toString(0), is("1969-07-21 02:56:15 PST"));
  assertThat(ts2.round(0).toString(1), is("1969-07-21 02:56:15.0 PST"));
  assertThat(ts2.round(0).toString(2), is("1969-07-21 02:56:15.00 PST"));
}