The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
simple issue.. the_learner UNIX for Advanced & Expert Users 2 01-17-2008 04:44 PM
date issue-find prevoius date in a patricular format bsandeep_80 UNIX for Advanced & Expert Users 3 11-15-2007 08:42 PM
simple scanf issue ? the_learner High Level Programming 4 04-11-2007 04:51 AM
a simple way of converting a date in seconds to normal date travian HP-UX 2 11-23-2006 12:25 PM
something simple, but i have no idea: a login issue penguin-friend UNIX for Dummies Questions & Answers 2 03-31-2005 09:05 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-05-2009
muay_tb muay_tb is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 33
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 (permalink)  
Old 05-05-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,972
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 (permalink)  
Old 05-05-2009
muay_tb muay_tb is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 33
thanks pludi
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0