47 releases (28 major breaking)

30.0.0 Mar 18, 2024
29.0.0 Feb 26, 2024
28.0.0 Feb 13, 2024
27.0.0 Jan 23, 2024
2.0.0-alpha.5 Mar 24, 2020

#1371 in Magic Beans

Download history 1514/week @ 2023-12-06 2104/week @ 2023-12-13 1490/week @ 2023-12-20 845/week @ 2023-12-27 2208/week @ 2024-01-03 2721/week @ 2024-01-10 1891/week @ 2024-01-17 2104/week @ 2024-01-24 1679/week @ 2024-01-31 2838/week @ 2024-02-07 3114/week @ 2024-02-14 4043/week @ 2024-02-21 2920/week @ 2024-02-28 2691/week @ 2024-03-06 2905/week @ 2024-03-13 2936/week @ 2024-03-20

12,339 downloads per month
Used in 133 crates (70 directly)

Apache-2.0

2MB
37K 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

Dependencies

~18–37MB
~629K SLoC