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 datetime functions #1182

Open
mbodmer opened this issue May 5, 2022 · 6 comments
Open

add datetime functions #1182

mbodmer opened this issue May 5, 2022 · 6 comments

Comments

@mbodmer
Copy link
Contributor

mbodmer commented May 5, 2022

  • 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:

@mbodmer
Copy link
Contributor Author

mbodmer commented May 5, 2022

maybe related: #1020

@casey
Copy link
Owner

casey commented May 7, 2022

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.

@nk9
Copy link
Contributor

nk9 commented Jul 6, 2022

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?

@casey
Copy link
Owner

casey commented Jul 6, 2022

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.

@runeimp
Copy link

runeimp commented Jul 8, 2022

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.

@casey
Copy link
Owner

casey commented Jun 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants