8000 Fix milliseconds bug in DateTime parseRfc3339. The fraction after the… · rasayana/google-http-java-client@57b3e74 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57b3e74

Browse files
emrekultursayejona86
authored andcommitted
Fix milliseconds bug in DateTime parseRfc3339. The fraction after the dot is not always 3
digits, in which case it must be multiplied by 10^n to get the correct milliseconds. Example1: 12:34.56 should have milliseconds=560, but it was returning only 56. Example2: 12:34.56789 should have milliseconds=567, but it was returning 56789. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=104921033
1 parent 926e04d commit 57b3e74

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ public static DateTime parseRfc3339(String str) throws NumberFormatException {
313313
second = Integer.parseInt(matcher.group(7)); // ss
314314
if (matcher.group(8) != null) { // contains .milliseconds?
315315
milliseconds = Integer.parseInt(matcher.group(8).substring(1)); // milliseconds
316+
// The number of digits after the dot may not be 3. Need to renormalize.
317+
int fractionDigits = matcher.group(8).substring(1).length() - 3;
318+
milliseconds = (int) ((float) milliseconds / Math.pow(10, fractionDigits));
316319
}
317320
}
318321
Calendar dateTime = new GregorianCalendar(GMT);

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void testParseRfc3339() {
120120
assertEquals(0, value.getValue() % 100);
121121
value = DateTime.parseRfc3339("1997-01-01T12:00:27.87+00:20");
122122
assertFalse(value.isDateOnly());
123-
assertEquals(87, value.getValue() % 1000); // check milliseconds
123+
assertEquals(870, value.getValue() % 1000); // check milliseconds
124124

125125
value = new DateTime("2007-06-01");
126126
assertTrue(value.isDateOnly());
@@ -138,6 +138,22 @@ public void testParseRfc3339() {
138138
DateTime.parseRfc3339("2007-06-01t22:50:00Z").getValue()); // from Section 4.2 Local Offsets
139139
}
140140

141+
public void testParseAndFormatRfc3339() {
142+
// .12 becomes .120
143+
String input = "1996-12-19T16:39:57.12-08:00";
144+
String expected = "1996-12-19T16:39:57.120-08:00";
145+
DateTime dt = DateTime.parseRfc3339(input);
146+
String output = dt.toStringRfc3339();
147+
assertEquals(expected, output);
148+
149+
// Truncated to milliseconds.
150+
input = "1996-12-19T16:39:57.123456789-08:00";
151+
expected = "1996-12-19T16:39:57.123-08:00";
152+
dt = DateTime.parseRfc3339(input);
153+
output = dt.toStringRfc3339();
154+
assertEquals(expected, output);
155+
}
156+
141157
private void expectExceptionForParseRfc3339(String input) {
142158
try {
143159
DateTime.parseRfc3339(input);

0 commit comments

Comments
 (0)
0