Simple date issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple date issue
# 1  
Old 05-05-2009
Simple date issue

Hi all,

i have used the search already before someone shouts at me and i have seen the 'datecalc' program but this is not working correctly for me in the shell and environment i am using.

I am using solaris 10 and bourne shell.

I have two dates '07-04-2009' and '05-05-2009'. I just need to know when the number of days exceeds 90 between two dates.

Code:
i.e. 

num_days='07-04-2009' - '05-05-2009'

if [ $num_days -gt 90 ]; then
    do action....
fi

I can do it in Perl but this doesnt handle leap years etc:

Code:
epoch()
{
        perl -e '
                use Time::Local;

                $fmt = "%s";  # %s = seconds in epoch
                $mday = substr("$ARGV[0]", 6, 2);
                $mon =  substr("$ARGV[0]", 4 ,2);
                $year = substr("$ARGV[0]", 0 ,4);
                $time = timelocal(0,0,0,$mday,$mon,$year);
                print int $time;
                ' "$1"
}

date1=$( epoch '070709' );
date2=$( epoch '070809' );

diff=$(( $date1 - $date2 ))

if [[ $diff -gt 7776000 ] ; then
  do action....
fi


Last edited by muay_tb; 05-05-2009 at 06:48 AM..
# 2  
Old 05-05-2009
If you only want the time between two dates, no need to bug yourself with Time::Local if the original POSIX functions are good enough:
Code:
$ perl -MPOSIX -e 'print mktime(0,0,0,1,4-1,2008-1900),"\n";'
1207004400
$ perl -MPOSIX -e 'print mktime(0,0,0,1,1-1,2008-1900),"\n";'
1199142000
$ echo "(1207004400-1199142000)/86400" | bc -q -l
91.00000000000000000000
$ perl -MPOSIX -e 'print mktime(0,0,0,1,4-1,2009-1900),"\n";'
1238540400
$ perl -MPOSIX -e 'print mktime(0,0,0,1,1-1,2009-1900),"\n";'
1230764400
$ echo "(1238540400-1230764400)/86400" | bc -q -l
90.00000000000000000000

# 3  
Old 05-05-2009
thanks pludi Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple date issue

Hi , Here is the smaller version of the problem. Working individually as command ************************>echo $SHELL /bin/bash ************************>TO_DAY=`date` ************************>echo $TO_DAY Tue Jul 16 02:28:31 EDT 2013 ************************> Not working when... (5 Replies)
Discussion started by: Anupam_Halder
5 Replies

2. Shell Programming and Scripting

Simple issue, what is wrong?

I had this working a few days ago but I since changed it. Heres the code x=1 while 1 2 3 4 5 6 1=$(ps -ef | grep process | awk '{ print $2}') if then echo "The database is accepting connections..." echo "Now I will check the next process" 2=$(ps -ef | grep process1 |... (10 Replies)
Discussion started by: jeffs42885
10 Replies

3. Shell Programming and Scripting

Simple SED issue

Hi! I'm a newbie and can't find the exact sed to make this work. I've installed CentOS and am trying to pass variables to a network-config file. variables: $ipAddress $netmask $gateway file: DeviceList.Ethernet.eth0.IP=192.168.1.10 DeviceList.Ethernet.eth0.Netmask=255.255.255.0... (5 Replies)
Discussion started by: greipr
5 Replies

4. Shell Programming and Scripting

Simple Cut issue

I have a long string that looks something like this.... <string>http://abc.com/40/20/zzz061-3472/dP3rXLpPPV2KC846nJ4VXpH7jt4b3LJgkL/tarfile_date.tar</string> I need to but the tar file name. So I need to put between "/" and ".tar</string>". My desired result should be "tarfile_date". (7 Replies)
Discussion started by: elbombillo
7 Replies

5. Shell Programming and Scripting

Simple date formatting?

Hi guys, I have some embedded perl within my shell script to get me the modification time/date of a file which returns me the following string: Fri May 1 09:52:58 2009 I have managed to get the bits i need such as 1-May-2009, but what i would prefer is 010509 instead... Here is my... (4 Replies)
Discussion started by: muay_tb
4 Replies

6. Shell Programming and Scripting

simple date problem

i have a script that grep for today date a=`date +"%F"`--------greps current/today date wat if suppose i want to grep a date for yesterday... how to do that using the above format: i,e 2008-01-20 (4 Replies)
Discussion started by: ali560045
4 Replies

7. UNIX for Advanced & Expert Users

simple issue..

I have a program. Everytime that I run that program ,it prints a disclaimer msg and prompts the user to accept the agreement (i.e: type 'y' or 'n'). Now I want to run the program multiple times with different inputs. So I wrote a script which runs in a loop and calls the program multiple times.... (2 Replies)
Discussion started by: the_learner
2 Replies

8. UNIX for Advanced & Expert Users

date issue-find prevoius date in a patricular format

Hi , I have written a shell script that takes the current date on the server and stores it in a file. echo get /usr/home/data-`date '+%Y%d'`.xml> /usr/local/sandeep/GetFILE.ini I call this GetFILE.ini file from an sftp program to fetch a file from /usr/home/ as location. The file is in... (3 Replies)
Discussion started by: bsandeep_80
3 Replies

9. Programming

simple scanf issue ?

Hello everyone, I hope someone is awake to help me on this.. hey How can I do something like this: The user is asked is asked to enter an int value, but I want to provide a default value on stdout, which they can back space and change it to whatever they want.. for e.g: Enter the... (4 Replies)
Discussion started by: the_learner
4 Replies

10. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies
Login or Register to Ask a Question