Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Subtract Hours
by Limbic~Region (Chancellor) on Jun 22, 2009 at 14:21 UTC | |
|
Re: Subtract Hours
by si_lence (Deacon) on Jun 22, 2009 at 14:00 UTC | |
by ikegami (Patriarch) on Jun 22, 2009 at 14:33 UTC | |
by Limbic~Region (Chancellor) on Jun 22, 2009 at 14:40 UTC | |
by ikegami (Patriarch) on Jun 22, 2009 at 15:13 UTC | |
|
Re: Subtract Hours
by CountZero (Bishop) on Jun 22, 2009 at 14:32 UTC | |
|
Re: Subtract Hours
by ikegami (Patriarch) on Jun 22, 2009 at 15:24 UTC |