APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Early reminders for first Monday of month events


Early reminders for first Monday of month events




2012/10/20


I kept forgetting to send our club meeting notices to the newsletter. Google Calendar couldn't help me, so I wrote this script.


I help run a monthly Computer Club meeting in our community. The club actually meets twice per month, once for a general meeting and once to offer troubleshooting services to members of the community.

I keep forgetting to put meeting notices in our community newsletter.

That sounds like a job for a calendar program, doesn't it? Yeah, that's what I thought too, so I fired up Google Calendar and added the two dates for the first Monday and the first Wednesday of every month. So far, so good and now all I had to do was to tell it to remind me at an appropriate time.

Google Calendars reminder

Unfortunately, I ran into an immediate problem. I need to get notices to the newsletter editor very early. If I want something to be in the December newsletter, I need to send it in before the first of November. Google Calendar won't let you set a reminder more than one month earlier than an event. If I try to set it for 5 weeks or 32 days, it won't allow that. That's dumb, but there it is: Google doesn't think you'd ever need more than 4 weeks advance notice for anything.

Perl to the rescue

So, I turned to Perl.

There are many ways to approach this in Perl. You could take a brute force approach, setting the date to December 1st, testing to see if that is a Monday and incrementing to December 2d and beyond if it is not. That's actually not such a terrible way to do it as the date we want has to be before the 8th, so there cannot be many iterations.


#!/usr/bin/perl
use Time::Local;

($day,$month,$year)=(localtime)[3,4,5];
$month+=2;
if ($month > 11) {
  $month=0; $year++;
}
$nextmonth=timelocal(0,0,0,1,$month,$year);
$dow=(localtime $nextmonth)[6];
while ($dow != 1) {
 $nextmonth+=24 * 60 * 60;
 $dow=(localtime $nextmonth)[6];
}

print scalar localtime $nextmonth, "\n";;
 

If you ran that on the 28th of each month, you'd have the first Monday of the 2d month following. Add some code to send email and you are done.

Date::Calc

I decided to use Date::Calc instead. It's probably no faster (it might even be slower) and it very likely uses a brute force approach itself, but it has a lot of useful functions (68, in fact) beyond just what we need to do here.

Installing Date::Calc is, however, a somewhat lengthy procedure. It's a lot of code with a whole army of dependencies and it will keep CPAN busy for quite a little while. It will be worth it, though, as our code becomes as simple as:

  ($year,$month,$day) = Today();
  ($year,$month,$day) = Add_Delta_YMD($year,$month,$day, 0,2,0);
  $monday=Nth_Weekday_of_Month_Year($year,$month,1,1);
 

We just need to wrap that up in some formatting and we're ready to put it in crontab:

10      6       28      *       *       /mybin/remindme 2>&1 > /dev/null
 

I'll run it on the 28th of each month and it will send me a reminder email that is ready for any last many editing before being forwarded along to the newsletter.

#!/usr/bin/perl
# "remindme" Tony Lawrence October 2012
# I'm going to run this on the 28th of the month
# I want to know the first Monday and First Wednesday of TWO months ahead
#
use Date::Calc qw( Today Date_to_Days Add_Delta_YMD Decode_Date_US Nth_Weekday_of_Month_Year );
@day=("1st","2d","3rd","4th","5th","6th","7th");
@months=("January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December");

  ($year,$month,$day) = Today();
  ($year,$month,$day) = Add_Delta_YMD($year,$month,$day, 0,2,0);
  $monday=Nth_Weekday_of_Month_Year($year,$month,1,1);
  $monday=~ s/$year$month//;
  $wednesday=Nth_Weekday_of_Month_Year($year,$month,3,1);
  $wednesday=~ s/$year$month//;

open(SENT,"|/opt/kerio/mailserver/sendmail tony\@aplawrence.com");
print SENT <<EOF;
Mime-Version: 1.0
Content-Type: text/html
Content-Transfer-Encoding: 8BIT
From: tony\@aplawrence.com
Subject: Computer club schedule for $months[$month-1] $year

<h2>Oak Point Computer Club $months[$month-1] $year</h2>

<p>The Oak Point Computer Club meets on the first Monday and the first Wednesday of every month.</p>

<ul>
<li>The Monday $months[$month-1] $day[$monday-1] meeting is at 2:00 PM in the Back Ballroom.</li>

<li>The Wednesday $months[$month-1] $day[$wednesday-1] meeting is at 5:00 PM in the Back Ballroom.</li>
</ul>

<p>Wednesday is Troubleshooting.  Bring us your problems and questions and we will try to help. First come, first served.</p>

<p>Monday is usually a lecture or informal discussion on some subject.</p>
<p><b>Not just for computer geeks!</b></p>
<p>All Oak Point homeowners are welcome; there are no dues or charges of any kind.</p>
EOF
exit 0;
 

This is the result:

Early reminder email

That should help me get my notices to the newsletter on time.




If this page was useful to you, please help others find it:  





Comments?




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!


Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Linux
       - MacOSX
       - Perl
       - Scripting















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)