#!/usr/bin/perl -wT use strict; use Date::Calc qw( Add_Delta_YM Add_Delta_YMD Today Week_of_Year Monday_of_Week ); my ($Y,$M,$D) = ('','',''); # Original year,month,day my ($y,$m,$d) = ('','',''); # Resulting year,month,day # First and Last in year ($y,$m,$d) = first_in_year(); print "First and last in this year:\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_year(); print " - " . format_date($y, $m, $d) . "\n"; ($y,$m,$d) = first_in_year(1999); print "First and last in year 1999:\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_year(1999); print " - " . format_date($y, $m, $d) . "\n"; ($y,$m,$d) = first_in_year_delta(2); print "First and last in the year 2 years ago: " . format_date($y, $m, $d); ($y,$m,$d) = last_in_year_delta(2); print " - " . format_date($y, $m, $d) . "\n"; print "\n"; # Last years (from today or specific date) ($y,$m,$d) = last_years_from_date(0); print "Last year from today:\t\t\t" . format_date($y, $m, $d) . " - " . format_date(Today()) . "\n"; ($Y, $M, $D) = (2006, 1, 5); ($y,$m,$d) = last_years_from_date(5, $Y, $M, $D); print "Last five years from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n"; ($Y, $M, $D) = (2005, 12, 31); ($y,$m,$d) = last_years_from_date(10, $Y, $M, $D); print "Last ten years from ". format_date_intl($Y, $M, $D) . ":\t\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n"; print "\n"; # First and Last in month ($y,$m,$d) = first_in_month(); print "First and last in this month:\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month(); print " - " . format_date($y, $m, $d) . "\n"; ($y,$m,$d) = first_in_month(1999,2); print "First and last in month 1999-02:\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month(1999,2); print " - " . format_date($y, $m, $d) . "\n"; # First and Last in last month ($y,$m,$d) = first_in_last_month(); print "First and last in last month:\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_last_month(); print " - " . format_date($y, $m, $d) . "\n"; print "\n"; # Last three calendar months (including current month) ($y,$m,$d) = first_in_month_delta(2); print "Last three calendar months (including this month):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month_delta(0); print " - " . format_date($y, $m, $d) . "\n"; # Last six calendar months (including current month) ($y,$m,$d) = first_in_month_delta(5); print "Last six calendar months (including this month):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month_delta(0); print " - " . format_date($y, $m, $d) . "\n"; # Last twelve calendar months (including current month) ($y,$m,$d) = first_in_month_delta(11); print "Last twelve calendar months (including this month):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month_delta(0); print " - " . format_date($y, $m, $d) . "\n"; # Last three calendar months (excluding current month) ($y,$m,$d) = first_in_month_delta(3); print "Last three calendar months (excluding this month):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month_delta(1); print " - " . format_date($y, $m, $d) . "\n"; # Last six calendar months (excluding current month) ($y,$m,$d) = first_in_month_delta(6); print "Last six calendar months (excluding this month):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month_delta(1); print " - " . format_date($y, $m, $d) . "\n"; # Last twelve calendar months (excluding current month) ($y,$m,$d) = first_in_month_delta(12); print "Last twelve calendar months (excluding this month):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_month_delta(1); print " - " . format_date($y, $m, $d) . "\n"; print "\n"; # Last months (from today or specific date) ($y,$m,$d) = last_months_from_date(3); print "Last three months from today:\t\t" . format_date($y, $m, $d) . " - " . format_date(Today()) . "\n"; ($Y, $M, $D) = (2006, 1, 5); ($y,$m,$d) = last_months_from_date(6, $Y, $M, $D); print "Last six months from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n"; ($Y, $M, $D) = (2005, 12, 31); ($y,$m,$d) = last_months_from_date(12, $Y, $M, $D); print "Last twelve months from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n"; print "\n"; # First and Last in week ($y,$m,$d) = first_in_week(); print "First and last in this week:\t\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week(); print " - " . format_date($y, $m, $d) . "\n"; ($Y,$M,$D) = (2005,3,1); ($y,$m,$d) = first_in_week($Y, $M, $D); print "First and last in week for date " . format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week($Y, $M, $D); print " - " . format_date($y, $m, $d) . "\n"; # First and Last in last week ($y,$m,$d) = first_in_last_week(); print "First and last in last week:\t\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_last_week(); print " - " . format_date($y, $m, $d) . "\n"; print "\n"; # Last three calendar weeks (including current week) ($y,$m,$d) = first_in_week_delta(2); print "Last three calendar weeks (including this week):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week_delta(0); print " - " . format_date($y, $m, $d) . "\n"; # Last six calendar weeks (including current week) ($y,$m,$d) = first_in_week_delta(5); print "Last six calendar weeks (including this week):\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week_delta(0); print " - " . format_date($y, $m, $d) . "\n"; # Last twelve calendar weeks (including current week) ($y,$m,$d) = first_in_week_delta(11); print "Last twelve calendar weeks (including this week):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week_delta(0); print " - " . format_date($y, $m, $d) . "\n"; # Last three calendar weeks (excluding current week) ($y,$m,$d) = first_in_week_delta(3); print "Last three calendar weeks (excluding this week):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week_delta(1); print " - " . format_date($y, $m, $d) . "\n"; # Last six calendar weeks (excluding current week) ($y,$m,$d) = first_in_week_delta(6); print "Last six calendar weeks (excluding this week):\t\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week_delta(1); print " - " . format_date($y, $m, $d) . "\n"; # Last twelve calendar weeks (excluding current week) ($y,$m,$d) = first_in_week_delta(12); print "Last twelve calendar weeks (excluding this week):\t" . format_date($y, $m, $d); ($y,$m,$d) = last_in_week_delta(1); print " - " . format_date($y, $m, $d) . "\n"; print "\n"; # Last weeks (from today or specific date) ($y,$m,$d) = last_weeks_from_date(3); print "Last three weeks from today:\t\t" . format_date($y, $m, $d) . " - " . format_date(Today()) . "\n"; ($Y, $M, $D) = (2006, 1, 5); ($y,$m,$d) = last_weeks_from_date(6, $Y, $M, $D); print "Last six weeks from ". format_date_intl($Y, $M, $D) . ":\t\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n"; ($Y, $M, $D) = (2005, 12, 31); ($y,$m,$d) = last_weeks_from_date(12, $Y, $M, $D); print "Last twelve weeks from ". format_date_intl($Y, $M, $D) . ":\t" . format_date($y, $m, $d) . " - " . format_date($Y, $M, $D) . "\n"; print "\n"; # Formatted output print "Formatted output: " . format_date($y, $m, $d) . "\n"; print "Formatted output: - International: " . format_date_intl($y, $m, $d) . "\n"; print "Formatted output: - Spanish: " . format_date_es($y, $m, $d) . "\n"; # ---------------------------------------- # subs # ---------------------------------------- # First in year # Usage: ($y) = first_in_year([$y]); sub first_in_year { my ($y,$m,$d) = (scalar(@_) == 1) ? (shift, 1, 1) : Today(); $m = 1; $d = 1; return ($y, $m, $d); } # Last in year # Usage: ($y) = last_in_year([$y]); sub last_in_year { my ($y,$m,$d) = (scalar(@_) == 1) ? (shift, 1, 1) : Today(); $m = 12; $d = 31; return ($y, $m, $d); } # First in year - $years years in the past # Usage: ($y) = first_in_year_delta($years); sub first_in_year_delta { my $years = shift; my ($y,$m,$d) = Today(); ($y,$m,$d) = Add_Delta_YM($y, 1, 1, -$years, 0); return ($y, $m, $d); } # Last in year - $years years in the past # Usage: ($y) = last_in_year_delta($years); sub last_in_year_delta { my $years = shift; my ($y,$m,$d) = Today(); ($y,$m,$d) = Add_Delta_YM($y, 12, 31, -$years, 0); return ($y, $m, $d); } # Last $years years in the past, counting from a specific date, or today # Usage: ($y, $m, $d) = last_years_from_date($years); sub last_years_from_date { my $years = shift || 1; my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); ($y,$m,$d) = Add_Delta_YM($y, $m, $d, -$years, 0); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 1); return ($y, $m, $d); } # First in month # Usage: ($y, $m, $d) = first_in_month([$y, $m]); sub first_in_month { my ($y,$m,$d) = (scalar(@_) == 2) ? (shift, shift, 1) : Today(); $d = 1; return ($y, $m, $d); } # Last in month # Usage: ($y, $m, $d) = last_in_month([$y, $m]); sub last_in_month { my ($y,$m,$d) = (scalar(@_) == 2) ? (shift, shift, 1) : Today(); ($y,$m,$d) = Add_Delta_YM($y, $m, 1, 0, 1); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -1); return ($y, $m, $d); } # First in last month # Usage: ($y, $m, $d) = first_in_last_month(); sub first_in_last_month { my ($y,$m,$d) = Today(); ($y,$m,$d) = Add_Delta_YM($y, $m, 1, 0, -1); return ($y, $m, $d); } # Last in last month # Usage: ($y, $m, $d) = last_in_last_month(); sub last_in_last_month { my ($y,$m,$d) = first_in_last_month(); return last_in_month($y, $m); } # First in $months months in the past # Usage: ($y, $m, $d) = first_in_month_delta($months); sub first_in_month_delta { my $months = shift; my ($y,$m,$d) = Today(); ($y,$m,$d) = Add_Delta_YM($y, $m, 1, 0, -$months); return ($y, $m, $d); } # Last in $months months in the past # Usage: ($y, $m, $d) = last_in_month_delta($months); sub last_in_month_delta { my $months = shift; my ($y,$m,$d) = first_in_month_delta($months); return last_in_month($y, $m); } # Last $months months in the past, counting from a specific date, or today # Usage: ($y, $m, $d) = last_months_from_date($months); sub last_months_from_date { my $months = shift || 1; my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); ($y,$m,$d) = Add_Delta_YM($y, $m, $d, 0, -$months); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 1); return ($y, $m, $d); } # First in week # Usage: ($y, $m, $d) = first_in_week([$y, $m, $d]); sub first_in_week { my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); ($y, $m, $d) = Monday_of_Week(Week_of_Year($y, $m, $d)); return ($y, $m, $d); } # Last in week # Usage: ($y, $m, $d) = last_in_week([$y, $m, $d]); sub last_in_week { my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); ($y, $m, $d) = Monday_of_Week(Week_of_Year($y, $m, $d)); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 6); return ($y, $m, $d); } # First in last week # Usage: ($y, $m, $d) = first_in_last_week(); sub first_in_last_week { my ($y,$m,$d) = first_in_week(); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7); return ($y, $m, $d); } # Last in last week # Usage: ($y, $m, $d) = last_in_last_week(); sub last_in_last_week { my ($y,$m,$d) = first_in_week(); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -1); return ($y, $m, $d); } # First in $weeks weeks in the past # Usage: ($y, $m, $d) = first_in_week_delta($weeks); sub first_in_week_delta { my $weeks = shift; my ($y,$m,$d) = Today(); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7 * $weeks); ($y,$m,$d) = first_in_week($y,$m,$d); return ($y, $m, $d); } # Last in $weeks weeks in the past # Usage: ($y, $m, $d) = last_in_week_delta($weeks); sub last_in_week_delta { my $weeks = shift; my ($y,$m,$d) = Today(); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7 * $weeks); ($y,$m,$d) = last_in_week($y,$m,$d); return ($y, $m, $d); } # Last $weeks weeks in the past, counting from a specific date, or today # Usage: ($y, $m, $d) = last_weeks_from_date($weeks); sub last_weeks_from_date { my $weeks = shift || 1; my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, -7 * $weeks); ($y,$m,$d) = Add_Delta_YMD($y,$m,$d, 0, 0, 1); return ($y, $m, $d); } # Formatted date (YYYYMMDD - possible zero padding) # Usage: $string = format_date([$y, $m, $d]); sub format_date { my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); return sprintf("%04d%02d%02d", $y, $m, $d); } # Formatted date - Intl. (YYYY-MM-DD - possible zero padding) # Usage: $string = format_date([$y, $m, $d]); sub format_date_intl { my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); return sprintf("%04d-%02d-%02d", $y, $m, $d); } # Formatted date - ES (DD/MM/YYYY - possible zero padding) # Usage: $string = format_date([$y, $m, $d]); sub format_date_es { my ($y,$m,$d) = (scalar(@_) == 3) ? (shift, shift, shift) : Today(); return sprintf("%d/%d/%04d", $d, $m, $y); }