Access to time during contract creation
- Enthusiast-BlockOP 2
I figured that out about how to assign time during a choice, but wanted to know how to assign it during the creation of contract?
Example :-
template Example with admin: Party updatedDate: Date where signatory admin controller admin can UpdateExample : ContractId Example with newUpdatedDate: Optional Date do newTime <- getTime let nowDate = toDateUTC newTime create this with updatedDate = nowDateNow, in this case I am able to assign time during choice, but how can we assign time during creation of contract?
- cocreature 1
Hi @Enthusiast-Block, welcome to the forum!
You cannot directly access time during contract creation. However you can create a contract from a choice as you already noticed. So what I’d recommend is to have a choice on a separate template which does only that: query for the current time and then creates the actual contract you want with the date set to the current date.
You can use
createAndExerciseto create the helper template and exercise the choice in a single step. Here is a full example:module Main where import DA.Date import Daml.Script template T with p: Party date: Date where signatory p template Helper with p : Party where signatory p choice CreateT : ContractId T controller p do time <- getTime create T with p = p date = toDateUTC time test = script do p <- allocateParty "p" submit p $ createAndExerciseCmd (Helper p) CreateT pure () - Enthusiast-Block 1
Hi @cocreature,
Thanks, got it.
Just wanted to ask, if there is any reason that you have made it this way that it is not able to allocate time during contract creation?
- cocreature 1
I’d say it’s mainly about simplicity of the resulting API. The way things are currently setup, you specify exactly the contract argument when you create a contract and as a result you will either get a contract with exactly that argument or creation of the contract will fail.
If you allow for querying time during contract creation or more generally run arbitrary
Updatecode you need to come up with a different API where you don’t specify the full contract argument but you do specify how the fields you haven’t specified will be computed. The most general way of doing that is to allow for specifying anUpdate Cexpression whereCis the contract argument. But that’s exactly what you can do viacreateAndExercise(or choices in general). So adding something like that wouldn’t allow you to do anything you can’t do at the moment but it would makecreatea significantly more complex and harder to understand operation. - Enthusiast-Block 1
Consider this example,
template Ex with p : Party date : Date or Time (Which One) where signatory pNow, I am having a Java service which would hit this endpoint -
/v1/createto create this Ex contract using DAML JSON APIsAnd for date, it would send -
"2007-12-03T10:15:30+01:00"as string, and I want to create the Ex contract with the given date in the date field? How can i achieve this?Thanks
- Gary_Verhaegen 1
From the documentation:
Timestamps are represented as ISO 8601 strings, rendered using the format
yyyy-mm-ddThh:mm:ss.ssssssZ.and further down:
[Dates are] Represented as an ISO 8601 date rendered using the format
yyyy-mm-dd.Which one you should use really depends on what you’ll do with it later on and whether you need the time part, but either way, you should be able to use a String in ISO format. Do bear in mind that the
Timestampformat only acceptsZas a timezone, i.e. you as the client-side programmer have to handle the conversion to UTC time before sending. You’d need to send2007-12-03T09:15:30Zinstead of2007-12-03T10:15:30+01:00. - Enthusiast-Block 1
Thanks for this,
Further I need to use both date and time.
Also, I need to query contracts according to the timestamp, such as greater than or less than.
I am using DAML on Fabric.What type should I use for “date” field - such that both date and time are included and I am able to query contracts according to date while fetching.
- Gary_Verhaegen 1
Timeseems like the right type for what you want on the Daml side. It’s a timestamp, i.e. date + hour, minute, second. It can be created and manipulated with the functions inDA.Time, can be created using ISO 8601 strings as described in my previous post, and the JSON API query language works on timestamps (that’s even one of the examples in the documentation).