aboutsummaryrefslogtreecommitdiff
path: root/challenge-038/javier-luque/perl5/ch-1.pl
blob: 31c44e3dc1b380bcac04a4b7894d65b11d3dd155 (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
28
29
30
31
32
33
34
35
36
#!/usr/bin/perl
# Test: ./ch-1.pl 2230120
use strict;
use warnings;
use feature qw /say/;
use Date::Manip;

say parse_date($ARGV[0]);

sub parse_date {
    my $date = shift;

    # Regex to test date format
    return "Invalid date format"
        unless $date && $date =~ /
            ^                     # start string
            ([12])                # 1 or 2
            (\d\d)                # year 00-99
            (0[1-9]|1[0-2])       # month 1-12
            (0[1-9]|[12]\d|3[01]) # day 1-31
            $                     # end string
        /x;

    # The date string
    my $date_string =
        ( ($1 == 1) ? '20' . $2  : '19' . $2 )
        . '-' . $3 . '-' . $4;

    # Make sure the date is valid
    # even if it passed the format check
    # using Date::Manip
    return "Invalid date"
        unless (ParseDate($date_string));

    return $date_string;
}