First work day in the month? (326 Views)
Reply
Frequent Advisor
Ryan Clerk
Posts: 44
Registered: ‎05-12-2002
Message 1 of 17 (326 Views)
Accepted Solution

First work day in the month?

Hello again experts,

I have another problem. I need to know if today is the first work day of the month. For example, if the 1st was Saturday then Monday the 3rd would be the first work day. Any ideas?

Thank you,
Ryan
Regular Advisor
Eric Bakken
Posts: 102
Registered: ‎08-08-2005
Message 2 of 17 (326 Views)

Re: First work day in the month?

In my opinion, Monday the 3rd would be the first "Workday" of the month. It really depends on the company though.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 3 of 17 (326 Views)

Re: First work day in the month?

This is actually a little bit trickier that you think because it's more than looking for the first Monday; it's really the first Monday - Friday that is also not a holiday.

I'll assume that you have been a good boy and set up /etc/acct/holidays and it's a better idea to set up multiple years by creating separate /etc/acct/holidays_YYYY because consider the problem of also crossing year boundaries and 1-Jan is a very common holiday.

I would set up a cron job that runs EVERY Monday-Friday (remember some folks have very long holidays) and then do this:

#!/usr/bin/sh

# first test to see if yesterday's month
# skipping past Sundays and Saturdays and
# holidays is not equal to today's month
if [[ $(caljd.pl -p 1 -x 0 -x 6 -h -M) != $(caljd.pl -M) ]]
then
# now check to see if today isn't also
# a holiday
if [[ $(caljd.pl) = $(caljd.pl -h) ]]
then
echo "Today is the first working day of the month."
fi
fi

Invoke as caljd.pl -u for full usage and examples. You should already have a copy of the script.
If it ain't broke, I can fix that.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 4 of 17 (326 Views)

Re: First work day in the month?

As Clay indicates... it is probably NOT enough to answer the question only taking weekdays into consideration.

But if you did, and did not count holidays, then the rule would appear to be:
It is the first workday in the month if it is the first of the months and not a Saturday nor Sunday or if it is a Monday and the 2nd or 3rd of the month.

In Perl:

my ($mday,$wday) = (localtime(time))[3,6];
print "YES\n" if (($mday==1 && $wday && $wday != 6) || ($mday < 4 && $wday == 1 ))


And in perl with taking a YYYYMMDD as argument:

--- test.pl ---
use Time::Local;
$x=shift;
$y=timelocal(0,0,0,substr($x,6,2),substr($x,4,2)-1,substr($x,0,4)-1900);
my ($mday,$wday) = (localtime($y))[3,6];
print "YES\n" if (($mday==1 && $wday && $wday != 6) || ($mday < 4 && $wday == 1 ))
----

perl test.pl 20070101

The first mondays in a month is a relatively popular holiday day!

fwiw,
Hein

Trusted Contributor
Steve Post
Posts: 1,058
Registered: ‎09-13-1998
Message 5 of 17 (326 Views)

Re: First work day in the month?

#!/bin/ksh
# start of SCHEDULE TEST
# 1. Have a $x1_listofdates in this program that gives a schedule.
# 2. Run the cronjob every day from 1st to 10th of the month.
# 3. Have the job exit out unless the day matches the schedule.
# note: typeset -i tells unix the value is an integer, not octal binary.
#------------------------------------------------------------------------------
typeset -i x1_day_of_month=`date +"%d"`
x1_month_of_year=`date +"%b"`
typeset -i x1_year=`date +"%Y"`

x1_datestring=`printf "%02d%s%4d\n" $x1_day_of_month $x1_month_of_year $x1_year`

# these days are when the script should run.
# they are the 2nd workday of each month.
x1_listofdates="
05Sep2007
02Oct2007
02Nov2007
04Dec2007
"
x1_FLAGRUN=0
echo "Run my special job today?"
for x1_D in $x1_listofdates
do
echo " today is $x1_datestring look at $x1_D from the list. "
if [ "$x1_D" = "$x1_datestring" ] ; then
echo "Yes. "
x1_FLAGRUN=1
else
echo "do not run today"
fi
done

if [ $x1_FLAGRUN != 1 ] ; then
echo "do not run today"
exit
fi

rest of program goes here
Trusted Contributor
Steve Post
Posts: 1,058
Registered: ‎09-13-1998
Message 6 of 17 (326 Views)

Re: First work day in the month?

My version is simple, but it requires you to put in a list of dates for when you want the program to run.

Of course if your schedule follows NO rules, this might be a good thing.
Regular Advisor
Michael D. Zorn
Posts: 129
Registered: ‎06-10-2004
Message 7 of 17 (326 Views)

Re: First work day in the month?

Here's a website, "Shell Corner: Today_is.pl and holidays.awk":

http://www.unixreview.com/documents/s=1344/ur0309c/

That one links to this one, "The American Secular Holidays Calendar":

http://www.smart.net/%7Emmontes/ushols.html#ALG

It goes into great detail about "the first workday of a month", with all the exceptions.
Frequent Advisor
Ryan Clerk
Posts: 44
Registered: ‎05-12-2002
Message 8 of 17 (326 Views)

Re: First work day in the month?

Hello (again) experts,

Thanks everyone for all the help. I am having a
little trouble making the holidays work as A. Clay correctly pointed out. I am getting this error message. "File /etc/acct/holidays year 1998 does not match expected year 2007".
Any ideas?

Thank you,
Ryan
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 9 of 17 (326 Views)

Re: First work day in the month?

Uh, Ryan. You will have to tell me which of those words in the error message that you do not understand. Sadly, most of them are monosyllabic. Ok, now I'll cut you a little slack. This simply means that the year field in your current /etc/acct/holidays is 1998 rather than the current year. Back when most UNIX boxes ran accounting, the holidays file was kept up to date and a common cron entry reminded you to update the file every January (or late December). Many flavors of UNIX don't even have a holidays man page anymore.
Anyway, update the file with the current holidays and the current year and you should be fixed.

One of the changes to the holidays file convention I implemented for caljd.xx, is to add a year extension Caljd.xx will always try to find holidays_YYYY before it falls back on the default file. This way, date calculations involving holidays which cross year boundaries work as expected. I always symbolically link the current year's /etc/acct/holidays_YYYY to /etc/acct/holidays so that any other UNIX software works as expected.

If it ain't broke, I can fix that.
Frequent Advisor
Ryan Clerk
Posts: 44
Registered: ‎05-12-2002
Message 10 of 17 (326 Views)

Re: First work day in the month?

Hi Clay,

Thanks. I updated /etc/acct/holidays and now it works!. I deserved to be called stupid because the error message described the problem exactly.

Thank you,
Ryan

PS, It's time for points!
Frequent Advisor
Ryan Clerk
Posts: 44
Registered: ‎05-12-2002
Message 11 of 17 (326 Views)

Re: First work day in the month?

Hi Clay,

My script now works great but I have a new problem. My company has offices in three countries and some of the holidays are different for each of them. Is there a way to use a different file instead of /etc/acct/holidays?

Thanks,
Ryan
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 12 of 17 (326 Views)

Re: First work day in the month?

Great Googly Moogly, Ryan.

You are wanting WAY too much sugar for a dime. You could move your holidays file(s) to a temporary location and replace with different version(s) and move the temporary
file(s) back to the original location when finished. The only downside is that this action could clobber accounting if someone happened to be running some of the accounting commands during the interim.

I'll take a look and see how hard this will be. One of my biggest problems is finding free letters for options. There have been so many requests for enhancements that the alphabet is beginning to run dry. There was a time when caljd.sh was only a few lines long.
If it ain't broke, I can fix that.
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 13 of 17 (326 Views)

Re: First work day in the month?

>> My script now works great but I have a new problem. My company has offices in three countries and some of the holidays are different for each of them. Is there a way to use a different file instead of /etc/acct/holidays?


Ryan,

You may want to further clarify this need.
I'm guessing here that you want to run the script in a central location, for all countries, otherwise it should prove easy enough to give each country the right holidays file.

Are you now looking for the first day in the months which is a work day in all countries? That's create serious missunderstandings I bet! Or do just want to deal with a country at a time?
If so, it would be nice if the script poicked up an environment variable to point to the right holidays file, or you coudl temporary softlink it.

Clay,
I tend to deal with running out of letter in the alphabet through env variables.

In your case without a letter of an explicit argument option I would code up something like:

my $CALJD_HOLIDAY_FILE='etc/acct/holidays';
my $CALJD_HOLIDAYS_ENV='CALJD_HOLIDAYS';
:
my $caljd_holiday_file = $ENV{$CALJD_HOLIDAYS_ENV};
$caljd_holiday_file = $CALJD_HOLIDAY_FILE unless $caljd_holiday_file ;


If I have a letter, then I might use getops as, in a 3-way naming scheme as

my $EVALUATE_CSV = "EVALUATE_CSV";
my $CSV = "evaluate.csv";
:
getopts ('c:dr:vis',\%opt) or &usage;
&usage unless @ARGV;
$verbose = $opt{v} ? 1 : 0;
$debug = $opt{d} ? 1 : 0;
$csv = $opt{c} ? $opt{c} : $ENV{$EVALUATE_CSV} ;
$csv = $CSV unless $csv;

So there is 3 places to get a name...
1) explicit argument
2) env variable
3) softcoded default.



But you knew all that!
Cheers,

Hein.




Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 14 of 17 (326 Views)

Re: First work day in the month?

Ok, Ryan. This proved to be not too difficult. I added a -H /home/user1/holidays option which behaves exactly like -h except that the specified filename, /home/user1/holidays, substitues for /etc/acct/holidays. Moreover, the filename_yyyy convention applies so that you can create multiple years.

Based upon your script logic, your should now be able to replace the "-h" argument with "-H ${YOURFILE}".

It took much longer to update the usage and examples than it did to implement the actual coding changes.

Here is caljd.sh, Vrsn 2.3.

Hein: Thanks for the advice but fortunately 'H' was still available.
If it ain't broke, I can fix that.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 15 of 17 (326 Views)

Re: First work day in the month?

... and here is the equivalent, caljd.pl, Vrsn 2.3 p.
If it ain't broke, I can fix that.
Frequent Advisor
Ryan Clerk
Posts: 44
Registered: ‎05-12-2002
Message 16 of 17 (326 Views)

Re: First work day in the month?

Clay,

Thank you very much! The new option works great!

Hein,
Thanks for your ideas as well.

This is the best unix website in the world!!!!!

Thank you,
Ryan
Trusted Contributor
Tor-Arne Nostdal
Posts: 268
Registered: ‎08-17-1998
Message 17 of 17 (326 Views)

Re: First work day in the month?

This is just to give some general comments and also more credit to Clay.

In general this topic once again show:
'somebody need to enter the holidays'
'there's local variations'
'many systems have the need for handling several calendars'
Our company is involved in operations many places around the world. Date/Time/Timezones/holidays/character sets seems to always be a returning problem.

Using Julian date have always been one of the trick's to overcome some of the date calculation problems.

Thanks Clay for yet a new version of caljd I've been using it for "the tricky dates" many times, and other times as reference lookup when "date scripting".

P.S.
* In some countries first working day would also be Sunday... :-)

* If you have Microsoft Outlook you could search for a file named: OUTLOOK.HOL
This is a flat-file which can contain dates that you need. It is also sectioned into countries.

/Tor-Arne
I'm trying to become President of the state I'm in...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.