Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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

by mmartin (Monk)
on Sep 17, 2012 at 19:39 UTC ( #994087=note: print w/replies, xml ) Need Help??

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

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"'


  • Comment on Re: Use 'strftime' to calculate a date/time in the Past.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994087]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2018-05-25 21:20 GMT
Find Nodes?
    Voting Booth?