Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.google.api.services.bigquery.model.QueryParameterType;
import com.google.auto.value.AutoValue;
import com.google.cloud.Timestamp;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -332,7 +333,9 @@ private static <T> String valueToStringOrNull(T value, StandardSQLTypeName type)
throw new IllegalArgumentException("Cannot convert ARRAY to String value");
case TIMESTAMP:
if (value instanceof Long) {
return timestampFormatter.format(Instant.ofEpochMilli(((Long) value) / 1000));
Timestamp timestamp = Timestamp.ofTimeMicroseconds((Long) value);
return timestampFormatter.format(
Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()));
} else if (value instanceof String) {
// verify that the String is in the right format
checkFormat(value, timestampValidator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,44 @@
package com.google.cloud.bigquery;

import static com.google.common.truth.Truth.assertThat;
import static org.threeten.bp.temporal.ChronoField.HOUR_OF_DAY;
import static org.threeten.bp.temporal.ChronoField.MINUTE_OF_HOUR;
import static org.threeten.bp.temporal.ChronoField.NANO_OF_SECOND;
import static org.threeten.bp.temporal.ChronoField.SECOND_OF_MINUTE;

import com.google.api.services.bigquery.model.QueryParameterType;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.threeten.bp.Instant;
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.format.DateTimeFormatter;
import org.threeten.bp.format.DateTimeFormatterBuilder;
import org.threeten.bp.jdk8.Jdk8Methods;

public class QueryParameterValueTest {

private static final DateTimeFormatter TIMESTAMPFORMATTER =
new DateTimeFormatterBuilder()
.parseLenient()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 6, 9, true)
.optionalStart()
.appendOffset("+HHMM", "+00:00")
.optionalEnd()
.toFormatter()
.withZone(ZoneOffset.UTC);

@Test
public void testBool() {
QueryParameterValue value = QueryParameterValue.bool(true);
Expand Down Expand Up @@ -182,6 +210,18 @@ public void testTimestampFromLong() {
assertThat(value.getArrayValues()).isNull();
}

@Test
public void testTimestampWithFormatter() {
long timestampInMicroseconds = 1571068536842L * 1000 + 123;
long microseconds = 1_000_000;
long secs = Jdk8Methods.floorDiv(timestampInMicroseconds, microseconds);
int nano = (int) Jdk8Methods.floorMod(timestampInMicroseconds, microseconds) * 1000;
Instant instant = Instant.ofEpochSecond(secs, nano);
String expected = TIMESTAMPFORMATTER.format(instant);
assertThat(expected)
.isEqualTo(QueryParameterValue.timestamp(timestampInMicroseconds).getValue());
}

@Test
public void testTimestamp() {
QueryParameterValue value = QueryParameterValue.timestamp("2014-08-19 12:41:35.220000+00:00");
Expand Down