[][src]Crate srml_timestamp

Timestamp Module

The Timestamp module provides functionality to get and set the on-chain time.

Overview

The Timestamp module allows the validators to set and validate a timestamp with each block.

It uses inherents for timestamp data, which is provided by the block author and validated/verified by other validators. The timestamp can be set only once per block and must be set each block. There could be a constraint on how much time must pass before setting the new timestamp.

NOTE: The Timestamp module is the recommended way to query the on-chain time instead of using an approach based on block numbers. The block number based time measurement can cause issues because of cumulative calculation errors and hence should be avoided.

Interface

Dispatchable Functions

Public functions

Trait Getters

Usage

The following example shows how to use the Timestamp module in your custom module to query the current timestamp.

Prerequisites

Import the Timestamp module into your custom module and derive the module configuration trait from the timestamp trait.

Get current timestamp

use support::{decl_module, dispatch::Result};
use system::ensure_signed;

pub trait Trait: timestamp::Trait {}

decl_module! {
	pub struct Module<T: Trait> for enum Call where origin: T::Origin {
		pub fn get_time(origin) -> Result {
			let _sender = ensure_signed(origin)?;
			let _now = <timestamp::Module<T>>::get();
			Ok(())
		}
	}
}

Example from the SRML

The Session module uses the Timestamp module for session management.

Structs

GenesisConfig
InherentDataProvider
Module
Now

Current time for the current block.

Enums

Call
InherentError

Errors that can occur while checking the timestamp inherent.

Constants

INHERENT_IDENTIFIER

The identifier for the timestamp inherent.

Traits

OnTimestampSet

A trait which is called when the timestamp is set.

TimestampInherentData

Auxiliary trait to extract timestamp inherent data.

Trait

The module configuration trait

Type Definitions

InherentType

The type of the inherent.