本文整理了Java中com.google.cloud.Timestamp.getNanos()
方法的一些代码示例,展示了Timestamp.getNanos()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Timestamp.getNanos()
方法的具体详情如下:
包路径:com.google.cloud.Timestamp
类名称:Timestamp
方法名:getNanos
[英]Returns the fractional seconds component, in nanoseconds.
[中]返回以纳秒为单位的分数秒分量。
代码示例来源: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 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 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 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 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: 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: spotify/styx
private static Instant timestampToInstant(Timestamp ts) {
return Instant.ofEpochSecond(ts.getSeconds(), ts.getNanos());
}
代码示例来源:origin: sai-pullabhotla/catatumbo
@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);
}
}
代码示例来源:origin: sai-pullabhotla/catatumbo
@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);
}
}
代码示例来源:origin: sai-pullabhotla/catatumbo
@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);
}
}
代码示例来源:origin: sai-pullabhotla/catatumbo
@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);
}
}
代码示例来源: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());
}
}
内容来源于网络,如有侵权,请联系作者删除!