Skip to content

Commit

Permalink
Ensure date parsing BWC compatibility (#37929)
Browse files Browse the repository at this point in the history
In order to retain BWC this changes the java date formatters to be able to
parse nanoseconds resolution, even if only milliseconds are supported.
This used to work on joda time as well so that a user could store a date
like `2018-10-03T14:42:44.613469+0000` and then just loose the precision
on anything lower than millisecond level.
  • Loading branch information
spinscale authored Jan 30, 2019
1 parent 908c8de commit 9ec4abc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,6 @@ public class DateFormatters {
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 3, 3, true)
.optionalEnd()
.optionalStart()
.appendFraction(NANO_OF_SECOND, 3, 9, true)
.optionalEnd()
.optionalEnd()
Expand Down Expand Up @@ -205,7 +202,7 @@ public class DateFormatters {
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter BASIC_TIME_PRINTER = new DateTimeFormatterBuilder()
Expand Down Expand Up @@ -311,7 +308,7 @@ public class DateFormatters {
private static final DateFormatter BASIC_ORDINAL_DATE_TIME = new JavaDateFormatter("basic_ordinal_date_time",
new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_PRINTER)
.appendZoneOrOffsetId().toFormatter(Locale.ROOT),
new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_PRINTER)
new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER)
.appendZoneOrOffsetId().toFormatter(Locale.ROOT),
new DateTimeFormatterBuilder().appendPattern("yyyyDDD").append(BASIC_T_TIME_FORMATTER)
.append(TIME_ZONE_FORMATTER_NO_COLON).toFormatter(Locale.ROOT)
Expand Down Expand Up @@ -419,7 +416,7 @@ public class DateFormatters {
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 3, 3, true)
.appendFraction(NANO_OF_SECOND, 3, 9, true)
.appendZoneOrOffsetId()
.toFormatter(Locale.ROOT),
new DateTimeFormatterBuilder()
Expand All @@ -428,7 +425,7 @@ public class DateFormatters {
.appendValue(HOUR_OF_DAY, 2, 2, SignStyle.NOT_NEGATIVE)
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 3, 3, true)
.appendFraction(NANO_OF_SECOND, 3, 9, true)
.append(TIME_ZONE_FORMATTER_NO_COLON)
.toFormatter(Locale.ROOT)
);
Expand Down Expand Up @@ -485,7 +482,7 @@ public class DateFormatters {
.appendLiteral('T')
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 3, 3, true)
.appendFraction(NANO_OF_SECOND, 3, 9, true)
.optionalEnd()
.toFormatter(Locale.ROOT);

Expand Down Expand Up @@ -542,7 +539,7 @@ public class DateFormatters {
// NOTE: this is not a strict formatter to retain the joda time based behaviour, even though it's named like this
private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_MILLIS_FORMATTER = new DateTimeFormatterBuilder()
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER = new DateTimeFormatterBuilder()
Expand Down Expand Up @@ -582,8 +579,8 @@ public class DateFormatters {
.append(STRICT_YEAR_MONTH_DAY_FORMATTER)
.appendLiteral("T")
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
// this one here is lenient as well to retain joda time based bwc compatibility
.appendFraction(NANO_OF_SECOND, 1, 3, true)
// this one here is lenient as well to retain joda time based bwc compatibility
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.toFormatter(Locale.ROOT)
);

Expand All @@ -599,7 +596,7 @@ public class DateFormatters {
.appendLiteral("T")
.append(STRICT_HOUR_MINUTE_SECOND_FORMATTER)
// this one here is lenient as well to retain joda time based bwc compatibility
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.toFormatter(Locale.ROOT)
);

Expand All @@ -625,7 +622,7 @@ public class DateFormatters {
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 3, 3, true)
.appendFraction(NANO_OF_SECOND, 3, 9, true)
.optionalEnd()
.toFormatter(Locale.ROOT);

Expand All @@ -649,7 +646,7 @@ public class DateFormatters {
.appendValue(MINUTE_OF_HOUR, 2, 2, SignStyle.NOT_NEGATIVE)
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter STRICT_TIME_PRINTER = new DateTimeFormatterBuilder()
Expand Down Expand Up @@ -880,7 +877,7 @@ public class DateFormatters {
.appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE)
.optionalEnd()
.optionalStart()
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.optionalEnd()
.optionalStart().appendZoneOrOffsetId().optionalEnd()
.optionalStart().appendOffset("+HHmm", "Z").optionalEnd()
Expand All @@ -904,6 +901,15 @@ public class DateFormatters {
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter HOUR_MINUTE_SECOND_FRACTION_FORMATTER = new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY, 1, 2, SignStyle.NOT_NEGATIVE)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 1, 2, SignStyle.NOT_NEGATIVE)
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter ORDINAL_DATE_FORMATTER = new DateTimeFormatterBuilder()
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
Expand Down Expand Up @@ -936,7 +942,7 @@ public class DateFormatters {

private static final DateTimeFormatter TIME_PREFIX = new DateTimeFormatterBuilder()
.append(TIME_NO_MILLIS_FORMATTER)
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.toFormatter(Locale.ROOT);

private static final DateTimeFormatter WEEK_DATE_FORMATTER = new DateTimeFormatterBuilder()
Expand Down Expand Up @@ -974,8 +980,7 @@ public class DateFormatters {
/*
* Returns a formatter that combines a full date, two digit hour of day,
* two digit minute of hour, two digit second of minute, and three digit
* fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS). Parsing will parse up
* to 3 fractional second digits.
* fraction of second (yyyy-MM-dd'T'HH:mm:ss.SSS).
*/
private static final DateFormatter DATE_HOUR_MINUTE_SECOND_MILLIS =
new JavaDateFormatter("date_hour_minute_second_millis",
Expand All @@ -990,7 +995,8 @@ public class DateFormatters {
.append(HOUR_MINUTE_SECOND_MILLIS_FORMATTER)
.toFormatter(Locale.ROOT));

private static final DateFormatter DATE_HOUR_MINUTE_SECOND_FRACTION = new JavaDateFormatter("date_hour_minute_second_fraction",
private static final DateFormatter DATE_HOUR_MINUTE_SECOND_FRACTION =
new JavaDateFormatter("date_hour_minute_second_fraction",
new DateTimeFormatterBuilder()
.append(STRICT_YEAR_MONTH_DAY_FORMATTER)
.appendLiteral("T")
Expand All @@ -999,7 +1005,7 @@ public class DateFormatters {
new DateTimeFormatterBuilder()
.append(DATE_FORMATTER)
.appendLiteral("T")
.append(HOUR_MINUTE_SECOND_MILLIS_FORMATTER)
.append(HOUR_MINUTE_SECOND_FRACTION_FORMATTER)
.toFormatter(Locale.ROOT));

/*
Expand Down Expand Up @@ -1034,7 +1040,7 @@ public class DateFormatters {
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.optionalEnd()
.toFormatter(Locale.ROOT);

Expand Down Expand Up @@ -1106,7 +1112,7 @@ public class DateFormatters {
STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, HOUR_MINUTE_SECOND_MILLIS_FORMATTER);

private static final DateFormatter HOUR_MINUTE_SECOND_FRACTION = new JavaDateFormatter("hour_minute_second_fraction",
STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, HOUR_MINUTE_SECOND_MILLIS_FORMATTER);
STRICT_HOUR_MINUTE_SECOND_MILLIS_PRINTER, HOUR_MINUTE_SECOND_FRACTION_FORMATTER);

/*
* Returns a formatter for a two digit hour of day and two digit minute of
Expand Down Expand Up @@ -1142,7 +1148,7 @@ public class DateFormatters {
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 1, 2, SignStyle.NOT_NEGATIVE)
.appendFraction(NANO_OF_SECOND, 1, 3, true)
.appendFraction(NANO_OF_SECOND, 1, 9, true)
.optionalEnd()
.toFormatter(Locale.ROOT);

Expand Down
Loading

0 comments on commit 9ec4abc

Please sign in to comment.