past time

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 )
}
 
G

George Mpouras

some corrections


use strict;
use warnings;
use Time::Local 'timelocal_nocheck';

#print scalar localtime TimeUnits_to_epochtime('Weeks' => 6 );
print scalar localtime TimeUnits_to_epochtime('Months' => 4 );


# 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( 'months' => 38,
'Weeks' => 3 );
# 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 );
#
sub TimeUnits_to_epochtime
{
my $startfrom = $^T;
my @NOW = localtime $^T;
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" } }
$Years += int $Months / 12;
$Months = $Months % 12;
my ($m,$y);

if ( $NOW[4] > $Months ) {
$m = $NOW[4] - $Months;
$y = $NOW[5] - $Years
}
else {
$m = 12 - $Months + $NOW[4];
$y = $NOW[5] - $Years - 1
}

$startfrom - $^T + timelocal_nocheck(@NOW[0..3],$m,$y) - $Secs
}
 
J

J. Gleixner

You may find it useful. Returns what time was before as much days, seconds,
etc.

ahhhh.. You do know there are many date classes that have been around
for many, many years that do this, and much, much more.. right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top