- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script to calculate duration
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
04-14-2005 03:59 PM
04-14-2005 03:59 PM
I have the following date time format:
start_date_time|stop_date_time
Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM
Wanted to get a duration = stop_date_time - start_date_time
Need help on scripting.
reagrds,
Munawar
Solved! Go to Solution.
- Tags:
- date arithmetic
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 04:27 PM
04-14-2005 04:27 PM
Re: Script to calculate duration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 04:31 PM
04-14-2005 04:31 PM
Re: Script to calculate duration
Any would do. I would prefer in shell script.
But if you have one in perl, would great to share.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 06:22 PM
04-14-2005 06:22 PM
Re: Script to calculate duration
I created someting like that with a while loop that prints every timeout period to screen and kills the process after one hour :
MAX_TIMEOUT=3600 # Maximum backup time in seconds per process (3600 is one hour)
TIMEOUT="60" # Verify period of one minute
while (( $(UNIX95= ps -xC $(basename ${PROG}) |grep ${ORACLE_SID} |wc -w) > 0 ))
do
# wait for the timeout period
sleep ${TIMEOUT}
# update counter with one
(( counter +=1 ))
print counter=${counter}
# calculate total waiting time
(( wait_time=${counter}*${TIMEOUT} ))
# print backup time to screen
print backup ${ORACLE_SID} running ${wait_time} seconds
if (( ${wait_time} >= ${MAX_TIMEOUT} ))
then
print "Killing ${ORACLE_SID} backup process after ${wait_time} seconds"
kill $(UNIX95= ps -xC $(basename ${PROG}) -o pid | tail -1 )
fi
done
Hope it helps,
Renarios
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 06:37 PM
04-14-2005 06:37 PM
Re: Script to calculate duration
I just want to get a duration for each data and write it on next field.
Existing file:
|Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM|
Expected Result:
|Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM|
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 07:14 PM
04-14-2005 07:14 PM
Solution$ ./script.pl
Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM|2067
It prints out duration in seconds (you can redirect output to the new file).
The script disregards 3 digits before PM/AM. I assume this is fraction of millisecond.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 07:15 PM
04-14-2005 07:15 PM
Re: Script to calculate duration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 07:17 PM
04-14-2005 07:17 PM
Re: Script to calculate duration
for the solution you should read:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xea948cc5e03fd6118fff0090279cd0f9,00.html
A. Clay Stephenson's script does this and a lot more!
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 07:31 PM
04-14-2005 07:31 PM
Re: Script to calculate duration
I will try now..will let you know soon :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2005 08:24 PM
04-14-2005 08:24 PM
Re: Script to calculate duration
The script confuse on AM and PM cause the duration wrong. For example:
|Jan 5 2005 12:39:48:890AM|Jan 5 2005 1:15:17:173AM|-41071
|Jan 6 2005 11:58:58:303AM|Jan 6 2005 12:16:39:466PM|44261
|Jan 9 2005 12:56:18:443PM|Jan 9 2005 1:30:49:986PM|-41129
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2005 01:45 AM
04-15-2005 01:45 AM
Re: Script to calculate duration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2005 01:53 AM
04-15-2005 01:53 AM
Re: Script to calculate duration
The perl below shouls be close... but you would need to verify the AM/PM handling.
As a european I never managed to figure that one out. for PM you add 12 right? 10:00 PM is really 22:00.
And 12:30 pm = 24:30 right?
Hmmm, how can that be, a day has only 24 hours!?
Is this 5-Jan 12:30PM really 6-Jan 00:30?
:-) :-)
--------------------------- try this ----
use Time::Local;
sub my_seconds {
# Jan 2 2005 7:45:57:206PM
my ($time)=@_;
my $t, $hour, $mon;
if ($time =~ /(\w+)\s+(\d+)\s+(\d+) (\d+):(\d+):(\d+):(\d+)(AM|PM)/) {
$hour = $4;
if ($8 eq "PM") { if ($hour == 12) { $hour=0 } else {$hour += 12}}
$mon = index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)/3;
$t = timelocal($6,$5,$hour,$2,$mon,$3-1900) + $7/1000;
}
return $t;
}
while (<>) {
chop;
($t1,$t2) = split (/\|/);
printf ("%s -- %s -- %9.3f\n", $t1, $t2, my_seconds($t2) - my_seconds($t1));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2005 01:59 AM
04-15-2005 01:59 AM
Re: Script to calculate duration
12:30 PM is 30 minutes past noon
So on a 24-hour clock
12:30 AM is 00:30
12:30 PM is 12:30
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2005 02:27 AM
04-15-2005 02:27 AM
Re: Script to calculate duration
# echo "Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM" | perl -MDate::Manip -lne'print DateCalc ((map { ParseDate ($_) } split m/\|/ => $_),\$err)'
+0:0:0:0:0:34:27
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2005 02:30 AM
04-15-2005 02:30 AM
Re: Script to calculate duration
Jan 5 2005 12:39:48:890AM|Jan 5 2005 1:15:17:173AM
Jan 6 2005 11:58:58:303AM|Jan 6 2005 12:16:39:466PM
Jan 9 2005 12:56:18:443PM|Jan 9 2005 1:30:49:986PM
lt09:/home/merijn 117 > perl -MDate::Manip -lpe'$_.="|".DateCalc ((map { ParseDate ($_) } split m/\|/ => $_),\$err)' xx.txt
Jan 5 2005 12:39:48:890AM|Jan 5 2005 1:15:17:173AM|+0:0:0:0:0:35:29
Jan 6 2005 11:58:58:303AM|Jan 6 2005 12:16:39:466PM|+0:0:0:0:0:17:41
Jan 9 2005 12:56:18:443PM|Jan 9 2005 1:30:49:986PM|+0:0:0:0:0:34:31
lt09:/home/merijn 118 >
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2005 03:00 AM
04-15-2005 03:00 AM
Re: Script to calculate duration
Ouch, in my feeble attempt to crack a joke I managed to actually confuse myself on the AM/PM thing! Thanks Stephen, for treating me kindly.
---------------------------
use Time::Local;
sub my_seconds {
# Jan 2 2005 7:45:57:206PM
my ($time)=@_;
my $t, $hour, $mon;
if ($time =~ /(\w+)\s+(\d+)\s+(\d+) (\d+):(\d+):(\d+):(\d+)(AM|PM)/) {
$hour = $4;
$hour = 0 if ($hour == 12 && $8 eq "AM");
$hour += 12 if ($hour != 12 && $8 eq "PM");
$mon = index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)/3;
$t = timelocal($6,$5,$hour,$2,$mon,$3-1900) + $7/1000;
}
return $t;
}
while (<>) {
chop;
($t1,$t2) = split (/\|/);
printf ("%s -- %s %9.3f\n", $t1, $t2, my_seconds($t2) - my_seconds($t1));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-17-2005 04:26 PM
04-17-2005 04:26 PM
Re: Script to calculate duration
Thanks a lot for the invaluable input.
Finally I got it work..modify lil bit to get :-)
use Time::Local;
#use Time::localtime;
(my $prog = $0) =~ s|.*/||;
my %mnames = (
Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6,
Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12,
);
my ($stime, $etime);
while (<>) {
chomp;
next unless /\d+/;
s/\r//;
($stime, $etime) = (split(/\|/, $_))[3,4];
printf "$_|%.3lf\n", unixtime($etime) - unixtime($stime);
}
# Time in: Jan 2 2005 8:19:24:503PM
#
sub unixtime {
my $t = shift;
return
unless $t =~ m/^(...)\s+(\d+)\s+(\d+)\s+(\d+):(\d+):(\d+):(\d\d\d)(..)/;
my $mon = $mnames{$1};
my $day = $2;
my $year = $3;
my $hour = $4;
my $min = $5;
my $sec = $6;
my $mili = $7;
my $am = $8;
$hour += 12 if $am eq 'PM';
return timelocal($sec, $min, $hour, $day, $mon-1, $year - 1900)
+ $mili/1000.0;
}
OK..time for giving the points to you.
/munawar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2005 05:51 AM
04-18-2005 05:51 AM
Re: Script to calculate duration
If you are using POSIX shell, you can use the SECONDS shell variable. This is the number of seconds since you logged in. Just do:
START=$SECONDS
FINISH=$SECONDS
ELAPSED=$(expr $FINISH - $START)
Regards,
Mark