use of jodd.time.JulianDate in project hive by apache.
the class TestParquetTimestampsHive2Compatibility method readHive2.
/**
* Converts the specified nano time to a java.sql.Timestamp using Hive2 legacy code.
*/
private static java.sql.Timestamp readHive2(NanoTime nt) {
// Current Hive parquet timestamp implementation stores it in UTC, but other components do not do that.
// If this file written by current Hive implementation itself, we need to do the reverse conversion, else skip the conversion.
int julianDay = nt.getJulianDay();
long nanosOfDay = nt.getTimeOfDayNanos();
long remainder = nanosOfDay;
julianDay += remainder / NANOS_PER_DAY;
remainder %= NANOS_PER_DAY;
if (remainder < 0) {
remainder += NANOS_PER_DAY;
julianDay--;
}
JulianDate jDateTime = JulianDate.of((double) julianDay);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(ZoneId.of("GMT")));
calendar.set(Calendar.YEAR, jDateTime.toLocalDateTime().getYear());
// java calendar index starting at 1.
calendar.set(Calendar.MONTH, jDateTime.toLocalDateTime().getMonthValue() - 1);
calendar.set(Calendar.DAY_OF_MONTH, jDateTime.toLocalDateTime().getDayOfMonth());
int hour = (int) (remainder / (NANOS_PER_HOUR));
remainder = remainder % (NANOS_PER_HOUR);
int minutes = (int) (remainder / (NANOS_PER_MINUTE));
remainder = remainder % (NANOS_PER_MINUTE);
int seconds = (int) (remainder / (NANOS_PER_SECOND));
long nanos = remainder % NANOS_PER_SECOND;
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
java.sql.Timestamp ts = new java.sql.Timestamp(calendar.getTimeInMillis());
ts.setNanos((int) nanos);
return ts;
}
use of jodd.time.JulianDate in project hive by apache.
the class NanoTimeUtils method getTimestamp.
/**
* Converts a nanotime representation in UTC, to a timestamp in the specified timezone.
*
* @param legacyConversion when true the conversion to the target timezone is done with legacy (backwards compatible)
* method.
*/
public static Timestamp getTimestamp(NanoTime nt, ZoneId targetZone, boolean legacyConversion) {
int julianDay = nt.getJulianDay();
long nanosOfDay = nt.getTimeOfDayNanos();
long remainder = nanosOfDay;
julianDay += remainder / NANOS_PER_DAY;
remainder %= NANOS_PER_DAY;
if (remainder < 0) {
remainder += NANOS_PER_DAY;
julianDay--;
}
JulianDate jDateTime;
jDateTime = JulianDate.of((double) julianDay);
Calendar calendar = getGMTCalendar();
calendar.set(Calendar.YEAR, jDateTime.toLocalDateTime().getYear());
// java calendar index starting at 1.
calendar.set(Calendar.MONTH, jDateTime.toLocalDateTime().getMonth().getValue() - 1);
calendar.set(Calendar.DAY_OF_MONTH, jDateTime.toLocalDateTime().getDayOfMonth());
int hour = (int) (remainder / (NANOS_PER_HOUR));
remainder = remainder % (NANOS_PER_HOUR);
int minutes = (int) (remainder / (NANOS_PER_MINUTE));
remainder = remainder % (NANOS_PER_MINUTE);
int seconds = (int) (remainder / (NANOS_PER_SECOND));
long nanos = remainder % NANOS_PER_SECOND;
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
Timestamp ts = Timestamp.ofEpochMilli(calendar.getTimeInMillis(), (int) nanos);
ts = TimestampTZUtil.convertTimestampToZone(ts, ZoneOffset.UTC, targetZone, legacyConversion);
return ts;
}
use of jodd.time.JulianDate in project hive by apache.
the class NanoTimeUtils method getNanoTime.
/**
* Converts a timestamp from the specified timezone to UTC and returns its representation in NanoTime.
*/
public static NanoTime getNanoTime(Timestamp ts, ZoneId sourceZone, boolean legacyConversion) {
ts = TimestampTZUtil.convertTimestampToZone(ts, sourceZone, ZoneOffset.UTC, legacyConversion);
Calendar calendar = getGMTCalendar();
calendar.setTimeInMillis(ts.toEpochMilli());
int year = calendar.get(Calendar.YEAR);
if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
year = 1 - year;
}
JulianDate jDateTime;
jDateTime = JulianDate.of(year, // java calendar index starting at 1.
calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0);
int days = jDateTime.getJulianDayNumber();
long hour = calendar.get(Calendar.HOUR_OF_DAY);
long minute = calendar.get(Calendar.MINUTE);
long second = calendar.get(Calendar.SECOND);
long nanos = ts.getNanos();
long nanosOfDay = nanos + NANOS_PER_SECOND * second + NANOS_PER_MINUTE * minute + NANOS_PER_HOUR * hour;
return new NanoTime(days, nanosOfDay);
}
use of jodd.time.JulianDate in project hive by apache.
the class TestParquetTimestampsHive2Compatibility method writeHive2.
/**
* Converts the specified timestamp to nano time using Hive2 legacy code.
*
* In Hive2, the input string is considered to be in the default system timezone and it is converted to GMT before
* it is transformed to nano time.
*/
private static NanoTime writeHive2(String str) {
java.sql.Timestamp ts = toTimestampHive2(str);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(ZoneId.of("GMT")));
calendar.setTime(ts);
int year = calendar.get(Calendar.YEAR);
if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
year = 1 - year;
}
JulianDate jDateTime = // java calendar index starting at 1.
JulianDate.of(// java calendar index starting at 1.
year, // java calendar index starting at 1.
calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0, 0);
int days = jDateTime.getJulianDayNumber();
long hour = calendar.get(Calendar.HOUR_OF_DAY);
long minute = calendar.get(Calendar.MINUTE);
long second = calendar.get(Calendar.SECOND);
long nanos = ts.getNanos();
long nanosOfDay = nanos + NANOS_PER_SECOND * second + NANOS_PER_MINUTE * minute + NANOS_PER_HOUR * hour;
return new NanoTime(days, nanosOfDay);
}
Aggregations