AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79425118
Accepted
SoT
SoT
Asked: 2025-02-09 23:04:14 +0800 CST2025-02-09 23:04:14 +0800 CST 2025-02-09 23:04:14 +0800 CST

ZonedDateTime 在欧洲/巴黎与 GMT+1 产生错误的日​​期

  • 772

我处于+07:00时区。

鉴于此:

ZonedDateTime.of( LocalDateTime.of( 1899, 12, 31, 23, 9, 20, 0 ), ZoneOffset.UTC )
.withZoneSameInstant( ZoneId.of( "Europe/Paris" ) )

会产生:1899-12-31T23:18:41+00:09:21[Europe/Paris]

另一方面:

ZonedDateTime.of( LocalDateTime.of( 1899, 12, 31, 23, 9, 20, 0 ), ZoneOffset.UTC )
.withZoneSameInstant( ZoneId.of( "GMT+1" ) )

会产生:1900-01-01T00:09:20+01:00[GMT+01:00]

为什么它们不同,因为ZoneId.of( "Europe/Paris" ).getRules().getOffset( LocalDateTime.now() )和都ZoneId.of( "GMT+1" ).getRules().getOffset( LocalDateTime.now() )产生+01:00?我希望第一次尝试应该会得到1900-01-01T00:09:20+01:00[Europe/Paris]。顺便问一下,第一次尝试时在哪里+00:09:21?

java
  • 2 2 个回答
  • 92 Views

2 个回答

  • Voted
  1. Best Answer
    Sweeper
    2025-02-09T23:31:16+08:002025-02-09T23:31:16+08:00

    您说得对Europe/Paris,现在GMT+1具有相同的偏移量(2025-02-09),但在 1899 年它们并没有相同的偏移量。

    让我们打印出Instant你真正感兴趣的偏移量,

    var zdt = ZonedDateTime.of( LocalDateTime.of(1899, 12, 31, 23, 9, 20, 0), ZoneOffset.UTC);
    System.out.println(ZoneId.of("Europe/Paris").getRules().getOffset(zdt.toInstant()));
    System.out.println(ZoneId.of("GMT+1").getRules().getOffset(zdt.toInstant()));
    

    这将打印

    +00:09:21
    +01:00
    

    这就是 +00:09:21 的由来。巴黎直到 1911 年才统一时区。它比格林威治标准时间早 9 分 21 秒,因为巴黎位于格林威治标准时间以东 9 分 21 秒。这是巴黎当地平均时间与格林威治标准时间的偏差。

    的时区偏移Europe/Paris在历史上发生了很大变化。有关更多详细信息,请参阅 维基百科。或者,您可以直接打印

    System.out.println(ZoneId.of("Europe/Paris").getRules().getTransitions());
    
    • 12
  2. Basil Bourque
    2025-02-10T07:16:32+08:002025-02-10T07:16:32+08:00

    Sweeper 的回答很正确也很聪明。我只是补充一些想法。

    时区跟踪非常新颖

    正如另一个答案中所建议的那样,让我们​​来看看巴黎地区人民使用的偏移量变化的历史,这些变化是由他们的政治家决定的。

    ZoneId
            .of ( "Europe/Paris" )
            .getRules ( )
            .getTransitions ( )
            .forEach (
                   System.out :: println
            );
    
    Transition[Overlap at 1911-03-11T00:00+00:09:21 to Z]
    Transition[Gap at 1916-06-14T23:00Z to +01:00]
    Transition[Overlap at 1916-10-02T00:00+01:00 to Z]
    Transition[Gap at 1917-03-24T23:00Z to +01:00]
    Transition[Overlap at 1917-10-08T00:00+01:00 to Z]
    Transition[Gap at 1918-03-09T23:00Z to +01:00]
    Transition[Overlap at 1918-10-07T00:00+01:00 to Z]
    Transition[Gap at 1919-03-01T23:00Z to +01:00]
    Transition[Overlap at 1919-10-06T00:00+01:00 to Z]
    Transition[Gap at 1920-02-14T23:00Z to +01:00]
    Transition[Overlap at 1920-10-24T00:00+01:00 to Z]
    Transition[Gap at 1921-03-14T23:00Z to +01:00]
    Transition[Overlap at 1921-10-26T00:00+01:00 to Z]
    Transition[Gap at 1922-03-25T23:00Z to +01:00]
    Transition[Overlap at 1922-10-08T00:00+01:00 to Z]
    Transition[Gap at 1923-05-26T23:00Z to +01:00]
    Transition[Overlap at 1923-10-07T00:00+01:00 to Z]
    Transition[Gap at 1924-03-29T23:00Z to +01:00]
    Transition[Overlap at 1924-10-05T00:00+01:00 to Z]
    Transition[Gap at 1925-04-04T23:00Z to +01:00]
    Transition[Overlap at 1925-10-04T00:00+01:00 to Z]
    Transition[Gap at 1926-04-17T23:00Z to +01:00]
    Transition[Overlap at 1926-10-03T00:00+01:00 to Z]
    Transition[Gap at 1927-04-09T23:00Z to +01:00]
    Transition[Overlap at 1927-10-02T00:00+01:00 to Z]
    Transition[Gap at 1928-04-14T23:00Z to +01:00]
    Transition[Overlap at 1928-10-07T00:00+01:00 to Z]
    Transition[Gap at 1929-04-20T23:00Z to +01:00]
    Transition[Overlap at 1929-10-06T00:00+01:00 to Z]
    Transition[Gap at 1930-04-12T23:00Z to +01:00]
    Transition[Overlap at 1930-10-05T00:00+01:00 to Z]
    Transition[Gap at 1931-04-18T23:00Z to +01:00]
    Transition[Overlap at 1931-10-04T00:00+01:00 to Z]
    Transition[Gap at 1932-04-02T23:00Z to +01:00]
    Transition[Overlap at 1932-10-02T00:00+01:00 to Z]
    Transition[Gap at 1933-03-25T23:00Z to +01:00]
    Transition[Overlap at 1933-10-08T00:00+01:00 to Z]
    Transition[Gap at 1934-04-07T23:00Z to +01:00]
    Transition[Overlap at 1934-10-07T00:00+01:00 to Z]
    Transition[Gap at 1935-03-30T23:00Z to +01:00]
    Transition[Overlap at 1935-10-06T00:00+01:00 to Z]
    Transition[Gap at 1936-04-18T23:00Z to +01:00]
    Transition[Overlap at 1936-10-04T00:00+01:00 to Z]
    Transition[Gap at 1937-04-03T23:00Z to +01:00]
    Transition[Overlap at 1937-10-03T00:00+01:00 to Z]
    Transition[Gap at 1938-03-26T23:00Z to +01:00]
    Transition[Overlap at 1938-10-02T00:00+01:00 to Z]
    Transition[Gap at 1939-04-15T23:00Z to +01:00]
    Transition[Overlap at 1939-11-19T00:00+01:00 to Z]
    Transition[Gap at 1940-02-25T02:00Z to +01:00]
    Transition[Gap at 1940-06-14T23:00+01:00 to +02:00]
    Transition[Overlap at 1942-11-02T03:00+02:00 to +01:00]
    Transition[Gap at 1943-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1943-10-04T03:00+02:00 to +01:00]
    Transition[Gap at 1944-04-03T02:00+01:00 to +02:00]
    Transition[Overlap at 1944-10-08T01:00+02:00 to +01:00]
    Transition[Gap at 1945-04-02T02:00+01:00 to +02:00]
    Transition[Overlap at 1945-09-16T03:00+02:00 to +01:00]
    Transition[Gap at 1976-03-28T01:00+01:00 to +02:00]
    Transition[Overlap at 1976-09-26T01:00+02:00 to +01:00]
    Transition[Gap at 1977-04-03T02:00+01:00 to +02:00]
    Transition[Overlap at 1977-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1978-04-02T02:00+01:00 to +02:00]
    Transition[Overlap at 1978-10-01T03:00+02:00 to +01:00]
    Transition[Gap at 1979-04-01T02:00+01:00 to +02:00]
    Transition[Overlap at 1979-09-30T03:00+02:00 to +01:00]
    Transition[Gap at 1980-04-06T02:00+01:00 to +02:00]
    Transition[Overlap at 1980-09-28T03:00+02:00 to +01:00]
    Transition[Gap at 1981-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1981-09-27T03:00+02:00 to +01:00]
    Transition[Gap at 1982-03-28T02:00+01:00 to +02:00]
    Transition[Overlap at 1982-09-26T03:00+02:00 to +01:00]
    Transition[Gap at 1983-03-27T02:00+01:00 to +02:00]
    Transition[Overlap at 1983-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1984-03-25T02:00+01:00 to +02:00]
    Transition[Overlap at 1984-09-30T03:00+02:00 to +01:00]
    Transition[Gap at 1985-03-31T02:00+01:00 to +02:00]
    Transition[Overlap at 1985-09-29T03:00+02:00 to +01:00]
    Transition[Gap at 1986-03-30T02:00+01:00 to +02:00]
    Transition[Overlap at 1986-09-28T03:00+02:00 to +01:00]
    Transition[Gap at 1987-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1987-09-27T03:00+02:00 to +01:00]
    Transition[Gap at 1988-03-27T02:00+01:00 to +02:00]
    Transition[Overlap at 1988-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1989-03-26T02:00+01:00 to +02:00]
    Transition[Overlap at 1989-09-24T03:00+02:00 to +01:00]
    Transition[Gap at 1990-03-25T02:00+01:00 to +02:00]
    Transition[Overlap at 1990-09-30T03:00+02:00 to +01:00]
    Transition[Gap at 1991-03-31T02:00+01:00 to +02:00]
    Transition[Overlap at 1991-09-29T03:00+02:00 to +01:00]
    Transition[Gap at 1992-03-29T02:00+01:00 to +02:00]
    Transition[Overlap at 1992-09-27T03:00+02:00 to +01:00]
    Transition[Gap at 1993-03-28T02:00+01:00 to +02:00]
    Transition[Overlap at 1993-09-26T03:00+02:00 to +01:00]
    Transition[Gap at 1994-03-27T02:00+01:00 to +02:00]
    Transition[Overlap at 1994-09-25T03:00+02:00 to +01:00]
    Transition[Gap at 1995-03-26T02:00+01:00 to +02:00]
    Transition[Overlap at 1995-09-24T03:00+02:00 to +01:00]
    Transition[Gap at 1996-03-31T02:00+01:00 to +02:00]
    Transition[Overlap at 1996-10-27T03:00+02:00 to +01:00]
    Transition[Gap at 1997-03-30T02:00+01:00 to +02:00]
    Transition[Overlap at 1997-10-26T03:00+02:00 to +01:00]
    

    我们发现巴黎地区在不同时期使用了四个偏移量:

    • +00:09:21
    • +00:00
    • +01:00
    • +02:00

    您认为巴黎时间始终使用 +01:00(GMT+1)的偏移量,这是不正确的。

    我们看到,该地区的时区追踪只能追溯到 1911 年。在铁路时代之前,几乎不需要设计一个时区。唯一重要的时间是显而易见的自然时间:中午是太阳直射头顶的时间,无论你站在哪里。中午在东部比在西部来得早。

    正如Wikipedia所述,1891 年,法国本土的时间通过穿过法国巴黎天文台的巴黎子午线统一了。该子午线恰好位于我们当代使用 UTC 时区定义所使用的格林威治子午线以东 ≈ 2°20′。2 °20′ 的经度距离意味着太阳直射巴黎人头顶的时间比太阳直射伦敦人头顶的时间早约 9 分 21 秒。因此,巴黎的第一个法国偏移量定义为比巴黎子午线早 +00:00。如今,我们对那个时代的现代官方时区定义是比UTC 子午线早 +00:09:21,以解释 2°20′ 的差异。这解释了您在结果中看到的偏移量。Europe/Paris

    所有这些都是在用冗长的方式表明,尝试使用当代时间之外的时区几乎没有意义:

    • 首先,时区是一项新发明,仅可追溯到 19 世纪后期。
    • 其次,因为历史上从来没有跟踪过时区!我们对它们的记录是错误的和不完整的。

    令人震惊的是,尽管我们拥有众多大学和政府机构,却没有人费心识别时区并跟踪时差的变化。只有在我们(白发苍苍)的有生之年,Arthur David Olson 才英勇地建立了一个正式的时区数据库,2005 年,责任移交给了 Paul Eggert。他们的努力主要集中在 1970 年及以后,每年对我们现在称为tzdata 的内容进行多次编辑、添加、更正和更改。

    因此,您试图精确地表示 1899 年的某个时刻是不明智的。

    OffsetDateTime

    让我们分解一下你的代码。

    ZonedDateTime
    .of( 
        LocalDateTime.of( 1899, 12, 31, 23, 9, 20, 0 ), 
        ZoneOffset.UTC 
    )
    .withZoneSameInstant( 
        ZoneId.of( "Europe/Paris" ) 
    )
    

    虽然并不重要,但您在第一部分中的使用ZonedDateTime具有误导性。您分配的只是一个偏移量,而不是时区。所以您应该使用OffsetDateTime。

    OffsetDateTime
    .of( 
        LocalDateTime.of( 1899, 12, 31, 23, 9, 20, 0 ), 
        ZoneOffset.UTC 
    )                                   // Returns an `OffsetDateTime` object. 
    .atZoneSameInstant( 
        ZoneId.of( "Europe/Paris" )    
    )                                   // Returns a `ZonedDateTime` object. 
    

    但如上所述,使用OffsetDateTime或ZonedDateTime表示历史时刻通常是不明智的。它们的使用表明了一种不可能的、也可能是不必要的精确性和权威性。

    定义:

    • 偏移量仅仅是时间子午线之前/之后的小时-分钟-秒数。如上所述,在现代协议(如ISO 8601)中,我们通常使用 UTC 子午线,而某些协议则使用巴黎子午线(或其他子午线)。ISO 8601 和其他常见协议使用+表示子午线之前,-使用 表示子午线之后,但请注意某些协议的作用相反。
    • 时区是指特定地区的人们所使用的偏移量的过去、现在和未来变化的历史记录,由其政治家决定。现代时区的命名格式Continent/Region为Europe/London、Europe/Paris、America/Edmonton、Pacific/Auckland等。
    • 3

相关问题

  • Lock Condition.notify 抛出 java.lang.IllegalMonitorStateException

  • 多对一微服务响应未出现在邮递员中

  • 自定义 SpringBoot Bean 验证

  • Java 套接字是 FIFO 的吗?

  • 为什么不可能/不鼓励在服务器端定义请求超时?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve