com.google.cloud.Timestamp.ofTimeMicroseconds()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(152)

本文整理了Java中com.google.cloud.Timestamp.ofTimeMicroseconds()方法的一些代码示例,展示了Timestamp.ofTimeMicroseconds()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Timestamp.ofTimeMicroseconds()方法的具体详情如下:
包路径:com.google.cloud.Timestamp
类名称:Timestamp
方法名:ofTimeMicroseconds

Timestamp.ofTimeMicroseconds介绍

[英]Creates an instance representing the value of microseconds.
[中]创建一个表示微秒值的实例。

代码示例

代码示例来源:origin: googleapis/google-cloud-java

/**
 * 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()));
}

代码示例来源:origin: googleapis/google-cloud-java

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

代码示例来源:origin: googleapis/google-cloud-java

@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);
}

代码示例来源:origin: googleapis/google-cloud-java

@Test
public void commitSucceeds() {
 when(session.newTransaction()).thenReturn(txn);
 Timestamp commitTimestamp = Timestamp.ofTimeMicroseconds(1);
 when(txn.commitTimestamp()).thenReturn(commitTimestamp);
 manager.begin();
 manager.commit();
 assertThat(manager.getState()).isEqualTo(TransactionState.COMMITTED);
 assertThat(manager.getCommitTimestamp()).isEqualTo(commitTimestamp);
}

代码示例来源:origin: googleapis/google-cloud-java

};
Timestamp[] timestampArray = {
 Timestamp.ofTimeMicroseconds(101),
 Timestamp.ofTimeMicroseconds(202),
 Timestamp.ofTimeMicroseconds(303)
};
Date[] dateArray = {
    .to(Value.bytes(ByteArray.copyFrom(byteVal)))
    .set("timestamp")
    .to(Timestamp.ofTimeMicroseconds(usecs))
    .set("date")
    .to(Date.fromYearMonthDay(year, month, day))
    .to(Value.bytes(ByteArray.copyFrom(byteVal)))
    .set("timestamp")
    .to(Timestamp.ofTimeMicroseconds(usecs))
    .set("date")
    .to(Date.fromYearMonthDay(year, month, day))
assertThat(rs.getBytes(5)).isEqualTo(ByteArray.copyFrom(byteVal));
assertThat(rs.getBytes("byteVal")).isEqualTo(ByteArray.copyFrom(byteVal));
assertThat(rs.getTimestamp(6)).isEqualTo(Timestamp.ofTimeMicroseconds(usecs));
assertThat(rs.getTimestamp("timestamp")).isEqualTo(Timestamp.ofTimeMicroseconds(usecs));
assertThat(rs.getDate(7)).isEqualTo(Date.fromYearMonthDay(year, month, day));
assertThat(rs.getDate("date")).isEqualTo(Date.fromYearMonthDay(year, month, day));

代码示例来源:origin: googleapis/google-cloud-java

private Struct structValue() {
 return Struct.newBuilder()
   .set("f_int")
   .to(10)
   .set("f_bool")
   .to(false)
   .set("f_double")
   .to(3.4)
   .set("f_timestamp")
   .to(Timestamp.ofTimeMicroseconds(20))
   .set("f_date")
   .to(Date.fromYearMonthDay(1, 3, 1))
   .set("f_string")
   .to("hello")
   .set("f_bytes")
   .to(ByteArray.copyFrom("bytes"))
   .build();
}

代码示例来源:origin: googleapis/google-cloud-java

Timestamp now = Timestamp.now();
tester.addEqualityGroup(Value.timestamp(now), Value.timestamp(now));
tester.addEqualityGroup(Value.timestamp(Timestamp.ofTimeMicroseconds(0)));

代码示例来源:origin: objectify/objectify

@Override
final protected P loadSafe(final Value<D> value, final LoadContext ctx, final Path path) throws SkipException {
  if (!isTypeExpected(value.getType())) {
    // Normally we would just throw an error here but there are some edge cases caused by projection queries.
    // For example, timestamps come back as LongValue and blobs come back as StringValue. We'll special-case them.
    // The downside is that a user who changes a field from 'long' to 'Date' will not trigger an error.
    // The exact logic here comes from com.google.cloud.datastore.ProjectionEntity
    if (value.getType() == ValueType.LONG && isTypeExpected(ValueType.TIMESTAMP)) {
      @SuppressWarnings("unchecked")
      final Value<D> timestampValue = (Value<D>)TimestampValue.of(Timestamp.ofTimeMicroseconds((Long)value.get()));
      return loadValue(timestampValue, ctx, path);
    }
    else if (value.getType() == ValueType.STRING && isTypeExpected(ValueType.BLOB)) {
      @SuppressWarnings("unchecked")
      final Value<D> blobValue = (Value<D>)BlobValue.of(Blob.copyFrom(((String)value.get()).getBytes(StandardCharsets.UTF_8)));
      return loadValue(blobValue, ctx, path);
    }
    else {
      path.throwIllegalState("Expected value of type " + Arrays.toString(expectedValueTypes) + ", got " + value.getType() + ": " + value);
    }
  }
  return loadValue(value, ctx, path);
}

代码示例来源:origin: com.google.cloud/google-cloud-core

/**
 * 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()));
}

代码示例来源:origin: sai-pullabhotla/catatumbo

/**
 * Converts the given OffsetDateTime to a Timestamp.
 * 
 * @param offsetDateTime
 *          the OffsetDateTime to convert
 * @return Timestamp object that is equivalent to the given OffsetDateTime.
 */
private static Timestamp toTimestamp(OffsetDateTime offsetDateTime) {
 long seconds = offsetDateTime.toEpochSecond();
 int nanos = offsetDateTime.getNano();
 long microseconds = TimeUnit.SECONDS.toMicros(seconds) + TimeUnit.NANOSECONDS.toMicros(nanos);
 return Timestamp.ofTimeMicroseconds(microseconds);
}

代码示例来源:origin: GoogleCloudPlatform/DataflowTemplates

private Optional<Timestamp> readTimestamp(
  GenericRecord record, Schema.Type avroType, LogicalType logicalType, String fieldName) {
 switch (avroType) {
  case LONG:
   if (LogicalTypes.timestampMillis().equals(logicalType)) {
    return Optional.ofNullable((Long) record.get(fieldName))
      .map(x -> Timestamp.ofTimeMicroseconds(1000L * x));
   }
   if (LogicalTypes.timestampMicros().equals(logicalType)) {
    return Optional.ofNullable((Long) record.get(fieldName))
      .map(Timestamp::ofTimeMicroseconds);
   }
   // Default to micro-seconds.
   return Optional.ofNullable((Long) record.get(fieldName)).map(Timestamp::ofTimeMicroseconds);
  case STRING:
   return Optional.ofNullable((Utf8) record.get(fieldName))
     .map(Utf8::toString)
     .map(Timestamp::parseTimestamp);
  default:
   throw new IllegalArgumentException("Cannot interpret " + avroType + " as TIMESTAMP");
 }
}

代码示例来源:origin: sai-pullabhotla/catatumbo

/**
 * Converts the given OffsetDateTime to a Timestamp.
 * 
 * @param zonedDateTime
 *          the {@link ZonedDateTime} to convert
 * @return Timestamp object that is equivalent to the given OffsetDateTime.
 */
private static Timestamp toTimestamp(ZonedDateTime zonedDateTime) {
 long seconds = zonedDateTime.toEpochSecond();
 int nanos = zonedDateTime.getNano();
 long microseconds = TimeUnit.SECONDS.toMicros(seconds) + TimeUnit.NANOSECONDS.toMicros(nanos);
 return Timestamp.ofTimeMicroseconds(microseconds);
}

代码示例来源:origin: sai-pullabhotla/catatumbo

@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));
}

代码示例来源:origin: sai-pullabhotla/catatumbo

@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));
}

代码示例来源:origin: GoogleCloudPlatform/DataflowTemplates

value
   .stream()
   .map(x -> x == null ? null : Timestamp.ofTimeMicroseconds(1000L * x))
   .collect(Collectors.toList()));
 value
   .stream()
   .map(x -> x == null ? null : Timestamp.ofTimeMicroseconds(x))
   .collect(Collectors.toList()));
value
  .stream()
  .map(x -> x == null ? null : Timestamp.ofTimeMicroseconds(x))
  .collect(Collectors.toList()));

代码示例来源:origin: com.google.cloud/google-cloud-datastore

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

代码示例来源:origin: org.apache.beam/beam-sdks-java-io-google-cloud-platform

@Test
public void readPipeline() throws Exception {
 Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
 TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);

代码示例来源:origin: org.apache.beam/beam-sdks-java-io-google-cloud-platform

@Test
public void readAllPipeline() throws Exception {
 Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
 TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);

代码示例来源:origin: org.apache.beam/beam-sdks-java-io-google-cloud-platform

Mutation.newInsertOrUpdateBuilder("test")
  .set("key")
  .to(Timestamp.ofTimeMicroseconds(10000))
  .set("keydesc")
  .to(Timestamp.ofTimeMicroseconds(50000))
  .build(),
Mutation.newInsertOrUpdateBuilder("test")
  .set("key")
  .to(Timestamp.ofTimeMicroseconds(20000))
  .set("keydesc")
  .to((Timestamp) null)
Mutation.newInsertOrUpdateBuilder("test")
  .set("key")
  .to(Timestamp.ofTimeMicroseconds(20000))
  .set("keydesc")
  .to(Timestamp.ofTimeMicroseconds(90000))
  .build(),
Mutation.newInsertOrUpdateBuilder("test")
  .set("key")
  .to(Timestamp.ofTimeMicroseconds(20000))
  .set("keydesc")
  .to(Timestamp.ofTimeMicroseconds(50000))
  .build(),
Mutation.newInsertOrUpdateBuilder("test")
  .set("key")
  .to(Timestamp.ofTimeMicroseconds(20000))
  .set("keydesc")
  .to(Timestamp.ofTimeMicroseconds(10000))

相关文章