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
calculations in awk/sed anchal_khare Shell Programming and Scripting 3 03-26-2009 11:41 PM
Date Calculations mitschcg UNIX for Dummies Questions & Answers 8 03-06-2009 04:40 AM
ksh, calculations using bc cesarNZ Shell Programming and Scripting 2 12-14-2008 10:42 PM
Time Calculations Nysif Steve UNIX for Dummies Questions & Answers 5 09-14-2007 09:49 AM
Float calculations sharmavr UNIX for Dummies Questions & Answers 1 07-26-2006 03:18 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 04-22-2009
jidsh jidsh is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 15
Need help with Date calculations in ksh

Hi Gurus,

I am writing a script where we enter two dates, one a FROM DATE and the other a TO DATE. I need to validate that difference between the two dates is always less than or equal to 60 days.
I could not find any date utility in ksh that could help me with this.
Finally, I am deciding to write a Java code and call it from the script as it is much easier and quicker to do it in Java than in Unix.
Can somebody guide me to a way in unix to do this rather than writing another script or Java or C code to do it and call it from here.

Thanks in advance.
  #2 (permalink)  
Old 04-22-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,346
A link to calculate days elapsed between 2 dates.

Regards
  #3 (permalink)  
Old 04-22-2009
colemar colemar is offline
Registered User
  
 

Join Date: Apr 2009
Location: Trento, Italy
Posts: 116
Quote:
Originally Posted by jidsh View Post
I need to validate that difference between the two dates is always less than or equal to 60 days.
If using the external command date is not against the rules, and provided that you are using date from GNU Coreutil then:

Code:
colemar@deb:~$ cat days_between
#!/bin/sh

typeset -i days_between
function days_between {
  days_between=$((($(date -d $2 +%s)-$(date -d $1 +%s))/86400))
}

days_between $1 $2
echo $days_between

colemar@deb:~$ ./days_between 2008-04-22 2009-04-22
365

  #4 (permalink)  
Old 04-22-2009
jidsh jidsh is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 15
Colemar,

Thanks for your help.
I tried it and this is what has happened.

Quote:
[~/jiddvish]> ./days_between 2008-04-22 2009-04-22
./days_between: typeset: not found
./days_between: function: not found
./days_between: syntax error at line 5: `days_between=$' unexpected

[~/jiddvish]> uname -a
SunOS <server name deleted from here> 5.9 Generic_118558-35 sun4u sparc SUNW,Sun-Fire-V490
Franklin,
I did go through the script that you sent but I felt it is too long and big to go through for this small validation. Thanks a lot for your time and efforts though.

There is one more approach I designed myself and working on it right now.
I am creating an array that will contain number of days elapsed at the end of each month in the calendar year. Based on the month entered i can get the number of days elapsed at the end of the previous month, add it with the day of the month entered and get number of days elapsed in that year. If the difference is negative, which means the year has changed. I am hopeful that this logic will work. Once the code is ready I will paste it for others who can use it.

Thanks once again. Appreciate it really.
  #5 (permalink)  
Old 04-23-2009
colemar colemar is offline
Registered User
  
 

Join Date: Apr 2009
Location: Trento, Italy
Posts: 116
Replace #!/bin/sh with #!/bin/ksh
  #6 (permalink)  
Old 04-23-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,346
Quote:
Originally Posted by jidsh View Post
Franklin,
I did go through the script that you sent but I felt it is too long and big to go through for this small validation. Thanks a lot for your time and efforts though.
Place the script in your directory with the name datecalc and call it within your script as:


Code:
var=$(./datecalc -a 2009 04 22 - 2008 04 22)

echo $var

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 11:44 PM.


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