Skip to content

[BUG] ZonedDateTime 夏令时回拨期间丢失偏移,JSON 和 JSONB 读回提前一小时 #7974

Backlog

Description

@dongyikuan919

问题描述

在夏令时回拨期间,Europe/Paris 的 2024-10-27 02:30 可以对应两个不同的瞬间。Fastjson2 将带 +01:00 和 +02:00 的两个 ZonedDateTime 序列化为完全相同的 JSON 和 JSONB。读回 +01:00 的值时,偏移变成 +02:00,实际时间提前一小时。即使输入的 JSON 字符串明确包含 +01:00[Europe/Paris],读回结果也会变成 +02:00。

环境信息

  • OS:macOS 15.0,Apple Silicon
  • JDK:Oracle Java 17.0.13 和 Oracle Java 8u341
  • Fastjson2:Maven Central com.alibaba.fastjson2:fastjson2:2.0.66

重现步骤

将下面的类与 fastjson2-2.0.66.jar 放在同一目录,然后执行 javac -cp fastjson2-2.0.66.jar ZonedOverlap.java 和 java -cp fastjson2-2.0.66.jar:. ZonedOverlap。

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;

public class ZonedOverlap {
    public static void main(String[] args) {
        ZonedDateTime later = ZonedDateTime.ofLocal(
                LocalDateTime.of(2024, 10, 27, 2, 30),
                ZoneId.of("Europe/Paris"), ZoneOffset.ofHours(1));
        ZonedDateTime earlier = later.withEarlierOffsetAtOverlap();
        String text = JSON.toJSONString(later);
        System.out.println("input: " + later + " / " + later.toInstant());
        System.out.println("JSON: " + text);
        System.out.println("same JSON: " + text.equals(JSON.toJSONString(earlier)));
        System.out.println("JSON readback: " + JSON.parseObject(text, ZonedDateTime.class));
        System.out.println("same JSONB: " + Arrays.equals(JSONB.toBytes(earlier), JSONB.toBytes(later)));
        System.out.println("JSONB readback: " + JSONB.parseObject(JSONB.toBytes(later), ZonedDateTime.class));
        System.out.println("explicit ISO readback: " + JSON.parseObject("\"2024-10-27T02:30:00+01:00[Europe/Paris]\"", ZonedDateTime.class));
    }
}

期待的正确结果

序列化再读回时保留 Europe/Paris、+01:00 和 2024-10-27T01:30:00Z。解析包含明确偏移和地区时区的 ISO 文本也应保留原偏移。

相关日志输出

input: 2024-10-27T02:30+01:00[Europe/Paris] / 2024-10-27T01:30:00Z
JSON: "2024-10-27T02:30:00[Europe/Paris]"
same JSON: true
JSON readback: 2024-10-27T02:30+02:00[Europe/Paris]
same JSONB: true
JSONB readback: 2024-10-27T02:30+02:00[Europe/Paris]
explicit ISO readback: 2024-10-27T02:30+02:00[Europe/Paris]

附加信息

将 main 的 460d2e4 相对 2.0.66 改动的 4 个核心 Java 源文件编译后覆盖 2.0.66 JAR 中的同名类,在 Java 17 上得到相同结果。复现验证使用了 OpenAI Codex 辅助。

Activity

  1. dongyikuan919 commented on Nov 29, 2026

    @dongyikuan919
    Author

    I tested the JSONB compatibility question separately from the text parser change in #7975. Starting with JSONB.toBytes(later), I kept the BC_TIMESTAMP_WITH_TIMEZONE marker and timestamp fields and replaced only the last encoded string. The generic JSONB.parse and typed JSONB.parseObject(..., ZonedDateTime.class) gave the same results in these cases.

    Input instant Final string What Fastjson2 2.0.66 reads
    02:30+01:00[Europe/Paris] Europe/Paris or +01:00[Europe/Paris] 02:30+02:00[Europe/Paris]
    02:30+01:00[Europe/Paris] +01:00 02:30+01:00 with a fixed offset
    01:30Z[Europe/London] Europe/London or Z[Europe/London] 01:30+01:00[Europe/London]
    01:30Z[Europe/London] Z 01:30Z with a fixed offset

    The composite string keeps the existing layout and was readable by 2.0.66, but those readers still use the earlier offset. An offset-only string preserves the instant at the cost of losing the named zone. This suggests updating both JSONB readers to interpret an offset-plus-region string before emitting it for ambiguous timestamps. The same probe gave identical output using the four changed core classes from main 460d2e4 over the 2.0.66 JAR.

    中文说明

    保留 BC_TIMESTAMP_WITH_TIMEZONE 标记和时间字段,只替换最后一个时区字符串。2.0.66 的通用和带类型读取都能读取 +01:00[Europe/Paris] 与 Z[Europe/London],但仍选回拨重叠区间中较早的时刻。只写偏移时可以保留时刻,但读回结果没有地区时区。主分支 460d2e4 相对正式版改动的四个核心类覆盖后,结果一致。后续可先让两种 JSONB 读法识别复合字符串,再考虑在存在歧义的记录上写入偏移和地区。

    OpenAI Codex assisted with the verification and drafting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions