Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public enum ClickHouseDataType implements SQLType {
AggregateFunction(String.class, true, true, false, 0, 0, 0, 0, 0, true),
Variant(List.class, true, true, false, 0, 0, 0, 0, 0, true, 0x2A),
Dynamic(Object.class, true, true, false, 0, 0, 0, 0, 0, true, 0x2B),
Time(LocalDateTime.class, true, false, false, 4, 9, 0, 0, 9, false, 0x32), // 0x33 for Time(Timezone)
Time64(LocalDateTime.class, true, false, false, 8, 9, 0, 0, 0, false, 0x34), // 0x35 for Time64(P, Timezone)
Time(LocalDateTime.class, false, false, false, 4, 9, 0, 0, 9, false, 0x32),
Time64(LocalDateTime.class, true, false, false, 8, 9, 0, 0, 0, false, 0x34),
;

public static final List<ClickHouseDataType> ORDERED_BY_RANGE_INT_TYPES =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.clickhouse.client.api;

import java.time.Instant;
import java.time.ZoneId;
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
import com.clickhouse.data.ClickHouseDataType;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.Temporal;
import java.util.Date;
import java.util.Objects;

import com.clickhouse.data.ClickHouseDataType;

import static com.clickhouse.client.api.data_formats.internal.BinaryStreamReader.BASES;

public class DataTypeUtils {
Expand All @@ -39,6 +43,19 @@ public class DataTypeUtils {
.appendFraction(ChronoField.NANO_OF_SECOND, 9, 9, true)
.toFormatter();

public static DateTimeFormatter TIME_FORMATTER = new DateTimeFormatterBuilder()
.appendFraction(ChronoField.HOUR_OF_DAY, 1, 4, false)
.appendFraction(ChronoField.MINUTE_OF_HOUR, 1, 3, false)
.appendFraction(ChronoField.SECOND_OF_MINUTE, 1, 3, false)
.toFormatter();

public static DateTimeFormatter TIME_WITH_NANOS_FORMATTER = new DateTimeFormatterBuilder()
.appendFraction(ChronoField.HOUR_OF_DAY, 1, 4, false)
.appendFraction(ChronoField.MINUTE_OF_HOUR, 1, 3, false)
.appendFraction(ChronoField.SECOND_OF_MINUTE, 1, 3, false)
.appendFraction(ChronoField.NANO_OF_SECOND, 9, 9, true)
.toFormatter();

/**
* Formats an {@link Instant} object for use in SQL statements or as query
* parameter.
Expand Down Expand Up @@ -138,4 +155,60 @@ public static Instant instantFromTime64Integer(int precision, long value) {

return Instant.ofEpochSecond(value, nanoSeconds);
}

/**
* Converts a Java object to a temporal accessor suitable for date only operations.
* It accepts Time and Date and converts to LocalDateTime
* @param date
* @return
*/
public static Temporal toLocalDateTemporal(Object date) {
if (date instanceof Temporal) {
return (Temporal) date;
} else if (date instanceof java.sql.Date) {
return ((java.sql.Date) date).toLocalDate();
} else if (date instanceof Timestamp) {
return ((java.sql.Timestamp) date).toLocalDateTime().toLocalDate();
} else if (date instanceof java.util.Date) {
return ((java.util.Date) date).toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

throw new IllegalArgumentException("Object of type '" + date.getClass() + "' cannot be converted to local date because has no date part");
}

/**
* Converts a Java object to a temporal accessor suitable for date and time operations.
* @param date
* @return
*/
public static Temporal toLocalDateTimeTemporal(Object date) {
if (date instanceof Temporal) {
return (Temporal) date;
} else if (date instanceof java.sql.Timestamp) {
return ((java.sql.Timestamp) date).toLocalDateTime();
} else if (date instanceof java.util.Date) {
return ((java.util.Date) date).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}

throw new IllegalArgumentException("Object of type '" + date.getClass() + "' cannot be converted to local date because has no date or time part");
}

/**
* Converts a Java object to a temporal accessor suitable for time only operations.
* @param date
* @return
*/
public static Temporal toLocalTimeTemporal(Object date) {
if (date instanceof Temporal) {
return (Temporal) date;
} else if (date instanceof Timestamp) {
return ((Timestamp) date).toLocalDateTime();
} else if (date instanceof Time) {
return ((Time) date).toLocalTime();
} else if (date instanceof java.util.Date) {
return ((java.util.Date) date).toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
}

throw new IllegalArgumentException("Object of type '" + date.getClass() + "' cannot be converted to local time because has no time part");
}
}
Loading
Loading