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

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

I want to get the day of the week (in word format) like my $time = localtime; does.

Using the example from the docs on localtime

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
It produces it in numeric form instead of saying Tue for tuesday.

My first question is why would saving it as a variable $time make it in text when breaking it apart put it into numerics?

Second question is, is the only way to convert the time so I can get the word day of the week the following?

if ($wkday = 0) {$day = "Sun";} elsif ($wkday = 1) {$day = "Mon";}
I don't see why that'd be the only quick way to do it when it prints the days of the week if you first save it all to a variable.

Replies are listed 'Best First'.
Re: getting day of the week
by moot (Chaplain) on Feb 01, 2005 at 22:02 UTC
    For your first question, the difference is 'wantarray' - if the function (localtime) sees you are calling it in scalar context, it returns a string. If you are calling it in array (or list) context, it returns a list (or array).
    For you second question, you might want to think about an array (or possibly a list. No, here, it's definitely an array).

    my @days = qw(Sun Mon Tue Wed Thu Fri Sat); $day = $days[(localtime)[6]];
    The magic happens in (localtime)[6] which means 'return localtime as an (array), and give me the seventh element of that array'.

    Update:Fixed erroneous bogo-link.

Re: getting day of the week
by bradcathey (Prior) on Feb 01, 2005 at 22:04 UTC

    I have one word for you: Date::Calc

    Otherwise:

    my @days = qw ( Sun Mon Tue Wed Thur Fri Sat ); my $weekday = $days[$wkday];

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: getting day of the week
by Roy Johnson (Monsignor) on Feb 01, 2005 at 23:52 UTC
    use POSIX 'strftime'; my $day = strftime('a', localtime(time));

    Caution: Contents may have been coded under pressure.

      The added benefit of using POSIX::strftime is that it will use the appropriate daynames according to your current locale settings, which is useful if you want to internationalize your application.

      /J\

Re: getting day of the week
by !1 (Hermit) on Feb 01, 2005 at 22:51 UTC
    use Time::Piece; print localtime->wdayname;
Re: getting day of the week
by bgreenlee (Friar) on Feb 01, 2005 at 22:09 UTC

    Answer 1: localtime returns a formatted date string if it knows you want a scalar rather than an array.

    Answer 2a:

    my $day = qw(Sun Mon Tue Wed Thu Fri Sat)[(localtime)[6]];

    Answer 2b:

    use Date::Calc qw(:all); my $day = Day_of_Week_to_Text(Day_of_Week(Today()));

    -b

Re: getting day of the week
by sh1tn (Priest) on Feb 01, 2005 at 22:09 UTC
    Weekday:
    ($wday) = (split '\s' , scalar localtime)[0];
Re: getting day of the week
by dragonchild (Archbishop) on Feb 02, 2005 at 03:27 UTC
    use DateTime; $day = DateTime->now->day_abbr;

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: getting day of the week
by chanakya (Friar) on Feb 02, 2005 at 06:24 UTC
    I suggest you to look at

    man Date::Manip