Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
joxerTMD committed Jun 30, 2016
1 parent 9d24e74 commit 3fca305
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class DateFormatSetup extends BaseFormatSetup {

private TimeZone _timeZone;

private Boolean adjustToContextTZOverride;

public DateFormatSetup() {

}
Expand All @@ -39,4 +41,12 @@ public TimeZone getTimeZone() {
public void setTimeZone(TimeZone timeZone) {
this._timeZone = timeZone;
}

public Boolean getAdjustToContextTZOverride() {
return adjustToContextTZOverride;
}

public void setAdjustToContextTZOverride(Boolean adjustToContextTZOverride) {
this.adjustToContextTZOverride = adjustToContextTZOverride;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class JacksonJodaDateFormat extends JacksonJodaFormatBase
protected transient DateTimeZone _jodaTimezone;

protected final boolean _explicitTimezone;

protected final Boolean _adjustToContextTZOverride;

public JacksonJodaDateFormat(DateTimeFormatter defaultFormatter)
{
Expand All @@ -34,6 +36,7 @@ public JacksonJodaDateFormat(DateTimeFormatter defaultFormatter)
DateTimeZone tz = defaultFormatter.getZone();
_jdkTimezone = (tz == null) ? null : tz.toTimeZone();
_explicitTimezone = false;
_adjustToContextTZOverride = null;
}

public JacksonJodaDateFormat(JacksonJodaDateFormat base, Boolean useTimestamp)
Expand All @@ -42,6 +45,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base, Boolean useTimestamp)
_formatter = base._formatter;
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
}

public JacksonJodaDateFormat(JacksonJodaDateFormat base,
Expand All @@ -51,6 +55,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base,
_formatter = formatter;
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
}

public JacksonJodaDateFormat(JacksonJodaDateFormat base, TimeZone jdkTimezone)
Expand All @@ -59,6 +64,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base, TimeZone jdkTimezone)
_formatter = base._formatter.withZone(DateTimeZone.forTimeZone(jdkTimezone));
_jdkTimezone = jdkTimezone;
_explicitTimezone = true;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
}

public JacksonJodaDateFormat(JacksonJodaDateFormat base, Locale locale)
Expand All @@ -67,6 +73,7 @@ public JacksonJodaDateFormat(JacksonJodaDateFormat base, Locale locale)
_formatter = base._formatter.withLocale(locale);
_jdkTimezone = base._jdkTimezone;
_explicitTimezone = base._explicitTimezone;
_adjustToContextTZOverride = base._adjustToContextTZOverride;
}

public JacksonJodaDateFormat(DateFormatSetup setup)
Expand All @@ -86,6 +93,7 @@ public JacksonJodaDateFormat(DateFormatSetup setup)
_jdkTimezone = setup.getTimeZone();
_explicitTimezone = true;
}
_adjustToContextTZOverride = setup.getAdjustToContextTZOverride();
}

@Override
Expand All @@ -95,6 +103,7 @@ protected DateFormatSetup getSetup() {
if (_explicitTimezone) {
setup.setTimeZone(_jdkTimezone);
}
setup.setAdjustToContextTZOverride(_adjustToContextTZOverride);
return setup;
}

Expand Down Expand Up @@ -152,6 +161,16 @@ public JacksonJodaDateFormat withLocale(Locale locale) {
return new JacksonJodaDateFormat(setup);
}

public JacksonJodaDateFormat withAdjustToContextTZOverride(Boolean adjustToContextTZOverride) {
if ((adjustToContextTZOverride == null) ||
(_adjustToContextTZOverride != null && _adjustToContextTZOverride.equals(adjustToContextTZOverride))) {
return this;
}
DateFormatSetup setup = getSetup();
setup.setAdjustToContextTZOverride(adjustToContextTZOverride);
return new JacksonJodaDateFormat(setup);
}

/*
/**********************************************************
/* Accessors
Expand Down Expand Up @@ -224,7 +243,7 @@ public DateTimeFormatter createParser(DeserializationContext ctxt)
}
}
if (!_explicitTimezone) {
if (ctxt.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)) {
if (isAdjustDatesToContextTimeZone(ctxt)) {
TimeZone tz = ctxt.getTimeZone();
if (tz != null && !tz.equals(_jdkTimezone)) {
formatter = formatter.withZone(DateTimeZone.forTimeZone(tz));
Expand All @@ -236,6 +255,11 @@ public DateTimeFormatter createParser(DeserializationContext ctxt)
return formatter;
}

private boolean isAdjustDatesToContextTimeZone(DeserializationContext ctxt) {
return _adjustToContextTZOverride != null ? _adjustToContextTZOverride :
ctxt.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
}

/**
* Differentiate if TimeZone is specified by caller
* @return true if TimeZone is specified by caller; false otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
format = format.withFormat(ann.getPattern().trim());
format = format.withLocale(ann.getLocale());
format = format.withTimeZone(ann.getTimeZone());
format = format.withAdjustToContextTZOverride(
ann.getFeature(JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE));
if (format != _format) {
return withFormat(format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.TimeZone;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.joda.time.*;
import org.joda.time.chrono.ISOChronology;

Expand Down Expand Up @@ -100,6 +101,37 @@ public void testDeserReadableDateTimeWithTimeZoneFromData() throws IOException {
assertEquals(DateTimeZone.forOffsetHours(-5), date.getZone());
}

static class ReadableDateTimeWithContextTZOverride {
@JsonFormat(with = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
public ReadableDateTime time;
}

public void testDeserReadableDateTimeWithContextTZOverride() throws IOException {
ObjectMapper mapper = jodaMapper();
mapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
mapper.setTimeZone(TimeZone.getTimeZone("UTC"));
ReadableDateTimeWithContextTZOverride date = mapper.readValue("{ \"time\" : \"2016-06-20T08:59:00.000+0300\"}",
ReadableDateTimeWithContextTZOverride.class);
DateTime expected = new DateTime(2016, 6, 20, 5, 59, DateTimeZone.forID("UTC"));
assertEquals(expected, date.time);
}

static class ReadableDateTimeWithoutContextTZOverride {

@JsonFormat(without = JsonFormat.Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
public ReadableDateTime time;
}

public void testDeserReadableDateTimeWithoutContextTZOverride() throws IOException {
ObjectMapper mapper = jodaMapper();
mapper.enable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
mapper.setTimeZone(TimeZone.getTimeZone("UTC"));
ReadableDateTimeWithoutContextTZOverride date = mapper.readValue("{ \"time\" : \"2016-06-20T08:59:00.000+0300\"}",
ReadableDateTimeWithoutContextTZOverride.class);
DateTime expected = new DateTime(2016, 6, 20, 8, 59, DateTimeZone.forOffsetHours(3));
assertEquals(expected, date.time);
}

public void testDeserReadableInstant() throws IOException {
ReadableInstant date = MAPPER.readValue(quote("1972-12-28T12:00:01.000+0000"), ReadableInstant.class);
assertNotNull(date);
Expand Down

0 comments on commit 3fca305

Please sign in to comment.