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 = nowDate
Now, in this case I am able to assign time during choice, but how can we assign time during creation of contract?
2 Likes
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 createAndExercise
to 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 ()
1 Like
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?
1 Like
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 Update
code 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 an Update C
expression where C
is the contract argument. But that’s exactly what you can do via createAndExercise
(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 make create
a significantly more complex and harder to understand operation.
1 Like
Consider this example,
template Ex
with
p : Party
date : Date or Time (Which One)
where
signatory p
Now, I am having a Java service which would hit this endpoint - /v1/create
to create this Ex contract using DAML JSON APIs
And 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
1 Like
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 Timestamp
format only accepts Z
as a timezone, i.e. you as the client-side programmer have to handle the conversion to UTC time before sending. You’d need to send 2007-12-03T09:15:30Z
instead of 2007-12-03T10:15:30+01:00
.
1 Like
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.
1 Like
Time
seems 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 in DA.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).
1 Like