Date time format for blogdown::write_toml

I had a little question regarding blogdown::write_toml. The thing is I am not quite sure how to deal with datetime objects when writing the toml file after I edit it. If I keep date time in native format (as.Posixct), then write_toml converts the date time to some float number (e.g. 2021-01-02 00:05:00 becomes 1546387200.0. I guess time after some origin timestep). If I convert datetime to character, single quotes occur around the datetime (e.g. 2021-01-02 00:05:00 becomes '2021-01-02 00:05:00'). What is the right way to keep the datetime in R object, so that write_toml would result in: start_date = 2021-01-02 00:05:00 ? (without quotes)

Thanks in advance,
Alex

blogdown::write_toml() is doing 2 step

  • Converting to YAML the R object using as.yaml(x) and writing to a file
  • Running Hugo to convert to toml using hugo convert to TOML

I believe when converting to file format, if you want to keep some date format as string, you need to already convert it in the desired format from the start.

See for example

yaml::as.yaml(Sys.time())
#> [1] "1.6732543e+09\n"
yaml::as.yaml(format(Sys.time(), "%Y-%m-%d %H:%M:%S %z"))
#> [1] "2023-01-09 09:51:01 +0100\n"

Created on 2023-01-09 with reprex v2.0.2

If you think we could improve this write_toml() somehow, please do open a feature request in blogdown repo. Thanks!

Hello Christophe,
Thank you for your reply. This as.yaml command indeed helps to recognize the R datetime format, however the resulting toml file contains the datetime in the format "2023-01-09 09:51:01 +0100\n" while I need 2023-01-09 09:51:01 (without quotes and +0100\n). Is there a way to manage that?
Thanks in advance,
Alex

If you are interested in converting datetime to string before writing the TOML as suggested above, just txeak the formatting in format .

format(Sys.time(), "%Y-%m-%d %H:%M:%S")
#> [1] "2023-01-12 10:27:42"

Created on 2023-01-12 with reprex v2.0.2

Then you'll have a string in your object, and TOML conversion should keep it.

See also date specific package like lubridate which have some user friendly formatting option.

Hello Christophe,
Thanks again for your attention to this topic. I guess essentially what I would like to know is how to avoid the quotes in the final output, e.g. getting 2023-01-09 09:51:01 instead of '2023-01-09 09:51:01' in the generated toml file.
Thanks in advance,
Alex

Oh i understand now. Probably because it is seen as string and then quoted :thinking:

So maybe it should be done this way, and there is a way to format date ?

As I said conversion happens with Hugo after writing YAML. So probably need to look in TOML type and how to get a date formatted. I don't know enough so I would need to try out different things - which you could do.
YAML conversion also can lose some date format.

At the bottom hugo convert is used (hugo convert toTOML | Hugo) I don't know if any other tools allow to convert to TOML.

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.