com.google.protobuf.Timestamp类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.3k)|赞(0)|评价(0)|浏览(1356)

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

Timestamp介绍

[英]```
A Timestamp represents a point in time independent of any time zone
or calendar, represented as seconds and fractions of seconds at
nanosecond resolution in UTC Epoch time. It is encoded using the
Proleptic Gregorian Calendar which extends the Gregorian calendar
backwards to year one. It is encoded assuming all minutes are 60
seconds long, i.e. leap seconds are "smeared" so that no leap second
table is needed for interpretation. Range is from
0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z.
By restricting to that range, we ensure that we can convert to
and from RFC 3339 date strings.
See https://www.ietf.org/rfc/rfc3339.txt.
Example 1: Compute Timestamp from POSIX time().
Timestamp timestamp;
timestamp.set_seconds(time(NULL));
timestamp.set_nanos(0);
Example 2: Compute Timestamp from POSIX gettimeofday().
struct timeval tv;
gettimeofday(&tv, NULL);
Timestamp timestamp;
timestamp.set_seconds(tv.tv_sec);
timestamp.set_nanos(tv.tv_usec * 1000);
Example 3: Compute Timestamp from Win32 GetSystemTimeAsFileTime().
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
// A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z
// is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z.
Timestamp timestamp;
timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL));
timestamp.set_nanos((INT32) ((ticks % 10000000) * 100));
Example 4: Compute Timestamp from Java System.currentTimeMillis().
long millis = System.currentTimeMillis();
Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000)
.setNanos((int) ((millis % 1000) * 1000000)).build();
Example 5: Compute Timestamp from current time in Python.
now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10**9)
timestamp = Timestamp(seconds=seconds, nanos=nanos)

 Protobuf type google.protobuf.Timestamp
[中]```
A Timestamp represents a point in time independent of any time zone 
or calendar, represented as seconds and fractions of seconds at 
nanosecond resolution in UTC Epoch time. It is encoded using the 
Proleptic Gregorian Calendar which extends the Gregorian calendar 
backwards to year one. It is encoded assuming all minutes are 60 
seconds long, i.e. leap seconds are "smeared" so that no leap second 
table is needed for interpretation. Range is from 
0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z. 
By restricting to that range, we ensure that we can convert to 
and from  RFC 3339 date strings. 
See [https://www.ietf.org/rfc/rfc3339.txt](https://www.ietf.org/rfc/rfc3339.txt). 
Example 1: Compute Timestamp from POSIX `time()`. 
Timestamp timestamp; 
timestamp.set_seconds(time(NULL)); 
timestamp.set_nanos(0); 
Example 2: Compute Timestamp from POSIX `gettimeofday()`. 
struct timeval tv; 
gettimeofday(&tv, NULL); 
Timestamp timestamp; 
timestamp.set_seconds(tv.tv_sec); 
timestamp.set_nanos(tv.tv_usec * 1000); 
Example 3: Compute Timestamp from Win32 `GetSystemTimeAsFileTime()`. 
FILETIME ft; 
GetSystemTimeAsFileTime(&ft); 
UINT64 ticks = (((UINT64)ft.dwHighDateTime) << 32) | ft.dwLowDateTime; 
// A Windows tick is 100 nanoseconds. Windows epoch 1601-01-01T00:00:00Z 
// is 11644473600 seconds before Unix epoch 1970-01-01T00:00:00Z. 
Timestamp timestamp; 
timestamp.set_seconds((INT64) ((ticks / 10000000) - 11644473600LL)); 
timestamp.set_nanos((INT32) ((ticks % 10000000) * 100)); 
Example 4: Compute Timestamp from Java `System.currentTimeMillis()`. 
long millis = System.currentTimeMillis(); 
Timestamp timestamp = Timestamp.newBuilder().setSeconds(millis / 1000) 
.setNanos((int) ((millis % 1000) * 1000000)).build(); 
Example 5: Compute Timestamp from current time in Python. 
now = time.time() 
seconds = int(now) 
nanos = int((now - seconds) * 10**9) 
timestamp = Timestamp(seconds=seconds, nanos=nanos)

Protobuf类型谷歌。protobuf。时间戳

代码示例

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder() {

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

/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */
public com.google.protobuf.TimestampOrBuilder getTimestampValueOrBuilder() {
 if ((typeCase_ == 5) && (timestampValueBuilder_ != null)) {
  return timestampValueBuilder_.getMessageOrBuilder();
 } else {
  if (typeCase_ == 5) {
   return (com.google.protobuf.Timestamp) type_;
  }
  return com.google.protobuf.Timestamp.getDefaultInstance();
 }
}
/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */

代码示例来源:origin: com.google.protobuf/protobuf-java

public static Builder newBuilder() {
 return DEFAULT_INSTANCE.toBuilder();
}
public static Builder newBuilder(com.google.protobuf.Timestamp prototype) {

代码示例来源:origin: com.google.protobuf/protobuf-java

public Builder mergeFrom(com.google.protobuf.Timestamp other) {
 if (other == com.google.protobuf.Timestamp.getDefaultInstance()) return this;
 if (other.getSeconds() != 0L) {
  setSeconds(other.getSeconds());
 }
 if (other.getNanos() != 0) {
  setNanos(other.getNanos());
 }
 this.mergeUnknownFields(other.unknownFields);
 onChanged();
 return this;
}

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

com.google.protobuf.UnknownFieldSet.newBuilder();
try {
 boolean done = false;
 while (!done) {
  int tag = input.readTag();
  switch (tag) {
   case 0:
      subBuilder = snapshotTime_.toBuilder();
       input.readMessage(com.google.protobuf.Timestamp.parser(), extensionRegistry);
     if (subBuilder != null) {
      subBuilder.mergeFrom(snapshotTime_);
      snapshotTime_ = subBuilder.buildPartial();
 throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
 throw new com.google.protobuf.InvalidProtocolBufferException(e).setUnfinishedMessage(this);
} finally {
 this.unknownFields = unknownFields.build();

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

@java.lang.Override
public int hashCode() {
 if (memoizedHashCode != 0) {
  return memoizedHashCode;
 }
 int hash = 41;
 hash = (19 * hash) + getDescriptor().hashCode();
 switch (consistencySelectorCase_) {
  case 2:
   hash = (37 * hash) + READ_TIME_FIELD_NUMBER;
   hash = (53 * hash) + getReadTime().hashCode();
   break;
  case 0:
  default:
 }
 hash = (29 * hash) + unknownFields.hashCode();
 memoizedHashCode = hash;
 return hash;
}

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

@java.lang.Override
public boolean equals(final java.lang.Object obj) {
 if (obj == this) {
  return true;
 }
 if (!(obj instanceof com.google.spanner.v1.CommitResponse)) {
  return super.equals(obj);
 }
 com.google.spanner.v1.CommitResponse other = (com.google.spanner.v1.CommitResponse) obj;
 boolean result = true;
 result = result && (hasCommitTimestamp() == other.hasCommitTimestamp());
 if (hasCommitTimestamp()) {
  result = result && getCommitTimestamp().equals(other.getCommitTimestamp());
 }
 result = result && unknownFields.equals(other.unknownFields);
 return result;
}

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

/**
 *
 *
 * <pre>
 * The timestamp indicating the time the event occurred.
 * </pre>
 *
 * <code>.google.protobuf.Timestamp time = 1;</code>
 */
public Builder mergeTime(com.google.protobuf.Timestamp value) {
 if (timeBuilder_ == null) {
  if (time_ != null) {
   time_ = com.google.protobuf.Timestamp.newBuilder(time_).mergeFrom(value).buildPartial();
  } else {
   time_ = value;
  }
  onChanged();
 } else {
  timeBuilder_.mergeFrom(value);
 }
 return this;
}
/**

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

/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */
public Builder mergeTimestampValue(com.google.protobuf.Timestamp value) {
 if (timestampValueBuilder_ == null) {
  if (typeCase_ == 5 && type_ != com.google.protobuf.Timestamp.getDefaultInstance()) {
   type_ =
     com.google.protobuf.Timestamp.newBuilder((com.google.protobuf.Timestamp) type_)
       .mergeFrom(value)
       .buildPartial();
  } else {
   type_ = value;
  }
  onChanged();
 } else {
  if (typeCase_ == 5) {
   timestampValueBuilder_.mergeFrom(value);
  }
  timestampValueBuilder_.setMessage(value);
 }
 typeCase_ = 5;
 return this;
}
/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */

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

/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */
public com.google.protobuf.Timestamp getTimestampValue() {
 if (timestampValueBuilder_ == null) {
  if (typeCase_ == 5) {
   return (com.google.protobuf.Timestamp) type_;
  }
  return com.google.protobuf.Timestamp.getDefaultInstance();
 } else {
  if (typeCase_ == 5) {
   return timestampValueBuilder_.getMessage();
  }
  return com.google.protobuf.Timestamp.getDefaultInstance();
 }
}
/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */

代码示例来源:origin: com.google.protobuf/protobuf-java

@java.lang.Override
public com.google.protobuf.Timestamp getDefaultInstanceForType() {
 return com.google.protobuf.Timestamp.getDefaultInstance();
}

代码示例来源:origin: pinterest/secor

@Test
public void testExtractPathTimestampMillis() throws Exception {
  Map<String, String> classPerTopic = new HashMap<String, String>();
  System.out.println(TimestampedMessages.UnitTestTimestamp1.class.getName());
  classPerTopic.put("test", TimestampedMessages.UnitTestTimestamp1.class.getName());
  Mockito.when(mConfig.getMessageTimestampName()).thenReturn("timestamp");
  Mockito.when(mConfig.getProtobufMessageClassPerTopic()).thenReturn(classPerTopic);
  ProtobufMessageParser parser = new ProtobufMessageParser(mConfig);
  Timestamp timestamp = Timestamp.newBuilder().setSeconds(1405970352l)
      .setNanos(0).build();
  TimestampedMessages.UnitTestTimestamp1 message = TimestampedMessages.UnitTestTimestamp1.newBuilder().setTimestamp(timestamp).build();
  assertEquals(1405970352000l,
      parser.extractTimestampMillis(new Message("test", 0, 0, null, message.toByteArray(), timestamp.getSeconds())));
  Timestamp timestampWithNano = Timestamp.newBuilder().setSeconds(1405970352l)
      .setNanos(123000000).build();
  message = TimestampedMessages.UnitTestTimestamp1.newBuilder().setTimestamp(timestampWithNano).build();
  assertEquals(1405970352123l,
      parser.extractTimestampMillis(new Message("test", 0, 0, null, message.toByteArray(), timestamp.getSeconds())));
}

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

com.google.protobuf.UnknownFieldSet.newBuilder();
try {
 boolean done = false;
 while (!done) {
  int tag = input.readTag();
  switch (tag) {
   case 0:
      subBuilder = ((com.google.protobuf.Timestamp) consistencySelector_).toBuilder();
       input.readMessage(com.google.protobuf.Timestamp.parser(), extensionRegistry);
     if (subBuilder != null) {
      subBuilder.mergeFrom((com.google.protobuf.Timestamp) consistencySelector_);
      consistencySelector_ = subBuilder.buildPartial();
 throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
 throw new com.google.protobuf.InvalidProtocolBufferException(e).setUnfinishedMessage(this);
} finally {
 this.unknownFields = unknownFields.build();

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

@java.lang.Override
public int hashCode() {
 if (memoizedHashCode != 0) {
  return memoizedHashCode;
 }
 int hash = 41;
 hash = (19 * hash) + getDescriptor().hashCode();
 if (hasCommitTimestamp()) {
  hash = (37 * hash) + COMMIT_TIMESTAMP_FIELD_NUMBER;
  hash = (53 * hash) + getCommitTimestamp().hashCode();
 }
 hash = (29 * hash) + unknownFields.hashCode();
 memoizedHashCode = hash;
 return hash;
}

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

@java.lang.Override
public boolean equals(final java.lang.Object obj) {
 if (obj == this) {
  return true;
 }
 if (!(obj instanceof com.google.cloud.kms.v1.KeyRing)) {
  return super.equals(obj);
 }
 com.google.cloud.kms.v1.KeyRing other = (com.google.cloud.kms.v1.KeyRing) obj;
 boolean result = true;
 result = result && getName().equals(other.getName());
 result = result && (hasCreateTime() == other.hasCreateTime());
 if (hasCreateTime()) {
  result = result && getCreateTime().equals(other.getCreateTime());
 }
 result = result && unknownFields.equals(other.unknownFields);
 return result;
}

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

/**
 *
 *
 * <pre>
 * The creation timestamp of a inspectTemplate, output only field.
 * </pre>
 *
 * <code>.google.protobuf.Timestamp create_time = 4;</code>
 */
public Builder mergeCreateTime(com.google.protobuf.Timestamp value) {
 if (createTimeBuilder_ == null) {
  if (createTime_ != null) {
   createTime_ =
     com.google.protobuf.Timestamp.newBuilder(createTime_).mergeFrom(value).buildPartial();
  } else {
   createTime_ = value;
  }
  onChanged();
 } else {
  createTimeBuilder_.mergeFrom(value);
 }
 return this;
}
/**

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

/**
 *
 *
 * <pre>
 * The time at which this operation failed or was completed successfully.
 * </pre>
 *
 * <code>.google.protobuf.Timestamp end_time = 4;</code>
 */
public com.google.protobuf.Timestamp getEndTime() {
 if (endTimeBuilder_ == null) {
  return endTime_ == null ? com.google.protobuf.Timestamp.getDefaultInstance() : endTime_;
 } else {
  return endTimeBuilder_.getMessage();
 }
}
/**

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

/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */
public com.google.protobuf.Timestamp getTimestampValue() {
 if (typeCase_ == 5) {
  return (com.google.protobuf.Timestamp) type_;
 }
 return com.google.protobuf.Timestamp.getDefaultInstance();
}
/** <code>.google.protobuf.Timestamp timestamp_value = 5;</code> */

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

com.google.protobuf.UnknownFieldSet.newBuilder();
try {
 boolean done = false;
 while (!done) {
  int tag = input.readTag();
  switch (tag) {
   case 0:
      subBuilder = commitTimestamp_.toBuilder();
       input.readMessage(com.google.protobuf.Timestamp.parser(), extensionRegistry);
     if (subBuilder != null) {
      subBuilder.mergeFrom(commitTimestamp_);
      commitTimestamp_ = subBuilder.buildPartial();
 throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
 throw new com.google.protobuf.InvalidProtocolBufferException(e).setUnfinishedMessage(this);
} finally {
 this.unknownFields = unknownFields.build();

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

@java.lang.Override
public int hashCode() {
 if (memoizedHashCode != 0) {
  return memoizedHashCode;
 }
 int hash = 41;
 hash = (19 * hash) + getDescriptor().hashCode();
 if (hasSnapshotTime()) {
  hash = (37 * hash) + SNAPSHOT_TIME_FIELD_NUMBER;
  hash = (53 * hash) + getSnapshotTime().hashCode();
 }
 hash = (29 * hash) + unknownFields.hashCode();
 memoizedHashCode = hash;
 return hash;
}

相关文章