in reply to Date Handling in Perl

I really appreciate everyone's advice. I even learned something about ternary conditional operators along the way! How I have addressed the issue for now is related to the suggestion by hbm:

my $yesterDay = `date -d "1 day ago" +%d`;

I'm keeping an eye on the Perl script and I am hoping this method will work. Thanx again!

Replies are listed 'Best First'.
Re^2: Date Handling in Perl
by Anonymous Monk on Jul 11, 2012 at 20:29 UTC

    The worst part of that method is that it is not portable. Only the GNU version of date (found on most Linuxes) supports that syntax. For example, on FreeBSD, the equivalent syntax is date -v-1d +%d. On other operating systems, date(1) might not even support such arithmetic.

    I highly recommend keeping things Perl when doing so is easy if not trivial (as it is in this case).

Re^2: Date Handling in Perl
by hbm (Hermit) on Jul 11, 2012 at 19:20 UTC

    Are you using '1 day ago' for month and year too?

      From what I've read:

      date -d "1 day ago"

      should be smart enough to know that on the first day of the month, 1 day ago was 30|31 (or 28|29) of LAST month. Also true for January 1 (and knowing it was last month AND last year). I tested like this:

      bash-3.2$ date +%j 193 bash-3.2$ date -d "193 day ago " Sat Dec 31 14:54:22 UTC 2011
      Should this hold true?

        Yes, '1 day ago' affects the full date, not just the day. But you get year, month, and day with separate calls - so if you need to get the day again with '1 day ago', you should also get the month and year again with '1 day ago'.

        You could get them with one system call:

        ($y,$m,$d) = split/\s/,`date "+%Y %M %d"`; ($y,$m,$d) = split/\s/,`date -d "1 day ago" "+%Y %M %d"`;