Skip to content

Commit

Permalink
Use <America/New_York> instead of {America/New_York} for placeholders…
Browse files Browse the repository at this point in the history
… because curly braces are reserved by DateTimeFormatter javadoc
  • Loading branch information
fluentfuture committed Dec 17, 2023
1 parent 16fade3 commit 96f5093
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions mug/src/main/java/com/google/mu/time/DateTimeFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
*
* <p>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.
*
* <p>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}
Expand All @@ -84,7 +84,7 @@
*
* <pre>{@code
* private static final DateTimeFormatter FORMATTER =
* formatOf("{Tue}, dd MM yyyy HH:mm:ss.SSS {America/New_York}");
* formatOf("<Tue>, dd MM yyyy HH:mm:ss.SSS <America/New_York>");
* }</pre>
*
* @since 7.1
Expand All @@ -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<List<?>, DateTimeFormatter> ISO_DATE_FORMATTERS =
BiStream.of(
Expand Down
46 changes: 23 additions & 23 deletions mug/src/test/java/com/google/mu/time/DateTimeFormatsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,23 @@ 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> <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("10/20/2023 01:02:03 America/Los_Angeles");
}

@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");
}

@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");
Expand All @@ -205,37 +205,37 @@ 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("<Tuesday>, <May> dd yyyy <12:10:00> <+08:00> <America/New_York>");
assertEquivalent(formatter, zonedTime, "EEEE, LLLL dd yyyy HH:mm:ss ZZZZZ VV");
}

@Test
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 <ad> hh:mm <pm> <+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("<Mon>, <Jan> 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");
}

@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");
}

@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> <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");
Expand Down Expand Up @@ -414,15 +414,15 @@ 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<Z>");
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));
}

@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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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 <America/New_York>");
ZonedDateTime dateTime = ZonedDateTime.parse("30 10 2023 10:20:30.123 Europe/Paris", formatter);
assertThat(dateTime)
.isEqualTo(
Expand All @@ -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(
Expand All @@ -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<PT>");
ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30PT", formatter);
assertThat(dateTime)
.isEqualTo(
Expand All @@ -479,15 +479,15 @@ 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<CAST>");
ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30CAST", formatter);
assertThat(dateTime.toLocalDateTime())
.isEqualTo(LocalDateTime.of(2023, 1, 10, 10, 20, 30, 0));
}

@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<GMT>");
ZonedDateTime dateTime = ZonedDateTime.parse("01 10 2023 10:20:30PST", formatter);
assertThat(dateTime)
.isEqualTo(
Expand All @@ -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 <Eastern Time>");
ZonedDateTime dateTime =
ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter);
assertThat(dateTime)
Expand All @@ -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 <Zulu Time Zone>");
ZonedDateTime dateTime =
ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter);
assertThat(dateTime)
Expand All @@ -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 <Eastern European Summer Time>");
ZonedDateTime dateTime =
ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter);
assertThat(dateTime)
Expand All @@ -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 <French Southern and Antarctic Time>");
ZonedDateTime dateTime =
ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter);
assertThat(dateTime)
Expand All @@ -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 <Further-Eastern European Time>");
ZonedDateTime dateTime =
ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Standard Time", formatter);
assertThat(dateTime)
Expand All @@ -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 <Dumont-d'Urville Time>");
ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30 Eastern Time", formatter);
assertThat(dateTime)
.isEqualTo(
Expand Down

0 comments on commit 96f5093

Please sign in to comment.