// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc;

import com.google.inject.ImplementedBy;
import com.yahoo.jdisc.core.SystemTimer;

import java.time.Instant;

/**
 * <p>This class provides access to the current time in milliseconds, as viewed by the {@link Container}. Inject an
 * instance of this class into any component that needs to access time, instead of using
 * <code>System.currentTimeMillis()</code>.</p>
 *
 * @author Simon Thoresen Hult
 */
@ImplementedBy(SystemTimer.class)
public interface Timer {

    /**
     * <p>Returns the current time in milliseconds. Note that while the unit of time of the return value is a
     * millisecond, the granularity of the value depends on the underlying operating system and may be larger. For
     * example, many operating systems measure time in units of tens of milliseconds.</p>
     *
     * <p> See the description of the class <code>Date</code> for a discussion of slight discrepancies that may arise
     * between "computer time" and coordinated universal time (UTC).</p>
     *
     * @return The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
     * @see java.util.Date
     */
    long currentTimeMillis();

    /**
     * Convenience method for getting an java.util.Instance from currentTimeMillis().
     */
    default Instant currentTime() {
        return Instant.ofEpochMilli(currentTimeMillis());
    }

}

@@ -237,22 +237,100 @@
 va.time.
-Instan
+Clock;%0Aimport java.time.Instant;%0Aimport java.time.ZoneId;%0Aimport java.time.ZoneOffse
 t;%0A%0A/**%0A
@@ -1398,23 +1398,16 @@
 %0A    /**
-%0A     *
  Conveni
@@ -1477,21 +1477,16 @@
 illis().
-%0A    
  */%0A    
@@ -1582,10 +1582,777 @@
 %0A    %7D%0A%0A
+    /** Return a UTC Clock backed by this timer. */%0A    default Clock toUtcClock() %7B%0A        return new ClockAdapter(this, ZoneOffset.UTC);%0A    %7D%0A%0A    /** Create a Timer backed by the given Clock. */%0A    static Timer fromClock(Clock clock) %7B%0A        return clock::millis;%0A    %7D%0A%0A    class ClockAdapter extends Clock %7B%0A        private final Timer timer;%0A        private final ZoneId zoneId;%0A%0A        private ClockAdapter(Timer timer, ZoneId zoneId) %7B%0A            this.timer = timer;%0A            this.zoneId = zoneId;%0A        %7D%0A%0A        @Override public ZoneId getZone() %7B return zoneId; %7D%0A        @Override public Clock withZone(ZoneId zone) %7B return new ClockAdapter(timer, zoneId); %7D%0A        @Override public Instant instant() %7B return timer.currentTime(); %7D%0A    %7D%0A%0A
 %7D%0A

Convert Timer to/from UTC Clock