You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by ca...@apache.org on 2022/03/14 01:04:55 UTC

[dolphinscheduler] branch dev updated: [BUG][UT] Fix DateUtilsTest failed due to timezone inconsistent (#8848)

This is an automated email from the ASF dual-hosted git repository.

caishunfeng pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new 7433dd2  [BUG][UT] Fix DateUtilsTest failed due to timezone inconsistent (#8848)
7433dd2 is described below

commit 7433dd24d9a922e12cf4cdbad1ffc0108721b6df
Author: Wenjun Ruan <we...@apache.org>
AuthorDate: Mon Mar 14 09:04:51 2022 +0800

    [BUG][UT] Fix DateUtilsTest failed due to timezone inconsistent (#8848)
---
 .../dolphinscheduler/common/utils/DateUtils.java   | 50 ++++++++++------------
 .../common/utils/DateUtilsTest.java                | 23 ++++++++--
 2 files changed, 42 insertions(+), 31 deletions(-)

diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java
index 83f50d4..3cc7d5f 100644
--- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java
+++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java
@@ -45,6 +45,7 @@ public final class DateUtils {
     static final long C6 = C5 * 24L;
 
     private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);
+    private static final DateTimeFormatter YYYY_MM_DD_HH_MM_SS = DateTimeFormatter.ofPattern(Constants.YYYY_MM_DD_HH_MM_SS);
 
     private DateUtils() {
         throw new UnsupportedOperationException("Construct DateUtils");
@@ -58,10 +59,7 @@ public final class DateUtils {
      */
     private static LocalDateTime date2LocalDateTime(Date date) {
         String timezone = ThreadLocalContext.getTimezoneThreadLocal().get();
-        ZoneId zoneId = ZoneId.systemDefault();
-        if (StringUtils.isNotEmpty(timezone)) {
-            zoneId = ZoneId.of(timezone);
-        }
+        ZoneId zoneId = StringUtils.isNotEmpty(timezone) ? ZoneId.of(timezone) : ZoneId.systemDefault();
         return date2LocalDateTime(date, zoneId);
     }
 
@@ -84,10 +82,7 @@ public final class DateUtils {
      */
     private static Date localDateTime2Date(LocalDateTime localDateTime) {
         String timezone = ThreadLocalContext.getTimezoneThreadLocal().get();
-        ZoneId zoneId = ZoneId.systemDefault();
-        if (StringUtils.isNotEmpty(timezone)) {
-            zoneId = ZoneId.of(timezone);
-        }
+        ZoneId zoneId = StringUtils.isNotEmpty(timezone) ? ZoneId.of(timezone) : ZoneId.systemDefault();
         return localDateTime2Date(localDateTime, zoneId);
     }
 
@@ -103,15 +98,6 @@ public final class DateUtils {
     }
 
     /**
-     * get current date str
-     *
-     * @return date string
-     */
-    public static String getCurrentTime() {
-        return getCurrentTime(Constants.YYYY_MM_DD_HH_MM_SS);
-    }
-
-    /**
      * get the date string in the specified format of the current time
      *
      * @param format date format
@@ -129,10 +115,12 @@ public final class DateUtils {
      * @return date string
      */
     public static String format(Date date, String format, String timezone) {
-        if (StringUtils.isEmpty(timezone)) {
-            return format(date2LocalDateTime(date), format);
-        }
-        return format(date2LocalDateTime(date, ZoneId.of(timezone)), format);
+        return format(date, DateTimeFormatter.ofPattern(format), timezone);
+    }
+
+    public static String format(Date date, DateTimeFormatter dateTimeFormatter, String timezone) {
+        LocalDateTime localDateTime = StringUtils.isEmpty(timezone) ? date2LocalDateTime(date) : date2LocalDateTime(date, ZoneId.of(timezone));
+        return format(localDateTime, dateTimeFormatter);
     }
 
     /**
@@ -143,7 +131,11 @@ public final class DateUtils {
      * @return date string
      */
     public static String format(LocalDateTime localDateTime, String format) {
-        return localDateTime.format(DateTimeFormatter.ofPattern(format));
+        return format(localDateTime, DateTimeFormatter.ofPattern(format));
+    }
+
+    public static String format(LocalDateTime localDateTime, DateTimeFormatter dateTimeFormatter) {
+        return localDateTime.format(dateTimeFormatter);
     }
 
     /**
@@ -153,7 +145,7 @@ public final class DateUtils {
      * @return date string
      */
     public static String dateToString(Date date) {
-        return format(date, Constants.YYYY_MM_DD_HH_MM_SS, null);
+        return format(date, YYYY_MM_DD_HH_MM_SS, null);
     }
 
     /**
@@ -164,7 +156,7 @@ public final class DateUtils {
      * @return date string
      */
     public static String dateToString(Date date, String timezone) {
-        return format(date, Constants.YYYY_MM_DD_HH_MM_SS, timezone);
+        return format(date, YYYY_MM_DD_HH_MM_SS, timezone);
     }
 
     /**
@@ -176,8 +168,12 @@ public final class DateUtils {
      * @return date
      */
     public static Date parse(String date, String format, String timezone) {
+        return parse(date, DateTimeFormatter.ofPattern(format), timezone);
+    }
+
+    public static Date parse(String date, DateTimeFormatter dateTimeFormatter, String timezone) {
         try {
-            LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format));
+            LocalDateTime ldt = LocalDateTime.parse(date, dateTimeFormatter);
             if (StringUtils.isEmpty(timezone)) {
                 return localDateTime2Date(ldt);
             }
@@ -195,7 +191,7 @@ public final class DateUtils {
      * @return yyyy-MM-dd HH:mm:ss format
      */
     public static Date stringToDate(String date) {
-        return parse(date, Constants.YYYY_MM_DD_HH_MM_SS, null);
+        return parse(date, YYYY_MM_DD_HH_MM_SS, null);
     }
 
     /**
@@ -206,7 +202,7 @@ public final class DateUtils {
      * @return yyyy-MM-dd HH:mm:ss format
      */
     public static Date stringToDate(String date, String timezone) {
-        return parse(date, Constants.YYYY_MM_DD_HH_MM_SS, timezone);
+        return parse(date, YYYY_MM_DD_HH_MM_SS, timezone);
     }
 
     /**
diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java
index 3fb1065..e61f581 100644
--- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java
+++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java
@@ -26,10 +26,23 @@ import java.util.TimeZone;
 
 import javax.management.timer.Timer;
 
+import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
 
 public class DateUtilsTest {
+
+    @Before
+    public void before() {
+        ThreadLocalContext.getTimezoneThreadLocal().remove();
+    }
+
+    @After
+    public void after() {
+        ThreadLocalContext.getTimezoneThreadLocal().remove();
+    }
+
     @Test
     public void format2Readable() throws ParseException {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -120,14 +133,16 @@ public class DateUtilsTest {
     public void getStartOfDay() {
         Date d1 = DateUtils.stringToDate("2019-01-31 11:59:59");
         Date curr = DateUtils.getStartOfDay(d1);
-        Assert.assertEquals(DateUtils.dateToString(curr), "2019-01-31 00:00:00");
+        String expected = new SimpleDateFormat("yyyy-MM-dd").format(d1) + " 00:00:00";
+        Assert.assertEquals(DateUtils.dateToString(curr), expected);
     }
 
     @Test
     public void getEndOfDay() {
         Date d1 = DateUtils.stringToDate("2019-01-31 11:00:59");
         Date curr = DateUtils.getEndOfDay(d1);
-        Assert.assertEquals(DateUtils.dateToString(curr), "2019-01-31 23:59:59");
+        String expected = new SimpleDateFormat("yyyy-MM-dd").format(d1) + " 23:59:59";
+        Assert.assertEquals(DateUtils.dateToString(curr), expected);
     }
 
     @Test
@@ -212,17 +227,17 @@ public class DateUtilsTest {
 
     @Test
     public void testTimezone() {
+
         String time = "2019-01-28 00:00:00";
         ThreadLocalContext.timezoneThreadLocal.set("UTC");
         Date utcDate = DateUtils.stringToDate(time);
-
         Assert.assertEquals(time, DateUtils.dateToString(utcDate));
 
         ThreadLocalContext.timezoneThreadLocal.set("Asia/Shanghai");
         Date shanghaiDate = DateUtils.stringToDate(time);
-
         Assert.assertEquals(time, DateUtils.dateToString(shanghaiDate));
 
         Assert.assertEquals(Timer.ONE_HOUR * 8, utcDate.getTime() - shanghaiDate.getTime());
+
     }
 }