G
George Mpouras
#I want the calendar date of any number of years/months before.
# Currently I use the following, but maybe there is a better way.
use strict;
use warnings;
use Time::Local 'timelocal_nocheck';
my $Months = 21;
my $Years = 4;
my $startfrom = $^T;
$Years += int $Months / 12;
$Months = $Months % 12;
my @STARTFROM = localtime $startfrom;
my $d = $STARTFROM[3];
my $m = 0;
my $y = 0;
if ( $Months <= $STARTFROM[4] )
{
$y = $STARTFROM[5] - $Years;
$m = $STARTFROM[4] - $Months
}
else
{
$m = 12 - $Months + $STARTFROM[4];
$y = $STARTFROM[5] - $Years - 1
}
my $max_month_days = How_many_days_have_a_month(1+$m, 1900+$y);
$d = $max_month_days if $d > $max_month_days;
my $backtime = Time::Local::timelocal_nocheck(@STARTFROM[0..2],
$d, $m, $y);
print "epoch : $backtime\n";
print "human : ", scalar(localtime $backtime) ,"\n";
# How_many_days_have_a_month(MONTH, YEAR)
# MONTH 1 .. 12
# YEAR e.g. 1970
#
sub How_many_days_have_a_month
{
my $month = $_[0];
my $year = $_[1];
my $days;
my $leap_year;
if ($year % 4)
{
$leap_year=0
}
elsif ($year % 100)
{
$leap_year=1
}
elsif ($year % 400)
{
$leap_year=0
}
else
{
$leap_year=1
}
if ($month == 1) {$days = 31}
elsif ($month == 2) {$days = $leap_year ? 29 : 28}
elsif ($month == 3) {$days = 31}
elsif ($month == 4) {$days = 30}
elsif ($month == 5) {$days = 31}
elsif ($month == 6) {$days = 30}
elsif ($month == 7) {$days = 31}
elsif ($month == 8) {$days = 31}
elsif ($month == 9) {$days = 30}
elsif ($month == 10) {$days = 31}
elsif ($month == 11) {$days = 30}
elsif ($month == 12) {$days = 31}
$days
}
# Currently I use the following, but maybe there is a better way.
use strict;
use warnings;
use Time::Local 'timelocal_nocheck';
my $Months = 21;
my $Years = 4;
my $startfrom = $^T;
$Years += int $Months / 12;
$Months = $Months % 12;
my @STARTFROM = localtime $startfrom;
my $d = $STARTFROM[3];
my $m = 0;
my $y = 0;
if ( $Months <= $STARTFROM[4] )
{
$y = $STARTFROM[5] - $Years;
$m = $STARTFROM[4] - $Months
}
else
{
$m = 12 - $Months + $STARTFROM[4];
$y = $STARTFROM[5] - $Years - 1
}
my $max_month_days = How_many_days_have_a_month(1+$m, 1900+$y);
$d = $max_month_days if $d > $max_month_days;
my $backtime = Time::Local::timelocal_nocheck(@STARTFROM[0..2],
$d, $m, $y);
print "epoch : $backtime\n";
print "human : ", scalar(localtime $backtime) ,"\n";
# How_many_days_have_a_month(MONTH, YEAR)
# MONTH 1 .. 12
# YEAR e.g. 1970
#
sub How_many_days_have_a_month
{
my $month = $_[0];
my $year = $_[1];
my $days;
my $leap_year;
if ($year % 4)
{
$leap_year=0
}
elsif ($year % 100)
{
$leap_year=1
}
elsif ($year % 400)
{
$leap_year=0
}
else
{
$leap_year=1
}
if ($month == 1) {$days = 31}
elsif ($month == 2) {$days = $leap_year ? 29 : 28}
elsif ($month == 3) {$days = 31}
elsif ($month == 4) {$days = 30}
elsif ($month == 5) {$days = 31}
elsif ($month == 6) {$days = 30}
elsif ($month == 7) {$days = 31}
elsif ($month == 8) {$days = 31}
elsif ($month == 9) {$days = 30}
elsif ($month == 10) {$days = 31}
elsif ($month == 11) {$days = 30}
elsif ($month == 12) {$days = 31}
$days
}