G
George Mpouras
You may find it useful. Returns what time was before as much days, seconds,
etc.
You can specify also a past date as start count time.
For example what time was before 160 days?
use strict;
use warnings;
use Time::Local;
my $T = TimeUnits_to_epochtime('FROM_PAST' => time , 'days' => 160 );
print scalar localtime $T;
# Returns the epoch seconds, from the time you specify as FROM_PAST
# FROM_PAST is seconds from epoch
# You can use (more than once) the time units as keys:
# Seconds, Minutes, Hours, Weeks, Days, Months, Years
#
# Some usage examples:
#
# print scalar localtime TimeUnits_to_epochtime('FROM_PAST' => time ,
'sec' => 3600, 'year' => 3 );
# print scalar localtime TimeUnits_to_epochtime('FROM_PAST' => 23746273
, 'minutes' => 180, 'min' => 360 , 'sec' => 120 );
# print scalar localtime TimeUnits_to_epochtime('FROM_PAST' => 472363433
, 'months' => 38, 'Weeks' => 3 );
#
sub TimeUnits_to_epochtime
{
my $startfrom = -1;
my $NOW = time;
my @NOW = localtime $NOW;
my $Secs = 0;
my $Months = 0;
my $Years = 0;
for (my ($i,$j)=(0,1); $i<=$#_; $i+=2, $j+=2) {
die "Argument ".(1+$j)." \"$_[$j]\" is not an integer\n" unless
$_[$j]=~/^\d+$/;
if ($_[$i]=~/(?i)^sec/){ $Secs += $_[$j] } # Seconds
elsif ($_[$i]=~/(?i)^min/){ $Secs += $_[$j]*60 } # Minutes
elsif ($_[$i]=~/(?i)^hou/){ $Secs += $_[$j]*3600 } # Hours
elsif ($_[$i]=~/(?i)^day/){ $Secs += $_[$j]*86400 } # Days
elsif ($_[$i]=~/(?i)^wee/){ $Secs += $_[$j]*604800 } # Weeks
elsif ($_[$i]=~/(?i)^mon/){ $Months += $_[$j] } # Months
elsif ($_[$i]=~/(?i)^yea/){ $Years += $_[$j] } # Years
elsif ($_[$i]=~/(?i)^fro/){ $startfrom= $_[$j] } # SECONDS FROM EPOCH
TIME that I want to look back
else { die "Argument unit \"$_[$i]\" is not Years, Months, Weeks, Days,
Hours, Minutes or Seconds\n" } }
die "Please define a valid seconds from epoch, for the key \"NOW\"\n" if
$startfrom == -1;
$Years += int $Months / 12;
$Months = $Months % 12;
my $m = $NOW[4] > $Months ? $NOW[4] - $Months : 12 - $Months + $NOW[4];
my $n = Time::Local::timelocal_nocheck($NOW[0],$NOW[1],$NOW[2],$NOW[3],
$m, ($NOW[5] - $Years));
$startfrom - ( $NOW - $n + $Secs )
}
etc.
You can specify also a past date as start count time.
For example what time was before 160 days?
use strict;
use warnings;
use Time::Local;
my $T = TimeUnits_to_epochtime('FROM_PAST' => time , 'days' => 160 );
print scalar localtime $T;
# Returns the epoch seconds, from the time you specify as FROM_PAST
# FROM_PAST is seconds from epoch
# You can use (more than once) the time units as keys:
# Seconds, Minutes, Hours, Weeks, Days, Months, Years
#
# Some usage examples:
#
# print scalar localtime TimeUnits_to_epochtime('FROM_PAST' => time ,
'sec' => 3600, 'year' => 3 );
# print scalar localtime TimeUnits_to_epochtime('FROM_PAST' => 23746273
, 'minutes' => 180, 'min' => 360 , 'sec' => 120 );
# print scalar localtime TimeUnits_to_epochtime('FROM_PAST' => 472363433
, 'months' => 38, 'Weeks' => 3 );
#
sub TimeUnits_to_epochtime
{
my $startfrom = -1;
my $NOW = time;
my @NOW = localtime $NOW;
my $Secs = 0;
my $Months = 0;
my $Years = 0;
for (my ($i,$j)=(0,1); $i<=$#_; $i+=2, $j+=2) {
die "Argument ".(1+$j)." \"$_[$j]\" is not an integer\n" unless
$_[$j]=~/^\d+$/;
if ($_[$i]=~/(?i)^sec/){ $Secs += $_[$j] } # Seconds
elsif ($_[$i]=~/(?i)^min/){ $Secs += $_[$j]*60 } # Minutes
elsif ($_[$i]=~/(?i)^hou/){ $Secs += $_[$j]*3600 } # Hours
elsif ($_[$i]=~/(?i)^day/){ $Secs += $_[$j]*86400 } # Days
elsif ($_[$i]=~/(?i)^wee/){ $Secs += $_[$j]*604800 } # Weeks
elsif ($_[$i]=~/(?i)^mon/){ $Months += $_[$j] } # Months
elsif ($_[$i]=~/(?i)^yea/){ $Years += $_[$j] } # Years
elsif ($_[$i]=~/(?i)^fro/){ $startfrom= $_[$j] } # SECONDS FROM EPOCH
TIME that I want to look back
else { die "Argument unit \"$_[$i]\" is not Years, Months, Weeks, Days,
Hours, Minutes or Seconds\n" } }
die "Please define a valid seconds from epoch, for the key \"NOW\"\n" if
$startfrom == -1;
$Years += int $Months / 12;
$Months = $Months % 12;
my $m = $NOW[4] > $Months ? $NOW[4] - $Months : 12 - $Months + $NOW[4];
my $n = Time::Local::timelocal_nocheck($NOW[0],$NOW[1],$NOW[2],$NOW[3],
$m, ($NOW[5] - $Years));
$startfrom - ( $NOW - $n + $Secs )
}