just
just copied to clipboard
add datetime functions
- When building archives (eg. git bundles) with a justfile I would like the filename to reflect the time of creation. Alphabetical sorting of such files should also make sense.
- sometimes the date is enough:
myproject_20220505.bundle
- sometimes time down to a certain precision is also desirable (seconds should probably be enough):
myproject_yyyymmdd_hhmmss.bundle
- sometimes the date is enough:
- In another situation I would like to have a datestring eg. compiled into a c++ project given as argument on the commandline:
cmake --build mybuilddir -DBUILDDATE="2022-05-05" -DBUILDTIME="hh:mm:ss" ..
- The "date" command is used on many places in quite different situations when building software on Linux. The date utility however is probably not standard when used cross platform.
I propose the following functions:
-
datetime(formatstring)
- Returns the current local time formatted according to the given formatstring. Called w/o parameter delivers theISO8601/RFC3339
representation:2009-06-30T18:30:00+02:00
-
datetime_utc(formatstring)
- Returns the current utc time formatted according to the given formatstring. Called w/o parameter delivers the ISO8601 representation:2007-12-24T18:21Z
- The formatstring format is defined by chrono/C strftime: - https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#specifiers
- Errors in the formatstring should be brought back as understandable error message to the user's stderr
- As an extension it could also be useful to have a
timediff(timestamp1, timestamp2)
and antimesince(timestamp)
function, which can parse two timestamps and calclate the difference. Or with only one parameter calculate the difference between the timestamp and now. The timestamps should be in theISO8601/RFC3339
format. This could be handy to measure execution time, albeit not very performant nor precise.
References:
- https://de.wikipedia.org/wiki/ISO_8601
- https://docs.rs/chrono/0.4.19/chrono/struct.DateTime.html#method.to_rfc3339
- https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#specifiers
- https://docs.rs/chrono/0.4.19/chrono/struct.DateTime.html#method.format_with_items
- https://docs.rs/chrono/0.4.19/chrono/naive/struct.NaiveDateTime.html#method.format_with_items
- https://docs.rs/chrono/0.4.19/chrono/struct.DateTime.html#method.parse_from_rfc3339