本文整理了Java中com.google.cloud.Timestamp
类的一些代码示例,展示了Timestamp
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Timestamp
类的具体详情如下:
包路径:com.google.cloud.Timestamp
类名称:Timestamp
[英]Represents a timestamp with nanosecond precision. Timestamps cover the range [0001-01-01, 9999-12-31].
Timestamp instances are immutable.
[中]表示纳秒精度的时间戳。时间戳覆盖范围为[0001-01-019999-12-31]。
时间戳实例是不可变的。
代码示例来源: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
/**
* Returns the current count of all documents, including the changes from the current changeMap.
*/
private int currentSize() {
ChangeSet changeSet = extractChanges(Timestamp.now());
return documentSet.size() + changeSet.adds.size() - changeSet.deletes.size();
}
代码示例来源: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
static Map<String, AttributeValue> getTransactionAnnotations(Transaction t) {
return ImmutableMap.of(
"Id",
AttributeValue.stringAttributeValue(t.getId().toStringUtf8()),
"Timestamp",
AttributeValue.stringAttributeValue(Timestamp.fromProto(t.getReadTimestamp()).toString()));
}
代码示例来源: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 timestampArray() {
String t1 = "2015-09-15T00:00:00Z";
String t2 = "2015-09-14T00:00:00Z";
Value v =
Value.timestampArray(
Arrays.asList(Timestamp.parseTimestamp(t1), null, Timestamp.parseTimestamp(t2)));
assertThat(v.isNull()).isFalse();
assertThat(v.getTimestampArray())
.containsExactly(Timestamp.parseTimestamp(t1), null, Timestamp.parseTimestamp(t2))
.inOrder();
assertThat(v.toString()).isEqualTo("[" + t1 + ",NULL," + t2 + "]");
}
代码示例来源: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 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 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
@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 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 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 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
tester.addEqualityGroup(
Value.timestamp(Value.COMMIT_TIMESTAMP), Value.timestamp(Value.COMMIT_TIMESTAMP));
Timestamp now = Timestamp.now();
tester.addEqualityGroup(Value.timestamp(now), Value.timestamp(now));
tester.addEqualityGroup(Value.timestamp(Timestamp.ofTimeMicroseconds(0)));
assertThat(Value.struct(structValue1).equals(Value.struct(structValue2))).isTrue();
tester.addEqualityGroup(Value.struct(structValue1), Value.struct(structValue2));
代码示例来源: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: googleapis/google-cloud-java
/** Creates an instance with current time. */
public static Timestamp now() {
java.sql.Timestamp date = new java.sql.Timestamp(System.currentTimeMillis());
return of(date);
}
代码示例来源: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 boundsSecondsMax() {
expectedException.expect(IllegalArgumentException.class);
Timestamp.ofTimeSecondsAndNanos(Timestamp.MAX_VALUE.getSeconds() + 1, 0);
}
代码示例来源: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
@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();
}
内容来源于网络,如有侵权,请联系作者删除!