From 9230ea4ab76e5f610cd8a095d2c9693c8926f2d1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 30 Jun 2020 17:25:16 +0100 Subject: [PATCH] Polish "Support iso-offset for date-time and time formatting with MVC" See gh-21630 --- .../web/format/DateTimeFormatters.java | 2 +- ...itional-spring-configuration-metadata.json | 8 ++--- .../web/format/WebConversionServiceTests.java | 33 ++++++++++++++----- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java index b41fb27ec2..3ed58d9e31 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/format/DateTimeFormatters.java @@ -107,7 +107,7 @@ public class DateTimeFormatters { } private static boolean isIsoOffset(String pattern) { - return "isooffset".equalsIgnoreCase(pattern); + return "isooffset".equalsIgnoreCase(pattern) || "iso-offset".equalsIgnoreCase(pattern); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 3a06b15b97..8c10eba693 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1966,8 +1966,8 @@ "description": "ISO-8601 extended local date-time format." }, { - "value": "isooffset", - "description": "ISO Offset local date-time format." + "value": "iso-offset", + "description": "ISO offset date-time format." } ], "providers": [ @@ -1988,8 +1988,8 @@ "description": "ISO-8601 extended local time format" }, { - "value": "isooffset", - "description": "ISO Offset local time format." + "value": "iso-offset", + "description": "ISO offset time format." } ], "providers": [ diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java index d8eb600e13..d248c1669b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/format/WebConversionServiceTests.java @@ -80,15 +80,23 @@ class WebConversionServiceTests { void isoTimeFormat() { WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().timeFormat("iso")); LocalTime time = LocalTime.of(12, 45, 23); - System.out.println(conversionService.convert(time, String.class)); assertThat(conversionService.convert(time, String.class)) .isEqualTo(DateTimeFormatter.ISO_LOCAL_TIME.format(time)); } - + @Test void isoOffsetTimeFormat() { - WebConversionService conversionService = new WebConversionService(new DateTimeFormatters().timeFormat("isooffset")); - OffsetTime offsetTime =OffsetTime.of(LocalTime.of(12, 45, 23), ZoneOffset.ofHoursMinutes(1, 30)); + isoOffsetTimeFormat(new DateTimeFormatters().timeFormat("isooffset")); + } + + @Test + void hyphenatedIsoOffsetTimeFormat() { + isoOffsetTimeFormat(new DateTimeFormatters().timeFormat("iso-offset")); + } + + private void isoOffsetTimeFormat(DateTimeFormatters formatters) { + WebConversionService conversionService = new WebConversionService(formatters); + OffsetTime offsetTime = OffsetTime.of(LocalTime.of(12, 45, 23), ZoneOffset.ofHoursMinutes(1, 30)); assertThat(conversionService.convert(offsetTime, String.class)) .isEqualTo(DateTimeFormatter.ISO_OFFSET_TIME.format(offsetTime)); } @@ -120,13 +128,22 @@ class WebConversionServiceTests { @Test void isoOffsetDateTimeFormat() { - WebConversionService conversionService = new WebConversionService( - new DateTimeFormatters().dateTimeFormat("isooffset")); - OffsetDateTime offsetdate =OffsetDateTime.of(LocalDate.of(2020, 4, 26), LocalTime.of(12, 45, 23), ZoneOffset.ofHoursMinutes(1, 30)); + isoOffsetDateTimeFormat(new DateTimeFormatters().dateTimeFormat("isooffset")); + } + + @Test + void hyphenatedIsoOffsetDateTimeFormat() { + isoOffsetDateTimeFormat(new DateTimeFormatters().dateTimeFormat("iso-offset")); + } + + private void isoOffsetDateTimeFormat(DateTimeFormatters formatters) { + WebConversionService conversionService = new WebConversionService(formatters); + OffsetDateTime offsetdate = OffsetDateTime.of(LocalDate.of(2020, 4, 26), LocalTime.of(12, 45, 23), + ZoneOffset.ofHoursMinutes(1, 30)); assertThat(conversionService.convert(offsetdate, String.class)) .isEqualTo(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetdate)); } - + @Test void customDateTimeFormat() { WebConversionService conversionService = new WebConversionService(