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 > New Dates in Unix

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-08-04, 18:59
adokli adokli is offline
Registered User
 
Join Date: Jan 2004
Posts: 4
New Dates in Unix

How do I derive a new date from a given date plus or minus a number of days in unix. For example:

25-dec-2003 + 4days to give me 29-dec-2003

Thank you very for your assistance
Reply With Quote
  #2 (permalink)  
Old 01-09-04, 09:24
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Re: New Dates in Unix

Unix machines keep time by storing the number of seconds from the epoch as a 32-bit integer. I would determine the current time (it's now 1073657881 o'clock), add the required number of seconds and convert back. Whilst it's not strictly a shell script [1], this should do it:

#!/path/to/perl -w
# Takes one argument, the number of days' offset
defined $ARGV[0] || exit 1;
$ARGV[0] =~ /^-?\d+$/ || exit 2;
$DBFORUMS=time;
$DBFORUMS+=($ARGV[0]*60*60*24);
print localtime($DBFORUMS) . "\n";
__END__

There's also a GNU extension to 'date' which returns the value of the number of seconds past the epoch. Depending on your system, you may have this utility and you can write a similar script in the shell.


[1] I have considered running the perl debugger as my login shell
Reply With Quote
  #3 (permalink)  
Old 01-09-04, 09:34
gurey gurey is offline
Registered User
 
Join Date: Aug 2003
Location: Argentina
Posts: 780
Re: New Dates in Unix

Quote:
Originally posted by adokli
How do I derive a new date from a given date plus or minus a number of days in unix. For example:

25-dec-2003 + 4days to give me 29-dec-2003

Thank you very for your assistance
Hi,

Please test this form

a=4 ; expr `date +%d` + $a

Depending on as you need use.

Gustavo.
Reply With Quote
  #4 (permalink)  
Old 01-09-04, 13:58
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi,
I have a little programm that do what you ask for. (infdate.x)
If you are intrested post a privat message. I can send it to you by Mail

DAYS_TO_BE_ADD=4 # or back then use -4

AKT_DATE=`date '+%d.%m.%Y'`
INFORMIX_DATE=`infdate.x -s $AKT_DATE`
INFORMIX_DATE=`expr $INFORMIX_DATE + $DAYS_TO_BE_ADD`
NEW_DATE=`infdate.x -s $INFORMIX_DATE`
echo "today ==> "$AKT_DATE
echo "new day ==> "$NEW_DATE

And this is the output
--------------------------------
today ==> 09.01.2004
new day ==> 13.01.2004
__________________
Greetings from germany
Peter F.
Reply With Quote
  #5 (permalink)  
Old 03-08-04, 00:04
arunbhandari arunbhandari is offline
Registered User
 
Join Date: Mar 2004
Posts: 1
Lightbulb Please mail me infdate.x program

Hi,

I would like to use yopur small utility for date manipulation in Unix shell scripts. Please send me your program, if you dont mind.
My e-mail address: abhandari@deloitte.com

Thank you so much,

Arun Bhandari.


Quote:
Originally posted by fla5do
Hi,
I have a little programm that do what you ask for. (infdate.x)
If you are intrested post a privat message. I can send it to you by Mail

DAYS_TO_BE_ADD=4 # or back then use -4

AKT_DATE=`date '+%d.%m.%Y'`
INFORMIX_DATE=`infdate.x -s $AKT_DATE`
INFORMIX_DATE=`expr $INFORMIX_DATE + $DAYS_TO_BE_ADD`
NEW_DATE=`infdate.x -s $INFORMIX_DATE`
echo "today ==> "$AKT_DATE
echo "new day ==> "$NEW_DATE

And this is the output
--------------------------------
today ==> 09.01.2004
new day ==> 13.01.2004
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