Skip to content

Commit

Permalink
Fix #2643: add colon in timezone offset for better ISO-8601 compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 5, 2020
1 parent e1d9be3 commit 4b51149
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
6 changes: 6 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ Project: jackson-databind
(reported by Bartosz B)
#2592: `ObjectMapper.setSerializationInclusion()` is ignored for `JsonAnyGetter`
(reported by Oleksii K)
#2643: Change default textual serialization of `java.util.Date`/`Calendar`
to include colon in timezone offset
- Add `SerializerProvider.findContentValueSerializer()` methods

2.10.4 (not yet released)

-

2.10.3 (03-Mar-2020)

#2482: `JSONMappingException` `Location` column number is one line Behind the actual
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
* an ISO-8601 compliant format (format String "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
* and for deserialization, both ISO-8601 and RFC-1123.
*<br>
* Note that `Z` in format String refers to RFC-822 timezone notation which produces
* values like "-0800" -- that is, full minute/hour combo without colon, and not using `Z`
* as alias for "+0000".
*<p>
* Note also that to enable use of colon in timezone is possible by using method
* {@link #withColonInTimeZone} for creating new differently configured format instance.
* Note that `Z` in format String refers to ISO-8601 time offset notation which produces
* values like "-08:00" -- that is, full minute/hour combo without colon, and not using `Z`
* as alias for "+00:00".
* Inclusion of colon as separator, as default setting, started in Jackson 2.11:
* prior versions omitted it.
* Note that it is possible to enable/disable use of colon in time offset by using method
* {@link #withColonInTimeZone} for creating new differently configured format instance,
* and configuring {@code ObjectMapper} with it.
*/
@SuppressWarnings("serial")
public class StdDateFormat
Expand Down Expand Up @@ -153,11 +155,12 @@ public class StdDateFormat
/**
* Whether the TZ offset must be formatted with a colon between hours and minutes ({@code HH:mm} format)
*<p>
* Defaults to {@code false} for backwards compatibility reasons
* Defaults to {@code true} since 2.11: earlier versions defaulted to {@code false}
* for backwards compatibility reasons
*
* @since 2.9.1
*/
private boolean _tzSerializedWithColon = false;
private boolean _tzSerializedWithColon = true;

/*
/**********************************************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,22 @@ public void testDateISO8601_customTZ() throws IOException
/**
* Configure the StdDateFormat to serialize TZ offset with a colon between hours and minutes
*
* See [databind#1744]
* See [databind#1744], [databind#2643]
*/
public void testDateISO8601_colonInTZ() throws IOException
{
// with [databind#2643], default now is to include
StdDateFormat dateFormat = new StdDateFormat();
assertFalse(dateFormat.isColonIncludedInTimeZone());
dateFormat = dateFormat.withColonInTimeZone(true);
assertTrue(dateFormat.isColonIncludedInTimeZone());
// but we can disable it
dateFormat = dateFormat.withColonInTimeZone(false);

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setDateFormat(dateFormat);

serialize( mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000+00:00");
serialize( mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000+00:00");
serialize(mapper, judate(1970, 1, 1, 02, 00, 00, 0, "GMT+2"), "1970-01-01T00:00:00.000+0000");
serialize(mapper, judate(1970, 1, 1, 00, 00, 00, 0, "UTC"), "1970-01-01T00:00:00.000+0000");
}

public void testDateOther() throws IOException
Expand Down Expand Up @@ -384,7 +385,7 @@ private void serialize(ObjectWriter w, Object date, String expected) throws IOEx

private String zoneOffset(String raw) {
// Add colon or not -- difference between 2.10 and earlier, 2.11 and later
// return raw.substring(0, 2) + ":" + raw.substring(2); // 2.11 and later
return raw; // 2.10 and earlier
return raw.substring(0, 2) + ":" + raw.substring(2); // 2.11 and later
// return raw; // 2.10 and earlier
}
}

0 comments on commit 4b51149

Please sign in to comment.