diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-12-23 03:14:27 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-23 03:14:27 +0000 |
| commit | d52452a21bc9e95ef462b1808dd63fabea90ec01 (patch) | |
| tree | f900ef45bdad3f61248c43010e7732dd349b1aeb | |
| parent | 78ce20025f7064c87c17e47e36757a24de50534e (diff) | |
| parent | cb51051651bbd1532ddbf211c0200efd6ac8770c (diff) | |
| download | perlweeklychallenge-club-d52452a21bc9e95ef462b1808dd63fabea90ec01.tar.gz perlweeklychallenge-club-d52452a21bc9e95ef462b1808dd63fabea90ec01.tar.bz2 perlweeklychallenge-club-d52452a21bc9e95ef462b1808dd63fabea90ec01.zip | |
Merge pull request #1060 from adamcrussell/challenge-039
updated solution for challenge 039
| -rw-r--r-- | challenge-039/adam-russell/perl/ch-1.pl | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/challenge-039/adam-russell/perl/ch-1.pl b/challenge-039/adam-russell/perl/ch-1.pl index 861fccd157..027500c438 100644 --- a/challenge-039/adam-russell/perl/ch-1.pl +++ b/challenge-039/adam-russell/perl/ch-1.pl @@ -6,13 +6,14 @@ use warnings; # is guest book which tracks all guest in/out time. # Write a script to find out how long in minutes the light were ON. ## +use DateTime; use DateTime::Duration; my $lights_on = new DateTime::Duration( hours => 0, minutes => 0, seconds => 0 ); -my $start_time; +my $start_time; while(my $line = <DATA>){ chomp($line); @@ -22,31 +23,29 @@ while(my $line = <DATA>){ $in_minute =~ tr/ OUT//d; $out_hour =~ tr/ //d; if(!$start_time){ - $start_time = new DateTime::Duration( - hours => $in_hour, - minutes => $in_minute, - seconds => 0 - ); + $start_time = DateTime -> now(); + $start_time->set_hour($in_hour); + $start_time->set_minute($in_minute); + $start_time->set_second(0); } - my $in = new DateTime::Duration( - hours => $in_hour, - minutes => $in_minute, - seconds => 0 - ); - my $out = new DateTime::Duration( - hours => $out_hour, - minutes => $out_minute, - seconds => 0 - ); - if(DateTime::Duration->compare($start_time, $in) <= 0){ - $lights_on -> add_duration($out->subtract_duration($in)); + + my $in = DateTime -> now(); + $in->set_hour($in_hour); + $in->set_minute($in_minute); + $in->set_second(0); + my $out = DateTime -> now(); + $out->set_hour($out_hour); + $out->set_minute($out_minute); + $out->set_second(0); + if(DateTime->compare($start_time, $in) <= 0){ + $lights_on -> add_duration($out->subtract_datetime($in)); } - if(DateTime::Duration->compare($start_time, $in) > 0 && - DateTime::Duration->compare($start_time, $out) < 0 + if(DateTime->compare($start_time, $in) > 0 && + DateTime->compare($start_time, $out) < 0 ){ - $lights_on -> add_duration($out->subtract_duration($start_time)); + $lights_on -> add_duration($out->subtract_datetime($start_time)); } - $start_time = $out; + $start_time = $out if (DateTime->compare($start_time, $out) < 0); } print $lights_on->hours() . " hours "; print $lights_on->minutes() . " minutes\n"; |
