past time

Discussion in 'Perl Misc' started by George Mpouras, Mar 27, 2012.

  1. 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 )
    }
    George Mpouras, Mar 27, 2012
    #1
    1. Advertising

  2. 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
    }
    George Mpouras, Mar 27, 2012
    #2
    1. Advertising

  3. George Mpouras

    J. Gleixner Guest

    On 03/27/12 06:53, George Mpouras wrote:
    > 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?
    J. Gleixner, Mar 27, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Sheen
    Replies:
    13
    Views:
    449
    Hermit Dave
    Jan 25, 2004
  2. Jim Fisher
    Replies:
    0
    Views:
    392
    Jim Fisher
    Oct 22, 2004
  3. knutivar
    Replies:
    2
    Views:
    321
    bruce barker
    Dec 9, 2004
  4. darrel
    Replies:
    0
    Views:
    353
    darrel
    Jan 7, 2005
  5. flamesrock
    Replies:
    8
    Views:
    430
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page