just icon indicating copy to clipboard operation
just copied to clipboard

add datetime functions

Open mbodmer opened this issue 3 years ago • 6 comments

  • 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
  • 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 the ISO8601/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 an timesince(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 the ISO8601/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

mbodmer avatar May 05 '22 21:05 mbodmer

maybe related: #1020

mbodmer avatar May 05 '22 21:05 mbodmer

Thanks for the detailed writeup!

This sounds good to me. I'd be inclined to leave out timediff and timesince for now. In general, I try to avoid functions that don't naturally take and return strings, since that's the only type that just currently supports.

casey avatar May 07 '22 20:05 casey

If we're going to go to the trouble of providing functions to operate on datetimes, shouldn't we also make it possible to specify a datetime other than now? If these functions took an epoch timestamp, now() by default, they would be much more flexible. I don't think it will be that much more complex to actually implement this either, if we're already linking in strftime. Biggest complication would be parsing the epoch string into a float I think?

nk9 avatar Jul 06 '22 10:07 nk9

We certainly could let them take a timestamp, although I'd wait to do that until someone has a specific use-case. That way we can ensure that it's both needed, and that what we implement works for the specific use-case.

So far I've avoided defining functions that take and return values that aren't strings, e.g., numbers.

casey avatar Jul 06 '22 18:07 casey

I'd just like to point out that colons in file/directory names can be very problematic, especially when considering multi-platform usage. You can quote or escape them much of the time for creation but many applications and some operating systems will barf hard attempting to manage a file or directory with a colon in its name.

Also the colon in the timezone offset is not recommended as well. Some ISO-8601 parsers don't like it for some reason. YMMV.

runeimp avatar Jul 08 '22 09:07 runeimp

I added datetime(format) and datetime_utc(format) in #2167. For now the format string must be specified, but it should be easy enough to add versions which have no arguments and which use a default format string.

casey avatar Jun 15 '24 05:06 casey