Skip to content

Commit 4478a23

Browse files
author
Praful Makani
authored
fix: fieldvalue gettimestamp (#279)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [X] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/java-bigquery/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [X] Appropriate docs were updated (if necessary) Fixes #16
1 parent f7e89ab commit 4478a23

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/FieldValue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ public boolean getBooleanValue() {
182182
public long getTimestampValue() {
183183
// timestamps are encoded in the format 1408452095.22 where the integer part is seconds since
184184
// epoch (e.g. 1408452095.22 == 2014-08-19 07:41:35.220 -05:00)
185-
return new Double(Double.valueOf(getStringValue()) * MICROSECONDS).longValue();
185+
BigDecimal secondsWithMicro = new BigDecimal(getStringValue());
186+
BigDecimal scaled = secondsWithMicro.scaleByPowerOfTen(6);
187+
return scaled.longValue();
186188
}
187189

188190
/**

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/FieldValueTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ public void testFromPb() {
8989
assertEquals(FieldValue.fromPb(TIMESTAMP_FIELD), value.getRepeatedValue().get(1));
9090
}
9191

92+
@Test
93+
public void testTimestamp() {
94+
FieldValue fieldValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "-1.9954383398377106E10");
95+
long received = fieldValue.getTimestampValue();
96+
long expected = -19954383398377106L;
97+
assertEquals(expected, received);
98+
}
99+
92100
@Test
93101
public void testEquals() {
94102
FieldValue booleanValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "false");

0 commit comments

Comments
 (0)