java - 尽管系统时钟不准确,仍解析 ISO_LOCAL_TIME

标签 java datetime timezone java-8 java-time

我在解析本地分区时间字符串(例如“12:00:00”)时遇到问题。某些远程服务提供带有本地时间字符串的数据。服务的本地时区会不时发生变化。有时服务的时钟不准确(最多几秒)。我的时钟也可能没有很好地校准。但我们清楚的是,服务器上的数据始终是最新的。 这是解释代码

private static ZoneOffset parseOffset(Clock now, String serviceLocal) {
    LocalTime lt = LocalTime.parse(serviceLocal);
    for (long s = HOURS.toSeconds(12); s > -HOURS.toSeconds(12); s -= MINUTES.toSeconds(30)) {
        final ZoneOffset offset = ZoneOffset.ofTotalSeconds((int) s);
        final LocalDate ld = LocalDate.now(now);
        final ZonedDateTime zdt = ZonedDateTime.of(ld, lt, offset);
        final long seconds = zdt.getLong(ChronoField.INSTANT_SECONDS);
        if (seconds > now.instant().minusSeconds(MINUTES.toSeconds(30)).getEpochSecond()
                && seconds <= now.instant().getEpochSecond()) {
            return offset;
        }
    }
    throw new DateTimeException(String.format("Can't determine time zone of \"%s\". Current UTC time is \"%s\".",
            serviceLocal, now.instant()));
}

/**
 * This test passes. Detected offset is "GMT+6:00".
 */
@Test
public void shouldParseOffset_1() {
    // given
    String serviceLocal = "23:59:59";
    Clock systemTime = Clock.fixed(Instant.parse("2016-02-25T18:00:00Z"), ZoneOffset.UTC);
    // when
    ZoneOffset offset = parseOffset(systemTime, serviceLocal);
    // then
    assertThat(offset, equalTo(ZoneOffset.of("+06:00")));
}

/**
 * This test fails. As I said before data is always latest. It is easy to
 * guess that some of systems (dedicated service or local) clock is not well
 * calibrated. So zone offset should be still equal to "GMT+6:00".
 */
@Test
public void shouldParseOffset_2() {
    // given
    String serviceLocal = "00:00:01";
    Clock systemTime = Clock.fixed(Instant.parse("2016-02-25T18:00:00Z"), ZoneOffset.UTC);
    // when
    ZoneOffset offset = parseOffset(systemTime, serviceLocal);
    // then
    assertThat(offset, equalTo(ZoneOffset.of("+06:00")));
} 

让总误差不超过10秒。如何在100%的情况下确定服务器数据的准确时间?

最佳答案

一种方法是首先调整服务器时间以反射(reflect)“准确”时间(即向上或向下调整几秒以匹配正确的分钟秒数)。

然后您可以计算服务器时间与本地时间之间的时间差。

它可能看起来像:

public static ZoneOffset parseOffset(Clock now, String serviceLocal) {
  LocalTime serverTime = LocalTime.parse(serviceLocal);
  LocalTime localTime = LocalTime.now(now);

  LocalTime adjustedServerTime = getAccurateServerTime(serverTime, localTime, 10);

  int seconds = getSecondsOffset(localTime, adjustedServerTime);
  return ZoneOffset.ofTotalSeconds(seconds);
}

/**
 *
 * @param actual the time on the server (may be inaccurate by 10 seconds)
 * @param accurate the accurate local time , may be a in a different time zone
 * @param maxSecondsInaccuracy the maximum inaccuracy of the time on the server in seconds
 * @return the server time, adjusted for seconds inaccuracy
 */
private static LocalTime getAccurateServerTime(LocalTime actual, LocalTime accurate, int maxSecondsInaccuracy) {
  int actualSeconds = actual.getSecond();
  int accurateSeconds = accurate.getSecond();
  if (Math.abs(actualSeconds - accurateSeconds) < maxSecondsInaccuracy) {
    return actual.withSecond(accurateSeconds);
  } else { //not in the same minute
    if (actualSeconds < accurateSeconds) {
      return actual.minusMinutes(1).withSecond(accurateSeconds);
    } else {
      return actual.plusMinutes(1).withSecond(accurateSeconds);
    }
  }
}

/**
 * @return the offset between the two times, in seconds, between -12 and +12
 */
private static int getSecondsOffset(LocalTime target, LocalTime serverTime) {
  Duration d = Duration.between(target, serverTime);
  long minutes = d.toMinutes();

  //limit offset to -12/+12
  if (minutes < -12 * 60) minutes = minutes + 24 * 60;
  if (minutes > 12 * 60) minutes = minutes - 24 * 60;
  return (int) (minutes * 60);
}

关于java - 尽管系统时钟不准确,仍解析 ISO_LOCAL_TIME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35629568/

相关文章:

java - 从 SQL DDL 语句创建 Java 对象

java - Java 中的 "internal address"是什么?

java - Maven/GAE - 缺少一些 Datanucleus 类

MySQL 根据日期从多个表中获取结果

python - 带有日期时间 tzaware 对象的 Pyspark 中的 TimeStampType

c# - 使用可移植类库将东部标准时间字符串转换为 UTC 日期时间

java - 使用 HIbernate 从数据库检索日期时获取错误的日期时间字段值

java - 保护用于分发的 Java jar 文件

php - 使用 PHP 为 MySQL 插入准备字符串日期

node.js - 如何在ejs中获取本地时区?