Show the crate…

1 stable release

2.0.0 Apr 4, 2021

#50 in #tetcoin

Download history 57/week @ 2024-04-05 67/week @ 2024-04-12 68/week @ 2024-04-19 70/week @ 2024-04-26 56/week @ 2024-05-03 68/week @ 2024-05-10 60/week @ 2024-05-17 57/week @ 2024-05-24 43/week @ 2024-05-31 36/week @ 2024-06-07 57/week @ 2024-06-14 61/week @ 2024-06-21 30/week @ 2024-06-28 21/week @ 2024-07-05 55/week @ 2024-07-12 63/week @ 2024-07-19

170 downloads per month
Used in 24 crates (14 directly)

Apache-2.0

1MB
24K SLoC

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

  • set - Sets the current time.

Public functions

  • get - Gets the current time for the current block. If this function is called prior to setting the timestamp, it will return the timestamp of the previous block.

Config Getters

  • MinimumPeriod - Gets the minimum (and advised) period between blocks for the chain.

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 fabric_support::{decl_module, dispatch};
use fabric_system::ensure_signed;

pub trait Config: timestamp::Config {}

decl_module! {
	pub struct Module<T: Config> for enum Call where origin: T::Origin {
		#[weight = 0]
		pub fn get_time(origin) -> dispatch::DispatchResult {
			let _sender = ensure_signed(origin)?;
			let _now = <timestamp::Module<T>>::get();
			Ok(())
		}
	}
}

Example from the FABRIC

The Session module uses the Timestamp module for session management.

License: Apache-2.0

Dependencies

~3–14MB
~168K SLoC