# What is Time's precision and does it support microsecond?

**URL:** <https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555>\
**Category:** App Development\
**Created:** [May 29, 2020, 3:32am UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555 "2020-05-29T03:32:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frankie](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/frankie/32/646_2.png) [@Frankie](https://forum.canton.network/u/Frankie)\
**Post date:** [May 29, 2020, 3:32am UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/1 "2020-05-29T03:32:42Z")

</div>

In DAML, the docs mention that `Time` type represents a `datetime` in UTC. Does it support microsecond precision. If it does, how to retrieve the microsecond part?

`show Time` doesn’t reveal microsecond part. However you can do `subTime -> Time -> Time -> RelTime` in which `RelTime` is to microsecond precision.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [May 29, 2020, 6:17am UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/2 "2020-05-29T06:17:08Z")

</div>

Hello Frankie, and welcome to the forums!

Great question. I think this Q, or a similar one has been discussed here: [Time with millisecond precision](https://discuss.daml.com/t/time-with-millisecond-precision/314)

Have a look and let us know if this satisfies you.

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [May 29, 2020, 7:30am UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/3 "2020-05-29T07:30:57Z")

</div>

I had a look at the answer @Luciano linked and found it wanting. Have look at the example in my answer [here](https://discuss.daml.com/t/time-with-millisecond-precision/314/3) on how to extract microseconds from times and dates.

Note that ledger time is a fuzzy concept, though. If your business logic starts relying on times in the second range, you are probably going to run into contention and race conditions.

---

<div class="post-metadata">

**Author:** ![Frankie](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/frankie/32/646_2.png) [@Frankie](https://forum.canton.network/u/Frankie)\
**Post date:** [May 30, 2020, 9:16am UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/4 "2020-05-30T09:16:44Z")

</div>

@Luciano @bernhard Thanks for that. Somehow I missed that post.

I am trying to use the getTime function to create some “random” numbers hence the question. Just curious, in getTime function, does it depend on the ledger implementation? i.e. its precision is derived from the ledger?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 31, 2020, 11:35am UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/5 "2020-05-31T11:35:39Z")

</div>

`getTime` returns the ledger time which is set by the participant server so different participant servers can definitely behave differently here. Not quite sure if that is already the case for the current ledger implementations. You can find more information on this at [https://docs.daml.com/app-dev/app-arch.html#dealing-with-time](https://docs.daml.com/app-dev/app-arch.html#dealing-with-time) which also describes the issues caused by relying on `getTime` being precise. [Semantics of min\_ledger\_time](https://discuss.daml.com/t/semantics-of-min-ledger-time/486/2) has further details.

As for random numbers, I wouldn’t recommend relying on `getTime` for that. Instead pass in the numbers from the client.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [June 4, 2020, 1:50pm UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/6 "2020-06-04T13:50:41Z")

</div>

I agree with @cocreature here. You probably don’t want to be using `getTime` to generate random numbers.

If your number doesn’t have to be cryptographically secure, you could use a seed + the [Mersenne Twister](https://en.wikipedia.org/wiki/Mersenne_Twister) (I never know how to pronounce that!), which provides pretty good results. Encapsulate this in a singleton contract on the ledger.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [June 4, 2020, 1:53pm UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/7 "2020-06-04T13:53:33Z")

</div>

Implementing a Mersenne twister (or really most rngs and most crypto stuff) in DAML isn’t as simple as it might seem at first due to the lack of bitwise operations in DAML and the fact that arithmetic operations on Int64 throw on overflow so you cannot abuse them as bitwise operations. Definitely a good puzzle and I’d be interested in seeing an implementation 🙂

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [June 4, 2020, 1:57pm UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/8 "2020-06-04T13:57:17Z")

</div>

> [@cocreature](#):
>
> lack of bitwise operations in DAML

Ha! I didn’t consider that at all. It seems that the topic of having a raw binary type & operations keeps on popping up in different discussions.

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [June 4, 2020, 5:11pm UTC](https://forum.canton.network/t/what-is-times-precision-and-does-it-support-microsecond/555/9 "2020-06-04T17:11:09Z")

</div>

Not by _any_ means secure or cryptographic, or, you know, _random_, but here is a simple function that returns a number given another number in a somewhat hard to predict way if you only have access to the sequence of numbers:

```haskell
rng_next : Int -> Int
rng_next =
  let a = 1103515245
      c = 12345
      m = 4294967296
  in \prev -> (a * prev + c) % m

setup = scenario do
  let a = rng_next 1
  let b = rng_next a
  debug([a, b])

```

Values for a, c, m stolen from [this blog post](https://aaronschlegel.me/linear-congruential-generator-r.html).
