diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2021-02-15 17:24:17 +0000 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2021-02-15 17:24:17 +0000 |
| commit | 2f00f00743a387be8803c73db8bee16d80ca49ed (patch) | |
| tree | 88fd91fb63875c5147f868650f6de7c806b21d39 | |
| parent | 01d1038e9a0b5110729960178848ee3dc840f98f (diff) | |
| download | perlweeklychallenge-club-2f00f00743a387be8803c73db8bee16d80ca49ed.tar.gz perlweeklychallenge-club-2f00f00743a387be8803c73db8bee16d80ca49ed.tar.bz2 perlweeklychallenge-club-2f00f00743a387be8803c73db8bee16d80ca49ed.zip | |
add solution week 100 task 1
| -rwxr-xr-x | challenge-100/steven-wilson/perl/ch-1.pl | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-100/steven-wilson/perl/ch-1.pl b/challenge-100/steven-wilson/perl/ch-1.pl new file mode 100755 index 0000000000..fd8552183c --- /dev/null +++ b/challenge-100/steven-wilson/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +# You are given a time (12 hour / 24 hour). +# Write a script to convert the given time from 12 hour format to 24 hour +# format and vice versa. +# Ideally we expect a one-liner. + +use strict; +use warnings; +use Test::More; + +my @input = ( "05:15 pm", "19:15", "12:01", "12:00 am", "00:00", "12:01 pm", "05:15 am" ); +my @output = ( "17:15", "07:15 pm", "12:01 pm", "00:00", "12:00 am", "12:01", "05:15" ); +ok( convert_t_fmt( $input[0] ) eq $output[0], "test 1" ); +ok( convert_t_fmt( $input[1] ) eq $output[1], "test 2" ); +ok( convert_t_fmt( $input[2] ) eq $output[2], "test 3" ); +ok( convert_t_fmt( $input[3] ) eq $output[3], "test 4" ); +ok( convert_t_fmt( $input[4] ) eq $output[4], "test 5" ); +ok( convert_t_fmt( $input[5] ) eq $output[5], "test 6" ); +ok( convert_t_fmt( $input[6] ) eq $output[6], "test 7" ); +done_testing(); + +sub convert_t_fmt { + my $input = shift; + my $output; + $input =~ /(\d{1,2}):(\d{1,2})/; + my $hour = $1; + my $mins = $2; + if ( $input =~ /pm/ ) { + if ( $hour != 12 ) { + $hour += 12; + } + } + elsif ( $hour == 12 && ( $input =~ /am/ ) ) { + $hour = 0; + } + elsif ( $hour > 12 ) { + $hour -= 12; + $mins .= " pm"; + } + elsif ( $hour == 12 ) { + $mins .= " pm"; + } + elsif ( $hour == 0 ) { + $hour = 12; + $mins .= " am"; + } + elsif ( $hour < 12 && !( $input =~ /am/ ) ) { + $mins .= " am"; + } + $output = sprintf "%02s:%02s", $hour, $mins; +} |
