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

Hai,

How to print date and time in perl. I want my system current time and date with AM or PM.

Replies are listed 'Best First'.
Re: How to get time in perl?
by jbrugger (Parson) on Jul 06, 2005 at 06:10 UTC
    perldoc -f localtime
    (localtime)

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: How to get time in perl?
by holli (Abbot) on Jul 06, 2005 at 06:56 UTC
    use strftime() from the POSIX module.


    holli, /regexed monk/

      Just to expand on holli's excellent suggestion ...

      perl -MPOSIX -le'print strftime "%x %X", localtime time' 07/06/05 17:05:49
      There are many more formatting options, have a look at the strftime docs

      Update

      Sorry did not read the OP too closely, he wants AM or PM so this may be more like it ...
      perl -MPOSIX -le'print strftime "%r %x", localtime time' 06:47:16 PM 07/07/05

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
Re: How to get time in perl?
by sh1tn (Priest) on Jul 06, 2005 at 06:13 UTC
    perldoc -q time.
    or
    Time get/set.


Re: How to get time in perl?
by gube (Parson) on Jul 06, 2005 at 06:20 UTC
    use Time::Localtime; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year = $year + 1900; $mon = $mon+1; if($hour > 12) { $hour = $hour - 12; $x = 'PM'; } elsif($hour == 12) { $x = 'PM'; } else { $x = 'AM'; } print sprintf("%02d", $mday).'/'.sprintf("%02d", $mon).'/'.sprintf("%0 +4d", $year).' '.sprintf("%02d", $hour).':'.sprintf("%02d", $min).':'. +sprintf("%02d", $sec).' '.$x;
    Update: Suggest by jbrugger. Thanks jbrugger
      A little comment gube
      Month = 0-based, so to display properly use $month++;
      The pragma use Time::Localtime is not needed here, and please use -w and scrict.
      eg:
      #!/usr/bin/perl -w use strict; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year = $year + 1900; $mon++; my $x = ""; if($hour > 12) { $hour = $hour - 12; $x = 'PM'; } else { $x = 'AM'; } print "$mday/$mon/$year $hour:$min:$sec $x\n";
      "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: How to get time in perl?
by jacques (Priest) on Jul 07, 2005 at 22:31 UTC
    This is in our FAQ section (which no one appears to read anymore):

    How do I get today's date

    Almost all of the answers in this thread are found in the FAQ!

Re: How to get time in perl?
by trammell (Priest) on Jul 07, 2005 at 01:39 UTC
    Just call localtime in scalar context:
    my $foo = localtime; print $foo;

    Update: Drat. Indeed the output from localtime follows the ctime() format, which does not include AM/PM. It does return a 24-hour time, but that's not what the OP wanted.

      or indeed:

      print scalar localtime;

      Perl is Huffman encoded by design.

        Nice, has that always worked in Perl 5 or is it a feature of newer Perl versions ?

        There is one problem, the OP explicitly asked for AM/PM in the output, this does not give me that. Perhaps it does in some locales?

        Cheers,
        R.

        Pereant, qui ante nos nostra dixerunt!