com.clickzetta.client.jdbc.arrow.util.CZTimestamp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clickzetta-java Show documentation
Show all versions of clickzetta-java Show documentation
The java SDK for clickzetta's Lakehouse
package com.clickzetta.client.jdbc.arrow.util;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Map;
import java.util.TimeZone;
/**
* Wrapper for java.sql.Timestamp to override toString method.
* To align with Apache Spark, trailing zeros of nanosecond are not printed.
*/
public class CZTimestamp extends java.sql.Timestamp {
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss")
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true)
.toFormatter();
private static Calendar sessionCalendar = null;
public static void resetSessionTimezone(Map settings) {
final String CZ_SQL_TIMEZONE = "cz.sql.timezone";
if (settings != null && settings.containsKey(CZ_SQL_TIMEZONE)) {
String timezone = settings.get(CZ_SQL_TIMEZONE);
sessionCalendar = Calendar.getInstance(TimeZone.getTimeZone(timezone));
} else {
sessionCalendar = null;
}
}
private final boolean isTimestampNTZ;
public CZTimestamp(long time, boolean isTimestampNTZ) {
super(time);
this.isTimestampNTZ = isTimestampNTZ;
}
public CZTimestamp(Timestamp time, boolean isTimestampNTZ) {
this(time.getYear(), time.getMonth(), time.getDate(), time.getHours(), time.getMinutes(), time.getSeconds(),
time.getNanos(), isTimestampNTZ);
}
public CZTimestamp(int year, int month, int date, int hour, int minute, int second, int nano, boolean isTimestampNTZ) {
super(year, month, date, hour, minute, second, nano);
this.isTimestampNTZ = isTimestampNTZ;
}
public static CZTimestamp valueOf(LocalDateTime dateTime, boolean isTimestampNTZ) {
return new CZTimestamp(dateTime.getYear() - 1900,
dateTime.getMonthValue() - 1,
dateTime.getDayOfMonth(),
dateTime.getHour(),
dateTime.getMinute(),
dateTime.getSecond(),
dateTime.getNano(),
isTimestampNTZ);
}
@Override
public String toString() {
Calendar calendar;
if (isTimestampNTZ) {
calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
} else {
calendar = sessionCalendar != null ? sessionCalendar : Calendar.getInstance(TimeZone.getDefault());
}
return toInstant().atZone(calendar.getTimeZone().toZoneId()).format(FORMATTER);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy