Home Reference Source
public class | source

Clock

Direct Subclass:

packages/core/src/Clock.js~FixedClock, packages/core/src/Clock.js~OffsetClock, packages/core/src/Clock.js~SystemClock

A clock providing access to the current instant, date and time using a time-zone.

Instances of this class are used to find the current instant, which can be interpreted using the stored time-zone to find the current date and time. As such, a clock can be used instead of System#currentTimeMillis and TimeZone#getDefault.

Use of a Clock is optional. All key date-time classes also have a now() factory method that uses the system clock in the default time zone. The primary purpose of this abstraction is to allow alternate clocks to be plugged in as and when required. Applications use an object to obtain the current time rather than a static method. This can simplify testing.

Best practice for applications is to pass a Clock into any method that requires the current instant.

This approach allows an alternate clock, such as fixed or offset to be used during testing.

The system factory methods provide clocks based on the best available system clock This may use System#currentTimeMillis, or a higher resolution clock if one is available.

The javascript Clock implementation differs from the openjdk.

Javascript only provides the UTC millis of epoch and the ZoneOffset in minutes of the system default time. Javascript do not provide the system default ZoneId.

the system default ZoneId is only guessable by the ZoneOffset, like moment-timezone does by returning one ZoneId with the same ZoneOffset.

Therefore we are doing a shortcut here, by defining a SystemUTCClock and a SystemDefaultClock, the Clock itself is returning the ZoneOffset and not the ZoneRules as in the jdk. We should change it, when introducing the iana timezone database and implementing the timezone domains.

Static Method Summary

Static Public Methods
public static

fixed(fixedInstant: Instant, zoneId: ZoneId): Clock

Obtains a clock that always returns the same instant.

public static

offset(baseClock: *, duration: *): *

Obtains a clock that returns instants from the specified clock with the specified duration added

This clock wraps another clock, returning instants that are later by the specified duration.

public static

system(zone: ZoneId): Clock

public static

Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.

public static

Obtains a clock that returns the current instant using the system clock, converting to date and time using the Date.getTime() UTC millis.

Method Summary

Public Methods
public

Gets the current instant of the clock.

public

millis(): *

Gets the current millisecond instant of the clock.

public

withZone(): *

Returns a copy of this clock with a different time-zone.

public

zone()

Static Public Methods

public static fixed(fixedInstant: Instant, zoneId: ZoneId): Clock source

Obtains a clock that always returns the same instant.

This clock simply returns the specified instant. As such, it is not a clock in the conventional sense. The main use case for this is in testing, where the fixed clock ensures tests are not dependent on the current clock.

Params:

NameTypeAttributeDescription
fixedInstant Instant

the instant to use as the clock, not null

zoneId ZoneId

the zoneOffset to use as zone Offset, not null

Return:

Clock

a clock that always returns the same instant, not null

public static offset(baseClock: *, duration: *): * source

Obtains a clock that returns instants from the specified clock with the specified duration added

This clock wraps another clock, returning instants that are later by the specified duration. If the duration is negative, the instants will be earlier than the current date and time. The main use case for this is to simulate running in the future or in the past.

A duration of zero would have no offsetting effect. Passing zero will return the underlying clock.

The returned implementation is immutable, thread-safe and {@code Serializable} providing that the base clock is.

Params:

NameTypeAttributeDescription
baseClock *

the base clock to add the duration to, not null

duration *

the duration to add, not null

Return:

*

a clock based on the base clock with the duration added, not null

public static system(zone: ZoneId): Clock source

Params:

NameTypeAttributeDescription
zone ZoneId

Return:

Clock

a clock that uses the specified time zone

public static systemDefaultZone(): Clock source

Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.

This clock is based on the available system clock using the Date.getTime() UTC millis

Using this method hard codes a dependency to the default time-zone into your application.

The UTC clock (see systemUTC) should be used when you need the current instant without the date or time.

Return:

Clock

a clock that uses the system clock in the default zone, not null

See:

public static systemUTC(): Clock source

Obtains a clock that returns the current instant using the system clock, converting to date and time using the Date.getTime() UTC millis.

This clock, rather than systemDefaultZone, should be used when you need the current instant without the date or time.

Return:

Clock

a clock that uses the system clock in the UTC zone, not null

Public Methods

public instant(): Instant source

Gets the current instant of the clock.

This returns an instant representing the current instant as defined by the clock.

Return:

Instant

the current instant from this clock, not null

public millis(): * source

Gets the current millisecond instant of the clock.

This returns the millisecond-based instant, measured from 1970-01-01T00:00Z (UTC). This is equivalent to the definition of Date#getTime.

Most applications should avoid this method and use Instant to represent an instant on the time-line rather than a raw millisecond value. This method is provided to allow the use of the clock in high performance use cases where the creation of an object would be unacceptable.

The default implementation currently calls instant.

Return:

*

the current millisecond instant from this clock, measured from the Java epoch of 1970-01-01T00:00Z (UTC), not null

public withZone(): * source

Returns a copy of this clock with a different time-zone.

A clock will typically obtain the current instant and then convert that to a date or time using a time-zone. This method returns a clock with similar properties but using a different time-zone.

Return:

*

a clock based on this clock with the specified time-zone, not null

public zone() source