Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Use 'strftime' to calculate a date/time in the Past.

by mmartin (Monk)
on Sep 17, 2012 at 16:54 UTC ( #994059=perlquestion: print w/ replies, xml ) Need Help??
mmartin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am trying to figure out a date/time in the past given I know it was 'a' days ago, 'b' hours ago, 'c' min ago and 'd' seconds ago.


I came across this perl command:
perl -e 'use POSIX 'strftime'; $_n_days_ago = strftime("%d", gmtime( t +ime-$DAY_AGO*86400 )); print "$_n_days_ago\n"'
Which calculates the day "$DAYS_AGO" into the past. i.e. If "$DAYS_AGO = 2" then the resulting output would be "15" since today
is the 17th.

I've tried a couple different ways using a similar formatted command but it's not giving what I need.
I know the "FORMAT" part of the strftime function would be --> strftime( "%S, %M, %k, %d, %m, %Y",...

Which the resulting Output I want would be in the format:
"Seconds, Minutes, Hours, Day-of-Month, Month, Year"

For Example:
If I want the date and time from, 2 days, 10 hours, 20 minutes, and 30 seconds Ago the output would be:
# If the current date/time was "9/17/2012 12:00:00".... # (i.e. time is in 24-Hour format) The Output would be: "30, 40, 02, 15, 09, 2012" Which in date format is = "9/15/2012 02:40:30"


Could anyone tell me if this is possible using the functions above?
Any suggestions would be very much appreciated..!



Thanks in Advance,
Matt

Comment on Use 'strftime' to calculate a date/time in the Past.
Select or Download Code
Re: Use 'strftime' to calculate a date/time in the Past.
by daxim (Chaplain) on Sep 17, 2012 at 17:01 UTC
    $ perl -mDateTime -E'say DateTime->now->subtract(days => 2, hours => 1 +0, minutes => 20, seconds => 30)->strftime("%F %T")' 2012-09-15 06:40:25
      Hey daxim, thanks for the reply.

      I tried your command but it seems I don't have the Perl Module "DateTime.pm" installed on my machine.
      And this command which is a part of a script I'm writing will be ran on a number of different servers,
      alot of which are AIX and SLES servers which I would rather not have to install this module on every
      server...

      Is it not possible to do it with the current way I have the command? Because that original command worked
      on all the servers I tested it on.


      I was thinking that to use my original command, would I have to convert the "amount of time into the past" into
      all seconds?

      i.e. If I want 2 days, 12 hours, 57 min, and 36 seconds into the past would I just convert each of those into
      seconds then subtract them by "time", like in the "gmtime(...)" part of the command?


      Thanks Again,
      Matt
        I would rather not have to install this module
        Just do it. You're not really using Perl if you refuse to take advantage of modules. Dependencies are a good thing. You're likely afraid of change and overestimating the effort of installing, but with the right knowledge and tools it's easy. I don't want to go off-topic in this thread, so open a new question if you want further advice.
        Is it not possible to do it with the current way I have the command?
        No, there's only one Perl module distro that accurately calculates date and time, and that's the one I named. POSIX and Time::Piece offer only crude calculation and place the burden on the implementing programmer to get the details right, which is wrong because it always results in inaccuracy.
        would I have to convert the "amount of time into the past" into all seconds … then subtract them
        Sounds like a plan that can't go wrong… except you neglect to account for leap seconds. The magic number 86400 you used earlier is likewise flawed due to daylight saving time changing the length of a day.
Re: Use 'strftime' to calculate a date/time in the Past.
by Anonymous Monk on Sep 17, 2012 at 19:28 UTC

    I've tried a couple different ways using a similar formatted command but it's not giving what I need. Could anyone tell me if this is possible using the functions above?

    Its possible, show what you tried

      Hey Anonymous Monk, thanks for the reply...

      Sorry I forget exactly how the command looked I was asking about at that point... But I was able to get this working!!
      And I was able to make it a little better then what I had posted at the end of this thread that I had my "resolution"
      written in.


      What I was doing was I had included an unneeded/unnecessary step. Here's what I did and what it is NOW...

      FIRST, I had this one Perl command (below):
      *This command would output the Seconds, Minutes, Hours, MDay, Month and Year.

      perl -e 'use POSIX 'strftime'; $_n_days_ago = strftime("%S, %M, %k, %d, %m, %Y", localtime( time-$SECONDS )); print "$_n_days_ago\n"'

      SECOND, I had this other Perl command (below):
      *This command would take the output from the previous Perl command (i.e. $sec, $min, $hr, $mday, etc....) and would use
      those values and feed them into this Perl Command to give me a UNIX Timestamp...


      perl -e 'use Time::Local; print timelocal($sec,$min,$hour,$mday,$month,$year), "\n"'


      Stupidly, not realizing that instead of those 2 commands, all's I needed to do was the 1st Perl Command and use the output
      modifier --> "%s" to print a UNIX Timestamp.... Duhhh me!!

      Here's the working Command:

      PAST_TIMESTAMP=$(perl -e 'use POSIX 'strftime'; $_n_days_ago = strftime("%s", localtime(time-$ENV{TOTAL_SECONDS} )); print "$_n_days_ago"')


      Anyway, thanks for all the help/suggestions, very much appreciated!!


      Thanks Again,
      Matt

Re: Use 'strftime' to calculate a date/time in the Past.
by mmartin (Monk) on Sep 17, 2012 at 19:39 UTC
    Ok, so I was able to figure this out.

    So I used the same exact formula from my OP and did the total seconds into the past minus 'time'.

    This was almost exactly what I wanted, but no matter what I put for a length into the past the
    output was ALWAYS showing 4 Hours off from what it should have been...


    I then realized that I should have been using "localtime()" instead of "gmtime()". I switched them
    up and now its working like a charm!!


    Here's how to do it if anyone ever gets stuck trying to calculate a Date AND Time into the past:


    For Example --> I want "2 Days, 12 Hours, 1 Minute, and 20 Seconds" into the past...

    From my Linux CMD Prompt: FYI... The integer '216080' is the total seconds into the past

    ### This will print the date and Time in a readable format (i.e. mm/dd/yyy hh:mm:ss in 24-Hour Format)...
    perl -e 'use POSIX 'strftime'; $_n_days_ago = strftime("%m/%d/%Y %T", localtime( time-216080 )); print "$_n_days_ago\n"'

    ### Now what I wanted for MY output is (i.e. "$seconds, $minutes, $hours, $mday, $month, $year")
    # I wanted it like this so I could get the UNIX Timestamp of that Past Date and Time by using those values from its output...
    perl -e 'use POSIX 'strftime'; $_n_days_ago = strftime("%S, %M, %k, %d, %m, %Y", localtime( time-216080 )); print "$_n_days_ago\n"'


    Thanks,
    Matt


Re: Use 'strftime' to calculate a date/time in the Past.
by Khen1950fx (Canon) on Sep 17, 2012 at 21:35 UTC
    FWIW, I used Time::Local for a one-liner:
    perl -MTime::Local=timelocal -e 'print scalar localtime timelocal qw(3 +0 40 2 15 8 2012)';
    Update: to get the day, date, and time 48 days ago:
    #!/usr/bin/perl -l use strict; use autodie; use warnings; use POSIX 'strftime'; use Time::Local 'timelocal'; my $days_ago = '48'; ($days_ago) = strftime("%d", gmtime( time - $days_ago * 86400 )); print scalar localtime timelocal( 30, 40, 2, $days_ago, 7, 2012, );
      But you don't do date math, the thing the OP is trying to do

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994059]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-08-21 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (138 votes), past polls