How do you get the current Date in Daml?

I checked the Daml docs for Date.

I can’t find what would instantiate the current date.

I’m using this in a scenario for which I have a defined model that specifies as part of the set of fields (“with s”) for the model class

I’m also not getting what I would expect to be the intellisense help from VSCode i.e. I have DA.Date imported which VS Code is not complaining about and yet its not picking up “Date” and thus I can’t readily find static side instance creation api etc, not that I see what I need in the docs

e.g. I have tried this , which doesn’t work

1 Like

Hi @amiracam, take a look at a related question on this.

Basically it boils down to using getTimeand extracting the date from taht new Date () is Java syntax and not valid Daml. You also cannot do this at the top-level, so you have to move it in the do-block. Putting things together, your example could be something like this:

repo_test_1 =
  scenario do
    jpm <- getParty "JPM"
    fed <- getParty " FED"
    nowTime <- getTime
    let nowDate = toDateUTC nowTime
    submit jpm do
      create Repo with
        owner = jpm
        regulator = fed
        par = 1.5
        cusip = "9999999"
        trateDate = nowDate

Lastly, I recommend to use Daml Script instead of scenarios for all new projects. Your example translates to Daml Script with minimal changes:

repo_test_1 =
  script do
    jpm <- allocateParty "JPM"
    fed <- allocateParty " FED"
    nowTime <- getTime
    let nowDate = toDateUTC nowTime
    submit jpm do
      createCmd Repo with
        owner = jpm
        regulator = fed
        par = 1.5
        cusip = "9999999"
        trateDate = nowDate
2 Likes

k, now that i’m doing all “script”
it compiles but I get a strange bug namely what I expected to me “Date now” shows up as Epoch ?

So why ?
and again how can I get the current timestamp so that I can extract the current day ?

thanks

meant to include this:

Transactions:
TX 0 1970-01-01T00:00:00Z (TestRepo:13:13)
#0:0
│ known to (since): ‘FED’ (0), ‘JPM’ (0)
└─> create FixedInstruments:Repo
with
owner = ‘JPM’;
regulator = ‘FED’;
par = 1.5000000000;
cusip = “9999999”;
tradeDate = 1970-01-01T

Active contracts: #0:0

Return value: #0:0

1 Like

Thank you @cocreature this was a great tip :+1:t2:

1 Like

Daml Studio runs in static time mode, meaning it starts at unix epoch and time only advances if you call pass or setTime. So in your example, nothing changes the time so you get the unix epoch.

Real ledgers usually run in wallclock mode where getTime will provide you with the current time and time will advance automatically. Some ledgers, most notably sandbox also provide a --static-time option which causes the ledger to run in the same static time mode that you get in Daml Studio.

1 Like

ok, thanks, but then what’s the significance of specifying “wall clock time” in the daml.yaml ?
I’ll assume that only controls the sandbox ledger i.e. the generated dar ?

1 Like

Exactly, the --wallclock-time option is only for Sandbox.

1 Like