Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I want to 'safely' print out the time and date. It must be in the form of 'Sun Sep 22 18:41:27 BST 2002', which is what is returned by running 'date' from the command-line.

Sorry for the stupid question

Replies are listed 'Best First'.
Re: Probably the easiest SOPW today!
by davorg (Chancellor) on Sep 22, 2002 at 18:30 UTC

      scalar localtime misses the time zone that appears in the OP's example. I'd use strftime to get it exact.

      $ perl -e 'print scalar localtime, "\n"' Sun Sep 22 13:01:42 2002 $ perl '-MPOSIX qw(strftime)' -e 'print strftime( "%a %b %d %T %Z %Y", + localtime ), "\n"' Sun Sep 22 13:01:48 PDT 2002

      _______________
      DamnDirtyApe
      Those who know that they are profound strive for clarity. Those who
      would like to seem profound to the crowd strive for obscurity.
                  --Friedrich Nietzsche
Re: Printing current date and time
by virtualsue (Vicar) on Sep 22, 2002 at 21:17 UTC
    I want to 'safely' print out the time and date. It must be in the form of 'Sun Sep 22 18:41:27 BST 2002', which is what is returned by running 'date' from the command-line.

    For quick & dirty scripts, the following will suffice:

    print `/usr/bin/date`;
    Update: D'oh! I forgot to specify the command with an absolute path. Mea culpa.
      What, if any, safety considerations are there in going with the simple backtick solution?
      Are we afraid of a Trojanned date program?

      What other security risks might we encounter here?

      --
      Microsoft delendum est.
        The biggest security concern with print `date` is not that system's date binary has been trojaned. If that were the case, you're already screwed. I'd be more concerned that combined with some sort of path munging, an entirely different file named date could be executed. If it used an absolute path name, I dont think there would be much of a security concern at all. i.e. print `/bin/date` would be a step in the right direction.

        From a coding standpoint, shelling out trivial things like this is a performance hit, and I'd flag it in any program that wasn't a throwaway. Of course, virtualsue, only advocated it for use in quick 'n dirty stuff anyway.

        -Blake