【发布时间】:2015-04-27 15:09:07
【问题描述】:
我在询问之前搜索了这个,但找不到任何有用的东西。我有一个使用 UTC 的服务器,客户端可以设置服务器每天执行的事件的时间(这是一个简化)。例如,法国的某个人希望在当地时间 23:00(22:00 UTC)发生事件。我将其转换为 UTC 并存储它;服务器每天 22:00 UTC 愉快地执行。然后到了夏天,现在与法国的偏移量是 +2h,服务器仍然在 22:00 UTC 执行,但对于用户来说这是错误的时间,因为法国的 22:00 UTC 现在是当地时间 00:00。
我以 UTC 存储,因为每个人都说要以 UTC 存储并在输出给用户时转换为本地时间,很好,但这对用户来说并不像预期的那样工作。我想我应该存储像法国时间 23:00 这样的东西,服务器应该接受它并说“好的,这是今天 22:00 UTC”或者好的,这是因为 DST 今天是 21:00 UTC”(我正在使用 Java与 JodaTime 顺便说一句),但这与每个人所说的我应该如何将它以 UTC 存储在数据库中的说法相矛盾......所以......我可以/应该做什么?
如果我存储带有偏移量的本地时间和日期,并且当我读取它时(在使用 UTC 的服务器上),我会这样做:
DateTime dbLocalHistoricDateTime = fromDB(); // suppose this 23:00 in France on the 01.01.2015 (UTC+1), 22:00 UTC on that date
DateTimeZone localZone = dbLocalHistoricDateTime.getZone();
DateTime currentLocalDateTime = DateTime.now().withZone(localZone);
DateTime localToday = dbLocalHistoricDateTime.withDate(currentLocalDateTime.getYear(), currentLocalDateTime.getMonthOfYear(), currentLocalDateTime.getDayOfMonth());
DateTime utcToday = localToday.withZone(DateTimeZone.UTC);
然后 utcToday 包含今天正确的 UTC 时间(21:00 UTC 是 27.04.2015 在法国的 23:00,然后服务器可以在该 UTC 时间执行此事件。
谢谢, 加布里埃尔
【问题讨论】:
-
另见:this answer
标签: java database timezone utc