Skip to main content

Module 0x2::clock

APIs for accessing time from move calls, via the Clock: a unique shared object that is created at 0x6 during genesis.

use 0x2::object;
use 0x2::transfer;
use 0x2::tx_context;

Resource Clock

Singleton shared object that exposes time to Move calls. This object is found at address 0x6, and can only be read (accessed via an immutable reference) by entry functions.

Entry Functions that attempt to accept Clock by mutable reference or value will fail to verify, and honest validators will not sign or execute transactions that use Clock as an input parameter, unless it is passed by immutable reference.

struct Clock has key
Fields
id: object::UID
timestamp_ms: u64
The clock's timestamp, which is set automatically by a system transaction every time consensus commits a schedule, or by sui::clock::increment_for_testing during testing.

Constants

Sender is not @0x0 the system address.

const ENotSystemAddress: u64 = 0;

Function timestamp_ms

The clock's current timestamp as a running total of milliseconds since an arbitrary point in the past.

public fun timestamp_ms(clock: &clock::Clock): u64
Implementation
public fun timestamp_ms(clock: &Clock): u64 {
    clock.timestamp_ms
}

Function create

Create and share the singleton Clock -- this function is called exactly once, during genesis.

fun create(ctx: &tx_context::TxContext)
Implementation
fun create(ctx: &TxContext) {
    assert!(ctx.sender() == @0x0, ENotSystemAddress);

    transfer::share_object(Clock {
        id: object::clock(),
        // Initialised to zero, but set to a real timestamp by a
        // system transaction before it can be witnessed by a move
        // call.
        timestamp_ms: 0,
    })
}

Function consensus_commit_prologue

fun consensus_commit_prologue(clock: &mut clock::Clock, timestamp_ms: u64, ctx: &tx_context::TxContext)
Implementation
fun consensus_commit_prologue(
    clock: &mut Clock,
    timestamp_ms: u64,
    ctx: &TxContext,
) {
    // Validator will make a special system call with sender set as 0x0.
    assert!(ctx.sender() == @0x0, ENotSystemAddress);

    clock.timestamp_ms = timestamp_ms
}