Time
The convention is to name the module alias after the file name it is defined in.
The time module exposes one function now
that returns the IC system time represented as nanoseconds since 1970-01-01 as an Int
.
import Time "mo:base/Time";
let currentTime : Int = Time.now();
Time is constant within async
call
The system time is constant within one async
function call and any sub calls.
import Time "mo:base/Time";
actor {
type Time = Time.Time;
func time1() : Time { Time.now() };
public shared query func time2() : async Time { Time.now() };
public shared func time() : async (Time, Time, Time) {
let t1 = time1();
let t2 = await time2();
let t3 = Time.now();
(t1, t2, t3);
};
};
We import the module and declare an actor. The Time module exposes one type Time
that that is equal to Int
. We bring it into scope by renaming it.
We then declare a private function time1
and a query function time2
that both return the system Time
. And we declare a third update function time
that calls the first function, awaits the second function and request the system time once more.
All three time values returned in the tuple will be equal.
Monotonically increasing time
The time, as observed by the canister smart contract, is monotonically increasing after each function call. This is also the case across canister upgrades.
This means that we are guaranteed to get an increasingly larger Time
value when calling our function time
multiple times.