Solved

Getting Previous Business Day Date in Unix

Posted on 2006-06-21
18
1,425 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:nmretd
  • 5
  • 4
  • 3
  • +2
18 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 64 total points
Comment Utility
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
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 62 total points
Comment Utility
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 62 total points
Comment Utility
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
 

Author Comment

by:nmretd
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
bpmurray  posted awk, not shell code!
0
 

Author Comment

by:nmretd
Comment Utility
Ok, but I am still getting a syntax error on line 11 ?
0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 62 total points
Comment Utility
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
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
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
 
LVL 65

Expert Comment

by:rockiroads
Comment Utility
values set

$THEN dayno

$WEEKDAY day e.g. mon,tue

$MONTH month number

$TMONTH month name

$YEAR year

0
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
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
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> .. 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
 
LVL 15

Expert Comment

by:bpmurray
Comment Utility
>> 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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now