diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-15 19:40:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-15 19:40:54 +0000 |
| commit | 230fa7f75f1b0255930f82702f3d4c74fff15040 (patch) | |
| tree | 7ddc4ca7d02fb1d25111f013b71c7b9ec6870ba3 | |
| parent | 2d566a98dfb43bf9e6ac2b65bb9a6a301295aa24 (diff) | |
| parent | a558cbbd44ae24357415d0935263a233b22450a2 (diff) | |
| download | perlweeklychallenge-club-230fa7f75f1b0255930f82702f3d4c74fff15040.tar.gz perlweeklychallenge-club-230fa7f75f1b0255930f82702f3d4c74fff15040.tar.bz2 perlweeklychallenge-club-230fa7f75f1b0255930f82702f3d4c74fff15040.zip | |
Merge pull request #3534 from vinodk89/master
Solution for challenge 100 - Task#1
| -rw-r--r-- | challenge-100/vinod-k/perl/ch-1.pl | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-100/vinod-k/perl/ch-1.pl b/challenge-100/vinod-k/perl/ch-1.pl new file mode 100644 index 0000000000..ccc963b686 --- /dev/null +++ b/challenge-100/vinod-k/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +use strict; use warnings; + +my $time = $ARGV[0]; + +die "Please enter the time..\n" unless($time); + +if($time =~ /(am|pm)/i){ + print "Converting time from 12hrs to 24hrs format:\n"; + if ($time =~ /(\d{1,2})\:(\d{1,2})(am|pm)/){ + my ($h, $m, $t) = ($1, $2, $3); + $h -= 12 if ($t eq 'am' && $h == 12); + $h += 12 if ($t eq 'pm' && $h != 12); + $h = sprintf "%02d", $h; + print "time:$h:$m\n"; + } +} else { + print "Converting time from 24hrs to 12hrs format:\n"; + my ($h, $m) = split(':', $time); + + if (($h < 0) || ($h > 23)) { + print ("$h is not a valid hour"); + exit; + } + + print "12:$m am\n" if $h == 0; + print "$h:$m am\n" if $h < 12; + print "$h:$m pm\n" if $h == 12; + my $hh = $h-12; + print "$hh:$m PM\n" if $h > 12; +} |
