Java Code Examples for java.time.Clock#instant()

The following examples show how to use java.time.Clock#instant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NodeFailer.java    From vespa with Apache License 2.0 6 votes vote down vote up
public NodeFailer(Deployer deployer, HostLivenessTracker hostLivenessTracker,
                  ServiceMonitor serviceMonitor, NodeRepository nodeRepository,
                  Duration downTimeLimit, Clock clock, Orchestrator orchestrator,
                  ThrottlePolicy throttlePolicy, Metric metric) {
    // check ping status every five minutes, but at least twice as often as the down time limit
    super(nodeRepository, min(downTimeLimit.dividedBy(2), Duration.ofMinutes(5)));
    this.deployer = deployer;
    this.hostLivenessTracker = hostLivenessTracker;
    this.serviceMonitor = serviceMonitor;
    this.downTimeLimit = downTimeLimit;
    this.clock = clock;
    this.orchestrator = orchestrator;
    this.constructionTime = clock.instant();
    this.throttlePolicy = throttlePolicy;
    this.metric = metric;
}
 
Example 2
Source File: MonitoringServerCall.java    From java-grpc-prometheus with Apache License 2.0 6 votes vote down vote up
MonitoringServerCall(
    ServerCall<R,S> delegate,
    Clock clock,
    GrpcMethod grpcMethod,
    ServerMetrics serverMetrics,
    Configuration configuration) {
  super(delegate);
  this.clock = clock;
  this.grpcMethod = grpcMethod;
  this.serverMetrics = serverMetrics;
  this.configuration = configuration;
  this.startInstant = clock.instant();

  // TODO(dino): Consider doing this in the onReady() method of the listener instead.
  reportStartMetrics();
}
 
Example 3
Source File: TCKClock_System.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
Example 4
Source File: TCKClock_System.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
Example 5
Source File: TCKClock_System.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
Example 6
Source File: TCKClock_Tick.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_tickMinutes_ZoneId() {
    Clock test = Clock.tickMinutes(PARIS);
    assertEquals(test.getZone(), PARIS);
    Instant instant = test.instant();
    assertEquals(instant.getEpochSecond() % 60, 0);
    assertEquals(instant.getNano(), 0);
}
 
Example 7
Source File: SleeperTest.java    From riptide with MIT License 5 votes vote down vote up
@Test
void sleep() {
    final Clock clock = Clock.systemUTC();
    final Instant start = clock.instant();

    unit.sleep(Duration.ofSeconds(1));

    final Instant end = clock.instant();

    assertThat(Duration.between(start, end), is(greaterThanOrEqualTo(Duration.ofSeconds(1))));
}
 
Example 8
Source File: TCKClock_System.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
Example 9
Source File: TCKClock_System.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
Example 10
Source File: AdminService.java    From kafka-pubsub-emulator with Apache License 2.0 5 votes vote down vote up
@Inject
AdminService(
    ConfigurationManager configurationManager, Clock clock, StatisticsManager statisticsManager) {
  this.configurationManager = configurationManager;
  this.clock = clock;
  this.startedAt = clock.instant();
  this.statisticsManager = statisticsManager;
}
 
Example 11
Source File: TCKClock_Tick.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void test_tickMinutes_ZoneId() {
    Clock test = Clock.tickMinutes(PARIS);
    assertEquals(test.getZone(), PARIS);
    Instant instant = test.instant();
    assertEquals(instant.getEpochSecond() % 60, 0);
    assertEquals(instant.getNano(), 0);
}
 
Example 12
Source File: TCKClock_System.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
Example 13
Source File: TCKClock_System.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_instant() {
    Clock system = Clock.systemUTC();
    assertEquals(system.getZone(), ZoneOffset.UTC);
    for (int i = 0; i < 10000; i++) {
        // assume can eventually get these within 10 milliseconds
        Instant instant = system.instant();
        long systemMillis = System.currentTimeMillis();
        if (systemMillis - instant.toEpochMilli() < 10) {
            return;  // success
        }
    }
    fail();
}
 
Example 14
Source File: TCKClock_Tick.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void test_tickMinutes_ZoneId() {
    Clock test = Clock.tickMinutes(PARIS);
    assertEquals(test.getZone(), PARIS);
    Instant instant = test.instant();
    assertEquals(instant.getEpochSecond() % 60, 0);
    assertEquals(instant.getNano(), 0);
}
 
Example 15
Source File: RefreshableLockManager.java    From kork with Apache License 2.0 5 votes vote down vote up
public HeartbeatLockRequest(
    Lock lock,
    AtomicInteger retriesOnFailure,
    Clock clock,
    Duration heartbeatDuration,
    boolean reuseLockVersion) {
  this.lock = new AtomicReference<>(lock);
  this.retriesOnFailure = retriesOnFailure;
  this.clock = clock;
  this.startedAt = clock.instant();
  this.heartbeatDuration = heartbeatDuration;
  this.reuseLockVersion = reuseLockVersion;
  this.requestId = lock.getName() + lock.getOwnerName() + clock.millis();
}
 
Example 16
Source File: Timing.java    From jenetics with Apache License 2.0 4 votes vote down vote up
private static long nanos(final Clock clock) {
	final Instant now = clock.instant();
	return now.getEpochSecond()*NanoClock.NANOS_PER_SECOND + now.getNano();
}
 
Example 17
Source File: TestClock_System.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static void testWithOffset(String name, long offset, Clock clock)
        throws IllegalAccessException {
    offsetField.set(clock, offset);
    long beforeMillis = System.currentTimeMillis();
    final Instant instant = clock.instant();
    long afterMillis = System.currentTimeMillis();
    long actualOffset = offsetField.getLong(clock);
    long instantMillis = instant.getEpochSecond() * MILLIS_IN_SECOND
            + instant.getNano() / NANOS_IN_MILLI;
    if (instantMillis < beforeMillis || instantMillis > afterMillis) {
        throw new RuntimeException(name
                + ": Invalid instant: " + instant
                + " (~" + instantMillis + "ms)"
                + " when time in millis is in ["
                + beforeMillis + ", " + afterMillis
                + "] and offset in seconds is " + offset);
    }
    Answer isOffLimits = isOffLimits(beforeMillis / MILLIS_IN_SECOND,
            afterMillis / MILLIS_IN_SECOND, offset);
    switch (isOffLimits) {
        case YES:
            if (actualOffset == offset) {
                throw new RuntimeException(name
                        + ": offset was offlimit but was not recomputed "
                        + " when time in millis is in ["
                        + beforeMillis + ", " + afterMillis
                        + "] and offset in seconds was " + offset);
            }
            break;
        case NO:
            if (actualOffset != offset) {
                throw new RuntimeException(name
                        + ": offset was not offlimit but was recomputed.");
            }
            break;
        default:
            break;
    }
    if (distance(actualOffset, instant.getEpochSecond()) >= MAX_OFFSET) {
        throw new RuntimeException(name + ": Actual offset is too far off:"
                + " offset=" + actualOffset
                + "instant.seconds=" + instant.getEpochSecond());
    }
    long adjustment = (instant.getEpochSecond() - actualOffset) * NANOS_IN_SECOND
            + instant.getNano();
    validateAdjustment(name, actualOffset, beforeMillis, afterMillis, adjustment);
}
 
Example 18
Source File: inetTest.java    From jbang with MIT License 4 votes vote down vote up
public NanoClock(final Clock clock)
{
    this.clock = clock;
    initialInstant = clock.instant();
    initialNanos = getSystemNanos();
}
 
Example 19
Source File: JavaClockExample.java    From journaldev with MIT License 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
	
	//1. Simple example with default system time zone
	Clock clock = Clock.systemDefaultZone();
	System.out.println(clock.getZone());

	//2. Clock instant example
	Instant instant = clock.instant();
	System.out.println(instant);
	
	//3. UTC Time zone
	clock = Clock.systemUTC();
	System.out.println(clock.getZone());

	//4. using specific time zone
	clock = Clock.system(ZoneId.of("Europe/Paris"));
	System.out.println(clock.instant());
	
	//5. Get current milliseconds
	System.out.println(clock.millis());
	System.out.println(System.currentTimeMillis());
	
	//6. Offset to future or past time
	Clock pastClock = Clock.offset(clock, Duration.ofMillis(-10000));
	System.out.println(clock.millis() - pastClock.millis());

	Clock futureClock = Clock.offset(clock, Duration.ofDays(1));
	System.out.println(futureClock.millis() - clock.millis());
	
	//7. tick example
	Clock nearestHourClock = Clock.tick(clock, Duration.ofHours(1));
	System.out.println(clock.instant());
	System.out.println(nearestHourClock.instant());
	
	Clock nearestSecondClock = Clock.tickSeconds(ZoneId.systemDefault());
	System.out.println(nearestSecondClock);
	System.out.println(nearestSecondClock.instant());
	
	//8. Fixed Clock
	Clock fixedClock = Clock.fixed(instant, ZoneId.systemDefault());
	System.out.println(fixedClock);
	System.out.println(fixedClock.instant());
	Thread.sleep(1000);
	System.out.println(fixedClock.instant());

}
 
Example 20
Source File: StopCondition.java    From synapse with Apache License 2.0 3 votes vote down vote up
/**
 * Returns a stop condition that is true if at least one message contained in the {@code ShardResponse} has arrived
 * after creating the predicate.
 *
 * @param clock the clock used to get the current time
 *
 * @return stop condition
 */
public static Predicate<ShardResponse> arrivalTimestampAfterNow(final Clock clock) {
    final Instant now = clock.instant();
    return (response) -> response
            .getMessages()
            .stream()
            .anyMatch(m -> m.getHeader().getAsInstant(DefaultHeaderAttr.MSG_ARRIVAL_TS, Instant.MIN).isAfter(now));
}