I was close to closing this question as you didn’t include your workings. But I’ve seen your comment – you should edit your question to include that.
So here’s your code:
use POSIX 'strftime';
print strftime "%Y_%m_%d_%H_%M_%S/n", localtime() - 24 * 60 * 60);
That’s pretty close. You have three small issues.
- You have
/n
where you meant\n
. - You have
24 * 60 * 60
, which is one day. You wanted45 *24 * 60 * 60
. - You need to pass subtract the 45 days from the output from
time()
and then pass that value tolocaltime()
.
So using your method, you would want:
use POSIX 'strftime';
print strftime "%Y_%m_%d_%H_%M_%S\n", localtime(time - 45 * 24 * 60 * 60;
But I’d write it using Time::Piece and Time::Seconds (both of which have been part of the Perl code since 5.10.
use 5.010; # for say()
use Time::Seconds;
use Time::Piece
say localtime(time - 45 * ONE_DAY)->strftime("%Y_%m_%d_%H_%M_%S");'
Update: If you’re going to be running this in the hours around a DST changeover, then you’re going to get the wrong answer. The usual approach to fixing this is to normalise the localtime to noon before doing the subtraction.
1
solved Subtracting no of days with system date and converting to MMDDYYYY format in perl [closed]