55 releases (35 major breaking)

37.0.0 Sep 26, 2024
36.0.1 Aug 29, 2024
36.0.0 Jul 18, 2024
35.0.0 Jul 12, 2024
2.0.0-alpha.5 Mar 24, 2020

#28 in #substrate

Download history 3062/week @ 2024-08-27 2675/week @ 2024-09-03 3249/week @ 2024-09-10 3200/week @ 2024-09-17 4368/week @ 2024-09-24 3454/week @ 2024-10-01 2689/week @ 2024-10-08 3890/week @ 2024-10-15 4714/week @ 2024-10-22 4022/week @ 2024-10-29 4921/week @ 2024-11-05 27494/week @ 2024-11-12 29302/week @ 2024-11-19 30217/week @ 2024-11-26 29599/week @ 2024-12-03 25641/week @ 2024-12-10

120,180 downloads per month
Used in 152 crates (75 directly)

Apache-2.0

2MB
38K 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 pallet_timestamp::{self as timestamp};

#[frame_support::pallet]
pub mod pallet {
    use super::*;
    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;

    #[pallet::pallet]
    pub struct Pallet<T>(_);

    #[pallet::config]
    pub trait Config: frame_system::Config + timestamp::Config {}

    #[pallet::call]
    impl<T: Config> Pallet<T> {
        #[pallet::weight(0)]
        pub fn get_time(origin: OriginFor<T>) -> DispatchResult {
            let _sender = ensure_signed(origin)?;
            let _now = <timestamp::Pallet<T>>::get();
            Ok(())
        }
    }
}

Example from the FRAME

The Session module uses the Timestamp module for session management.

License: Apache-2.0

Release

Polkadot SDK stable2409

Dependencies

~17–32MB
~530K SLoC