Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing datetimes from strings #4107

Open
mathrick opened this issue May 10, 2024 · 4 comments
Open

Add support for parsing datetimes from strings #4107

mathrick opened this issue May 10, 2024 · 4 comments
Labels
feature request New feature or request scripting About Typst's coding capabilities

Comments

@mathrick
Copy link

Description

Currently, the only way to create a datetime is to use now() or the datetime constructor with individual components explicitly provided. However, that is essentially never useful for ingesting external inputs, meaning that for any data coming in from JSON, YAML, etc., the user has to hack in a string parser themselves to be able to use time manipulation functions in any capacity. That's a big drawback for things like invoice templates.

There should be a way to parse a datetime-containing string into a datetime that's built-in. There was a previous PR in #1668 which aimed to implement parsing Unix timestamps, but it got closed in favour of a generalised implementation that could parse more formats. It doesn't seem that that implementation ever materialised, nor does there seem to be an existing discussion of it. I'm opening this issue to hopefully revive the topic.

Use Case

Any situation in which external data are ingested and contain dates that need to be manipulated. For example:

  • Generating invoices with due dates automatically calculated, based on the contents of a JSON file
  • Displaying a date value in a different format than the one given (i.e. "2024-07-01" → "1 July 2024")
  • Extracting a date to be displayed out of a a full timestamp given as a string
  • Generating a calendar for a given month, based on a value passed in through sys.inputs
@mathrick mathrick added the feature request New feature or request label May 10, 2024
@istudyatuni
Copy link
Contributor

Yaml support datetime. You can even do something like

#let date = yaml.decode("date: 2024-07-01").date

@mathrick
Copy link
Author

Not inside typst, that is actually a string. Try this:

#let date = yaml.decode("date: 2024-07-01").date
#type(date)

I don't believe YAML supports timestamps as much as it allows applications to recognise certain values and convert them to timestamps automatically. Either way, that does nothing for other formats such as JSON or sys.inputs, so the need for a parser is still there.

@istudyatuni
Copy link
Contributor

istudyatuni commented May 10, 2024

I was wrong, it works for toml, not yaml, so you can define function

#let parse(s) = toml.decode("date = " + s).date

@mathrick
Copy link
Author

I was wrong, it works for toml, not yaml, so you can define function

#let parse(s) = toml.decode("date = " + s).date

That's a neat hack! Though I still think it should be promoted to properly supported functionality :)

@Enivex Enivex added scripting About Typst's coding capabilities labels May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request scripting About Typst's coding capabilities
Projects
None yet
Development

No branches or pull requests

3 participants