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);
}
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();
}
}
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);
}
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);
}
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()));
}
Aggregations