aboutsummaryrefslogtreecommitdiff
path: root/challenge-100/mohammad-anwar/perl/ch-1.pl
blob: f9a3b88de9873cb5d2e89408f16e9abdc319259c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

foreach (['20:20', '08:20pm'],
         ['22:05', '10:05pm'],
         ['05:00', '05:00am'],
         ['12:00', '12:00pm'],
         ['00:01', '12:01am'],
         ['11:55', '11:55am']) {
    is(t($_->[0]), $_->[1]);
    is(t($_->[1]), $_->[0]);
}

done_testing;

sub t {
    return
    sprintf("%02d:%02d%s",
    ($_[0] =~ /(\d+)?\:(\d+)\s?([ap]m)$/i)
    ?((uc($3) eq 'PM')?(($1==12)?($1,$2,''):($1+12,$2,'')):(($1==12)?($1-12,$2,''):($1,$2,'')))
    :(($_[0] =~ /(\d+)?\:(\d+)/) and (($1 eq '00')?('12',$2,'am'):(($1==12)?('12',$2,'pm'):(($1>12)?($1-12,$2,'pm'):($1,$2,'am')))))
    );
}