Skip to content

Commit 47e9055

Browse files
Woodzcowtowncoder
authored andcommitted
Change IntervalDeserializer to respect input timezone when enabled
Change `IntervalDeserializer` to use `Interval.parseWithOffset` and override timezone only if `shouldAdjustToContextTimeZone` is set or explicit timezone is specified in the format Add unit tests for `IntervalDeserializer` to cover the 3 scenarios: - Timezone explicitly provided in deserialization format - Context timezone enabled - Neither context timezone enabled nor explicit timezone provided Resolve: #104
1 parent f3f44dc commit 47e9055

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/main/java/com/fasterxml/jackson/datatype/joda/deser/IntervalDeserializer.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Interval deserialize(JsonParser p, DeserializationContext ctxt)
5757
try {
5858
// !!! TODO: configurable formats...
5959
if (hasSlash) {
60-
result = Interval.parse(v);
60+
result = Interval.parseWithOffset(v);
6161
} else {
6262
start = Long.valueOf(str);
6363
str = v.substring(index + 1);
@@ -70,7 +70,9 @@ public Interval deserialize(JsonParser p, DeserializationContext ctxt)
7070
str, v);
7171
}
7272

73-
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : DateTimeZone.forTimeZone(ctxt.getTimeZone());
73+
final DateTimeZone contextTimezone =
74+
_format.shouldAdjustToContextTimeZone(ctxt) ? DateTimeZone.forTimeZone(ctxt.getTimeZone()) : null;
75+
DateTimeZone tz = _format.isTimezoneExplicit() ? _format.getTimeZone() : contextTimezone;
7476
if (tz != null) {
7577
if (!tz.equals(result.getStart().getZone())) {
7678
result = new Interval(result.getStartMillis(), result.getEndMillis(), tz);

src/test/java/com/fasterxml/jackson/datatype/joda/deser/IntervalDeserTest.java

+38
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.TimeZone;
55

6+
import com.fasterxml.jackson.databind.DeserializationFeature;
67
import org.joda.time.DateTimeZone;
78
import org.joda.time.Interval;
89
import org.joda.time.chrono.ISOChronology;
@@ -64,4 +65,41 @@ public void testIntervalDeserWithTypeInfo() throws IOException
6465
assertEquals(1396439982, interval.getStartMillis());
6566
assertEquals(1396440001, interval.getEndMillis());
6667
}
68+
69+
public void testIntervalDeserFromIso8601WithTimezoneWithContextTimeZone() throws IOException
70+
{
71+
final ObjectMapper mapper = mapperWithModuleBuilder()
72+
.defaultTimeZone(TimeZone.getTimeZone("GMT-2:00"))
73+
.enable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
74+
.build();
75+
final Interval expectedInterval = new Interval(1000, 2000, DateTimeZone.forOffsetHours(-2));
76+
final Interval actualInterval =
77+
mapper.readValue(quote("1970-01-01T06:00:01.000+06:00/1970-01-01T06:00:02.000+06:00"), Interval.class);
78+
79+
assertEquals(expectedInterval, actualInterval);
80+
}
81+
82+
public void testIntervalDeserFromIso8601WithTimezoneWithoutContextTimeZone() throws IOException
83+
{
84+
final ObjectMapper mapper = mapperWithModuleBuilder()
85+
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
86+
.build();
87+
final Interval expectedInterval = new Interval(1000, 2000, DateTimeZone.forOffsetHours(6));
88+
final Interval actualInterval =
89+
mapper.readValue(quote("1970-01-01T06:00:01.000+06:00/1970-01-01T06:00:02.000+06:00"), Interval.class);
90+
91+
assertEquals(expectedInterval, actualInterval);
92+
}
93+
94+
public void testIntervalDeserFromIso8601WithTimezoneWithDefaultTimeZone() throws IOException
95+
{
96+
final ObjectMapper mapper = mapperWithModuleBuilder()
97+
.enable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
98+
.build();
99+
final Interval expectedInterval = new Interval(1000, 2000, DateTimeZone.UTC);
100+
final Interval actualInterval =
101+
mapper.readValue(quote("1970-01-01T06:00:01.000+06:00/1970-01-01T06:00:02.000+06:00"), Interval.class);
102+
103+
assertEquals(expectedInterval, actualInterval);
104+
}
67105
}

0 commit comments

Comments
 (0)