Hi,

I have start and end time like this:

$start = "Wed Apr 27 03:57:21 EDT 2011"
$end = "Wed Apr 27 03:58:42 EDT 2011"

I need to compute the exact time taken some thing like 2 hours or 1 min.

I have to do this with out using Perl Modules.

I tried like this:

$start = gettime;

#some piece of code....

$end = gettime;

$diff = $start - $end;

print "\n diff --- $diff \n";

sub gettime
{
    my ($sec,$min,$hour,$mday,$mon,$year)=localtime(time);
    $mon++;
    $year+=1900;

    return "$mday/$mon/$year $hour:$min:$sec";
}

How can i get the difference between start time and end time?

For example:

50 secs
10 mins

How can i get the above time with out any perl modules in perl?

Any ideas?

Regards
amith

Recommended Answers

All 5 Replies

Why can't you use modules? Yes, even you can use CPAN

Hi,

Thanks but i cannot use modules that is not prohibited.

why it can't be done with out modules?

Please suggest me a way to do with out modules.

Regards
Archana

#!/usr/bin/perl
use strict;
use warnings;
use 5.008;

my $start = time;

#some piece of code....
sleep(10); #For example, do nothing for 10 seconds;

my $end = time;

my $diff = $end - $start;

print "\n diff --- $diff seconds\n";

You could start with the above, but the modules will handle special cases that I have ignored such as changing from Standard Time to Daylight Savings Time before the job finishes, etc. That is one reason why it is better to use modules.

localtime and gmtime are not modules, these are built-in perl functions, same as time.

localtime and gmtime are not modules, these are built-in perl functions, same as time.

Yes. I used only the time function rather than localtime in my example because I don't see how localtime helps in calculating the difference between two times. The time function returns the time as number of seconds since epoch (some fixed date and time assumed by the platform on which perl runs) so subtracting time at the start of the job from time at the end gives the duration of the job in seconds.

If @amithlaxman wants to convert the number of seconds into hours and minutes, he can divide the seconds by the number of seconds in an hour, then divide the remainder by the number of seconds in a minute.

@amithlaxman hasn't told us who prohibited the use of modules. A manager who tells his Perl programmers not to use modules must have a reason, and I can't think of a good one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.