Skip to content

Commit 38e3a53

Browse files
authored
fix millisecond parsing errors (#895)
* fix millisecond parsing errors * nits
1 parent 832abc9 commit 38e3a53

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

google-http-client/src/main/java/com/google/api/client/util/DateTime.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public int hashCode() {
273273
* exception is thrown if {@code str} doesn't match {@code RFC3339_REGEX} or if it contains a
274274
* time zone shift but no time.
275275
*/
276-
public static DateTime parseRfc3339(String str) throws NumberFormatException {
276+
public static DateTime parseRfc3339(String str) {
277277
return parseRfc3339WithNanoSeconds(str).toDateTime();
278278
}
279279

@@ -285,9 +285,9 @@ public static DateTime parseRfc3339(String str) throws NumberFormatException {
285285
* exception is thrown if {@code str} doesn't match {@code RFC3339_REGEX} or if it contains a
286286
* time zone shift but no time.
287287
*/
288-
public static SecondsAndNanos parseRfc3339ToSecondsAndNanos(String str)
289-
throws IllegalArgumentException {
290-
return parseRfc3339WithNanoSeconds(str).toSecondsAndNanos();
288+
public static SecondsAndNanos parseRfc3339ToSecondsAndNanos(String str) {
289+
Rfc3339ParseResult time = parseRfc3339WithNanoSeconds(str);
290+
return time.toSecondsAndNanos();
291291
}
292292

293293
/** A timestamp represented as the number of seconds and nanoseconds since Epoch. */
@@ -335,7 +335,7 @@ public String toString() {
335335
}
336336
}
337337

338-
/** Result of parsing a Rfc3339 string. */
338+
/** Result of parsing an RFC 3339 string. */
339339
private static class Rfc3339ParseResult implements Serializable {
340340
private final long seconds;
341341
private final int nanos;
@@ -400,6 +400,7 @@ private static Rfc3339ParseResult parseRfc3339WithNanoSeconds(String str)
400400
}
401401
}
402402
Calendar dateTime = new GregorianCalendar(GMT);
403+
dateTime.clear();
403404
dateTime.set(year, month, day, hourOfDay, minute, second);
404405
long value = dateTime.getTimeInMillis();
405406

google-http-client/src/test/java/com/google/api/client/util/DateTimeTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ public class DateTimeTest extends TestCase {
2828

2929
private TimeZone originalTimeZone;
3030

31-
public DateTimeTest() {}
32-
33-
public DateTimeTest(String testName) {
34-
super(testName);
35-
}
36-
3731
@Override
3832
protected void setUp() throws Exception {
3933
originalTimeZone = TimeZone.getDefault();
@@ -225,8 +219,18 @@ public void testParseRfc3339ToSecondsAndNanos() {
225219
assertParsedRfc3339(
226220
"2018-03-01T10:11:12.1000Z", SecondsAndNanos.ofSecondsAndNanos(1519899072L, 100000000));
227221
}
222+
223+
public void testEpoch() {
224+
assertParsedRfc3339(
225+
"1970-01-01T00:00:00.000Z", SecondsAndNanos.ofSecondsAndNanos(0, 0));
226+
}
227+
228+
public void testOneSecondBeforeEpoch() {
229+
assertParsedRfc3339(
230+
"1969-12-31T23:59:59.000Z", SecondsAndNanos.ofSecondsAndNanos(-1, 0));
231+
}
228232

229-
private void assertParsedRfc3339(String input, SecondsAndNanos expected) {
233+
private static void assertParsedRfc3339(String input, SecondsAndNanos expected) {
230234
SecondsAndNanos actual = DateTime.parseRfc3339ToSecondsAndNanos(input);
231235
assertEquals(
232236
"Seconds for " + input + " do not match", expected.getSeconds(), actual.getSeconds());
@@ -249,7 +253,7 @@ public void testParseAndFormatRfc3339() {
249253
assertEquals(expected, output);
250254
}
251255

252-
private void expectExceptionForParseRfc3339(String input) {
256+
private static void expectExceptionForParseRfc3339(String input) {
253257
try {
254258
DateTime.parseRfc3339(input);
255259
fail("expected NumberFormatException");

0 commit comments

Comments
 (0)