#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Date::Format;
use strict;

print header;

my($sec ,$min,$hr,$mday,$month,$year,$dayofweek,$dayofyear,$isdst) = 
   localtime(time);

# Days of the year count from 0 (Jan 1) to 364 (Dec 31)
# or 0 to 365 on leap years

my $days_in_year = 364;
if (&is_leap($year)) {
   print "This is a leap year! ";
   $days_in_year = 365;
}

# Christmas is Dec 25, which is 6 days before the end of the year
my $xmas = $days_in_year - 6;

my $now = time2str("%B %e",time);

if ($dayofyear > $xmas) { 
   my $days_ago = $dayofyear - $xmas;
   print "Today is $now. Christmas was $days_ago days ago. Happy New Year!\n";
} elsif ($dayofyear == $xmas) {
   print "Today is $now. Merry Christmas!\n";
} else {
   my $days_til_xmas = $xmas - $dayofyear;
   my $then = time2str("%B %e", time + (86400 * $days_til_xmas));
   print "Today is $now. Christmas is $then. Only $days_til_xmas days left!\n";
}

sub is_leap {
    my ($yr) = @_;
    return 1 unless $yr % 400;
    return unless $yr % 100;
    return 1 unless $yr % 4;
    return;
}