# Timestamp or date in Rust or Motoko

**URL:** <https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391>\
**Category:** Language Support\
**Created:** [October 8, 2020, 5:59am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391 "2020-10-08T05:59:53Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 8, 2020, 5:59am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/1 "2020-10-08T05:59:53Z")

</div>

I’m using the Rust CDK, but this is relevant to Motoko as well. I want to generate a timestamp or get the current date and time…how do I do this? I did a quick skim through the Motoko documentation and didn’t see anything.

---

<div class="post-metadata">

**Author:** ![wang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/wang/32/446_2.png) [@wang](https://forum.dfinity.org/u/wang)\
**Post date:** [October 8, 2020, 7:01am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/2 "2020-10-08T07:01:54Z")

</div>

[`Time.now()`](https://github.com/dfinity/motoko-base/blob/master/src/Time.mo#L13) returns the canisters’ timestamp in nanoseconds

---

<div class="post-metadata">

**Author:** ![morrolan](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@morrolan](https://forum.dfinity.org/u/morrolan)\
**Post date:** [October 8, 2020, 9:51am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/3 "2020-10-08T09:51:07Z")

</div>

NB that timestamp seems to be static within the context of a message/request. So can’t use it to do any kind of timing within one such request.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 8, 2020, 5:25pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/4 "2020-10-08T17:25:45Z")

</div>

That is what you’re going to have to go with because of the distributed nature of the calculation. If your output depends on the timing of internal processes you lose determinism due to different memory management, processing, etc. You’d never be able to reach consensus.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [October 8, 2020, 5:27pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/5 "2020-10-08T17:27:28Z")

</div>

On second thought you could split the call into two functions where the second one is gated by a certain timestep. Microseconds is going to be tough though and you minimum unit is going to need to be blocks/second.

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [October 8, 2020, 5:29pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/6 "2020-10-08T17:29:37Z")

</div>

Echoing the comments above. The timestamp is provided by the block maker and ratified by the notary group.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [October 8, 2020, 8:14pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/7 "2020-10-08T20:14:31Z")

</div>

Great information everyone, thanks. Does anyone have the Rust code to get the time?

---

<div class="post-metadata">

**Author:** ![enzo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/enzo/32/268_2.png) [@enzo](https://forum.dfinity.org/u/enzo)\
**Post date:** [October 8, 2020, 8:17pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/8 "2020-10-08T20:17:08Z")

</div>

> **[ic\_cdk::api::time - Rust](https://docs.rs/ic-cdk/0.1.1/ic_cdk/api/fn.time.html)**

---

<div class="post-metadata">

**Author:** ![wang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/wang/32/446_2.png) [@wang](https://forum.dfinity.org/u/wang)\
**Post date:** [October 9, 2020, 5:48am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/9 "2020-10-09T05:48:58Z")

</div>

Here’s a hack to update the timestamp by `await`-ing:

```auto
actor Timer {
  func noop(): async () {};

  public func call(): async () {
    let a = Time.now();
    await noop();
    let b = Time.now();
  }
}

```

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [October 10, 2020, 7:35pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/10 "2020-10-10T19:35:08Z")

</div>

I believe this hack should be considered harmful. Why would someone use it?  
I mean, one could just plus some value to the first timestamp.

---

<div class="post-metadata">

**Author:** ![wang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/wang/32/446_2.png) [@wang](https://forum.dfinity.org/u/wang)\
**Post date:** [October 11, 2020, 3:33am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/11 "2020-10-11T03:33:22Z")

</div>

Haha this really is a hack, it introduces the latency of another async call which could be several seconds. I guess if you’re doing really long synchronous operations and you need an updated timestamp, this could be useful…

---

<div class="post-metadata">

**Author:** ![Harsh](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Harsh](https://forum.dfinity.org/u/Harsh)\
**Post date:** [March 14, 2022, 3:14pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/12 "2022-03-14T15:14:51Z")

</div>

Hi. Is this local time?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [March 14, 2022, 10:24pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/13 "2022-03-14T22:24:57Z")

</div>

### System time

The canister can query the IC for the current time.

`ic0.time : () -> i64`

The time is given as nanoseconds since 1970-01-01. The IC guarantees that

- the time, as observed by the canister, is monotonically increasing, even across canister upgrades.
- within an invocation of one entry point, the time is constant.

The system times of different canisters are unrelated, and calls from one canister to another may appear to travel “backwards in time”.

* * *

**NOTE**

> While an implementation will likely try to keep the System Time close to the real time, this is not formally part of this specification.

> **[World Computer | Internet Computer](https://internetcomputer.org/)**
>
> The Internet Computer hosts secure, network-resident code and data. Build web apps without Big Tech and current IT. Applications are immune to cyber attacks and unstoppable, capable of processing tokens, and can run under exclusive DAO control. Build...

---

<div class="post-metadata">

**Author:** ![jzxchiang](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/jzxchiang/32/2592_2.png) [@jzxchiang](https://forum.dfinity.org/u/jzxchiang)\
**Post date:** [March 15, 2022, 12:24am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/14 "2022-03-15T00:24:17Z")

</div>

Also be careful that even though the IC timestamp has nanosecond granularity, I believe it is still coarse in the sense that all messages in the same block will have the same time.

So if you request IC time in quick succession, the timestamps will have duplicates.

---

<div class="post-metadata">

**Author:** ![amaralc](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/amaralc/32/2081_2.png) [@amaralc](https://forum.dfinity.org/u/amaralc)\
**Post date:** [May 23, 2022, 5:45pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/15 "2022-05-23T17:45:29Z")

</div>

How can I call ic from within my canister? Can you point me to an example? Do I have to import something?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [May 23, 2022, 5:54pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/16 "2022-05-23T17:54:02Z")

</div>

In Motoko, `import Time "mo:base/Time";` then call `Time.now()`.

In Rust, call `ic_cdk::api::time()`.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [December 12, 2022, 7:49pm UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/17 "2022-12-12T19:49:09Z")

</div>

A few timely questions ⌚ 🙂

Why was the decision made to use nanosecond precision instead of ms precision for canister facing APIs?

How reliable is the timestamp I receive in a canister during a query call where the first instruction is to call `Time.now()`?

When I call `Time.now()` am I getting the exact nanosecond time of the entire IC (all subnets’ and canister’s internal times are in complete synchronicity), or at some point in the past (several nanosecond drift)?

How does the IC sync it’s system time with the outside world? Does it use a single external API or consensus from several different APIs?

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [December 13, 2022, 12:06am UTC](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/18 "2022-12-13T00:06:33Z")

</div>

> [@icme](#):
>
> Why was the decision made to use nanosecond precision instead of ms precision for canister facing APIs?

I can only speculate that this is less restrictive and allows for more use cases.

I personally didn’t appreciate it until I worked in a sector that relied on such precision.

How practical it is on the IC given the other properties of how time works is probably a different matter 🙂

* * *

> [@icme](#):
>
> How reliable is the timestamp I receive in a canister during a query call where the first instruction is to call `Time.now()`?
> 
> When I call `Time.now()` am I getting the exact nanosecond time of the entire IC (all subnets’ and canister’s internal times are in complete synchronicity), or at some point in the past (several nanosecond drift)?

My previous post in this thread might answer some of this:

> [@Timestamp or date in Rust or Motoko](https://forum.dfinity.org/t/timestamp-or-date-in-rust-or-motoko/1391/13):
>
> System time The canister can query the IC for the current time. ic0.time : () -\> i64 The time is given as nanoseconds since 1970-01-01. The IC guarantees that the time, as observed by the canister, is monotonically increasing, even across canister upgrades. within an invocation of one entry point, the time is constant. The system times of different canisters are unrelated, and calls from one canister to another may appear to travel “backwards in time”. NOTE While an implementation will …
