aboutsummaryrefslogtreecommitdiff
path: root/challenge-039
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2019-12-22 21:53:33 -0500
committerAdam Russell <ac.russell@live.com>2019-12-22 21:53:33 -0500
commit49fbaae14e9b50add7d98f122df8bda5acb53e1d (patch)
treeed7b73fd338d952c062d204e6e436165f55df222 /challenge-039
parent90749682d34a31134faeaff4075467a533bc45ff (diff)
downloadperlweeklychallenge-club-49fbaae14e9b50add7d98f122df8bda5acb53e1d.tar.gz
perlweeklychallenge-club-49fbaae14e9b50add7d98f122df8bda5acb53e1d.tar.bz2
perlweeklychallenge-club-49fbaae14e9b50add7d98f122df8bda5acb53e1d.zip
updated solution for challenge 039
Diffstat (limited to 'challenge-039')
-rw-r--r--challenge-039/adam-russell/perl/ch-1.pl41
1 files changed, 20 insertions, 21 deletions
diff --git a/challenge-039/adam-russell/perl/ch-1.pl b/challenge-039/adam-russell/perl/ch-1.pl
index 861fccd157..645fbd42de 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,29 +23,27 @@ 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;
}