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

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

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

Timestamp.getSeconds介绍

[英]Returns the number of seconds since January 1, 1970, 00:00:00 UTC. A negative value is the number of seconds before January 1, 1970, 00:00:00 UTC.
[中]返回自1970年1月1日00:00:00 UTC以来的秒数。负值是1970年1月1日00:00:00 UTC之前的秒数。

代码示例

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

@Test
public void maxValue() {
 TimeZone tz = TimeZone.getTimeZone("UTC");
 GregorianCalendar calendar = new GregorianCalendar(tz);
 calendar.set(9999, Calendar.DECEMBER, 31, 23, 59, 59);
 java.sql.Timestamp expectedMin = new java.sql.Timestamp(calendar.getTimeInMillis());
 expectedMin.setNanos(999999999);
 assertThat(Timestamp.MAX_VALUE.getSeconds()).isEqualTo(calendar.getTimeInMillis() / 1000L);
 assertThat(Timestamp.MAX_VALUE.getNanos()).isEqualTo(999999999);
}

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

@Test
public void ofDate() {
 Timestamp timestamp = Timestamp.of(TEST_DATE);
 Long expectedSeconds = TimeUnit.MILLISECONDS.toSeconds(TEST_TIME_MILLISECONDS);
 Long expectedNanos =
   TimeUnit.MILLISECONDS.toNanos(TEST_TIME_MILLISECONDS)
     - TimeUnit.SECONDS.toNanos(expectedSeconds);
 assertThat(timestamp.getSeconds()).isEqualTo(expectedSeconds);
 assertThat(timestamp.getNanos()).isEqualTo(expectedNanos);
}

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

@Test
public void boundsSqlTimestampMax() {
 expectedException.expect(IllegalArgumentException.class);
 Timestamp.of(new java.sql.Timestamp((Timestamp.MAX_VALUE.getSeconds() + 1) * 1000));
}

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

@Test
public void boundsSqlTimestampMin() {
 expectedException.expect(IllegalArgumentException.class);
 Timestamp.of(new java.sql.Timestamp((Timestamp.MIN_VALUE.getSeconds() - 1) * 1000));
}

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

@Test
public void minValue() {
 // MIN_VALUE is before the start of the Gregorian calendar... use magic value.
 assertThat(Timestamp.MIN_VALUE.getSeconds()).isEqualTo(-62135596800L);
 assertThat(Timestamp.MIN_VALUE.getNanos()).isEqualTo(0);
}

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

@Test
public void toFromSqlTimestamp() {
 long seconds = TEST_TIME_SECONDS;
 int nanos = 500000000;
 java.sql.Timestamp sqlTs = new java.sql.Timestamp(seconds * 1000);
 sqlTs.setNanos(nanos);
 Timestamp ts = Timestamp.of(sqlTs);
 assertThat(ts.getSeconds()).isEqualTo(seconds);
 assertThat(ts.getNanos()).isEqualTo(nanos);
 assertThat(ts.toSqlTimestamp()).isEqualTo(sqlTs);
}

代码示例来源: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 boundsSecondsMax() {
 expectedException.expect(IllegalArgumentException.class);
 Timestamp.ofTimeSecondsAndNanos(Timestamp.MAX_VALUE.getSeconds() + 1, 0);
}

代码示例来源: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 fromProto() {
 com.google.protobuf.Timestamp proto =
   com.google.protobuf.Timestamp.newBuilder().setSeconds(1234).setNanos(567).build();
 Timestamp timestamp = Timestamp.fromProto(proto);
 assertThat(timestamp.getSeconds()).isEqualTo(1234);
 assertThat(timestamp.getNanos()).isEqualTo(567);
}

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

@Test
public void testFlushLevelConfigUpdatesLoggingFlushSeverity() {
 LogEntry logEntry =
   LogEntry.newBuilder(StringPayload.of("this is a test"))
     .setTimestamp(100000L)
     .setSeverity(Severity.WARNING)
     .setLabels(
       new ImmutableMap.Builder<String, String>()
         .put("levelName", "WARN")
         .put("levelValue", String.valueOf(30000L))
         .build())
     .build();
 logging.setFlushSeverity(Severity.WARNING);
 Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
 logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
 replay(logging);
 Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
 LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds());
 // error is the default, updating to warn for test
 loggingAppender.setFlushLevel(Level.WARN);
 loggingAppender.start();
 loggingAppender.doAppend(loggingEvent);
 verify(logging);
 assertThat(capturedArgument.getValue().iterator().hasNext()).isTrue();
 assertThat(capturedArgument.getValue().iterator().next()).isEqualTo(logEntry);
}

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

@Test
public void testEnhancersAddCorrectLabelsToLogEntries() {
 LogEntry logEntry =
   LogEntry.newBuilder(StringPayload.of("this is a test"))
     .setTimestamp(100000L)
     .setSeverity(Severity.WARNING)
     .setLabels(
       new ImmutableMap.Builder<String, String>()
         .put("levelName", "WARN")
         .put("levelValue", String.valueOf(30000L))
         .put("test-label-1", "test-value-1")
         .put("test-label-2", "test-value-2")
         .build())
     .build();
 logging.setFlushSeverity(Severity.ERROR);
 Capture<Iterable<LogEntry>> capturedArgument = Capture.newInstance();
 logging.write(capture(capturedArgument), (WriteOption) anyObject(), (WriteOption) anyObject());
 expectLastCall().once();
 replay(logging);
 loggingAppender.addEnhancer("com.example.enhancers.TestLoggingEnhancer");
 loggingAppender.addEnhancer("com.example.enhancers.AnotherTestLoggingEnhancer");
 loggingAppender.start();
 Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
 LoggingEvent loggingEvent = createLoggingEvent(Level.WARN, timestamp.getSeconds());
 loggingAppender.doAppend(loggingEvent);
 verify(logging);
 assertThat(capturedArgument.getValue().iterator().hasNext()).isTrue();
 assertThat(capturedArgument.getValue().iterator().next()).isEqualTo(logEntry);
}

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

replay(logging);
Timestamp timestamp = Timestamp.ofTimeSecondsAndNanos(100000, 0);
LoggingEvent loggingEvent1 = createLoggingEvent(Level.INFO, timestamp.getSeconds());
ThresholdFilter thresholdFilter = new ThresholdFilter();
thresholdFilter.setLevel("ERROR");
LoggingEvent loggingEvent2 = createLoggingEvent(Level.ERROR, timestamp.getSeconds());

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

@Test
public void writeAtLeastOnce() throws ParseException {
 String timestampString = "2015-10-01T10:54:20.021Z";
 ArgumentCaptor<CommitRequest> commit = ArgumentCaptor.forClass(CommitRequest.class);
 CommitResponse response =
   CommitResponse.newBuilder().setCommitTimestamp(Timestamps.parse(timestampString)).build();
 Mockito.when(rpc.commit(commit.capture(), Mockito.eq(options))).thenReturn(response);
 Timestamp timestamp =
   session.writeAtLeastOnce(
     Arrays.asList(Mutation.newInsertBuilder("T").set("C").to("x").build()));
 assertThat(timestamp.getSeconds())
   .isEqualTo(utcTimeSeconds(2015, Calendar.OCTOBER, 1, 10, 54, 20));
 assertThat(timestamp.getNanos()).isEqualTo(TimeUnit.MILLISECONDS.toNanos(21));
 CommitRequest request = commit.getValue();
 assertThat(request.getSingleUseTransaction()).isNotNull();
 assertThat(request.getSingleUseTransaction().getReadWrite()).isNotNull();
 com.google.spanner.v1.Mutation mutation =
   com.google.spanner.v1.Mutation.newBuilder()
     .setInsert(
       Write.newBuilder()
         .setTable("T")
         .addColumns("C")
         .addValues(
           ListValue.newBuilder()
             .addValues(com.google.protobuf.Value.newBuilder().setStringValue("x"))))
     .build();
 assertThat(request.getMutationsList()).containsExactly(mutation);
}

代码示例来源: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 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: GoogleCloudPlatform/java-docs-samples

@Override
 public MutationGroup apply(String userId) {
  // Immediately block the user.
  Mutation userMutation = Mutation.newUpdateBuilder("Users")
    .set("id").to(userId)
    .set("state").to("BLOCKED")
    .build();
  long generatedId = Hashing.sha1().newHasher()
    .putString(userId, Charsets.UTF_8)
    .putLong(timestamp.getSeconds())
    .putLong(timestamp.getNanos())
    .hash()
    .asLong();
  // Add an entry to pending review requests.
  Mutation pendingReview = Mutation.newInsertOrUpdateBuilder("PendingReviews")
    .set("id").to(generatedId)  // Must be deterministically generated.
    .set("userId").to(userId)
    .set("action").to("REVIEW ACCOUNT")
    .set("note").to("Suspicious activity detected.")
    .build();
  return MutationGroup.create(userMutation, pendingReview);
 }
}));

代码示例来源:origin: spring-cloud/spring-cloud-gcp

@Nullable
  @Override
  public Instant convert(Timestamp timestamp) {
    return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
  }
};

代码示例来源:origin: org.springframework.cloud/spring-cloud-gcp-data-spanner

@Nullable
  @Override
  public Instant convert(Timestamp timestamp) {
    return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
  }
};

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

private void writeTimestamp(OrderedCode orderedCode, KeyPart part, Timestamp v) {
 if (part.isDesc()) {
  orderedCode.writeNumDecreasing(v.getSeconds());
  orderedCode.writeNumDecreasing(v.getNanos());
 } else {
  orderedCode.writeNumIncreasing(v.getSeconds());
  orderedCode.writeNumIncreasing(v.getNanos());
 }
}

相关文章