Getting Previous Business Day Date in Unix

I want to obtain the previous business date in Unix (mon-fri only).

i.e
if today's date is Mon 19th June (20060619), I want to obtain the previous friday's date - 20060616.
if today's date is Tue 20th June (20060620), I want to obtain monday's date - 20060619.
nmretdAsked:
Who is Participating?
 
bpmurrayCommented:
From script? You could use

  export yesterday=`date '+%a %Y %m %d' | awk -f foobar.awk -`

where foobar.awk contains:

{
      decr = 1;
      if ($1 == "Mon")
         decr = 3;
      year = $2;
      mon = $3;
      day = $4 - decr;
      if (day < 1)
      {
           if (mon == 2)
            day += (year % 4 == 0 && year % 100 != 0) ? 29 : 28;
         else
         if (mon == 8 || mon == 4 || mon == 6 || mon == 11)
            day += 30;
         else
            day += 31
         mon--;
      }
      if (mon < 1)
      {
         mon = 12;
         year--;
      }
      print day "/" mon "/" year;
}
0
 
TintinCommented:
Do you really mean "business date" or just want the previous day excluding weekends?  Business date rules are more complicated.

Anyway, if you are on a system that has GNU date, you can do

if [ `date +%a` = Fri ]
then
    date -d 'last monday' +%Y%m%d
else
   date -d 'yesterday' +%Y%m%d
fi

0
 
ahoffmannCommented:
Do you really mean "business date"?
Then please define what a business date is for you (does it include holidays, which seems to be the case as you said mon-fri).
If you define business date, then please give a complete list for each year you want to perform your calculation.

I guess you better use perl with Date::Manip module where you can "define" business date yourself.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
nmretdAuthor Commented:
I am using the korn shell and am getting a syntax error at line 11 when I run this command ? I think it may be to do with the curly braces but not sure as I'm not too familiar with shell scripting syntax. Can someone please advise ?

Thanks.
0
 
ahoffmannCommented:
bpmurray  posted awk, not shell code!
0
 
nmretdAuthor Commented:
Ok, but I am still getting a syntax error on line 11 ?
0
 
bpmurrayCommented:
Hmm - just tried it again on two different versions of awk and it worked fine. Try "awk -f filename.awk" on its own to see if it gives an error, and then try using "gawk -f filename.awk", just to see if that helps.
0
 
rockiroadsCommented:
try this, its a function which gets the day where u specify how many to go back to
GetLastBusinessDay 3, gets date 3 days ago etc




#!/bin/ksh                                                              
                                                                         
GetLastBusinessDay()                                                    
{                                                                        
        set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
        BACK=$1                                                          
        THEN=$((`date +%d` - $BACK))                                    
        MONTH=`date +%m`                                                
        YEAR=`date +%Y`                                                  
        WEEKDAY=${DAYS[`date +%u`]}                                      
        if [ $THEN -le "0" ]                                            
        then                                                            
                MONTH=$((MONTH-1))                                      
                if [ $MONTH -eq "0" ]                                    
                then                                                    
                        MONTH=12                                        
                        YEAR=$((YEAR-1))                                
                fi                                                      
                set `cal $MONTH $YEAR`                                  
                SHIFT=$(( $THEN * -1 + 1 ))                              
                shift $(($# - $SHIFT))                                  
                THEN=$1                                                  
        fi                                                              
        TMONTH=${MONTHS[MONTH]}                                          
        echo $THEN $WEEKDAY $MONTH $TMONTH $YEAR                        
}                                                                        


# Get Current day, then determine how many days to go back to                                                                        
CurrentDD=`date '+%a'`                                                  
                                                                         
if [ "$CurrentDD" = "Mon" ]                                              
then                                                                    
        GetLastBusinessDay 3                                            
elif [ "$CurrentDB" = "Sun" ]                                            
then                                                                    
        GetLastBusinessDay 2
else                        
        GetLastBusinessDay 1
fi                          


0
 
rockiroadsCommented:
oh, sorry, forgot to copy one line

add this

set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat

before u set

set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

this way, WEEKDAY will ensure it has a day set

0
 
rockiroadsCommented:
values set

$THEN dayno

$WEEKDAY day e.g. mon,tue

$MONTH month number

$TMONTH month name

$YEAR year

0
 
bpmurrayCommented:
Actually, Tintin's solution is the best, despite my terrific awk solution :-)

Put his script into a function and simply set it to the format you want.
0
 
ahoffmannCommented:
so you all agree that for example following days are "business days":
  25-dec-2006
  01-jan-2007

in which world do you live? or which calender do you use? or which days are business days? (argh, I'm repeating ...)

BTW, IIRC Sun is a business day in most arabic countries, a bit short-sighted all these guesses ;-)
0
 
bpmurrayCommented:
Well, it all depends on your locale:

   Friday is the Muslim prayer day
   Saturday is the Hebrew prayer day
   Sunday is the Christian prayer day

   In the west, we usually work Monday-Friday; in other countries they also work on Saturdays.

   Holidays vary according to country: March 17 in Ireland, July 4 in USA, etc. Christmas is Dec 25 in the West, but Jan 6 in Orthodox countries

Calendrical calculations are actually very complex, but if you're looking for a simple mechanism to do e.g. backups, tintin's solution is the best one here.
0
 
ahoffmannCommented:
> .. calculations are actually very complex, ..
that's why I said perl's Date::Manip 'cause it handles all this complexity in any calender, any year, any timezone, etc. etc.

I'm still waiting for the definition of "business day", all suggestions are guessing ...
0
 
bpmurrayCommented:
>> I'm still waiting for the definition of "business day", all suggestions are guessing ...

There's always someone doing some work, so the term is meaningless unless it's defined by a particular business, i.e. "my business day".
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.