com.google.cloud.datastore.TimestampValue Java Examples

The following examples show how to use com.google.cloud.datastore.TimestampValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DateMapper.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  try {
    Timestamp ts = ((TimestampValue) input).get();
    long millis = TimeUnit.SECONDS.toMillis(ts.getSeconds())
        + TimeUnit.NANOSECONDS.toMillis(ts.getNanos());
    return new Date(millis);
  } catch (ClassCastException exp) {
    String pattern = "Expecting %s, but found %s";
    throw new MappingException(
        String.format(pattern, TimestampValue.class.getName(), input.getClass().getName()), exp);
  }
}
 
Example #2
Source File: ZonedDateTimeMapper.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  try {
    Timestamp ts = ((TimestampValue) input).get();
    long seconds = ts.getSeconds();
    int nanos = ts.getNanos();
    return ZonedDateTime.ofInstant(Instant.ofEpochSecond(seconds, nanos), ZoneId.systemDefault());
  } catch (ClassCastException exp) {
    String pattern = "Expecting %s, but found %s";
    throw new MappingException(
        String.format(pattern, TimestampValue.class.getName(), input.getClass().getName()), exp);
  }
}
 
Example #3
Source File: OffsetDateTimeMapper.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  try {
    Timestamp ts = ((TimestampValue) input).get();
    long seconds = ts.getSeconds();
    int nanos = ts.getNanos();
    return OffsetDateTime.ofInstant(Instant.ofEpochSecond(seconds, nanos),
        ZoneId.systemDefault());
  } catch (ClassCastException exp) {
    String pattern = "Expecting %s, but found %s";
    throw new MappingException(
        String.format(pattern, TimestampValue.class.getName(), input.getClass().getName()), exp);
  }
}
 
Example #4
Source File: CalendarMapper.java    From catatumbo with Apache License 2.0 6 votes vote down vote up
@Override
public Object toModel(Value<?> input) {
  if (input instanceof NullValue) {
    return null;
  }
  try {
    Timestamp ts = ((TimestampValue) input).get();
    long millis = TimeUnit.SECONDS.toMillis(ts.getSeconds())
        + TimeUnit.NANOSECONDS.toMillis(ts.getNanos());
    return new Calendar.Builder().setInstant(millis).build();
  } catch (ClassCastException exp) {
    String pattern = "Expecting %s, but found %s";
    throw new MappingException(
        String.format(pattern, TimestampValue.class.getName(), input.getClass().getName()), exp);
  }
}
 
Example #5
Source File: GoogleJobStore.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> getProperties(Entity entity)
    throws IOException, ClassNotFoundException {
  if (entity == null) {
    return null;
  }
  ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>();
  for (String property : entity.getNames()) {
    // builder.put(property, entity.getValue(property));
    if (entity.getValue(property) instanceof StringValue) {
      builder.put(property, (String) entity.getString(property));
    } else if (entity.getValue(property) instanceof LongValue) {
      // This conversion is safe because of integer to long conversion above
      builder.put(property, new Long(entity.getLong(property)).intValue());
    } else if (entity.getValue(property) instanceof DoubleValue) {
      builder.put(property, (Double) entity.getDouble(property));
    } else if (entity.getValue(property) instanceof BooleanValue) {
      builder.put(property, (Boolean) entity.getBoolean(property));
    } else if (entity.getValue(property) instanceof TimestampValue) {
      builder.put(property, (Timestamp) entity.getTimestamp(property));
    } else {
      Blob blob = entity.getBlob(property);
      Object obj = null;
      try (ObjectInputStream in = new ObjectInputStream(blob.asInputStream())) {
        obj = in.readObject();
      }
      builder.put(property, obj); // BlobValue
    }
  }

  return builder.build();
}
 
Example #6
Source File: DateMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  return TimestampValue.newBuilder(Timestamp.of((Date) input));
}
 
Example #7
Source File: ZonedDateTimeMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  ZonedDateTime zonedDateTime = (ZonedDateTime) input;
  long seconds = zonedDateTime.toEpochSecond();
  int nanos = zonedDateTime.getNano();
  long microseconds = TimeUnit.SECONDS.toMicros(seconds) + TimeUnit.NANOSECONDS.toMicros(nanos);
  return TimestampValue.newBuilder(Timestamp.ofTimeMicroseconds(microseconds));
}
 
Example #8
Source File: OffsetDateTimeMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  OffsetDateTime offsetDateTime = (OffsetDateTime) input;
  long seconds = offsetDateTime.toEpochSecond();
  int nanos = offsetDateTime.getNano();
  long microseconds = TimeUnit.SECONDS.toMicros(seconds) + TimeUnit.NANOSECONDS.toMicros(nanos);
  return TimestampValue.newBuilder(Timestamp.ofTimeMicroseconds(microseconds));
}
 
Example #9
Source File: CalendarMapper.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Override
public ValueBuilder<?, ?, ?> toDatastore(Object input) {
  if (input == null) {
    return NullValue.newBuilder();
  }
  Calendar calendar = (Calendar) input;
  return TimestampValue.newBuilder(Timestamp.of(calendar.getTime()));
}
 
Example #10
Source File: ZonedDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToDatastore_Now() {
  ZonedDateTimeMapper mapper = new ZonedDateTimeMapper();
  ZonedDateTime now = ZonedDateTime.now();
  Timestamp ts = ((TimestampValue) mapper.toDatastore(now).build()).get();
  assertEquals(now.toEpochSecond(), ts.getSeconds());
  assertEquals(now.getNano(), ts.getNanos());
}
 
Example #11
Source File: ZonedDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToDatastorel_Nanos() {
  ZonedDateTime input = ZonedDateTime.now().withNano(999999999);
  ZonedDateTimeMapper mapper = new ZonedDateTimeMapper();
  Timestamp ts = ((TimestampValue) mapper.toDatastore(input).build()).get();
  assertEquals(ts.getSeconds(), input.toEpochSecond());
  assertEquals(TimeUnit.NANOSECONDS.toMicros(input.getNano()),
      TimeUnit.NANOSECONDS.toMicros(ts.getNanos()));
}
 
Example #12
Source File: ZonedDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_Now() {
  Timestamp now = Timestamp.now();
  TimestampValue v = TimestampValue.newBuilder(now).build();
  ZonedDateTimeMapper mapper = new ZonedDateTimeMapper();
  ZonedDateTime output = (ZonedDateTime) mapper.toModel(v);
  assertEquals(now.getSeconds(), output.toEpochSecond());
  assertEquals(now.getNanos(), output.getNano());
}
 
Example #13
Source File: OffsetDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToDatastore_Now() {
  OffsetDateTimeMapper mapper = new OffsetDateTimeMapper();
  OffsetDateTime now = OffsetDateTime.now();
  Timestamp ts = ((TimestampValue) mapper.toDatastore(now).build()).get();
  assertEquals(now.toEpochSecond(), ts.getSeconds());
  assertEquals(now.getNano(), ts.getNanos());
}
 
Example #14
Source File: OffsetDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToDatastorel_Nanos() {
  OffsetDateTime input = OffsetDateTime.now().withNano(999999999);
  OffsetDateTimeMapper mapper = new OffsetDateTimeMapper();
  Timestamp ts = ((TimestampValue) mapper.toDatastore(input).build()).get();
  assertEquals(ts.getSeconds(), input.toEpochSecond());
  assertEquals(TimeUnit.NANOSECONDS.toMicros(input.getNano()),
      TimeUnit.NANOSECONDS.toMicros(ts.getNanos()));
}
 
Example #15
Source File: OffsetDateTimeMapperTest.java    From catatumbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testToModel_Now() {
  Calendar now = Calendar.getInstance();
  TimestampValue v = TimestampValue.newBuilder(Timestamp.of(now.getTime())).build();
  OffsetDateTimeMapper mapper = new OffsetDateTimeMapper();
  OffsetDateTime output = (OffsetDateTime) mapper.toModel(v);
  assertTrue(now.getTimeInMillis() == output.toInstant().toEpochMilli());
}