Skip to content

Commit

Permalink
Merge pull request #65 from qzhello/main
Browse files Browse the repository at this point in the history
fix: kibana的时间字段改为utc解析 #64
  • Loading branch information
qzhello authored Aug 19, 2024
2 parents 4b8da86 + dd3b073 commit 69b684d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/ly/ckibana/util/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ private static Long toEpochMilli(String dateStr, DateTimeFormatter dateTimeForma
.atZone(ZONE_LOCAL).toInstant().toEpochMilli();
}

private static Long toEpochMilliWithUTC(String dateStr, DateTimeFormatter dateTimeFormatter) {
return LocalDateTime.parse(dateStr, dateTimeFormatter)
.atZone(ZoneId.of("UTC")).toInstant().toEpochMilli();
}

/**
* 支持时间转换为时间戳.
*
Expand All @@ -135,7 +140,7 @@ public static Long toEpochMilli(Object dateTime) {
if (dateTime instanceof String dateTimeStr) {
for (DateTimeFormatter formatter : DEFAULT_DATE_FORMATTER_LIST) {
if (isDateFormatValid(dateTimeStr, formatter)) {
return toEpochMilli(dateTimeStr, formatter);
return toEpochMilliWithUTC(dateTimeStr, formatter);
}
}
}
Expand Down
33 changes: 25 additions & 8 deletions src/test/java/com/ly/ckibana/utils/DateUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
import org.junit.Assert;
import org.junit.Test;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
* 时间round测试
*
Expand Down Expand Up @@ -66,16 +73,26 @@ public void formatDurationWordsTest() {
long hour = 4 * 60 * 60 * 1000 + min;
long day = 5 * 24 * 60 * 60 * 1000 + hour;

Assert.assertTrue("formatDurationWordsTest ms正常","100ms".equals(DateUtils.formatDurationWords(millSeconds)));
Assert.assertTrue("formatDurationWordsTest s正常","2 seconds".equals(DateUtils.formatDurationWords(seconds)));
Assert.assertTrue("formatDurationWordsTest min正常","3 minutes 2 seconds".equals(DateUtils.formatDurationWords(min)));
Assert.assertTrue("formatDurationWordsTest hour正常","4 hours 3 minutes 2 seconds".equals(DateUtils.formatDurationWords(hour)));
Assert.assertTrue("formatDurationWordsTest day正常","5 days 4 hours 3 minutes 2 seconds".equals(DateUtils.formatDurationWords(day)));



Assert.assertTrue("formatDurationWordsTest ms正常", "100ms".equals(DateUtils.formatDurationWords(millSeconds)));
Assert.assertTrue("formatDurationWordsTest s正常", "2 seconds".equals(DateUtils.formatDurationWords(seconds)));
Assert.assertTrue("formatDurationWordsTest min正常", "3 minutes 2 seconds".equals(DateUtils.formatDurationWords(min)));
Assert.assertTrue("formatDurationWordsTest hour正常", "4 hours 3 minutes 2 seconds".equals(DateUtils.formatDurationWords(hour)));
Assert.assertTrue("formatDurationWordsTest day正常", "5 days 4 hours 3 minutes 2 seconds".equals(DateUtils.formatDurationWords(day)));
}

@Test
public void toEpochMilliTest() {
String utc = "2024-07-12T08:46:21.659Z";
Locale locale = Locale.getDefault();
long ts1 = LocalDateTime.parse(utc, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").localizedBy(locale)).atZone(ZoneId.of("UTC")).toInstant().toEpochMilli();
Assert.assertEquals(ts1, 1720773981659L);

Instant instant = Instant.parse(utc);
ZoneId shanghaiZoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime shanghaiTime = instant.atZone(shanghaiZoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String formattedShanghaiTime = shanghaiTime.format(formatter);
Assert.assertEquals(formattedShanghaiTime, "2024-07-12T16:46:21.659Z");
}


Expand Down

0 comments on commit 69b684d

Please sign in to comment.