- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to get calculation time from date command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2004 09:07 PM
08-26-2004 09:07 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2004 01:18 AM
08-27-2004 01:18 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2004 12:17 PM
08-27-2004 12:17 PM
Re: How to get calculation time from date command
caljd.sh is possible calculation time in seconds?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-27-2004 04:57 PM
08-27-2004 04:57 PM
Re: How to get calculation time from date command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2004 01:51 AM
08-29-2004 01:51 AM
Re: How to get calculation time from date command
It is helpful many.
But I need the solution to use a date command.
I need to use at a Shell Script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2004 06:46 AM
08-29-2004 06:46 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2004 06:45 PM
08-29-2004 06:45 PM
Re: How to get calculation time from date command
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2004 08:05 PM
08-29-2004 08:05 PM
Re: How to get calculation time from date command
#!/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2004 08:06 PM
08-29-2004 08:06 PM
Re: How to get calculation time from date command
Not sure but just wanted to know whether "timex" command would help you out!!
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2004 01:02 AM
08-30-2004 01:02 AM
Re: How to get calculation time from date command
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2004 01:43 AM
08-30-2004 01:43 AM
Re: How to get calculation time from date command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2004 07:21 AM
08-30-2004 07:21 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2004 04:38 PM
08-30-2004 04:38 PM