Search in sources :

Example 1 with TimeTZ

use of io.crate.types.TimeTZ in project crate by crate.

the class CurrentTimeFunction method evaluate.

@Override
@SafeVarargs
public final TimeTZ evaluate(TransactionContext txnCtx, NodeContext nodeCtx, Input<Integer>... args) {
    Integer precision = MICRO_PRECISION;
    if (args.length == 1) {
        precision = args[0].value();
        if (precision == null) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "NULL precision not supported for %s", signature.getName().name()));
        }
        if (precision < 0 || precision > MICRO_PRECISION) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH, "precision must be between [0..%d]", MICRO_PRECISION));
        }
    }
    Instant currentInstant = txnCtx.currentInstant();
    long microsSinceMidnight = ChronoUnit.MICROS.between(currentInstant.truncatedTo(ChronoUnit.DAYS), currentInstant);
    long factor = (long) Math.pow(10, MICRO_PRECISION - precision);
    return new TimeTZ((microsSinceMidnight / factor) * factor, 0);
}
Also used : TimeTZ(io.crate.types.TimeTZ) Instant(java.time.Instant)

Example 2 with TimeTZ

use of io.crate.types.TimeTZ in project crate by crate.

the class TimeTZTypeTest method testBinaryRoundTrip.

@Test
public void testBinaryRoundTrip() {
    ByteBuf buffer = Unpooled.buffer();
    try {
        TimeTZ value = new TimeTZ(53005278000L, 0);
        int written = pgType.writeAsBinary(buffer, value);
        int length = buffer.readInt();
        assertThat(written - 4, is(length));
        TimeTZ readValue = (TimeTZ) pgType.readBinaryValue(buffer, length);
        assertThat(readValue, is(value));
    } finally {
        buffer.release();
    }
}
Also used : TimeTZ(io.crate.types.TimeTZ) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 3 with TimeTZ

use of io.crate.types.TimeTZ in project crate by crate.

the class StreamInput method readTimeTZ.

private TimeTZ readTimeTZ() throws IOException {
    long microsFromMidnight = readLong();
    int secondsFromUTC = readInt();
    return new TimeTZ(microsFromMidnight, secondsFromUTC);
}
Also used : TimeTZ(io.crate.types.TimeTZ) GeoPoint(org.elasticsearch.common.geo.GeoPoint)

Example 4 with TimeTZ

use of io.crate.types.TimeTZ in project crate by crate.

the class CurrentTimeFunctionTest method time_is_created_correctly.

@Test
public void time_is_created_correctly() {
    TimeTZ expected = microsFromMidnightUTC(txnCtx.currentInstant());
    assertEvaluate("current_time", expected);
    assertEvaluate("current_time(6)", expected);
}
Also used : TimeTZ(io.crate.types.TimeTZ) Test(org.junit.Test)

Example 5 with TimeTZ

use of io.crate.types.TimeTZ in project crate by crate.

the class SelectStatementAnalyzerTest method test_cast_time_from_string_literal.

@Test
public void test_cast_time_from_string_literal() {
    var executor = SQLExecutor.builder(clusterService).build();
    AnalyzedRelation relation = executor.analyze("select time with time zone '23:59:59.999+02'");
    assertThat(relation.outputs().get(0).valueType(), is(DataTypes.TIMETZ));
    assertThat(relation.outputs().get(0).toString(), is("23:59:59.999+02:00"));
    relation = executor.analyze("select '23:59:59.999+02'::timetz");
    assertThat(relation.outputs().get(0).valueType(), is(DataTypes.TIMETZ));
    assertThat(relation.outputs().get(0).toString(), is(new TimeTZ(86399999000L, 7200).toString()));
}
Also used : TimeTZ(io.crate.types.TimeTZ) AnalyzedRelation(io.crate.analyze.relations.AnalyzedRelation) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest) Test(org.junit.Test)

Aggregations

TimeTZ (io.crate.types.TimeTZ)6 Test (org.junit.Test)3 AnalyzedRelation (io.crate.analyze.relations.AnalyzedRelation)1 RowN (io.crate.data.RowN)1 BitString (io.crate.sql.tree.BitString)1 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 Regclass (io.crate.types.Regclass)1 Regproc (io.crate.types.Regproc)1 ByteBuf (io.netty.buffer.ByteBuf)1 Instant (java.time.Instant)1 Period (java.time.Period)1 HashMap (java.util.HashMap)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 GeoPoint (org.elasticsearch.common.geo.GeoPoint)1 Point (org.locationtech.spatial4j.shape.Point)1 JtsPoint (org.locationtech.spatial4j.shape.jts.JtsPoint)1