From 96f5093140093909fc9e2102ab08dc8d2f0486c5 Mon Sep 17 00:00:00 2001 From: Ben Yu Date: Sun, 17 Dec 2023 14:50:59 -0800 Subject: [PATCH] Use instead of {America/New_York} for placeholders because curly braces are reserved by DateTimeFormatter javadoc --- .../com/google/mu/time/DateTimeFormats.java | 8 ++-- .../google/mu/time/DateTimeFormatsTest.java | 46 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/mug/src/main/java/com/google/mu/time/DateTimeFormats.java b/mug/src/main/java/com/google/mu/time/DateTimeFormats.java index 555071e287..6be0664b9c 100644 --- a/mug/src/main/java/com/google/mu/time/DateTimeFormats.java +++ b/mug/src/main/java/com/google/mu/time/DateTimeFormats.java @@ -75,7 +75,7 @@ * *

If the variant of the date time pattern you need exceeds the out-of-box support, you can * explicitly mix the {@link DateTimeFormatter} specifiers with example placeholders - * (between a pair of curly braces) to be translated. + * (between a pair of pointy brackets) to be translated. * *

For example the following code uses the {@code dd}, {@code MM} and {@code yyyy} specifiers as * is but translates the {@code Tue} and {@code America/New_York} example snippets into {@code E} @@ -84,7 +84,7 @@ * *

{@code
  * private static final DateTimeFormatter FORMATTER =
- *     formatOf("{Tue}, dd MM yyyy HH:mm:ss.SSS {America/New_York}");
+ *     formatOf(", dd MM yyyy HH:mm:ss.SSS ");
  * }
* * @since 7.1 @@ -106,8 +106,8 @@ public final class DateTimeFormats { .repeatedly(); private static final Substring.RepeatingPattern PLACEHOLDERS = - Substring.consecutive(CharPredicate.noneOf("{}")) - .immediatelyBetween("{", Substring.BoundStyle.INCLUSIVE, "}", INCLUSIVE) + Substring.consecutive(CharPredicate.noneOf("<>")) + .immediatelyBetween("<", Substring.BoundStyle.INCLUSIVE, ">", INCLUSIVE) .repeatedly(); private static final Map, DateTimeFormatter> ISO_DATE_FORMATTERS = BiStream.of( diff --git a/mug/src/test/java/com/google/mu/time/DateTimeFormatsTest.java b/mug/src/test/java/com/google/mu/time/DateTimeFormatsTest.java index 88d7abb0e5..3bb2cd93fb 100644 --- a/mug/src/test/java/com/google/mu/time/DateTimeFormatsTest.java +++ b/mug/src/test/java/com/google/mu/time/DateTimeFormatsTest.java @@ -179,7 +179,7 @@ public void mmddyyyy_notSupported() { @Test public void formatOf_mmddyyMixedIn() { - DateTimeFormatter formatter = formatOf("MM/dd/yyyy {12:10:00} {America/New_York}"); + DateTimeFormatter formatter = formatOf("MM/dd/yyyy <12:10:00> "); ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); assertThat(zonedTime.format(formatter)).isEqualTo("10/20/2023 01:02:03 America/Los_Angeles"); @@ -187,7 +187,7 @@ public void formatOf_mmddyyMixedIn() { @Test public void formatOf_ddmmyyMixedIn() { - DateTimeFormatter formatter = formatOf("dd MM yyyy {12:10:00 America/New_York}"); + DateTimeFormatter formatter = formatOf("dd MM yyyy <12:10:00 America/New_York>"); ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); assertThat(zonedTime.format(formatter)).isEqualTo("20 10 2023 01:02:03 America/Los_Angeles"); @@ -195,7 +195,7 @@ public void formatOf_ddmmyyMixedIn() { @Test public void formatOf_monthOfYearMixedIn() { - DateTimeFormatter formatter = formatOf("E, LLL dd yyyy {12:10:00 America/New_York}"); + DateTimeFormatter formatter = formatOf("E, LLL dd yyyy <12:10:00 America/New_York>"); ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); assertEquivalent(formatter, zonedTime, "E, LLL dd yyyy HH:mm:ss VV"); @@ -205,7 +205,7 @@ public void formatOf_monthOfYearMixedIn() { public void formatOf_fullWeekdayAndMonthNamePlaceholder() { ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); - DateTimeFormatter formatter = formatOf("{Tuesday}, {May} dd yyyy {12:10:00} {+08:00} {America/New_York}"); + DateTimeFormatter formatter = formatOf(", dd yyyy <12:10:00> <+08:00> "); assertEquivalent(formatter, zonedTime, "EEEE, LLLL dd yyyy HH:mm:ss ZZZZZ VV"); } @@ -213,13 +213,13 @@ public void formatOf_fullWeekdayAndMonthNamePlaceholder() { public void formatOf_12HourFormat() { ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); - DateTimeFormatter formatter = formatOf("dd MM yyyy {ad} hh:mm {pm} {+08:00}"); + DateTimeFormatter formatter = formatOf("dd MM yyyy hh:mm <+08:00>"); assertThat(zonedTime.format(formatter)).isEqualTo("20 10 2023 AD 01:02 AM -07:00"); } @Test public void formatOf_zoneNameNotRetranslated() { - DateTimeFormatter formatter = formatOf("{Mon}, {Jan} dd yyyy {12:10:00} VV"); + DateTimeFormatter formatter = formatOf(", dd yyyy <12:10:00> VV"); ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); assertEquivalent(formatter, zonedTime, "E, LLL dd yyyy HH:mm:ss VV"); @@ -227,7 +227,7 @@ public void formatOf_zoneNameNotRetranslated() { @Test public void formatOf_zoneOffsetNotRetranslated() { - DateTimeFormatter formatter = formatOf("E, LLL dd yyyy {12:10:00} zzzz"); + DateTimeFormatter formatter = formatOf("E, LLL dd yyyy <12:10:00> zzzz"); ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); assertEquivalent(formatter, zonedTime, "E, LLL dd yyyy HH:mm:ss zzzz"); @@ -235,7 +235,7 @@ public void formatOf_zoneOffsetNotRetranslated() { @Test public void formatOf_monthOfYearMixedIn_withDayOfWeek() { - DateTimeFormatter formatter = formatOf("E, LLL dd yyyy {12:10:00} {America/New_York}"); + DateTimeFormatter formatter = formatOf("E, LLL dd yyyy <12:10:00> "); ZonedDateTime zonedTime = ZonedDateTime.of(LocalDateTime.of(2023, 10, 20, 1, 2, 3), ZoneId.of("America/Los_Angeles")); assertEquivalent(formatter, zonedTime, "E, LLL dd yyyy HH:mm:ss VV"); @@ -414,7 +414,7 @@ public void offsetTimeExamples( @Test public void timeZoneMixedIn_zeroOffset() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss{Z}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss"); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30Z", formatter); assertThat(dateTime) .isEqualTo(ZonedDateTime.of(LocalDateTime.of(2023, 1, 10, 10, 20, 30, 0), ZoneOffset.UTC)); @@ -422,7 +422,7 @@ public void timeZoneMixedIn_zeroOffset() { @Test public void timeZoneMixedIn_offsetWithoutColon() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("MM dd yyyy HH:mm:ss{+0100}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("MM dd yyyy HH:mm:ss<+0100>"); ZonedDateTime dateTime = ZonedDateTime.parse("01 10 2023 10:20:30-0800", formatter); assertThat(dateTime) .isEqualTo( @@ -431,7 +431,7 @@ public void timeZoneMixedIn_offsetWithoutColon() { @Test public void timeZoneMixedIn_hourOffset() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss{+01}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss<+01>"); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30-08", formatter); assertThat(dateTime) .isEqualTo( @@ -440,7 +440,7 @@ public void timeZoneMixedIn_hourOffset() { @Test public void timeZoneMixedIn_offsetWithColon() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss{-01:00}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss<-01:00>"); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30-08:00", formatter); assertThat(dateTime) .isEqualTo( @@ -450,7 +450,7 @@ public void timeZoneMixedIn_offsetWithColon() { @Test public void timeZoneMixedIn_zoneNameWithEuropeDateStyle() { DateTimeFormatter formatter = - DateTimeFormats.inferFromExample("dd MM yyyy HH:mm:ss.SSS {America/New_York}"); + DateTimeFormats.inferFromExample("dd MM yyyy HH:mm:ss.SSS "); ZonedDateTime dateTime = ZonedDateTime.parse("30 10 2023 10:20:30.123 Europe/Paris", formatter); assertThat(dateTime) .isEqualTo( @@ -460,7 +460,7 @@ public void timeZoneMixedIn_zoneNameWithEuropeDateStyle() { @Test public void timeZoneMixedIn_offsetWithAmericanDateStyle() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss{+01:00}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss<+01:00>"); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30-07:00", formatter); assertThat(dateTime) .isEqualTo( @@ -469,7 +469,7 @@ public void timeZoneMixedIn_offsetWithAmericanDateStyle() { @Test public void timeZoneMixedIn_twoLetterZoneNameAbbreviation() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss{PT}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss"); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30PT", formatter); assertThat(dateTime) .isEqualTo( @@ -479,7 +479,7 @@ public void timeZoneMixedIn_twoLetterZoneNameAbbreviation() { @Test public void timeZoneMixedIn_fourLetterZoneNameAbbreviation() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss{CAST}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss"); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30CAST", formatter); assertThat(dateTime.toLocalDateTime()) .isEqualTo(LocalDateTime.of(2023, 1, 10, 10, 20, 30, 0)); @@ -487,7 +487,7 @@ public void timeZoneMixedIn_fourLetterZoneNameAbbreviation() { @Test public void timeZoneMixedIn_abbreviatedZoneName() { - DateTimeFormatter formatter = DateTimeFormats.inferFromExample("MM dd yyyy HH:mm:ss{GMT}"); + DateTimeFormatter formatter = DateTimeFormats.inferFromExample("MM dd yyyy HH:mm:ss"); ZonedDateTime dateTime = ZonedDateTime.parse("01 10 2023 10:20:30PST", formatter); assertThat(dateTime) .isEqualTo( @@ -498,7 +498,7 @@ public void timeZoneMixedIn_abbreviatedZoneName() { @Test public void timeZoneMixedIn_twoWordsZoneName() { DateTimeFormatter formatter = - DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss {Eastern Time}"); + DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss "); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter); assertThat(dateTime) @@ -510,7 +510,7 @@ public void timeZoneMixedIn_twoWordsZoneName() { @Test public void timeZoneMixedIn_threeWordsZoneName() { DateTimeFormatter formatter = - DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss {Zulu Time Zone}"); + DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss "); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter); assertThat(dateTime) @@ -522,7 +522,7 @@ public void timeZoneMixedIn_threeWordsZoneName() { @Test public void timeZoneMixedIn_fourWordsZoneName() { DateTimeFormatter formatter = - DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss {Eastern European Summer Time}"); + DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss "); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter); assertThat(dateTime) @@ -535,7 +535,7 @@ public void timeZoneMixedIn_fourWordsZoneName() { public void timeZoneMixedIn_fiveWordsZoneName() { DateTimeFormatter formatter = DateTimeFormats.inferFromExample( - "M dd yyyy HH:mm:ss {French Southern and Antarctic Time}"); + "M dd yyyy HH:mm:ss "); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter); assertThat(dateTime) @@ -547,7 +547,7 @@ public void timeZoneMixedIn_fiveWordsZoneName() { @Test public void timeZoneMixedIn_zoneNameWithDash() { DateTimeFormatter formatter = - DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss {Further-Eastern European Time}"); + DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss "); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter); assertThat(dateTime) @@ -559,7 +559,7 @@ public void timeZoneMixedIn_zoneNameWithDash() { @Test public void timeZoneMixedIn_zoneNameWithApostrophe() { DateTimeFormatter formatter = - DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss {Dumont-d'Urville Time}"); + DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss "); ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Time", formatter); assertThat(dateTime) .isEqualTo(