io.opencensus.common.Timestamp类的使用及代码示例

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

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

Timestamp介绍

[英]A representation of an instant in time. The instant is the number of nanoseconds after the number of seconds since the Unix Epoch.

Use Tracing.getClock().now() to get the current timestamp since epoch (1970-01-01T00:00:00Z).
[中]一个瞬间在时间上的表现。instant是自Unix时代以来的秒数之后的纳秒数。
使用追踪。getClock()。now()获取自epoch(1970-01-01T00:00:00Z)以来的当前时间戳。

代码示例

代码示例来源:origin: census-instrumentation/opencensus-java

private static long timestampToNanos(final Timestamp timestamp) {
 return TimeUnit.SECONDS.toNanos(timestamp.getSeconds()) + timestamp.getNanos();
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void timestampCreate() {
 assertThat(Timestamp.create(24, 42).getSeconds()).isEqualTo(24);
 assertThat(Timestamp.create(24, 42).getNanos()).isEqualTo(42);
 assertThat(Timestamp.create(-24, 42).getSeconds()).isEqualTo(-24);
 assertThat(Timestamp.create(-24, 42).getNanos()).isEqualTo(42);
 assertThat(Timestamp.create(315576000000L, 999999999).getSeconds()).isEqualTo(315576000000L);
 assertThat(Timestamp.create(315576000000L, 999999999).getNanos()).isEqualTo(999999999);
 assertThat(Timestamp.create(-315576000000L, 999999999).getSeconds()).isEqualTo(-315576000000L);
 assertThat(Timestamp.create(-315576000000L, 999999999).getNanos()).isEqualTo(999999999);
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Override
public Timestamp now() {
 return Timestamp.fromMillis(System.currentTimeMillis());
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

/**
 * Creates a new timestamp from the given milliseconds.
 *
 * @param epochMilli the timestamp represented in milliseconds since epoch.
 * @return new {@code Timestamp} with specified fields. For invalid inputs, a {@code Timestamp} of
 *     zero is returned.
 */
public static Timestamp fromMillis(long epochMilli) {
 long secs = floorDiv(epochMilli, MILLIS_PER_SECOND);
 int mos = (int) floorMod(epochMilli, MILLIS_PER_SECOND);
 return create(secs, (int) (mos * NANOS_PER_MILLI)); // Safe int * NANOS_PER_MILLI
}

代码示例来源:origin: census-instrumentation/opencensus-java

private Timestamp plus(long secondsToAdd, long nanosToAdd) {
 if ((secondsToAdd | nanosToAdd) == 0) {
  return this;
 }
 long epochSec = TimeUtils.checkedAdd(getSeconds(), secondsToAdd);
 epochSec = TimeUtils.checkedAdd(epochSec, nanosToAdd / NANOS_PER_SECOND);
 nanosToAdd = nanosToAdd % NANOS_PER_SECOND;
 long nanoAdjustment = getNanos() + nanosToAdd; // safe int + NANOS_PER_SECOND
 return ofEpochSecond(epochSec, nanoAdjustment);
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
 public void timestamp_Equal() {
  // Positive tests.
  assertThat(Timestamp.create(0, 0)).isEqualTo(Timestamp.create(0, 0));
  assertThat(Timestamp.create(24, 42)).isEqualTo(Timestamp.create(24, 42));
  assertThat(Timestamp.create(-24, 42)).isEqualTo(Timestamp.create(-24, 42));
  // Negative tests.
  assertThat(Timestamp.create(25, 42)).isNotEqualTo(Timestamp.create(24, 42));
  assertThat(Timestamp.create(24, 43)).isNotEqualTo(Timestamp.create(24, 42));
  assertThat(Timestamp.create(-25, 42)).isNotEqualTo(Timestamp.create(-24, 42));
  assertThat(Timestamp.create(-24, 43)).isNotEqualTo(Timestamp.create(-24, 42));
 }
}

代码示例来源:origin: census-instrumentation/opencensus-java

@SuppressWarnings("deprecation")
private static void emitSingleSpan(Formatter formatter, SpanData span) {
 Calendar calendar = Calendar.getInstance();
 calendar.setTimeInMillis(TimeUnit.SECONDS.toMillis(span.getStartTimestamp().getSeconds()));
 long microsField = TimeUnit.NANOSECONDS.toMicros(span.getStartTimestamp().getNanos());
 String elapsedSecondsStr =
   span.getEndTimestamp() != null
     ? String.format(
       "%13.6f",
       durationToNanos(span.getEndTimestamp().subtractTimestamp(span.getStartTimestamp()))
         * 1.0e-9)
     : String.format("%13s", " ");
      durationToNanos(event.getTimestamp().subtractTimestamp(lastTimestampNanos)));
  String deltaString;
  if (deltaMicros >= 1000000) {
    TimeUnit.SECONDS.toMillis(event.getTimestamp().getSeconds())
      + TimeUnit.NANOSECONDS.toMillis(event.getTimestamp().getNanos()));
  microsField = TimeUnit.NANOSECONDS.toMicros(event.getTimestamp().getNanos());

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void timestampFromMillis_Negative() {
 assertThat(Timestamp.fromMillis(-1)).isEqualTo(Timestamp.create(-1, 999000000));
 assertThat(Timestamp.fromMillis(-999)).isEqualTo(Timestamp.create(-1, 1000000));
 assertThat(Timestamp.fromMillis(-3456)).isEqualTo(Timestamp.create(-4, 544000000));
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void timestampAddDuration() {
 Timestamp timestamp = Timestamp.create(1234, 223);
 assertThat(timestamp.addDuration(Duration.create(1, 0))).isEqualTo(Timestamp.create(1235, 223));
 assertThat(timestamp.addDuration(Duration.create(0, 1))).isEqualTo(Timestamp.create(1234, 224));
 assertThat(timestamp.addDuration(Duration.create(1, 1))).isEqualTo(Timestamp.create(1235, 224));
 assertThat(timestamp.addDuration(Duration.create(1, 999999900)))
   .isEqualTo(Timestamp.create(1236, 123));
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void timestamp_CompareTo() {
 assertThat(Timestamp.create(0, 0).compareTo(Timestamp.create(0, 0))).isEqualTo(0);
 assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(24, 42))).isEqualTo(0);
 assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-24, 42))).isEqualTo(0);
 assertThat(Timestamp.create(25, 42).compareTo(Timestamp.create(24, 42))).isEqualTo(1);
 assertThat(Timestamp.create(24, 45).compareTo(Timestamp.create(24, 42))).isEqualTo(1);
 assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(25, 42))).isEqualTo(-1);
 assertThat(Timestamp.create(24, 42).compareTo(Timestamp.create(24, 45))).isEqualTo(-1);
 assertThat(Timestamp.create(-25, 42).compareTo(Timestamp.create(-24, 42))).isEqualTo(-1);
 assertThat(Timestamp.create(-24, 45).compareTo(Timestamp.create(-24, 42))).isEqualTo(1);
 assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-25, 42))).isEqualTo(1);
 assertThat(Timestamp.create(-24, 42).compareTo(Timestamp.create(-24, 45))).isEqualTo(-1);
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void timestampSubtractTimestamp_NegativeResult() {
 Timestamp timestamp = Timestamp.create(1234, 223);
 assertThat(timestamp.subtractTimestamp(Timestamp.create(1235, 223)))
   .isEqualTo(Duration.create(-1, 0));
 assertThat(timestamp.subtractTimestamp(Timestamp.create(1234, 224)))
   .isEqualTo(Duration.create(0, -1));
 assertThat(timestamp.subtractTimestamp(Timestamp.create(1235, 224)))
   .isEqualTo(Duration.create(-1, -1));
 assertThat(timestamp.subtractTimestamp(Timestamp.create(1236, 123)))
   .isEqualTo(Duration.create(-1, -999999900));
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void timestampAddNanos_Negative() {
 Timestamp timestamp = Timestamp.create(1234, 223);
 assertThat(timestamp.addNanos(-223)).isEqualTo(Timestamp.create(1234, 0));
 assertThat(timestamp.addNanos(-1000000223)).isEqualTo(Timestamp.create(1233, 0));
 assertThat(timestamp.addNanos(-1300200500)).isEqualTo(Timestamp.create(1232, 699799723));
 assertThat(timestamp.addNanos(-4123456213L)).isEqualTo(Timestamp.create(1229, 876544010));
 assertThat(timestamp.addNanos(Long.MIN_VALUE))
   .isEqualTo(Timestamp.create(1234L - 9223372036L - 1, 223 + 145224192));
}

代码示例来源:origin: census-instrumentation/opencensus-java

/**
 * Advances the time by a duration.
 *
 * @param duration the increase in time.
 * @since 0.5
 */
public synchronized void advanceTime(Duration duration) {
 currentTime = validateNanos(currentTime.addDuration(duration));
}

代码示例来源:origin: census-instrumentation/opencensus-java

private void refreshBucketList(Timestamp now) {
 if (buckets.size() != N + 1) {
  throw new AssertionError("Bucket list must have exactly " + (N + 1) + " buckets.");
 }
 Timestamp startOfLastBucket =
   CheckerFrameworkUtils.castNonNull(buckets.peekLast()).getStart();
 // TODO(songya): decide what to do when time goes backwards
 checkArgument(
   now.compareTo(startOfLastBucket) >= 0,
   "Current time must be within or after the last bucket.");
 long elapsedTimeMillis = now.subtractTimestamp(startOfLastBucket).toMillis();
 long numOfPadBuckets = elapsedTimeMillis / bucketDuration.toMillis();
 shiftBucketList(numOfPadBuckets, now);
}

代码示例来源:origin: census-instrumentation/opencensus-java

/**
  * Constructs a new {@link CumulativeData}.
  *
  * @since 0.8
  */
 public static CumulativeData create(Timestamp start, Timestamp end) {
  if (start.compareTo(end) > 0) {
   throw new IllegalArgumentException("Start time is later than end time.");
  }
  return new AutoValue_ViewData_AggregationWindowData_CumulativeData(start, end);
 }
}

代码示例来源:origin: census-instrumentation/opencensus-java

private static long toMillis(Timestamp start, Timestamp end) {
 Duration duration = end.subtractTimestamp(start);
 return SECONDS.toMillis(duration.getSeconds()) + NANOSECONDS.toMillis(duration.getNanos());
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void setAndGetTime() {
 TestClock clock = TestClock.create(Timestamp.create(1, 2));
 assertThat(clock.now()).isEqualTo(Timestamp.create(1, 2));
 clock.setTime(Timestamp.create(3, 4));
 assertThat(clock.now()).isEqualTo(Timestamp.create(3, 4));
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

private Timestamp plus(long secondsToAdd, long nanosToAdd) {
 if ((secondsToAdd | nanosToAdd) == 0) {
  return this;
 }
 long epochSec = LongMath.checkedAdd(getSeconds(), secondsToAdd);
 epochSec = LongMath.checkedAdd(epochSec, nanosToAdd / NANOS_PER_SECOND);
 nanosToAdd = nanosToAdd % NANOS_PER_SECOND;
 long nanoAdjustment = getNanos() + nanosToAdd; // safe int + NANOS_PER_SECOND
 return ofEpochSecond(epochSec, nanoAdjustment);
}

代码示例来源:origin: census-instrumentation/opencensus-java

@Test
public void timestampFromMillis() {
 assertThat(Timestamp.fromMillis(0)).isEqualTo(Timestamp.create(0, 0));
 assertThat(Timestamp.fromMillis(987)).isEqualTo(Timestamp.create(0, 987000000));
 assertThat(Timestamp.fromMillis(3456)).isEqualTo(Timestamp.create(3, 456000000));
}

代码示例来源:origin: census-instrumentation/opencensus-java

/**
 * Creates a new timestamp from the given milliseconds.
 *
 * @param epochMilli the timestamp represented in milliseconds since epoch.
 * @return new {@code Timestamp} with specified fields.
 * @throws IllegalArgumentException if the number of milliseconds is out of the range that can be
 *     represented by {@code Timestamp}.
 * @since 0.5
 */
public static Timestamp fromMillis(long epochMilli) {
 long secs = floorDiv(epochMilli, MILLIS_PER_SECOND);
 int mos = (int) floorMod(epochMilli, MILLIS_PER_SECOND);
 return create(secs, (int) (mos * NANOS_PER_MILLI)); // Safe int * NANOS_PER_MILLI
}

相关文章