1. 23
  1. 3

    There’s also dateutils with commands like dateadd, dateseq, and others: https://www.fresse.org/dateutils/

    1. 2

      Wow! This is great!

      1. 1

        this is cool.

      2. 2

        I recently implemented a simpler version of this solely focused on duration math which avoids ambiguities by 1) only accepting one format for durations, 2) only using units with a consistent definition (weeks, days, hours, minutes, seconds): https://github.com/alilleybrinker/dcalc

        1. 2

          I’ve made an implementation of the duration math that also works pretty reasonably with months and years. The calculation for a difference between dates A and B is in short:

          • Shift A to midnight at the start of its month, so that it’s of the form YYYY-MM-01 00:00:00. Shift B the same amount
          • Subtract A’s year and month from B’s to get the number of years and months
          • Read off the day, hour, minute and second from B’s date to get the number of days, hours, minutes, seconds

          This leads to fairly intuitive differences, such as

          • 2020-02-01 — 2020-03-01 = 1 month
          • 2020-01-29 — 2020-03-01 = 30 days
          • 2020-01-31 — 2020-03-31 = 2 months
          • 2020-02-29 — 2020-03-31 = 1 months, 2 days
          • 2021-11-14 23:02:20 — 2023-01-01 00:00:00 = 1 year, 1 month, 17 days, 0 hours, 57 minutes, 40 seconds

          Having longer durations be expressed as years and months instead of just weeks is really nice.

          Note that this works because you have access to the original dates the duration is calculated from, and you can’t do any calculations with the resulting difference. So this is mainly just useful for presenting differences between dates

          1. 1

            Interesting, personally I would view the second example as 1 month, 3 days (3 days until the February 1st, then one month until March 1st). To me, if you go from a date in one month to at least the same date in the following month, then one month has passed.

        2. 1

          I love this.

          I’ve struggled to word this before, but one of the things I like about shell (the payoff for suffering its sharp corners) is that every function and external program can accept its own humane word-oriented syntax–as long as the domain doesn’t need indentation or punctuation that shell doesn’t cede control of.

          It often gets snowed under other layers of complexity, but quick access to idiomatic DSLs such as sql, jq, awk, sed, expect, etc. is one of those things that always strikes me as an under-appreciated superpower.

          (I realize dte isn’t shell–but it’s got the kind of very-compatible grammar that I can imagine being pleasant to drop into for datetime operations.)

          1. 1

            dte is minimalist cool text editor.