[Edinburgh-pm] meeting reminders
Aaron Crane
perl at aaroncrane.co.uk
Thu Feb 16 07:26:13 PST 2006
Murray writes:
> 15 9 * * 6 perl -e 'use Date::Manip;$t="today";@d=ParseRecur( \
> "0:1*2:3:0:0:0","2006-02-08",$t,DateCalc($t,"+ 1 month")); \
> if (Date_Cmp($d[0],DateCalc($t,"+ 4 days"))==0){print( \
> "Whiskey Wednesday!\n");}'|exim edinburgh-pm at pm.org
Did someone say "one-liner"? Well, I always find it hard to resist a
golf challenge.[1]
This version saves a lot of code by precalculating most of the date
arithmetic at programming time:
15 9 4-10 * * perl -e'exit 1if(localtime)[6]!=6'&&echo Whiskey Wednesday!|exim edinburgh-pm at pm.org
The trick there is that we can easily determine that the Saturday before
the second Wednesday of the month is never earlier than the 4th or later
than the 10th, so we can persuade cron to handle the range test for an
expenditure of only 3 strokes ("4-10" instead of "*"), while removing
all the code that calculates the date of the Saturday before the second
Wednesday. Then the only thing to do is check whether today is a
Saturday.
I think that's by far the most interesting thing I've come up with; the
rest is just golfing. For example, we can save 4 strokes with an
algebraic identity:
15 9 4-10 * * perl -e'exit 6-(localtime)[6]'&&echo Whiskey Wednesday!|exim edinburgh-pm at pm.org
And 2 further strokes by abusing the backtick operator:
15 9 4-10 * * perl -e'6-(localtime)[6]||`echo Whiskey Wednesday!|exim edinburgh-pm\@pm.org`'
And, of course, we can save 3 strokes from any of the preceding ones if
we assume that local time is always close enough to UTC:
15 9 4-10 * * perl -e'6-(gmtime)[6]||`echo Whiskey Wednesday!|exim edinburgh-pm\@pm.org`'
Finally, we might as well save an extra stroke for the time of day we
send the message:
9 9 4-10 * * perl -e'6-(gmtime)[6]||`echo Whiskey Wednesday!|exim edinburgh-pm\@pm.org`'
Here's an even shorter non-Perl solution:
9 9 4-10 * * date +%w|grep -q 6&&echo Whiskey Wednesday!|exim edinburgh-pm at pm.org
Were it not for Cron's carefully-designed but, sadly, broken behaviour
of OR-ing the day-of-week and day-of-month specifications together, you
could do this, which seems much neater to me, not to mention
substantially shorter:
9 9 4-10 * 6 echo Whiskey Wednesday!|exim edinburgh-pm at pm.org
[1] Yes, I do know that the original suggestion was to come up with the
best one-liner, not the shortest implementation. But, hey, golf can be
fun anyway, right?
--
Aaron Crane
More information about the Edinburgh-pm
mailing list