1

I can get the current timestamp as <timestamp::Module<T>>::get() in substrate runtime module.

How can I perform basic arithmetic (addition, substraction) with it?

decl_module! {
  pub struct Module<T: Trait> for enum Call where origin: T::Origin {
    fn deposit_event<T>() = default;

    pub fn func1(origin) -> Result {
      let now = <timestamp::Module<T>>::get();
      const DURATION = 60 * 5;
      // what is the proper way of performing the following operation?
      // let future = now + DURATION; 

      // At some point in future, can I perform the following comparison?
      if now > future { 
        // ... some code 
      }
    }
  }
}

Further Question:

This brings out a question I am not sure about Rust / Rust doc. Type T::Moment has to have trait SimpleArithmetic, which in turns require the type has trait TryInto<u32>.

So this should work,

let tmp: u32 = DURATION + now.try_into()?;

but actually returning:

error[E0277]: cannot add `()` to `u32`
| no implementation for `u32 + ()`
|
= help: the trait `core::ops::Add<()>` is not implemented for `u32`

error[E0271]: type mismatch resolving `<() as core::convert::TryFrom<<T as srml_timestamp::Trait>::Moment>>::Error == &str`
| note: expected type `core::convert::Infallible`
=               found type `&str`

Further Question - 2

Basically, I went through this thread. Could you post an example how to convert from Timestamp to u32/u64, and from u32/u64 to Timestamp, and what additional modules need to be brought in?

Thanks.

Jimmy Chu
  • 972
  • 8
  • 27

1 Answers1

2

I wasn't able to figure out how to use into(), try_into(), from(), try_from().

But from Shawn example and what Bryan said to avoid, I can convert timestamp to u64 easily by: now.as_().

If anyone can show me the answer using into(), from() or its variant, I will be happy to update this thread and mark it as the correct answer.

Jimmy Chu
  • 972
  • 8
  • 27
  • 1
    The `into()` and `from()` stuff is only in Substrate v2.0 and later. If you are using Substrate v1.0, this is where you might be struggling. – Shawn Tabrizi Jul 10 '19 at 13:46