Skip to content

Commit

Permalink
Drop support for yyyyMMdd because when you use example like 10302023 …
Browse files Browse the repository at this point in the history
…it can misinterpret. Let the users do yyyyMMdd with placeholders
  • Loading branch information
fluentfuture committed Dec 19, 2023
1 parent 85cde5f commit 50a0ea8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
13 changes: 4 additions & 9 deletions mug/src/main/java/com/google/mu/time/DateTimeFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.mu.util.CharPredicate.anyOf;
import static com.google.mu.util.CharPredicate.is;
import static com.google.mu.util.CharPredicate.noneOf;
import static com.google.mu.util.Substring.consecutive;
import static com.google.mu.util.Substring.first;
import static com.google.mu.util.Substring.firstOccurrence;
Expand Down Expand Up @@ -61,8 +62,8 @@
* DateTimeFormats.formatOf("2023/12/09 Sat 10:00+08:00");
* }</pre>
*
* <p>Most ISO 8601 formats are supported, except ISO_WEEK_DATE ('2012-W48-6') and ISO_ORDINAL_DATE
* ('2012-337').
* <p>Most ISO 8601 formats are supported, except BASIC_ISO_DATE, ISO_WEEK_DATE ('2012-W48-6')
* and ISO_ORDINAL_DATE ('2012-337').
*
* <p>For the date part of custom patterns, {@code MM/dd/yyyy} or {@code dd/MM/yyyy} or any variant
* where the {@code yyyy} is after the {@code MM} or {@code dd} are not supported. However if
Expand Down Expand Up @@ -99,19 +100,14 @@ public final class DateTimeFormats {

/** Punctuation chars, such as '/', ':', '-' are essential part of the pattern syntax. */
private static final CharPredicate PUNCTUATION = DIGIT.or(ALPHA).or(DELIMITER).not();

private static final Substring.RepeatingPattern TOKENIZER =
Stream.of(consecutive(DIGIT), consecutive(ALPHA), first(PUNCTUATION))
.collect(firstOccurrence())
.repeatedly();

private static final Substring.RepeatingPattern PLACEHOLDERS =
Substring.consecutive(CharPredicate.noneOf("<>"))
.immediatelyBetween("<", Substring.BoundStyle.INCLUSIVE, ">", INCLUSIVE)
.repeatedly();
consecutive(noneOf("<>")).immediatelyBetween("<", INCLUSIVE, ">", INCLUSIVE).repeatedly();
private static final Map<List<?>, DateTimeFormatter> ISO_DATE_FORMATTERS =
BiStream.of(
forExample("20111203"), DateTimeFormatter.BASIC_ISO_DATE,
forExample("2011-12-03"), DateTimeFormatter.ISO_LOCAL_DATE,
forExample("2011-12-03+08:00"), DateTimeFormatter.ISO_DATE,
forExample("2011-12-03-08:00"), DateTimeFormatter.ISO_DATE).toMap();
Expand Down Expand Up @@ -151,7 +147,6 @@ public final class DateTimeFormats {
.add(forExample("2011-12-3"), "yyyy-MM-d")
.add(forExample("2011/12/03"), "yyyy/MM/dd")
.add(forExample("2011/12/3"), "yyyy/MM/d")
.add(forExample("20111201"), "yyyyMMdd")
.add(forExample("Jan 11 2011"), "LLL dd yyyy")
.add(forExample("Jan 1 2011"), "LLL d yyyy")
.add(forExample("11 Jan 2011"), "dd LLL yyyy")
Expand Down
16 changes: 5 additions & 11 deletions mug/src/test/java/com/google/mu/time/DateTimeFormatsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,6 @@ public void isoLocalTimeExample() {
.isEqualTo(LocalTime.of(10, 20, 10));
}

@Test
public void basicIsoDateExample() {
assertThat(LocalDate.parse("19881010", formatOf("20231020")))
.isEqualTo(LocalDate.of(1988, 10, 10));
}

@Test
public void rfc1123Example() {
assertThat(
Expand Down Expand Up @@ -260,7 +254,7 @@ public void localTimeExamples(

@Test
public void localDateExamplesFromDifferentFormatters(
@TestParameter({"BASIC_ISO_DATE", "ISO_LOCAL_DATE", "yyyy/MM/dd", "yyyyMMdd"})
@TestParameter({"ISO_LOCAL_DATE", "yyyy/MM/dd"})
String formatterName,
@TestParameter({"2020-01-01", "1979-01-01", "2035-12-31"}) String date)
throws Exception {
Expand All @@ -278,7 +272,7 @@ public void zonedDateTimeExamplesFromDifferentFormatters(
"RFC_1123_DATE_TIME",
"yyyy/MM/dd HH:mm:ss.SSSSSSX",
"yyyy/MM/dd HH:mm:ss.SSSSSSx",
"yyyyMMdd HH:mm:ssZ",
"yyyy/MM/dd HH:mm:ssZ",
"yyyy/MM/dd HH:mm:ssZ",
"yyyy/MM/dd HH:mm:ss.nnnZZ",
"yyyy-MM-dd HH:mm:ss.nnnZZZ",
Expand Down Expand Up @@ -311,7 +305,7 @@ public void withZoneIdExamplesFromDifferentFormatters(
"ISO_DATE_TIME",
"ISO_ZONED_DATE_TIME",
"RFC_1123_DATE_TIME",
"yyyyMMdd HH:mm:ssa VV",
"yyyy/MM/dd HH:mm:ssa VV",
"yyyy/MM/dd HH:mm:ss VV",
"yyyy/MM/dd HH:mm:ss.nnn VV",
"yyyy/MM/dd HH:mm:ss.nnn VV",
Expand Down Expand Up @@ -348,7 +342,7 @@ public void zoneIdRetainedExamples(
@TestParameter({
"ISO_DATE_TIME",
"ISO_ZONED_DATE_TIME",
"yyyyMMdd HH:mm:ss VV",
"yyyy/MM/dd HH:mm:ss VV",
"yyyy/MM/dd HH:mm:ss VV",
"yyyy/MM/dd HH:mm:ss.nnn VV",
"yyyy/MM/dd HH:mm:ss.nnn VV",
Expand Down Expand Up @@ -379,7 +373,7 @@ public void zonedDateTimeWithNanosExamples(
"ISO_ZONED_DATE_TIME",
"yyyy/MM/dd HH:mm:ss.SSSSSSX",
"yyyy/MM/dd HH:mm:ss.SSSSSSVV",
"yyyyMMdd HH:mm:ss.SSSSSSZ",
"yyyy/MM/dd HH:mm:ss.SSSSSSZ",
"yyyy/MM/dd HH:mm:ss.SSSSSSZ",
"yyyy/MM/dd HH:mm:ss.SSSSSSVV",
"yyyy/MM/dd HH:mm:ss.SSSSSSz",
Expand Down

0 comments on commit 50a0ea8

Please sign in to comment.