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
39 changes: 38 additions & 1 deletion google-cloud-core/src/main/java/com/google/cloud/Timestamp.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.google.common.base.Strings;
import com.google.protobuf.util.Timestamps;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
Expand All @@ -34,7 +36,10 @@
*
* <p>{@code Timestamp} instances are immutable.
*/
public final class Timestamp implements Comparable<Timestamp> {
public final class Timestamp implements Comparable<Timestamp>, Serializable {

private static final long serialVersionUID = 5152143600571559844L;

/** The smallest legal timestamp ("0001-01-01T00:00:00Z"). */
public static final Timestamp MIN_VALUE = new Timestamp(-62135596800L, 0);

Expand Down Expand Up @@ -73,6 +78,38 @@ public static Timestamp ofTimeSecondsAndNanos(long seconds, int nanos) {
return new Timestamp(seconds, nanos);
}

/**
* Creates an instance representing the value of {@code microseconds}.
*
* @throws IllegalArgumentException if the timestamp is outside the representable range
*/
public static Timestamp ofTimeMicroseconds(long microseconds) {
long seconds = TimeUnit.MICROSECONDS.toSeconds(microseconds);
int nanos = (int) TimeUnit.MICROSECONDS.toNanos(
microseconds - TimeUnit.SECONDS.toMicros(seconds));
checkArgument(
Timestamps.isValid(seconds, nanos), "timestamp out of range: %s, %s", seconds, nanos);
return new Timestamp(seconds, nanos);
}

/**
* Creates an instance representing the value of {@code Date}.
*
* @throws IllegalArgumentException if the timestamp is outside the representable range
*/
public static Timestamp of(Date date) {
return ofTimeMicroseconds(TimeUnit.MILLISECONDS.toMicros(date.getTime()));
}


/**
* Creates an instance with current time.
*/
public static Timestamp now() {
java.sql.Timestamp date = new java.sql.Timestamp(System.currentTimeMillis());
return of(date);
}

/**
* Creates an instance representing the value of {@code timestamp}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
public class TimestampTest {
private static final String TEST_TIME_ISO = "2015-10-12T15:14:54Z";
private static final long TEST_TIME_SECONDS = 1444662894L;
private static final long TEST_TIME_MICROSECONDS = 10000100L;

@Rule public ExpectedException expectedException = ExpectedException.none();

@Test
Expand All @@ -54,6 +56,13 @@ public void maxValue() {
assertThat(Timestamp.MAX_VALUE.getNanos()).isEqualTo(999999999);
}

@Test
public void ofMicroseconds() {
Timestamp timestamp = Timestamp.ofTimeMicroseconds(TEST_TIME_MICROSECONDS);
assertThat(timestamp.getSeconds()).isEqualTo(TEST_TIME_MICROSECONDS / 1000000L);
assertThat(timestamp.getNanos()).isEqualTo(TEST_TIME_MICROSECONDS % 1000000L * 1000);
}

@Test
public void toFromSqlTimestamp() {
long seconds = TEST_TIME_SECONDS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.google.cloud.datastore.BlobValue.of;
import static com.google.cloud.datastore.BooleanValue.of;
import static com.google.cloud.datastore.DateTimeValue.of;
import static com.google.cloud.datastore.TimestampValue.of;
import static com.google.cloud.datastore.DoubleValue.of;
import static com.google.cloud.datastore.EntityValue.of;
import static com.google.cloud.datastore.KeyValue.of;
Expand All @@ -28,6 +28,7 @@
import static com.google.cloud.datastore.NullValue.of;
import static com.google.cloud.datastore.StringValue.of;

import com.google.cloud.Timestamp;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Maps;
Expand Down Expand Up @@ -266,29 +267,29 @@ public B set(String name, boolean first, boolean second, boolean... others) {
}

/**
* Sets a property of type {@link DateTimeValue}.
* Sets a property of type {@link TimestampValue}.
*
* @param name name of the property
* @param value value associated with the property
*/
public B set(String name, DateTime value) {
public B set(String name, Timestamp value) {
properties.put(name, of(value));
return self();
}

/**
* Sets a list property containing elements of type {@link DateTimeValue}.
* Sets a list property containing elements of type {@link TimestampValue}.
*
* @param name name of the property
* @param first the first {@link DateTime} in the list
* @param second the second {@link DateTime} in the list
* @param others other {@link DateTime}s in the list
* @param first the first {@link Timestamp} in the list
* @param second the second {@link Timestamp} in the list
* @param others other {@link Timestamp}s in the list
*/
public B set(String name, DateTime first, DateTime second, DateTime... others) {
List<DateTimeValue> values = new LinkedList<>();
public B set(String name, Timestamp first, Timestamp second, Timestamp... others) {
List<TimestampValue> values = new LinkedList<>();
values.add(of(first));
values.add(of(second));
for (DateTime other : others) {
for (Timestamp other : others) {
values.add(of(other));
}
properties.put(name, of(values));
Expand Down Expand Up @@ -579,14 +580,14 @@ public boolean getBoolean(String name) {
}

/**
* Returns the property value as a DateTime.
* Returns the property value as a Timestamp.
*
* @throws DatastoreException if not such property
* @throws ClassCastException if value is not a DateTime
* @throws ClassCastException if value is not a Timestamp
*/
@SuppressWarnings("unchecked")
public DateTime getDateTime(String name) {
return ((Value<DateTime>) getValue(name)).get();
public Timestamp getTimestamp(String name) {
return ((Value<Timestamp>) getValue(name)).get();
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.cloud.datastore.Validator.validateNamespace;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.Timestamp;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -220,8 +221,8 @@ public Builder<V> setBinding(String name, boolean... value) {
return this;
}

public Builder<V> setBinding(String name, DateTime... value) {
namedBindings.put(name, toBinding(DateTimeValue.MARSHALLER, Arrays.asList(value)));
public Builder<V> setBinding(String name, Timestamp... value) {
namedBindings.put(name, toBinding(TimestampValue.MARSHALLER, Arrays.asList(value)));
return this;
}

Expand Down Expand Up @@ -265,8 +266,8 @@ public Builder<V> addBinding(boolean... value) {
return this;
}

public Builder<V> addBinding(DateTime... value) {
positionalBindings.add(toBinding(DateTimeValue.MARSHALLER, Arrays.asList(value)));
public Builder<V> addBinding(Timestamp... value) {
positionalBindings.add(toBinding(TimestampValue.MARSHALLER, Arrays.asList(value)));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.datastore.v1.Value.ARRAY_VALUE_FIELD_NUMBER;

import com.google.cloud.Timestamp;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -137,12 +138,12 @@ public Builder addValue(boolean first, boolean... other) {
}

/**
* Adds the provided {@code DateTime} values to the {@code ListValue} builder.
* Adds the provided {@code Timestamp} values to the {@code ListValue} builder.
*/
public Builder addValue(DateTime first, DateTime... other) {
listBuilder.add(DateTimeValue.of(first));
for (DateTime value : other) {
listBuilder.add(DateTimeValue.of(value));
public Builder addValue(Timestamp first, Timestamp... other) {
listBuilder.add(TimestampValue.of(first));
for (Timestamp value : other) {
listBuilder.add(TimestampValue.of(value));
}
return this;
}
Expand Down Expand Up @@ -283,9 +284,9 @@ public static ListValue of(boolean first, boolean... other) {
}

/**
* Creates a {@code ListValue} object given a number of {@code DateTime} values.
* Creates a {@code ListValue} object given a number of {@code Timestamp} values.
*/
public static ListValue of(DateTime first, DateTime... other) {
public static ListValue of(Timestamp first, Timestamp... other) {
return newBuilder().addValue(first, other).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.datastore;

import com.google.cloud.Timestamp;
import com.google.protobuf.ByteString;

/**
Expand Down Expand Up @@ -53,12 +54,12 @@ public ProjectionEntity build() {

@SuppressWarnings({"unchecked", "deprecation"})
@Override
public DateTime getDateTime(String name) {
public Timestamp getTimestamp(String name) {
Value<?> value = getValue(name);
if (value.getMeaning() == 18 && value instanceof LongValue) {
return new DateTime(getLong(name));
return Timestamp.ofTimeMicroseconds(getLong(name));
}
return ((Value<DateTime>) value).get();
return ((Value<Timestamp>) value).get();
}

@SuppressWarnings({"unchecked", "deprecation"})
Expand Down
Loading