diff options
| author | Ryan Thompson <i@ry.ca> | 2019-12-09 16:29:25 -0600 |
|---|---|---|
| committer | Ryan Thompson <i@ry.ca> | 2019-12-09 16:29:25 -0600 |
| commit | 6d5b7ea76794ee7331d22b2e901ea724d8304409 (patch) | |
| tree | ec6b4947faca9542da7400fe751846bf1f179772 /challenge-038/ryan-thompson | |
| parent | 4da3102815ca5d833b542c148fb961b0ae50c20a (diff) | |
| download | perlweeklychallenge-club-6d5b7ea76794ee7331d22b2e901ea724d8304409.tar.gz perlweeklychallenge-club-6d5b7ea76794ee7331d22b2e901ea724d8304409.tar.bz2 perlweeklychallenge-club-6d5b7ea76794ee7331d22b2e901ea724d8304409.zip | |
Challenge #1: Cyymmdd -> yyyy-mm-dd dates in Perl6
Diffstat (limited to 'challenge-038/ryan-thompson')
| -rw-r--r-- | challenge-038/ryan-thompson/perl6/ch-1.p6 | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-038/ryan-thompson/perl6/ch-1.p6 b/challenge-038/ryan-thompson/perl6/ch-1.p6 new file mode 100644 index 0000000000..a773d8297a --- /dev/null +++ b/challenge-038/ryan-thompson/perl6/ch-1.p6 @@ -0,0 +1,40 @@ +#!/usr/bin/env perl6 + +# ch-1.p6 - Weird dates. Convert Cyymmdd -> yyyy-mm-dd +# +# Ryan Thompson <rjt@cpan.org> + +use v6; + +sub MAIN( Int $date ) { + $date ~~ /^ + $<cent> = [ <[12]> ] # Century (1:1900,2:2000) + $<yy> = [ \d \d ] # Year (2-digit) + $<mm> = [ 0<[1..9]> | 1<[012]> ] # Month (01..12) + $<dd> = [ 0<[1..9]> | <[12]>\d | 3<[01]> ] # Day (01..31) + $/ or die "Usage: $*PROGRAM Cyymmdd"; + + my Int $yyyy = ($<cent> + 18 ~ $<yy>).Int; + + die "$yyyy-$<mm> does not have $<dd> days" + if days-in($yyyy, $<mm>.Int) < $<dd>; + + say "$yyyy-$<mm>-$<dd>"; + +} + +# Return the number of days in the given month (with year specified so +# we can check if it is a leap year) +sub days-in( Int $y, Int $m ) { + my @days = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); + @days[2]++ if leap-year($y); + @days[$m]; +} + +# Return true if year is a leap year +sub leap-year( Int $y ) { + return 1 unless $y % 400; + return 0 unless $y % 100; + return 1 unless $y % 4; + return 0; +} |
