Macro for "Next Sunday"

The above version seems to work accurately now, and it is very fast for me.
So I see no reason to further optimize it.

But some coders prefer a minimalist approach, using the fewest possible lines of code or number of Actions.

For those who prefer this, I have created a "minimal" version (below).
Personally, I prefer the "full", albeit more verbose, version, because:

  1. It is easier to read and understand, especially by those who did not author it.
  2. It is easier to maintain, to debug, to modify.

To each his own.

##Macro Library   @DATE Get Date for Prior/Next Day of Week [Minimal] [Example]


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/4/4da87eb7fc44af27d293c46a906d6576c2b6b891.kmmacros">@DATE Get Date for Prior-Next Day of Week [Minimal] [Example].kmmacros</a> (12 KB)

---

<img src="/uploads/default/original/2X/9/934ef5ff8e6753d13901032fba1d1924a96531c1.png" width="759" height="763">
1 Like

That only gives you the month-day-year. I don't know what else is going on, but the "Monday" is not coming from that token. If you want the DOW, you would do:

%ICUDateTimePlus%(7+5-DOW(NOW)) MOD 1%Days%EEEE MM-d-yy%

You’re correct, I had written that before I discovered a TextExpander shortcut was corrupting the output.

Here is another option. The GNU coreutils date command is a very feature-rich version of the [basic] macOS date command line utility, including an option for a “mostly free format human readable date string”.

Some examples

  • what time would it be locally (to me in the Mountain Standard timezone) when it is 9am in Istanbul this next Friday.
    $ gdate --date='TZ="Asia/Istanbul" 9:00 next Friday' Thu Jan 5 23:00:00 MST 2017

  • The UTC/GMT time when it is noon in Hawaii
    $ gdate --date='TZ="Pacific/Honolulu" 12:00' -u Wed Jan 4 22:00:00 UTC 2017

  • but to answer your original question
    $ gdate --date='next Sunday' "+%Y-%m-%d" 2017-01-08 $ gdate --date='last Sunday' "+%Y-%m-%d" 2017-01-01

  • Here are more examples of the “mostly free format human readable date string”
    $ gdate --date='3 weeks ago last Friday' "+%Y-%m-%d" 2016-12-09 $ gdate --date='2 weeks next Tuesday' "+%Y-%m-%d" 2017-01-24

  • and an all-time favorite
    $ gdate --date='a fortnight ago' "+%Y-%m-%d" 2016-12-20

Tip: Don’t even try to download the source code and compile coreutils yourself. The easiest way is to install the Homebrew package manager (just a single line ruby command) then simply install coreutils with brew install coreutils.

Note: Since the coreutils executable names would conflict with the macOS versions, they are prefixed with ‘g’, so you have to run gdate instead of date.

To bring this back to KeyboardMaestro, in your a KM macro, add an “Execute shell script” action to run /usr/local/bin/gdate with whatever --date="" string you require (Read over the GNU coreutils date command link above for all the details). Have the action save the output to the clipboard or a KM variable to be used later in your macro.

Note: Be sure to include the full path to the Homebrew bin directory (which is by default /usr/local/bin/) and not just gdate. The shell environment created by KM’s “Execute shell script” action is not the same as when you open a shell in Terminal.app so it won’t be aware of a custom PATH.

Hope this helps!

2 Likes