diff options
| -rw-r--r-- | challenge-100/lance-wicks/perl/ch-1.pl | 11 | ||||
| -rw-r--r-- | challenge-100/lance-wicks/perl/lib/Fun.pm | 35 | ||||
| -rw-r--r-- | challenge-100/lance-wicks/perl/t/00-fun.t | 10 |
3 files changed, 56 insertions, 0 deletions
diff --git a/challenge-100/lance-wicks/perl/ch-1.pl b/challenge-100/lance-wicks/perl/ch-1.pl new file mode 100644 index 0000000000..78310d45ab --- /dev/null +++ b/challenge-100/lance-wicks/perl/ch-1.pl @@ -0,0 +1,11 @@ +use strict; +use warnings; + +use lib './lib'; +use Fun; + +my $fun = Fun->new; + +print $fun->convert($ARGV[0]); +print "\n"; + diff --git a/challenge-100/lance-wicks/perl/lib/Fun.pm b/challenge-100/lance-wicks/perl/lib/Fun.pm new file mode 100644 index 0000000000..d1e1088614 --- /dev/null +++ b/challenge-100/lance-wicks/perl/lib/Fun.pm @@ -0,0 +1,35 @@ +package Fun; + +use Moo; + +sub convert { + my ( $self, $time ) = @_; + + if ( $time =~ m/([ap]m)/ ) { + my $am_pm = $1; + + $time =~ m/^(\d+):(\d+)/; + my $hours = $1; + my $minutes = $2; + + if ( $am_pm eq 'pm' ) { + $hours += 12; + } + return "$hours:$minutes"; + } + else { + $time =~ m/^(\d+):(\d+)/; + my $hours = $1; + my $minutes = $2; + + if ( $hours > 12 ) { + $hours -= 12; + return sprintf( "%02d", $hours ) . ":$minutes" . 'pm'; + } + else { + return sprintf( "%02d", $hours ) . ":$minutes" . 'am'; + } + } +} + +1; diff --git a/challenge-100/lance-wicks/perl/t/00-fun.t b/challenge-100/lance-wicks/perl/t/00-fun.t new file mode 100644 index 0000000000..8cba91420a --- /dev/null +++ b/challenge-100/lance-wicks/perl/t/00-fun.t @@ -0,0 +1,10 @@ +use Test2::V0 -target => "Fun"; + +is $CLASS->convert("05:15 pm"), '17:15', 'Example 1-1'; +is $CLASS->convert("05:15pm"), '17:15', 'Example 1-2'; + +is $CLASS->convert("19:15"), '07:15pm', 'Example 2-1'; + +is $CLASS->convert("09:15"), '09:15am', '24hr AM'; + +done_testing; |
