Difference between two Timestamp

I’m trying to get the difference between two Timestamp typed variables.

First tried this:

let now:Timestamp  = ctx.metadata().slot_time();;
let time_since_last_withdrawal = now - host.state().last_withdrawal_time;
    if time_since_last_withdrawal < host.state().time_interval {
        return Err(Error::WithdrawalTimeNotReached);
    }

And got this error: cannot subtract concordium_std::Timestamp from `concordium_std::Timestamp

Below is another attempt:

let now:Timestamp  = ctx.metadata().slot_time();;
    let time_since_last_withdrawal = now.duration_since(host.state().last_withdrawal_time);
    if time_since_last_withdrawal < Some(host.state().time_interval.duration_between(host.state().time_interval)) {
        return Err(Error::WithdrawalTimeNotReached);
    }

And another error: mismatched types
expected enum Option<concordium_std::Duration>
** found struct `concordium_std::Timestamp**

Please how do I go about these type issues?

The Timestamp type does not support arithmetic operations like that.

But there are functions duration_since and duration_between you can use for your exact use case so.

if now.duration_since(host.state().last_withdrawal_time).map_or(false, |dur| dur < host.state().time_interval) {
   return Err(Error:WithdrawalTimeNotReached)
}

Thank you.

But the compiler still complains of mismatched type.

 if now
        .duration_since(host.state().last_withdrawal_time)
        .map_or(false, |dur| dur < host.state().time_interval)
    {
        return Err(Error::WithdrawalTimeNotReached);
    }

The error is in host.state().time_interval inside the map_or function.

Error:

mismatched types
expected struct `concordium_std::Duration`, found struct `concordium_std::Timestamp`

Can you show the definition of your state?
Specifically

time_interval

Does that have type Duration?

No, is of type Timestamp.

How do I initialise the init function, if I change it to type Duration?

You would pass in Duration as parameters to the init function.

1 Like