Browse Source

[BUG][UT] Fix DateUtilsTest failed due to timezone inconsistent (#8848)

3.0.0/version-upgrade
Wenjun Ruan 3 years ago committed by GitHub
parent
commit
7433dd24d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/DateUtils.java
  2. 23
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/DateUtilsTest.java

48
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; static final long C6 = C5 * 24L;
private static final Logger logger = LoggerFactory.getLogger(DateUtils.class); 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() { private DateUtils() {
throw new UnsupportedOperationException("Construct DateUtils"); throw new UnsupportedOperationException("Construct DateUtils");
@ -58,10 +59,7 @@ public final class DateUtils {
*/ */
private static LocalDateTime date2LocalDateTime(Date date) { private static LocalDateTime date2LocalDateTime(Date date) {
String timezone = ThreadLocalContext.getTimezoneThreadLocal().get(); String timezone = ThreadLocalContext.getTimezoneThreadLocal().get();
ZoneId zoneId = ZoneId.systemDefault(); ZoneId zoneId = StringUtils.isNotEmpty(timezone) ? ZoneId.of(timezone) : ZoneId.systemDefault();
if (StringUtils.isNotEmpty(timezone)) {
zoneId = ZoneId.of(timezone);
}
return date2LocalDateTime(date, zoneId); return date2LocalDateTime(date, zoneId);
} }
@ -84,10 +82,7 @@ public final class DateUtils {
*/ */
private static Date localDateTime2Date(LocalDateTime localDateTime) { private static Date localDateTime2Date(LocalDateTime localDateTime) {
String timezone = ThreadLocalContext.getTimezoneThreadLocal().get(); String timezone = ThreadLocalContext.getTimezoneThreadLocal().get();
ZoneId zoneId = ZoneId.systemDefault(); ZoneId zoneId = StringUtils.isNotEmpty(timezone) ? ZoneId.of(timezone) : ZoneId.systemDefault();
if (StringUtils.isNotEmpty(timezone)) {
zoneId = ZoneId.of(timezone);
}
return localDateTime2Date(localDateTime, zoneId); return localDateTime2Date(localDateTime, zoneId);
} }
@ -102,15 +97,6 @@ public final class DateUtils {
return Date.from(instant); return Date.from(instant);
} }
/**
* 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 * get the date string in the specified format of the current time
* *
@ -129,10 +115,12 @@ public final class DateUtils {
* @return date string * @return date string
*/ */
public static String format(Date date, String format, String timezone) { public static String format(Date date, String format, String timezone) {
if (StringUtils.isEmpty(timezone)) { return format(date, DateTimeFormatter.ofPattern(format), timezone);
return format(date2LocalDateTime(date), format);
} }
return format(date2LocalDateTime(date, ZoneId.of(timezone)), format);
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 * @return date string
*/ */
public static String format(LocalDateTime localDateTime, String format) { 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 * @return date string
*/ */
public static String dateToString(Date date) { 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 * @return date string
*/ */
public static String dateToString(Date date, String timezone) { 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 * @return date
*/ */
public static Date parse(String date, String format, String timezone) { 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 { try {
LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ofPattern(format)); LocalDateTime ldt = LocalDateTime.parse(date, dateTimeFormatter);
if (StringUtils.isEmpty(timezone)) { if (StringUtils.isEmpty(timezone)) {
return localDateTime2Date(ldt); return localDateTime2Date(ldt);
} }
@ -195,7 +191,7 @@ public final class DateUtils {
* @return yyyy-MM-dd HH:mm:ss format * @return yyyy-MM-dd HH:mm:ss format
*/ */
public static Date stringToDate(String date) { 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 * @return yyyy-MM-dd HH:mm:ss format
*/ */
public static Date stringToDate(String date, String timezone) { 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);
} }
/** /**

23
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 javax.management.timer.Timer;
import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class DateUtilsTest { public class DateUtilsTest {
@Before
public void before() {
ThreadLocalContext.getTimezoneThreadLocal().remove();
}
@After
public void after() {
ThreadLocalContext.getTimezoneThreadLocal().remove();
}
@Test @Test
public void format2Readable() throws ParseException { public void format2Readable() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ -120,14 +133,16 @@ public class DateUtilsTest {
public void getStartOfDay() { public void getStartOfDay() {
Date d1 = DateUtils.stringToDate("2019-01-31 11:59:59"); Date d1 = DateUtils.stringToDate("2019-01-31 11:59:59");
Date curr = DateUtils.getStartOfDay(d1); 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 @Test
public void getEndOfDay() { public void getEndOfDay() {
Date d1 = DateUtils.stringToDate("2019-01-31 11:00:59"); Date d1 = DateUtils.stringToDate("2019-01-31 11:00:59");
Date curr = DateUtils.getEndOfDay(d1); 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 @Test
@ -212,17 +227,17 @@ public class DateUtilsTest {
@Test @Test
public void testTimezone() { public void testTimezone() {
String time = "2019-01-28 00:00:00"; String time = "2019-01-28 00:00:00";
ThreadLocalContext.timezoneThreadLocal.set("UTC"); ThreadLocalContext.timezoneThreadLocal.set("UTC");
Date utcDate = DateUtils.stringToDate(time); Date utcDate = DateUtils.stringToDate(time);
Assert.assertEquals(time, DateUtils.dateToString(utcDate)); Assert.assertEquals(time, DateUtils.dateToString(utcDate));
ThreadLocalContext.timezoneThreadLocal.set("Asia/Shanghai"); ThreadLocalContext.timezoneThreadLocal.set("Asia/Shanghai");
Date shanghaiDate = DateUtils.stringToDate(time); Date shanghaiDate = DateUtils.stringToDate(time);
Assert.assertEquals(time, DateUtils.dateToString(shanghaiDate)); Assert.assertEquals(time, DateUtils.dateToString(shanghaiDate));
Assert.assertEquals(Timer.ONE_HOUR * 8, utcDate.getTime() - shanghaiDate.getTime()); Assert.assertEquals(Timer.ONE_HOUR * 8, utcDate.getTime() - shanghaiDate.getTime());
} }
} }

Loading…
Cancel
Save