<timestamp::Module<T>>::get()
我可以像在基板运行时模块中一样获得当前时间戳。
如何使用它执行基本算术(加法、减法)?
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
}
}
}
}
进一步的问题:
这带来了一个我不确定 Rust / Rust doc 的问题。类型T::Moment
必须具有 trait SimpleArithmetic
,这反过来又要求类型具有 trait TryInto<u32>
。
所以这应该工作,
let tmp: u32 = DURATION + now.try_into()?;
但实际上返回:
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`
进一步的问题 - 2
基本上,我经历了这个线程。您能否发布一个示例如何从Timestamp
to u32
/u64
和 from u32
/ u64
to转换Timestamp
,以及需要引入哪些额外的模块?
谢谢。