Is there a way to express seconds since epoch in Typst?

Currently I use something like

echo "#let SEED=$(shell date +%s)" > seed.typ

which I then #import in order to seed the suiji random generator via a Makefile.

While this is random enough for my purposes I wonder if I can’t implement this in typst directly so I don’t have to go the make way.

I don’t think that’s possible, it would be a violation of function purity. But you can avoid the extra file by passing the value as a command-line parameter:

typst compile file.typ --input seed=$(date +%s)

and get the value in the document with

#let seed = int(sys.inputs.at("seed", default: "0"))

Thank you.

That’s more to type (not a problem with command line recall or watch and can’t be done (easily) in Sublime Text. The extra file itself is not the issue, I can run make which I can fire up under ST.

I was wondering if there are plans of supporting finer grained date/time in typst itself, ie down to the second.

You had a typo date +%2 should be date +%s

There’s an issue (closed) here: Add support for parsing datetimes from strings · Issue #4107 · typst/typst · GitHub and a (not very constructive) recent comment sparked some more discussion on Discord.

To give a quick and incomplete summary:

  • Using the current timestamp as the document’s date invalidates a lot of previously compiled stuff. Since Typst uses incremental compilation to achieve its speed, this would increase compile times for reasons that users could not easily understand and identify, leading to frustration.
  • With instant preview, it’s not obvious whether the passage of time should update the preview or whether only changes in the document should. Both ways feel “off” in some ways, and the former way leads to unnecessary recompilations.
  • One of the use cases cited is to change document appearance based on the time of day (switching between light/dark mode). This in particular would be better served by support from the editor, since here you’re only interested in the preview’s appearance, not the finished document.

A propos finished document: to me personally it always felt wrong to give work-in-progress documents a timestamp to indicate when it was “finished” – and once a document is finished, the last time I saved the document doesn’t really change the timestamp. So I always preferred manual timestamps anyway, admittedly not at second granularity because it wasn’t important to me. (Also admittedly, this preference may be a holdover from WYSIWYG word processors where it was never super transparent to me when these fields would and wouldn’t get updated, which doesn’t apply to Typst.)

1 Like