-
Notifications
You must be signed in to change notification settings - Fork 448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for nanosecond precision when parsing rfc3339 strings #752
Changes from 3 commits
15e808d
5f37367
b53e75f
e8cf84a
1c058b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,11 @@ | |
|
||
package com.google.api.client.util; | ||
|
||
import com.google.api.client.util.DateTime.SecondsAndNanos; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.TimeZone; | ||
import junit.framework.TestCase; | ||
|
||
|
@@ -142,6 +146,89 @@ public void testParseRfc3339() { | |
assertEquals( | ||
DateTime.parseRfc3339("2007-06-01t18:50:00-04:00").getValue(), | ||
DateTime.parseRfc3339("2007-06-01t22:50:00Z").getValue()); // from Section 4.2 Local Offsets | ||
|
||
// Test truncating beyond millisecond precision. | ||
assertEquals( | ||
DateTime.parseRfc3339( | ||
"2018-12-31T23:59:59.999999999Z"), // This value would be rounded up prior to version | ||
// 1.30.2 | ||
DateTime.parseRfc3339("2018-12-31T23:59:59.999Z")); | ||
assertEquals( | ||
DateTime.parseRfc3339( | ||
"2018-12-31T23:59:59.9999Z"), // This value would be truncated prior to version 1.30.2 | ||
DateTime.parseRfc3339("2018-12-31T23:59:59.999Z")); | ||
} | ||
|
||
/** | ||
* The following test values have been generated and verified using the {@link DateTimeFormatter} | ||
* in Java 8. | ||
* | ||
* <pre> | ||
* Timestamp | Seconds | Nanos | ||
* 2018-03-01T10:11:12.999Z | 1519899072 | 999000000 | ||
* 2018-10-28T02:00:00+02:00 | 1540684800 | 0 | ||
* 2018-10-28T03:00:00+01:00 | 1540692000 | 0 | ||
* 2018-01-01T00:00:00.000000001Z | 1514764800 | 1 | ||
* 2018-10-28T02:00:00Z | 1540692000 | 0 | ||
* 2018-12-31T23:59:59.999999999Z | 1546300799 | 999999999 | ||
* 2018-03-01T10:11:12.9999Z | 1519899072 | 999900000 | ||
* 2018-03-01T10:11:12.000000001Z | 1519899072 | 1 | ||
* 2018-03-01T10:11:12.100000000Z | 1519899072 | 100000000 | ||
* 2018-03-01T10:11:12.100000001Z | 1519899072 | 100000001 | ||
* 2018-03-01T10:11:12-10:00 | 1519935072 | 0 | ||
* 2018-03-01T10:11:12.999999999Z | 1519899072 | 999999999 | ||
* 2018-03-01T10:11:12-12:00 | 1519942272 | 0 | ||
* 2018-10-28T03:00:00Z | 1540695600 | 0 | ||
* 2018-10-28T02:30:00Z | 1540693800 | 0 | ||
* 2018-03-01T10:11:12.123Z | 1519899072 | 123000000 | ||
* 2018-10-28T02:30:00+02:00 | 1540686600 | 0 | ||
* 2018-03-01T10:11:12.123456789Z | 1519899072 | 123456789 | ||
* 2018-03-01T10:11:12.1000Z | 1519899072 | 100000000 | ||
* </pre> | ||
*/ | ||
public void testParseRfc3339ToSecondsAndNanos() { | ||
Map<String, SecondsAndNanos> map = new HashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: it seems odd to collect these into a map to just iterate over them. We could implement a helper test assertion like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point. I've changed the test case accordingly. |
||
map.put("2018-03-01T10:11:12.999Z", SecondsAndNanos.ofSecondsAndNanos(1519899072L, 999000000)); | ||
map.put("2018-10-28T02:00:00+02:00", SecondsAndNanos.ofSecondsAndNanos(1540684800L, 0)); | ||
map.put("2018-10-28T03:00:00+01:00", SecondsAndNanos.ofSecondsAndNanos(1540692000L, 0)); | ||
map.put("2018-01-01T00:00:00.000000001Z", SecondsAndNanos.ofSecondsAndNanos(1514764800L, 1)); | ||
map.put("2018-10-28T02:00:00Z", SecondsAndNanos.ofSecondsAndNanos(1540692000L, 0)); | ||
map.put( | ||
"2018-12-31T23:59:59.999999999Z", | ||
SecondsAndNanos.ofSecondsAndNanos(1546300799L, 999999999)); | ||
map.put("2018-03-01T10:11:12.9999Z", SecondsAndNanos.ofSecondsAndNanos(1519899072L, 999900000)); | ||
map.put("2018-03-01T10:11:12.000000001Z", SecondsAndNanos.ofSecondsAndNanos(1519899072L, 1)); | ||
map.put( | ||
"2018-03-01T10:11:12.100000000Z", | ||
SecondsAndNanos.ofSecondsAndNanos(1519899072L, 100000000)); | ||
map.put( | ||
"2018-03-01T10:11:12.100000001Z", | ||
SecondsAndNanos.ofSecondsAndNanos(1519899072L, 100000001)); | ||
map.put("2018-03-01T10:11:12-10:00", SecondsAndNanos.ofSecondsAndNanos(1519935072L, 0)); | ||
map.put( | ||
"2018-03-01T10:11:12.999999999Z", | ||
SecondsAndNanos.ofSecondsAndNanos(1519899072L, 999999999)); | ||
map.put("2018-03-01T10:11:12-12:00", SecondsAndNanos.ofSecondsAndNanos(1519942272L, 0)); | ||
map.put("2018-10-28T03:00:00Z", SecondsAndNanos.ofSecondsAndNanos(1540695600L, 0)); | ||
map.put("2018-10-28T02:30:00Z", SecondsAndNanos.ofSecondsAndNanos(1540693800L, 0)); | ||
map.put("2018-03-01T10:11:12.123Z", SecondsAndNanos.ofSecondsAndNanos(1519899072L, 123000000)); | ||
map.put("2018-10-28T02:30:00+02:00", SecondsAndNanos.ofSecondsAndNanos(1540686600L, 0)); | ||
map.put( | ||
"2018-03-01T10:11:12.123456789Z", | ||
SecondsAndNanos.ofSecondsAndNanos(1519899072L, 123456789)); | ||
map.put("2018-03-01T10:11:12.1000Z", SecondsAndNanos.ofSecondsAndNanos(1519899072L, 100000000)); | ||
|
||
for (Entry<String, SecondsAndNanos> entry : map.entrySet()) { | ||
SecondsAndNanos gTimestamp = DateTime.parseRfc3339ToSecondsAndNanos(entry.getKey()); | ||
assertEquals( | ||
"Seconds for " + entry + " do not match", | ||
gTimestamp.getSeconds(), | ||
entry.getValue().getSeconds()); | ||
assertEquals( | ||
"Nanos for " + entry + " do not match", | ||
gTimestamp.getNanos(), | ||
entry.getValue().getNanos()); | ||
} | ||
} | ||
|
||
public void testParseAndFormatRfc3339() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what our plans are for Java 8, but if we could use java.time I think we could avoid introducing this class and associated public API. @chingor13 has any final decision been made on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This library is very heavily used on android, so even if we move google-cloud-java to Java 8, we'd likely avoid using new features for a long time. java.time was introduced in Android at API level 26 (Oreo).