Operating System - HP-UX
1832516 Members
3609 Online
110043 Solutions
New Discussion

How to get calculation time from date command

 
양홍석_1
New Member

How to get calculation time from date command

How to get calculation time from date command

Hi,

How to get calculation time(seconds) from date
command ?

For example
current_date=08/01/2004 00:00:20

after_date=${current_date} + 50
result -> 08/01/2004 00:01:10

before_date=${current_date} - 50
result -> 07/31/2004 23:59:30

Thanks
12 REPLIES 12
Hein van den Heuvel
Honored Contributor

Re: How to get calculation time from date command


Please use the Forum SEARCH function to find dozens of prior discussions and answers to this and similar question.

You'll probably end up using: caljd.sh


Cheers,
Hein.

양홍석_1
New Member

Re: How to get calculation time from date command

hi hein,

caljd.sh is possible calculation time in seconds?
Hein van den Heuvel
Honored Contributor

Re: How to get calculation time from date command

Argh, now I feel bad... caljd stands for calendar / julian / date and is only concerned with dates, not times.

Here is a little bit of perl that will do the job. Convert to subroutine if needed:

$cat time.pl
use Time::Local;
if (/(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)\s*([- +])\s*(\d+)/) {
$time = timelocal($6,$5,$4,$2,$1-1,$3);
$time += ($7 eq "-")? -$8 : $8;
($sec,$min,$hour,$mday,$mon,$year) = localtime $time;
printf ("%02d/%02d/%4d %02d:%02d:%02d\n",
1+$mon,$mday,1900+$year,$hour,$min,$sec);
}

This perl code matches an input string against "mm/dd/yyyy hh:mm:ss", followed by a plus, minus, or space, followed by a number.
Each component is remembered in $1 thru $8 using ()'s.
Then we use the perl module Time::Local function 'timelocal' to convert this to seconds since begin of time.
Next add or subtract the delta and convert back to components with standard 'localtime'.
Finally print result with format similar to input.

Working example:

$ echo "08/01/2004 00:00:20 -50" | perl -n time.pl
07/31/2004 23:59:30
$ echo "08/01/2004 00:00:20 +50" | perl -n time.pl
08/01/2004 00:01:10


양홍석_1
New Member

Re: How to get calculation time from date command

hi

It is helpful many.
But I need the solution to use a date command.
I need to use at a Shell Script.
Hein van den Heuvel
Honored Contributor

Re: How to get calculation time from date command


Yeah well... just put the whole thing in backticks and feed to date?! Or READ the result into a symbol and feed that into date.

Or... just call 'date' in the perl script directly.

Of course if you want to feed into date, then you do NOT want results like '07/31/2004 23:59:30' but you possibly would like a format: mmddhhmm[[cc]yy
So just change the file printf (or sprintf if you are going to do all within perl) to

printf ("%02d%02d%02d%02d%4d\n", 1+$mon,$mday,$hour,$min,1900+$year);

However... what problem are you really trying to solve? Do you intend to adjust the clock? The please first check out 'man date' and look for he -a (adjust) option!

Good luck,
Hein.

Elmar P. Kolkman
Honored Contributor

Re: How to get calculation time from date command

perl -e 'print(localtime(time()+@ARGV[1])."\n")' --

So, your lines would become:
after_date=$(perl -e 'print(localtime(time()+@ARGV[1])."\n")' -- 50)

etc.

If you want to calculate from currant_date, you would need to convert it back to seconds-since-jan-1970... and replace the time() in the perl routine to that value.

Using the date command will not work, I'm afraid. You could do what you want using awk instead of perl, but the result would be the same...


Every problem has at least one solution. Only some solutions are harder to find.
Muthukumar_5
Honored Contributor

Re: How to get calculation time from date command

We can do this as,

#!/usr/bin/ksh
set -x
check="yes"

while [[ "$check" = "yes" ]]
do

TIME=`date +'%X'`
echo "Current time: `date`"

echo "Enter your value to increment / decrement time(seconds)"
echo "Example +10 or -10"

read sec

TOTAL=`echo $TIME | awk -F ":" '{ print ($1*3600)+($2*60)+$3 }'`
let NEWVAL=$TOTAL+$sec

DATE=$(echo "`echo $(($NEWVAL/3600))`:`echo $(($(($NEWVAL%3600))/60))`:`echo $(($(($NEWVAL%3600))%60))`")

echo "Current date+timing"
echo "`date +'%a %b %d'` `echo $DATE` `date +'%z %Y'`"

echo "Do you want to continue (yes/no)"
read check

sleep 1

done
#
#

It will give the way to do.

Easy to suggest when don't know about the problem!
Bharat Katkar
Honored Contributor

Re: How to get calculation time from date command

HI,
Not sure but just wanted to know whether "timex" command would help you out!!
Regards,
You need to know a lot to actually know how little you know
john korterman
Honored Contributor

Re: How to get calculation time from date command

Hi,

I would prefer the perl solution; it looks very elegant.

If you insist on using shell command, you can try the attached script, return_seconds.sh. It does nothing more than count seconds back to e.g. 1970, and it cannot take your format as input.

In order to turn a number of seconds into a date (not in your requested format either) I suggest adb: in the example below I change my TZ before converting, else 2 hours are added. Example:

jk# CURRENT_DATE=$( ./return_seconds.sh 1970 2004 08 01 00 00 20 )
jk# echo $CURRENT_DATE
1091318420
jk# AFTER_DATE=$(( $CURRENT_DATE + 50 ))
jk# echo $AFTER_DATE
1091318470
jk# BEFORE_DATE=$(( $CURRENT_DATE - 50 ))
jk# echo $BEFORE_DATE
1091318370
jk# echo $TZ
MET-1METDST
jk# TZ=METMETDST
jk# echo 0d${AFTER_DATE}=Y|adb
2004 Aug 1 00:01:10
jk# echo 0d${BEFORE_DATE}=Y|adb
2004 Jul 31 23:59:30
jk#


No guarantee that the script works!

regards,
John K.
it would be nice if you always got a second chance
john korterman
Honored Contributor

Re: How to get calculation time from date command

hope the file is attached now...
it would be nice if you always got a second chance
Hein van den Heuvel
Honored Contributor

Re: How to get calculation time from date command


Along those lines, here is a shell script I did to get the time in seconds for the start of a given day (YYYY MM DD)

#!/usr/bin/sh
typeset -i YEAR=1970 MONTH=0 DAYS=0 SECS=0 LEAP=0
while [ $YEAR -lt $1 ]
do
# script only valid from 1970 - 2032, so 100 and 400 year rule cancel
# echo $YEAR $DAYS $SECS
DAYS=$(( $DAYS + 365 + $LEAP ))
YEAR=$(( $YEAR + 1 ))
if [ $(($YEAR % 4 )) -eq 0 ]; then LEAP=1; else LEAP=0; fi
done

# script could pre-add days in month and select from list without loop
for DAYS_IN_MONTH in 31 28 31 30 31 30 31 31 30 31 30 31
do
MONTH=$(( $MONTH + 1 ))
if [ $MONTH -eq 3 ]; then DAYS=$(( $DAYS + $LEAP )); fi
if [ $MONTH -ge $2 ]; then break; fi
DAYS=$(( $DAYS + $DAYS_IN_MONTH ))
done

DAYS=$(( $DAYS + $3 - 1 ))
SECS=$(( $DAYS * 86400 ))

echo $DAYS $SECS



The same thing in perl, but with error handling:


($year,$month,$day) = @ARGV;
die "Please provide year month day arguments" unless $day;
die "Year must be between 1970 and 2032" if (($year < 1970) || ($year > 2032));
use Time::Local;
$time = timegm(0,0,0,$day,$month-1,$year);
$days = $time / 86400;
print "$days $time\n";


Back to the real problem....
Hein.
양홍석_1
New Member

Re: How to get calculation time from date command

thanks for helpful.