Skip to content

Commit

Permalink
fix: fieldvalue gettimestamp (#279)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Praful Makani committed Apr 16, 2020
1 parent f7e89ab commit 4478a23
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Expand Up @@ -182,7 +182,9 @@ public boolean getBooleanValue() {
public long getTimestampValue() {
// timestamps are encoded in the format 1408452095.22 where the integer part is seconds since
// epoch (e.g. 1408452095.22 == 2014-08-19 07:41:35.220 -05:00)
return new Double(Double.valueOf(getStringValue()) * MICROSECONDS).longValue();
BigDecimal secondsWithMicro = new BigDecimal(getStringValue());
BigDecimal scaled = secondsWithMicro.scaleByPowerOfTen(6);
return scaled.longValue();
}

/**
Expand Down
Expand Up @@ -89,6 +89,14 @@ public void testFromPb() {
assertEquals(FieldValue.fromPb(TIMESTAMP_FIELD), value.getRepeatedValue().get(1));
}

@Test
public void testTimestamp() {
FieldValue fieldValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "-1.9954383398377106E10");
long received = fieldValue.getTimestampValue();
long expected = -19954383398377106L;
assertEquals(expected, received);
}

@Test
public void testEquals() {
FieldValue booleanValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "false");
Expand Down

0 comments on commit 4478a23

Please sign in to comment.