Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print hours

Status
Not open for further replies.

donny750

Programmer
Jul 13, 2006
145
0
0
FR
hello,

I've made a perl script,
I will print the start hour of the script and the end hour.
I've make this

Code:
#!/usr/bin/perl

use strict;
use POSIX qw(strftime);

...code


print "Started  ".strftime( '%H:%M:%S', localtime )."\n";
...code
print "Stopped  ".strftime( '%H:%M:%S', localtime )."\n";

I've problem beacause i've the same hours;
the script start at 14:56:25
and ended at 15:00:12

and my script print
Started 14:56:25
Stopped 14:56:25
instead of
Started 14:56:25
Stopped 15:00:12

Can you help me , please

 
You could use Time::Local

No really very nice, buy it does the job

Code:
use Time::Local;

# start runtime here 
$time_esec11 = timelocal(localtime());
($SS11,$MI11,$HH11,$DD11,$MM11,$YY11,$DAY11) = localtime($time_esec11);

# at the end of your script
getruntime();


sub getruntime{

	my $time_esec2 = timelocal(localtime());
	my ($SS2,$MI2,$HH2,$DD2,$MM2,$YY2,$DAY2) = localtime($time_esec2);
	#print "TEND: $HH11.$MI11.$SS11\n";
	$HH3 =  $HH2-$HH11;	# 10:59.34 -> 11:06.34
	if ($MI2<$MI11){		#start min is greater than end min
		$HH3 -= 1;
		$MI3 = ($MI2 + 60) - $MI11;
	}else { 
		$MI3 =  $MI2-$MI11;
	}
	if ($SS2<$SS11){		#start min is greater than end min
		$MI3 -= 1;
		$SS3 = ($SS2 + 60) - $SS11;
	}else { 
		$SS3 =  $SS2-$SS11;
	}
		
	$runtime = "Execution Time: $HH3 HOURS, $MI3 MINUTES, $SS3 SECONDS";
	print "$runtime\n";
	return $runtime;

}

dmazzini
GSM System and Telecomm Consultant

 
the problem is the syntax, you are trying to use concatentation to print the start and end time strings, but you have the concatenation operator (.) inside the double-quotes which treats it like a literal dot. Since print expects a list you can use the comma operator but you could use concatentation if you put the operator in the right plce:

Code:
#!/usr/bin/perl

use strict;
use POSIX qw(strftime);

print 'Started ' . strftime( '%H:%M:%S', localtime ) . "\n";
sleep(5);
print 'Stopped ' . strftime( '%H:%M:%S', localtime ) . "\n";

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Donny, I'm a bit confused. I ran
Code:
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(strftime);

print "Started  ".strftime( '%H:%M:%S', localtime )."\n";;
sleep 5;
print "Stopped  ".strftime( '%H:%M:%S', localtime )."\n";
which is effectively your original post with a sleep in it, and got
Code:
Started  21:27:17
Stopped  21:27:22
Did you perhaps have your code between the two timing points commented out for testing? [wink]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
oops, I see I made an error in viewing the code. The concatenation operator is not inside the double-quotes a I thought for some reason (lack of coffee or brains or both) so the original code should have worked sans any other problems or errors.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
sorry, it's run good
i've made an error,
firstly i've use this


Code:
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(strftime);
my $x = strftime( '%H:%M:%S', localtime );
print "Started  ".$x."\n";;
....
print "Stopped  ".$x."\n";
i was mistaken, i don't run the good script

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top