aboutsummaryrefslogtreecommitdiff
path: root/challenge-030/trenton-langer/perl5/ch-1.pl
blob: e0d464001b48367b11ef11818197a12c03776520 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/mu/bin/perl -w
use strict;
no warnings;



##################################################################################################################################################################
##################################################################################################################################################################
## Authorship
##################################################################################################################################################################
##################################################################################################################################################################
### Project
############# Perl Weekly Challenge - 030
### Name
############# ch-1.pl
### Author
############# Trenton Langer
### Creation Date
############# 20191014
### Description
############# Sunday Christmas List



#####################################################################################################################
#####################################################################################################################
## PWC - 030 - Task 1
#####################################################################################################################
#####################################################################################################################
#Write a script to list dates for Sunday Christmas between 2019 and 2100. For example, 25 Dec 2022 is Sunday.    



#####################################################################################################################
#####################################################################################################################
## Solution
#####################################################################################################################
#####################################################################################################################
my $jan_01_2019 = "1546300800";
my $jan_01_2100 = "4102444800";
my $day_seconds = 24*60*60;
my $year_seconds = 365*$day_seconds;

my $time = $jan_01_2019;
while ($time < $jan_01_2100)
{
    my ($sec,$min,$hour,$dayOfMonth,$month,$year,$dayOfWeek,$dayOfYear,$isDayLightSavings) = localtime($time);
    $year += 1900;
    my $last_year = $year - 1;
    print "25 Dec $last_year is a Sunday\n" if($dayOfWeek == 0);

    $time += $year_seconds;
    my $extra_day = daysInAMonth(2, $year) - 28; 
    $time += $day_seconds if($extra_day);
}



#####################################################################################################################
#####################################################################################################################
## Subs
#####################################################################################################################
#####################################################################################################################
sub daysInAMonth
{
    my $month = $_[0];
    my $year  = $_[1];
    
    # Input validation
    my %name_hash = ( "jan" => 1, "feb" => 2, "mar" => 3, "apr" => 4, "may" => 5, "jun" => 6, "jul" => 7, "aug" => 8, "sep" => 9, "oct" => 10, "nov" => 11, "dec" => 12);
    if   ($month =~ /^[a-z]+$/i) { $month = $name_hash{lc(substr($month,0,3))}; }   # Convert name to number
    elsif($month =~ /^[0-9]+$/)  { $month = $month*1; }                             # Cast int from str to number
    else                         { die "Month ($month) not understandable\n\n"; }    
    die "Year ($year) not understandable\n\n" if(!($year =~ /^[0-9]+$/)); # Blocks decimal points, negative signs, etc...
    
    # Calc
    my %days_hash = ( 1 => 31, 2 => 28, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31);
    if   ( $month != 2 )    { }                     # Not Feb, don't care about leap years
    elsif( $year % 4 > 0 )  { }                     # Regular year, do nothing
    elsif( $year % 100 > 0 ){ $days_hash{2} = 29; } # Leap Year
    elsif( $year % 400 > 0 ){ }                     # Regular year, do nothing
    else                    { $days_hash{2} = 29; } # Leap Year
    
    # Return
    return $days_hash{$month};
}