Skip to content

Commit

Permalink
Merge branch 'master' of github.com:google/mug
Browse files Browse the repository at this point in the history
  • Loading branch information
fluentfuture committed Dec 17, 2023
2 parents 08e9f94 + 3936250 commit 92a1c34
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion mug-errorprone/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java_library(
"@maven//:com_google_errorprone_error_prone_check_api",
"@maven//:com_google_guava_guava",
"//mug:base",
"//mug:time",
"//mug:time_lib",
"//mug-guava",
],
)
Expand Down
11 changes: 9 additions & 2 deletions mug/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ java_library(
)

java_library(
name = "time",
visibility = ["//visibility:public"],
name = "time_lib",
visibility = ["//mug-errorprone:__pkg__"],
srcs = glob(["src/main/java/com/google/mu/time/*.java"]),
deps = [":base"],
)

java_library(
name = "time",
visibility = ["//visibility:public"],
exports = [":time_lib"],
exported_plugins = ["//mug-errorprone:plugin"],
)

Expand Down Expand Up @@ -70,6 +76,7 @@ junit_tests(
":format",
":graph",
":tests",
":time",
"@maven//:com_google_guava_guava",
"@maven//:com_google_guava_guava_testlib",
"@maven//:com_google_truth_truth",
Expand Down
5 changes: 0 additions & 5 deletions mug/src/main/java/com/google/mu/time/DateTimeFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,12 @@ static DateTimeFormatter inferFromExample(String example) {
example,
placeholder -> {
placeholderCount.incrementAndGet();
System.out.println("translating placeholder " + placeholder);
return inferDateTimePattern(placeholder.skip(1, 1).toString());
});
try {
if (placeholderCount.get() > 0) {
// There is at least 1 placeholder. The input isn't a pure datetime "example".
// So we can't validate using parse().
System.out.println("after placeholder translated: " + pattern);
return DateTimeFormatter.ofPattern(pattern);
}
pattern = inferDateTimePattern(example, signature);
Expand Down Expand Up @@ -307,18 +305,15 @@ private static List<?> forExample(String example) {
.map(
match -> {
if (DIGIT.matchesAnyOf(match)) {
System.out.println("got number token " + match);
return match.length(); // the number of digits in the example matter
}
String name = match.toString();
if (PUNCTUATION.matchesAnyOf(name)) {
System.out.println("got punctuation token " + match);
// A punctuation that must be matched literally.
// But + and - are interchangeable (as example) in timezone spec.
return name.replace('+', '-');
}
if (DELIMITER.matchesAnyOf(name)) {
System.out.println("got delimiter " + match);
// whitespaces are treated as is and will be ignored from prefix pattern matching
return name;
}
Expand Down
18 changes: 4 additions & 14 deletions mug/src/test/java/com/google/mu/time/DateTimeFormatsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,14 @@ public void rfc1123Example() {
}

@Test
@SuppressWarnings("DateTimeExampleStringCheck")
public void monthOfYear_notSupported() {
assertThrows(
IllegalArgumentException.class, () -> formatOf("Dec 31, 2023 12:00:00 America/New_York"));
}

@Test
@SuppressWarnings("DateTimeExampleStringCheck")
public void mmddyyyy_notSupported() {
assertThrows(IllegalArgumentException.class, () -> formatOf("10/20/2023 10:10:10"));
}
Expand Down Expand Up @@ -479,10 +481,8 @@ public void timeZoneMixedIn_twoLetterZoneNameAbbreviation() {
public void timeZoneMixedIn_fourLetterZoneNameAbbreviation() {
DateTimeFormatter formatter = DateTimeFormats.inferFromExample("M dd yyyy HH:mm:ss{CAST}");
ZonedDateTime dateTime = ZonedDateTime.parse("1 10 2023 10:20:30CAST", formatter);
assertThat(dateTime)
.isEqualTo(
ZonedDateTime.of(
LocalDateTime.of(2023, 1, 10, 10, 20, 30, 0), ZoneId.of("Africa/Maputo")));
assertThat(dateTime.toLocalDateTime())
.isEqualTo(LocalDateTime.of(2023, 1, 10, 10, 20, 30, 0));
}

@Test
Expand Down Expand Up @@ -605,16 +605,6 @@ private static ComparableSubject<LocalTime> assertLocalTime(
return assertThat(time);
}

private static ComparableSubject<ZonedDateTime> assertZonedDateTime(
@CompileTimeConstant String example, String equivalentPattern) {
String pattern = DateTimeFormats.inferDateTimePattern(example);
assertThat(pattern).isEqualTo(equivalentPattern);
DateTimeFormatter formatter = formatOf(example);
ZonedDateTime datetime = ZonedDateTime.parse(example, formatter);
assertThat(datetime.format(formatter)).isEqualTo(example);
return assertThat(datetime);
}

private static DateTimeFormatter getFormatterByName(String formatterName) throws Exception {
try {
return (DateTimeFormatter) DateTimeFormatter.class.getDeclaredField(formatterName).get(null);
Expand Down

0 comments on commit 92a1c34

Please sign in to comment.