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.
3 Likes
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
Have a look and let us know if this satisfies you.
2 Likes
I had a look at the answer @Luciano linked and found it wanting. Have look at the example in my answer here 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.
2 Likes
@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?
2 Likes
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 which also describes the issues caused by relying on getTime
being precise. Semantics of min_ledger_time has further details.
As for random numbers, I wouldn’t recommend relying on getTime
for that. Instead pass in the numbers from the client.
2 Likes
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 (I never know how to pronounce that!), which provides pretty good results. Encapsulate this in a singleton contract on the ledger.
2 Likes
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
2 Likes
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.
3 Likes
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:
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.
3 Likes