Solved

Getting Previous Business Day Date in Unix

Posted on 2006-06-21
18
1,641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
18 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 64 total points
ID: 16952824
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
ID: 16956556
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
ID: 16957301
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nmretd
ID: 16967698
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
ID: 16967849
bpmurray  posted awk, not shell code!
0
 

Author Comment

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

Expert Comment

by:bpmurray
ID: 16968093
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
 
LVL 65

Assisted Solution

by:rockiroads
rockiroads earned 62 total points
ID: 16968268
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
ID: 16968286
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
ID: 16968299
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
ID: 16968345
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
ID: 16969789
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
ID: 16969938
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
ID: 16970041
> .. 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
ID: 16970111
>> 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
List out all word 7 357
has22 challenge 11 109
Window placement 17 99
Trying to run powershell  in a batch file. How do I do this? 8 123
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
Suggested Courses

734 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