http://www.perlmonks.org?node_id=994060


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

$ 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

Replies are listed 'Best First'.
Re^2: Use 'strftime' to calculate a date/time in the Past.
by mmartin (Monk) on Sep 17, 2012 at 18:25 UTC
    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.
          No, there's only one Perl module distro that accurately calculates date and time, and that's the one I named.

        I'm going to have to disagree with you there. Date::Manip will also calculate date and time accurately.

        I agree with your other points.
        Hey Daxim, thanks again for the reply...

        Yea I agree, it was just that I didn't want to go and install this Module on 40-50 different servers
        if I didn't have to. Ya know...?

        But actually, now that I'm thinking about it, I believe I do have that Perl Module... I think I just forgot
        to include the "use DateTime..." directive within the Perl command I was trying with your exmaple...


        Anyways, thanks again for all your help!


        Thanks Again,
        Matt