Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions core/src/main/java/com/alibaba/fastjson2/util/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4372,11 +4372,12 @@ public static ZonedDateTime parseZonedDateTime(byte[] str, int off, int len, Zon
zoneId = defaultZoneId;
} else {
char first = (char) str[off + zoneIdBegin];
if (first == 'Z') {
if (first == 'Z' && (zoneIdBegin + 1 == len || str[off + zoneIdBegin + 1] != '[')) {
zoneId = UTC;
} else {
boolean hasOffset = first == '+' || first == '-' || first == 'Z';
if (zoneIdStr == null) {
if (first == '+' || first == '-') {
if (hasOffset) {
zoneIdStr = new String(str, off + zoneIdBegin, len - zoneIdBegin);
} else if (first == ' ') {
zoneIdStr = new String(str, off + zoneIdBegin + 1, len - zoneIdBegin - 1);
Expand All @@ -4387,6 +4388,9 @@ public static ZonedDateTime parseZonedDateTime(byte[] str, int off, int len, Zon
}
}
zoneId = getZoneId(zoneIdStr, defaultZoneId);
if (hasOffset) {
return resolveOffsetAndZone(ldt, zoneId, zoneIdStr);
}
}
}

Expand Down Expand Up @@ -5578,11 +5582,12 @@ public static ZonedDateTime parseZonedDateTime(char[] str, int off, int len, Zon
zoneId = defaultZoneId;
} else {
char first = str[off + zoneIdBegin];
if (first == 'Z') {
if (first == 'Z' && (zoneIdBegin + 1 == len || str[off + zoneIdBegin + 1] != '[')) {
zoneId = UTC;
} else {
boolean hasOffset = first == '+' || first == '-' || first == 'Z';
if (zoneIdStr == null) {
if (first == '+' || first == '-') {
if (hasOffset) {
zoneIdStr = new String(str, off + zoneIdBegin, len - zoneIdBegin);
// zoneIdStr = new String(chars, zoneIdBegin, len - zoneIdBegin);
} else if (first == ' ') {
Expand All @@ -5595,6 +5600,9 @@ public static ZonedDateTime parseZonedDateTime(char[] str, int off, int len, Zon
}
}
zoneId = getZoneId(zoneIdStr, defaultZoneId);
if (hasOffset) {
return resolveOffsetAndZone(ldt, zoneId, zoneIdStr);
}
}
}

Expand All @@ -5609,6 +5617,15 @@ public static ZonedDateTime parseZonedDateTime(char[] str, int off, int len, Zon
return ZonedDateTime.ofLocal(ldt, zoneId, null);
}

private static ZonedDateTime resolveOffsetAndZone(LocalDateTime dateTime, ZoneId zoneId, String offsetAndZone) {
int regionBegin = offsetAndZone.indexOf('[');
if (regionBegin > 0) {
ZoneOffset offset = ZoneOffset.of(offsetAndZone.substring(0, regionBegin));
return ZonedDateTime.ofInstant(dateTime, offset, zoneId);
}
return ZonedDateTime.ofLocal(dateTime, zoneId, null);
}

static ZonedDateTime parseZonedDateTimeCookie(String str) {
if (str.endsWith(" CST")) {
DateTimeFormatter formatter = DATE_TIME_FORMATTER_COOKIE_LOCAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -73,6 +78,21 @@ public void test3() {
assertEquals(s, '"' + zonedDateTime2.toString() + '"');
}

@ParameterizedTest
@CsvSource({
"2024-10-27T02:30:00+01:00[Europe/Paris], Europe/Paris, 2024-10-27T01:30:00Z",
"2024-10-27T01:30:00Z[Europe/London], Europe/London, 2024-10-27T01:30:00Z",
"2024-11-03T01:30:00-05:00[America/New_York], America/New_York, 2024-11-03T06:30:00Z"
})
public void testExplicitOffsetWithRegion(String value, String region, String instant) {
String json = '"' + value + '"';
ZonedDateTime expected = Instant.parse(instant).atZone(ZoneId.of(region));

assertEquals(expected, JSON.parseObject(json, ZonedDateTime.class));
assertEquals(expected, JSON.parseObject(json.toCharArray(), ZonedDateTime.class));
assertEquals(expected, JSON.parseObject(json.getBytes(StandardCharsets.UTF_8), ZonedDateTime.class));
}

public static class Student2 {
public ZonedDateTime birthday;
}
Expand Down