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

I am trying to print out todays date such as: Monday, February 13,2006 10:09
Please advise what I am doing wrong:
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; $mon++; $year -= 100; my $theDate = sprintf "%10i %02i %02i,%02i %02i:%02i", $wday, $mon, $m +day, $year, $hour, $min;

Replies are listed 'Best First'.
Re: Date format
by Fletch (Bishop) on Feb 13, 2006 at 15:33 UTC
    • you should be adding 1900 to $year
    • you're not using POSIX::strftime, which would have made that mistake moot

      With POSIX, it would be:

      use POSIX; print strftime("%A, %B %d, %Y %H:%M", localtime(time));

      The big advantage is that with POSIX you can get day and month name in your language, e.g. for me the output looks like:

      Dienstag, Februar 14, 2006 08:43

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Date format
by mbeast (Beadle) on Feb 13, 2006 at 15:44 UTC
    localtime() returns integers, so $wday and $mon will not contain the string "Monday" or "February" but rather a digit. You will also need to add 1900 to the $year.

    my @days = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); my @months = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); print $days[$wday] . ' ' . $months[$mon];

    I think there are some CPAN modules that will handle this, but I've never bothered.

    Edit: ok, I think it works now.
      Thanks that worked great!
Re: Date format
by davorg (Chancellor) on Feb 13, 2006 at 15:54 UTC
    perl -MPOSIX=strftime -le 'print strftime "%A, %B %d, %Y %H:%M", local +time'
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Date format
by McDarren (Abbot) on Feb 13, 2006 at 15:49 UTC
    Of course, you could always do:
    perl -le 'print scalar localtime' Mon Feb 13 23:45:02 2006
Re: Date format
by saintmike (Vicar) on Feb 13, 2006 at 17:53 UTC
    The DateTime module with its DateTime::Format::Strptime companion makes it easy to print out your date string in different locales:
    en_US: Monday, February 13, 2006 17:57 de_DE: Montag, Februar 13, 2006 17:57 fr_FR: lundi, février 13, 2006 17:57 es_ES: lunes, febrero 13, 2006 17:57
    which is easily accomplished with this code:
    use DateTime; use DateTime::Format::Strptime; my $dt = DateTime->now( time_zone => "America/Los_Angeles", ); for my $locale (qw(en_US de_DE fr_FR es_ES)) { $dt->set_locale($locale); my $format = DateTime::Format::Strptime->new( pattern => "%A, %B %d, %Y %H:%M", ); $dt->set_formatter($format); print "$locale: $dt\n"; }
    However, different countries have different formatting rules as well:
    en_GB: 13 February 2006 17:49:49 UTC en_US: February 13, 2006 5:49:49 PM UTC de_DE: 13. Februar 2006 17:49:49 UTC fr_FR: 13 février 2006 17:49:49 UTC es_ES: 13 de febrero de 2006 17:49:49 UTC es_MX: 13 de febrero de 2006 5:49:49 PM UTC
    The code for this goes like that:

    use strict; use DateTime; use DateTime::Format::Strptime; my $dt = DateTime->now(); for my $locale (qw(en_GB en_US de_DE fr_FR es_ES es_MX)) { $dt->set_locale($locale); my $format = DateTime::Format::Strptime->new( pattern => $dt->locale()-> long_datetime_format() ); $dt->set_formatter($format); print "$locale: $dt\n"; }
      Thanks for ALL your responses. I learned alot from this question.
Re: Date format
by thedoe (Monk) on Feb 13, 2006 at 17:49 UTC

    I would really recommend using a CPAN module for formatting dates instead of building your own solution from the bottom.

    One of my favorites is Date::Format.

    This is very easy to use. You also get the added bonus of easily changing the format of your output date. Simply change the template string, and you are set. There is no need to modify what variables you are getting, possibly change the format of those variables (numeric or string context), then change the print statement.

    For example, in your desire to get the format "Monday, February 13,2006 10:09" you would do:

    time2str('%A, %B %d,%Y %I', time);

Re: Date format
by ambrus (Abbot) on Feb 13, 2006 at 19:34 UTC

    Since no-one has given a solution with Date::Manip yet, I'll give one. I assume that the missing space before the year in your example is just an error.

    use Date::Manip; print UnixDate("now", "%F %R\n");
Re: Date format
by mikeock (Hermit) on Feb 13, 2006 at 17:28 UTC
    Remeber that you can retrieve the slice of an array.
    Instead of : my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
    You could do
    my($sec,$min,$hour,$mday,$mon,$year,$wday,undef,undef) = localtime;
    Notice the undef for the values that are not being used. I would recomend this as then you are not declaring things that will never be used.
Re: Date format
by smokemachine (Hermit) on Feb 13, 2006 at 17:40 UTC
    can be this?
    perl -e 'print $date=localtime time'
    out put:
    Mon Feb 13 15:38:33 2006
Re: Date format
by Praveen (Friar) on Feb 15, 2006 at 10:49 UTC
    Try This
    my($min,$hour,$day, $month, $year,$wday) = (localtime)[1,2,3,4,5,6]; if ($year < 70){$year += 2000;}else{$year += 1900;} my @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday +','Saturday'); my @month_array =('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug +','Sep','Oct','Nov','Dec'); print "$days[$wday], $month_array[$month] $day, $year $hour:$min";
    Rds/Praveen
      if ($year < 70){$year += 2000;}else{$year += 1900;}

      Whoa! where did that come from? You really need to read the documentation for localtime. The year value that you get back is always the number of years since 1900. You never need to add 2000 to the year - it's always 1900.

      Of course you'll never come across that bug if you're calling localtime without any arguments as the year you'll get now will always be over 100. But you really don't want to be leaving potential bugs like that around in code that you're giving people as an example.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

A reply falls below the community's threshold of quality. You may see it by logging in.