net.i2p.util

## Class Clock

- java.lang.Object
- - net.i2p.util.Clock

- All Implemented Interfaces:
    Timestamper.UpdateListener
  Direct Known Subclasses:
    RouterClock

      public class Clock
      extends Object
      implements Timestamper.UpdateListener

  Alternate location for determining the time which takes into account an offset. This offset will ideally be periodically updated so as to serve as the difference between the local computer's current time and the time as known by some reference (such as an NTP synchronized clock). Protected members are used in the subclass RouterClock, which has access to a router's transports (particularly peer clock skews) to second-guess the sanity of clock adjustments.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `Clock.ClockUpdateListener` Listener for clock offset changes.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected boolean` | `_alreadyChanged`
    `protected I2PAppContext` | `_context`
    `protected boolean` | `_isSystemClockBad`
    `protected long` | `_offset`
    `protected long` | `_startedOn`
    `protected boolean` | `_statCreated`
    `static long` | `MAX_LIVE_OFFSET` after we've started up and shifted the clock, don't allow shifts of more than 10 minutes
    `static long` | `MAX_OFFSET` if the clock is skewed by 3+ days, forget it
    `static long` | `MIN_OFFSET_CHANGE` if the clock skewed changes by less than this, ignore the update (so we don't slide all over the place)

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Clock(I2PAppContext context)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addUpdateListener(Clock.ClockUpdateListener lsnr)`
    `protected void` | `fireOffsetChanged(long delta)`
    `static Clock` | `getInstance()`
    `protected Log` | `getLog()` we fetch it on demand to avoid circular dependencies (logging uses the clock)
    `long` | `getOffset()`
    `Timestamper` | `getTimestamper()` This is a dummy, see RouterClock and RouterTimestamper for the real thing
    `boolean` | `getUpdatedSuccessfully()`
    `long` | `now()` Retrieve the current time synchronized with whatever reference clock is in use.
    `void` | `removeUpdateListener(Clock.ClockUpdateListener lsnr)`
    `void` | `setNow(long realTime)`
    `void` | `setNow(long realTime, int stratum)` Warning - overridden in RouterClock
    `void` | `setOffset(long offsetMs)` Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast.
    `void` | `setOffset(long offsetMs, boolean force)` Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### _alreadyChanged

          protected boolean _alreadyChanged

    - #### _context

          protected final I2PAppContext _context

    - #### _isSystemClockBad

          protected final boolean _isSystemClockBad

    - #### _offset

          protected volatile long _offset

    - #### _startedOn

          protected long _startedOn

    - #### _statCreated

          protected boolean _statCreated

    - #### MAX_LIVE_OFFSET

          public static final long MAX_LIVE_OFFSET

      after we've started up and shifted the clock, don't allow shifts of more than 10 minutes
      See Also:
        Constant Field Values

    - #### MAX_OFFSET

          public static final long MAX_OFFSET

      if the clock is skewed by 3+ days, forget it
      See Also:
        Constant Field Values

    - #### MIN_OFFSET_CHANGE

          public static final long MIN_OFFSET_CHANGE

      if the clock skewed changes by less than this, ignore the update (so we don't slide all over the place)
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### Clock

          public Clock(I2PAppContext context)

  - ### Method Detail

    - #### addUpdateListener

          public void addUpdateListener(Clock.ClockUpdateListener lsnr)

    - #### fireOffsetChanged

          protected void fireOffsetChanged(long delta)

    - #### getInstance

          public static Clock getInstance()

    - #### getLog

          protected Log getLog()

      we fetch it on demand to avoid circular dependencies (logging uses the clock)

    - #### getOffset

          public long getOffset()

    - #### getTimestamper

          public Timestamper getTimestamper()

      This is a dummy, see RouterClock and RouterTimestamper for the real thing

    - #### getUpdatedSuccessfully

          public boolean getUpdatedSuccessfully()

    - #### now

          public long now()

      Retrieve the current time synchronized with whatever reference clock is in use.

    - #### removeUpdateListener

          public void removeUpdateListener(Clock.ClockUpdateListener lsnr)

    - #### setNow

          public void setNow(long realTime)

    - #### setNow

          public void setNow(long realTime,
                             int stratum)

      Warning - overridden in RouterClock
      Specified by:
        `setNow` in interface `Timestamper.UpdateListener`
      Parameters:
        `stratum` - ignored
        `realTime` - the current time

    - #### setOffset

          public void setOffset(long offsetMs)

      Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast.
      Parameters:
        `offsetMs` - the delta from System.currentTimeMillis() (NOT the delta from now())

    - #### setOffset

          public void setOffset(long offsetMs,
                                boolean force)

      Specify how far away from the "correct" time the computer is - a positive value means that the system time is slow, while a negative value means the system time is fast. Warning - overridden in RouterClock
      Parameters:
        `offsetMs` - the delta from System.currentTimeMillis() (NOT the delta from now())