Hi,
the below script gets 2 sepereate hour and give me the difference between them.
(ex, 08:00 - 17:00 = 9Hrs)
the only problem is when i enter somthing like this(
ex, (23:00 - 6:00(Am) = 17:00 should be 7)
any idea how can i modify this to handle my problem or maybe use other module for me needs
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
sub str2time{
my $in = shift;
my ($hour, $min, $sec) = split (/:/, $in);
my ($mday,$mon,$year) = (localtime(time))[3,4,5];
my $retn;
eval { $retn = timelocal($sec,$min,$hour,$mday,$mo
+n,$year); };
return $retn;
}
print "Please enter the first time in HH:MM:SS format: ";
my $intime = <STDIN>;
chomp $intime;
my $time1 = str2time($intime);
die "Invalid time: $intime\n" if !defined $time1;
print "Please enter the 2nd time in HH:MM:SS format: ";
$intime = <STDIN>;
chomp $intime;
my $time2 = str2time($intime);
die "Invalid time: $intime\n" if !defined $time1;
my $diff;
if ( $time1 > $time2 ) { $diff = $time1 - $time2}
else { $diff = $time2 - $time1}
my $mins = int($diff / 60);
my $hours = int($mins / 60);
$mins = $mins - ($hours * 60);
my $secs = $diff - ($mins * 60);
printf "Difference: %02d:%02d:%02d\n", $hours,$mins,$secs;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.