If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to increment dates in ksh script ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-25-04, 16:56
bluwulf bluwulf is offline
Registered User
 
Join Date: Oct 2004
Posts: 31
How to increment dates in ksh script ?

Is there a way (in the Korn Shell) to accurately increment a date ?



Say for example that I awk out the following "start_date" and "end_date" (in the format below) from a file:

start_date = 10/25/2004

end_date = 11/12/2004

How do I increment from the start_date to the end_date ?
(also note the change in months for the dates it is also possible that the years could be different)

I wuz given an example like this though :

Quote:

my_date=`TZ=EST-24 date` # where date is the system's date (assume that date returned Mon Oct 25 15:51:18 EST 2004)

echo $my_date # my_date returns Tue Oct 26 20:53:08 EST 2004


# Each time I subtract 24 the date is incremented
(The above seem to work but I don't really understand it)

For my purposes I could very well work with this "solution"....except for the little problem that the dates that I would be feeding the script would look like :


10/26/2004 NOT Tue Oct 26 20:53:08 EST 2004


Is there a way to convert 10/26/2004 TO Tue Oct 26 20:53:08 EST 2004

Last edited by bluwulf; 10-25-04 at 17:02.
Reply With Quote
  #2 (permalink)  
Old 10-27-04, 12:06
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi,
may be this can help.
http://www.dbforums.com/t1004206.html
__________________
Greetings from germany
Peter F.
Reply With Quote
  #3 (permalink)  
Old 10-27-04, 12:48
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Try...
Code:
TZ=EST-24 date +%d/%m/%Y
...and you aren't subtracting 24 hours from the date, you are setting theTimeZone to be "EST-24" which is interpreted as 24 hours after EST.

btw This isn't a particularly portable, so don't be surprised if it doesn;t work in every shell you use!
Reply With Quote
  #4 (permalink)  
Old 10-27-04, 13:48
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
################################################## ###################
# Example for your application
# start_date = 10/25/2004
# end_date = 11/12/2004
################################################## ###################

######## example to use idate.sc ####################################
######## Remember it is the german Format // Day / Month / Year // ##

start_date="25.10.2004"
end___date="12.11.2004"

echo "Start with "$start_date" and end with "$end___date
echo "--------------------------------------------------"

INF_start_date=`idate.sc $start_date`
INF_end___date=`idate.sc $end___date`
days_between_s_e=`expr $INF_end___date - $INF_start_date`

echo "INFORMIX Start_Date "$INF_start_date
echo "INFORMIX End_Date "$INF_end___date
echo "Days between Start and End "$days_between_s_e

echo "------- and now the way back ---------------------"
echo "--------------------------------------------------"

x=`expr $INF_end___date - 18`
start_date=`idate.sc $x`
y=`expr $INF_start_date + 18`
end_date=`idate.sc $y`

echo "Start_Date "$start_date
echo "End_Date "$end_date

##### And this will be the output ###################
##### for this example ###################
#
#Start with 25.10.2004 and end with 12.11.2004
#--------------------------------------------------
#INFORMIX Start_Date 38284
#INFORMIX End_Date 38302
#Days between Start and End 18
#------- and now the way back -----------------
#----------------------------------------------
#Start_Date 25.10.2004
#End_Date 12.11.2004
__________________
Greetings from germany
Peter F.
Reply With Quote
  #5 (permalink)  
Old 10-27-04, 15:29
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Smile

More informations to idate.sc

My english ist not so good. So I want to explain the handling again.

idate.sc have one parameter.

STEP1
If you use an integer value like this "idate.sc 38284" you get back the date in this format "25.10.2004" (dd.mm.yyyy)

If you use a date value like this "idate.sc 25.10.2004" (dd.mm.yyyy) you get back an integer value in this format "38284" . Now you can add or subtract this value and give it back to STEP1 to get the date you have manipulate.

Bye
__________________
Greetings from germany
Peter F.
Reply With Quote
  #6 (permalink)  
Old 11-19-04, 17:50
bluwulf bluwulf is offline
Registered User
 
Join Date: Oct 2004
Posts: 31
I jus would like to say thanx for all the suggestions thus far.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On