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

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

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

Timestamp.ofTimeSecondsAndNanos介绍

[英]Creates an instance representing the value of seconds and nanos since January 1, 1970, 00:00:00 UTC.
[中]创建一个实例,表示自1970年1月1日00:00:00 UTC以来的秒和毫微秒的值。

代码示例

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

/**
 * Creates an instance representing the value of {@code timestamp}.
 *
 * @throws IllegalArgumentException if the timestamp is outside the representable range
 */
public static Timestamp of(java.sql.Timestamp timestamp) {
 return ofTimeSecondsAndNanos(timestamp.getTime() / 1000, timestamp.getNanos());
}

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

public static Timestamp defaultTimestamp() {
 return Timestamp.ofTimeSecondsAndNanos(0, 0);
}

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

public static Iterable<Timestamp> defaultTimestampIterable() {
 return Arrays.asList(
   Timestamp.ofTimeSecondsAndNanos(0, 0), Timestamp.ofTimeSecondsAndNanos(0, 1));
}

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

/**
 * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without
 * the timezone offset (always ends in "Z").
 */
public static Timestamp parseTimestamp(String timestamp) {
 Instant instant = Instant.parse(timestamp);
 return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano());
}

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

@Test
public void comparable() {
 assertThat(Timestamp.MIN_VALUE).isLessThan(Timestamp.MAX_VALUE);
 assertThat(Timestamp.MAX_VALUE).isGreaterThan(Timestamp.MIN_VALUE);
 assertThat(Timestamp.ofTimeSecondsAndNanos(100, 0))
   .isAtLeast(Timestamp.ofTimeSecondsAndNanos(100, 0));
 assertThat(Timestamp.ofTimeSecondsAndNanos(100, 0))
   .isAtMost(Timestamp.ofTimeSecondsAndNanos(100, 0));
 assertThat(Timestamp.ofTimeSecondsAndNanos(100, 1000))
   .isLessThan(Timestamp.ofTimeSecondsAndNanos(101, 0));
 assertThat(Timestamp.ofTimeSecondsAndNanos(100, 1000))
   .isAtMost(Timestamp.ofTimeSecondsAndNanos(101, 0));
 assertThat(Timestamp.ofTimeSecondsAndNanos(101, 0))
   .isGreaterThan(Timestamp.ofTimeSecondsAndNanos(100, 1000));
 assertThat(Timestamp.ofTimeSecondsAndNanos(101, 0))
   .isAtLeast(Timestamp.ofTimeSecondsAndNanos(100, 1000));
}

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

@Test
public void equalsAndHashCode() {
 EqualsTester tester = new EqualsTester();
 tester.addEqualityGroup(
   Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0),
   Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0),
   Timestamp.of(new java.sql.Timestamp(TEST_TIME_SECONDS * 1000)));
 tester.addEqualityGroup(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS + 1, 0));
 tester.addEqualityGroup(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 1));
 tester.testEquals();
}

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

@Test
public void boundsNanosMin() {
 expectedException.expect(IllegalArgumentException.class);
 Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, -1);
}

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

@Test
public void toDate() {
 Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 1234 * 1000);
 Date date = timestamp.toDate();
 assertThat(TEST_TIME_MILLISECONDS).isEqualTo(date.getTime());
}

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

@Test
public void boundsNanosMax() {
 expectedException.expect(IllegalArgumentException.class);
 Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 1000000000);
}

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

@Test
public void testToString() {
 assertThat(Timestamp.MIN_VALUE.toString()).isEqualTo("0001-01-01T00:00:00Z");
 assertThat(Timestamp.MAX_VALUE.toString()).isEqualTo("9999-12-31T23:59:59.999999999Z");
 assertThat(Timestamp.ofTimeSecondsAndNanos(0, 0).toString()).isEqualTo("1970-01-01T00:00:00Z");
 assertThat(Timestamp.ofTimeSecondsAndNanos(0, 100).toString())
   .isEqualTo("1970-01-01T00:00:00.000000100Z");
 assertThat(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0).toString())
   .isEqualTo(TEST_TIME_ISO);
}

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

@Test
public void boundsSecondsMax() {
 expectedException.expect(IllegalArgumentException.class);
 Timestamp.ofTimeSecondsAndNanos(Timestamp.MAX_VALUE.getSeconds() + 1, 0);
}

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

@Test
public void boundsSecondsMin() {
 expectedException.expect(IllegalArgumentException.class);
 Timestamp.ofTimeSecondsAndNanos(Timestamp.MIN_VALUE.getSeconds() - 1, 999999999);
}

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

@Test
public void parseTimestamp() {
 assertThat(Timestamp.parseTimestamp("0001-01-01T00:00:00Z")).isEqualTo(Timestamp.MIN_VALUE);
 assertThat(Timestamp.parseTimestamp("9999-12-31T23:59:59.999999999Z"))
   .isEqualTo(Timestamp.MAX_VALUE);
 assertThat(Timestamp.parseTimestamp(TEST_TIME_ISO))
   .isEqualTo(Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0));
}

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

@Test
public void readTimestamp() {
 Timestamp ts = Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0);
 TimestampBound bound = TimestampBound.ofReadTimestamp(ts);
 assertThat(bound.getMode()).isEqualTo(Mode.READ_TIMESTAMP);
 assertThat(bound.getReadTimestamp()).isEqualTo(ts);
 assertThat(bound.toString()).isEqualTo("exact_timestamp: " + TEST_TIME_ISO);
 assertProto(bound, "read_timestamp { seconds: " + TEST_TIME_SECONDS + " }");
}

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

@Test
public void minReadTimestamp() {
 Timestamp ts = Timestamp.ofTimeSecondsAndNanos(TEST_TIME_SECONDS, 0);
 TimestampBound bound = TimestampBound.ofMinReadTimestamp(ts);
 assertThat(bound.getMode()).isEqualTo(Mode.MIN_READ_TIMESTAMP);
 assertThat(bound.getMinReadTimestamp()).isEqualTo(ts);
 assertThat(bound.toString()).isEqualTo("min_read_timestamp: " + TEST_TIME_ISO);
 assertProto(bound, "min_read_timestamp { seconds: " + TEST_TIME_SECONDS + " }");
}

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

@Test
public void notFound() throws Exception {
 final BatchGetDocumentsResponse.Builder getDocumentResponse =
   BatchGetDocumentsResponse.newBuilder();
 getDocumentResponse.setMissing(DOCUMENT_NAME);
 getDocumentResponse.setReadTime(
   com.google.protobuf.Timestamp.newBuilder().setSeconds(5).setNanos(6));
 doAnswer(streamingResponse(getDocumentResponse.build()))
   .when(firestoreMock)
   .streamRequest(
     getAllCapture.capture(),
     streamObserverCapture.capture(),
     Matchers.<ServerStreamingCallable>any());
 DocumentSnapshot snapshot = documentReference.get().get();
 assertEquals(documentReference, snapshot.getReference());
 assertFalse(snapshot.exists());
 assertEquals(snapshot.getReadTime(), Timestamp.ofTimeSecondsAndNanos(5, 6));
 assertNull(snapshot.getData());
}

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

@Test
public void testDefaultWriteOptionsHasExpectedDefaults() {
 logging.setFlushSeverity(Severity.ERROR);
 Capture<WriteOption> logNameArg = Capture.newInstance();
 Capture<WriteOption> resourceArg = Capture.newInstance();
 logging.write((Iterable<LogEntry>) anyObject(), capture(logNameArg), capture(resourceArg));
 expectLastCall().once();
 replay(logging);
 loggingAppender.start();
 Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
 LoggingEvent loggingEvent = createLoggingEvent(Level.ERROR, timestamp.getSeconds());
 loggingAppender.doAppend(loggingEvent);
 assertThat(logNameArg.getValue()).isEqualTo(defaultWriteOptions[0]);
 // TODO(chingor): Fix this test to work on GCE and locally
 // assertThat(resourceArg.getValue()).isEqualTo(defaultWriteOptions[1]);
}

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

@Test
public void timestampDoesntGetTruncatedDuringUpdate() throws Exception {
 DocumentReference documentReference =
   addDocument("time", Timestamp.ofTimeSecondsAndNanos(0, 123000));
 DocumentSnapshot documentSnapshot = documentReference.get().get();
 Timestamp timestamp = documentSnapshot.getTimestamp("time");
 documentReference.update("time", timestamp);
 documentSnapshot = documentReference.get().get();
 timestamp = documentSnapshot.getTimestamp("time");
 assertEquals(123000, timestamp.getNanos());
}

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

@Test
public void deleteDocument() throws Exception {
 randomDoc.delete().get();
 WriteResult writeResult = randomDoc.set(ALL_SUPPORTED_TYPES_MAP).get();
 try {
  randomDoc.delete(Precondition.updatedAt(Timestamp.ofTimeSecondsAndNanos(1, 0))).get();
  fail();
 } catch (ExecutionException e) {
  assertTrue(e.getMessage().contains("FAILED_PRECONDITION"));
 }
 writeResult = randomDoc.delete(Precondition.updatedAt(writeResult.getUpdateTime())).get();
 DocumentSnapshot documentSnapshot = randomDoc.get().get();
 assertFalse(documentSnapshot.exists());
 assertTrue(writeResult.getUpdateTime().getSeconds() > 0);
}

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

@Test
public void queryWithMicrosecondPrecision() throws Exception {
 Timestamp microsecondTimestamp = Timestamp.ofTimeSecondsAndNanos(0, 123000);
 DocumentReference documentReference = addDocument("time", microsecondTimestamp);
 DocumentSnapshot documentSnapshot = documentReference.get().get();
 Query query = randomColl.whereEqualTo("time", microsecondTimestamp);
 QuerySnapshot querySnapshot = query.get().get();
 assertEquals(1, querySnapshot.size());
 // Using `.toDate()` truncates to millseconds, and hence the query doesn't match.
 query = randomColl.whereEqualTo("time", microsecondTimestamp.toDate());
 querySnapshot = query.get().get();
 assertEquals(0, querySnapshot.size());
}

相关文章