slinkydeveloper commented on a change in pull request #17878:
URL: https://github.com/apache/flink/pull/17878#discussion_r755267687
##########
File path:
flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/DateTimeUtils.java
##########
@@ -1251,133 +1356,24 @@ private static long julianDateFloor(TimeUnitRange
range, int julian, boolean flo
}
}
- //
--------------------------------------------------------------------------------------------
- // DATE DIFF/ADD
- //
--------------------------------------------------------------------------------------------
-
- /**
- * NOTE: (1). JDK relies on the operating system clock for time. Each
operating system has its
- * own method of handling date changes such as leap seconds(e.g. OS will
slow down the clock to
- * accommodate for this). (2). DST(Daylight Saving Time) is a legal issue,
governments changed
- * it over time. Some days are NOT exactly 24 hours long, it could be
23/25 hours long on the
- * first or last day of daylight saving time. JDK can handle DST
correctly. TODO: carefully
- * written algorithm can improve the performance
- */
- public static int dateDiff(long t1, long t2, TimeZone tz) {
- ZoneId zoneId = tz.toZoneId();
- LocalDate ld1 = Instant.ofEpochMilli(t1).atZone(zoneId).toLocalDate();
- LocalDate ld2 = Instant.ofEpochMilli(t2).atZone(zoneId).toLocalDate();
- return (int) ChronoUnit.DAYS.between(ld2, ld1);
- }
-
- public static int dateDiff(String t1Str, long t2, TimeZone tz) {
- long t1 = parseToTimeMillis(t1Str, tz);
- return dateDiff(t1, t2, tz);
- }
-
- public static int dateDiff(long t1, String t2Str, TimeZone tz) {
- long t2 = parseToTimeMillis(t2Str, tz);
- return dateDiff(t1, t2, tz);
- }
-
- public static int dateDiff(String t1Str, String t2Str, TimeZone tz) {
- long t1 = parseToTimeMillis(t1Str, tz);
- long t2 = parseToTimeMillis(t2Str, tz);
- return dateDiff(t1, t2, tz);
- }
-
- public static int dateDiff(long t1, long t2) {
- return dateDiff(t1, t2, UTC_ZONE);
- }
-
- public static int dateDiff(String t1Str, long t2) {
- return dateDiff(t1Str, t2, UTC_ZONE);
- }
-
- public static int dateDiff(long t1, String t2Str) {
- return dateDiff(t1, t2Str, UTC_ZONE);
- }
-
- public static int dateDiff(String t1Str, String t2Str) {
- return dateDiff(t1Str, t2Str, UTC_ZONE);
- }
-
- /**
- * Do subtraction on date string.
- *
- * @param dateStr formatted date string.
- * @param days days count you want to subtract.
- * @param tz time zone of the date time string
- * @return datetime string.
- */
- public static String dateSub(String dateStr, int days, TimeZone tz) {
- long ts = parseToTimeMillis(dateStr, tz);
- if (ts == Long.MIN_VALUE) {
- return null;
- }
- return dateSub(ts, days, tz);
- }
-
- /**
- * Do subtraction on date string.
- *
- * @param ts the timestamp.
- * @param days days count you want to subtract.
- * @param tz time zone of the date time string
- * @return datetime string.
- */
- public static String dateSub(long ts, int days, TimeZone tz) {
- ZoneId zoneId = tz.toZoneId();
- Instant instant = Instant.ofEpochMilli(ts);
- ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
- long resultTs = zdt.minusDays(days).toInstant().toEpochMilli();
- return dateFormat(resultTs, DATE_FORMAT_STRING, tz);
- }
-
- public static String dateSub(String dateStr, int days) {
- return dateSub(dateStr, days, UTC_ZONE);
- }
-
- public static String dateSub(long ts, int days) {
- return dateSub(ts, days, UTC_ZONE);
- }
-
/**
- * Do addition on date string.
+ * Convert datetime string from a time zone to another time zone.
*
- * @param dateStr formatted date string.
- * @param days days count you want to add.
- * @return datetime string.
+ * @param dateStr the date time string
+ * @param tzFrom the original time zone
+ * @param tzTo the target time zone
*/
- public static String dateAdd(String dateStr, int days, TimeZone tz) {
- long ts = parseToTimeMillis(dateStr, tz);
- if (ts == Long.MIN_VALUE) {
+ public static String convertTz(String dateStr, String tzFrom, String tzTo)
{
+ try {
+ return dateFormatTz(parseTimestampTz(dateStr, tzFrom), tzTo);
+ } catch (ParseException e) {
return null;
}
- return dateAdd(ts, days, tz);
- }
-
- /**
- * Do addition on timestamp.
- *
- * @param ts the timestamp.
- * @param days days count you want to add.
- * @return datetime string.
- */
- public static String dateAdd(long ts, int days, TimeZone tz) {
- ZoneId zoneId = tz.toZoneId();
- Instant instant = Instant.ofEpochMilli(ts);
- ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
- long resultTs = zdt.plusDays(days).toInstant().toEpochMilli();
- return dateFormat(resultTs, DATE_FORMAT_STRING, tz);
- }
-
- public static String dateAdd(String dateStr, int days) {
- return dateAdd(dateStr, days, UTC_ZONE);
}
- public static String dateAdd(long ts, int days) {
- return dateAdd(ts, days, UTC_ZONE);
+ private static String dateFormatTz(long ts, String tzStr) {
Review comment:
Renamed `formatTimestampTz`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]