Skip to content

Commit f9b82b0

Browse files
olavloitesduskis
authored andcommitted
---
yaml --- r: 30957 b: refs/heads/autosynth-bigquerystorage c: 6846268 h: refs/heads/master i: 30955: 08c9404
1 parent 088058b commit f9b82b0

3 files changed

Lines changed: 88 additions & 15 deletions

File tree

  • branches/autosynth-bigquerystorage/google-cloud-clients/google-cloud-core/src

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ refs/tags/v0.69.0: 78f67a29e8b9c46ba01de566a2eae0fd1c03edea
123123
refs/heads/autosynth-asset: bdb45634a0fe8f7a510692b56b31f5312e25f453
124124
refs/heads/autosynth-automl: 22f9dd5b6f5df8dbfa7da0126864d565229519b2
125125
refs/heads/autosynth-bigquerydatatransfer: 71b6b6d2a992d05999b53e6e130c4fc1c8c8b4d7
126-
refs/heads/autosynth-bigquerystorage: a1ca85526993c3907f25a6f2bbf9a407f06133ac
126+
refs/heads/autosynth-bigquerystorage: 6846268d30318f69abbfa20437bf4933f56b1a46
127127
refs/heads/autosynth-bigtable: 9e5429f45cf9face9fed585d0233534993e36b58
128128
refs/heads/autosynth-bigtable-admin: 6379a2bc712f2736c83de0e009b4d26da4fa82ca
129129
refs/heads/autosynth-containeranalysis: af5b804492292b43372c9fe00386696136ccae89

branches/autosynth-bigquerystorage/google-cloud-clients/google-cloud-core/src/main/java/com/google/cloud/Date.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import java.io.Serializable;
2222
import java.util.Calendar;
2323
import java.util.Objects;
24-
import java.util.regex.Matcher;
25-
import java.util.regex.Pattern;
2624

2725
/** Represents a Date without time, such as 2017-03-17. Date is timezone independent. */
2826
@BetaApi("This is going to be replaced with LocalDate from threetenbp")
2927
public final class Date implements Comparable<Date>, Serializable {
3028

3129
// Date format "yyyy-mm-dd"
32-
private static final Pattern FORMAT_REGEXP = Pattern.compile("(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)");
3330
private static final long serialVersionUID = 8067099123096783929L;
3431
private final int year;
3532
private final int month;
@@ -57,14 +54,19 @@ public static Date fromYearMonthDay(int year, int month, int dayOfMonth) {
5754

5855
/** @param date Data in RFC 3339 date format (yyyy-mm-dd). */
5956
public static Date parseDate(String date) {
60-
Matcher matcher = FORMAT_REGEXP.matcher(date);
61-
if (!matcher.matches()) {
62-
throw new IllegalArgumentException("Invalid date: " + date);
57+
Preconditions.checkNotNull(date);
58+
final String invalidDate = "Invalid date: " + date;
59+
Preconditions.checkArgument(date.length() == 10, invalidDate);
60+
Preconditions.checkArgument(date.charAt(4) == '-', invalidDate);
61+
Preconditions.checkArgument(date.charAt(7) == '-', invalidDate);
62+
try {
63+
int year = Integer.parseInt(date.substring(0, 4));
64+
int month = Integer.parseInt(date.substring(5, 7));
65+
int dayOfMonth = Integer.parseInt(date.substring(8, 10));
66+
return new Date(year, month, dayOfMonth);
67+
} catch (NumberFormatException e) {
68+
throw new IllegalArgumentException(invalidDate, e);
6369
}
64-
int year = Integer.parseInt(matcher.group(1));
65-
int month = Integer.parseInt(matcher.group(2));
66-
int dayOfMonth = Integer.parseInt(matcher.group(3));
67-
return new Date(year, month, dayOfMonth);
6870
}
6971

7072
/**

branches/autosynth-bigquerystorage/google-cloud-clients/google-cloud-core/src/test/java/com/google/cloud/DateTest.java

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static org.junit.Assert.fail;
2122

2223
import com.google.common.testing.EqualsTester;
2324
import java.text.ParseException;
@@ -34,10 +35,80 @@ public class DateTest {
3435

3536
@Test
3637
public void parseDate() {
37-
Date date = Date.parseDate("2016-09-18");
38-
assertThat(date.getYear()).isEqualTo(2016);
39-
assertThat(date.getMonth()).isEqualTo(9);
40-
assertThat(date.getDayOfMonth()).isEqualTo(18);
38+
verifyDate("2016-09-18", 2016, 9, 18);
39+
verifyDate("2000-01-01", 2000, 1, 1);
40+
verifyDate("9999-12-31", 9999, 12, 31);
41+
verifyDate("0001-01-01", 1, 1, 1);
42+
verifyDate("2000-02-29", 2000, 2, 29); // This is a valid leap year.
43+
verifyDate("1900-02-29", 1900, 2, 29); // This is NOT a valid leap year.
44+
verifyDate("2001-02-29", 2001, 2, 29); // Also not a valid leap year.
45+
verifyDate("2000-04-31", 2000, 4, 31); // Not a valid date.
46+
}
47+
48+
private void verifyDate(String input, int year, int month, int day) {
49+
Date date = Date.parseDate(input);
50+
assertThat(date.getYear()).isEqualTo(year);
51+
assertThat(date.getMonth()).isEqualTo(month);
52+
assertThat(date.getDayOfMonth()).isEqualTo(day);
53+
}
54+
55+
@Test
56+
public void parseInvalidDates() {
57+
parseInvalidDate("2016/09/18");
58+
parseInvalidDate("2016 09 18");
59+
parseInvalidDate("2016-9-18");
60+
parseInvalidDate("2016-09-18T10:00");
61+
parseInvalidDate("");
62+
parseInvalidDate("test");
63+
parseInvalidDate("aaaa-bb-cc");
64+
parseInvalidDate("aaaa-01-01");
65+
parseInvalidDate("2019-bb-01");
66+
parseInvalidDate("2019-01-cc");
67+
parseInvalidMonth("2000-13-01");
68+
parseInvalidMonth("2000-00-01");
69+
parseInvalidDay("2000-12-32");
70+
parseInvalidDay("2000-12-00");
71+
parseInvalidDate("10000-01-01");
72+
parseInvalidYear("0000-01-01");
73+
parseInvalidYear("-001-01-01");
74+
parseInvalidMonth("0001--1-01");
75+
parseInvalidDay("0001-01--1");
76+
}
77+
78+
private void parseInvalidDate(String input) {
79+
try {
80+
Date.parseDate(input);
81+
fail("Expected exception");
82+
} catch (IllegalArgumentException e) {
83+
assertThat(e.getMessage()).contains("Invalid date");
84+
}
85+
}
86+
87+
private void parseInvalidYear(String input) {
88+
try {
89+
Date.parseDate(input);
90+
fail("Expected exception");
91+
} catch (IllegalArgumentException e) {
92+
assertThat(e.getMessage()).contains("Invalid year");
93+
}
94+
}
95+
96+
private void parseInvalidMonth(String input) {
97+
try {
98+
Date.parseDate(input);
99+
fail("Expected exception");
100+
} catch (IllegalArgumentException e) {
101+
assertThat(e.getMessage()).contains("Invalid month");
102+
}
103+
}
104+
105+
private void parseInvalidDay(String input) {
106+
try {
107+
Date.parseDate(input);
108+
fail("Expected exception");
109+
} catch (IllegalArgumentException e) {
110+
assertThat(e.getMessage()).contains("Invalid day");
111+
}
41112
}
42113

43114
@Test

0 commit comments

Comments
 (0)