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

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

Hello,
I am very new to Perl and have not done a great deal of programming either but am keen to learn. My problem is as follows:

I am trying to write a Perl script to tell which Tapes from a backup set need to be changed, and for which tapes they should be changed. For this to work I need to be able to identify the day of the week from the current date.

I found the follwoing bit of code:

my $time = str2time('2002-02-21'); my $day_of_wk = time2str("%w", $time); print "day of week is $day_of_wk\n"; $day_of_wk = time2str("%A", $time); print "day of week is $day_of_wk\n";

This works fine but how do I make my $time = str2time(localtime)?

I can get localtime into a string but str2time appears to need the actual text date. Can someone can put me right?

Regards
Gavrilo

Replies are listed 'Best First'.
Re: Get day of week from current date
by rjt (Curate) on Jul 08, 2013 at 11:34 UTC

    Use DateTime.

    #!/usr/bin/env perl use 5.012; use warnings; use DateTime; my $dt = DateTime->new(year => 2002, month => 2, day => 21); say "I'm pretty sure it was a " . $dt->day_name; say "Today is " . DateTime->now()->day_name;

    Output:

    I'm pretty sure it was a Thursday Today is Monday

    If that output is not to your liking, look at the many methods in DateTime for more options such as numeric or abbreviated weekdays.

      Date:Calc is easy and more versatile than Date::Simple and DateTime. use Date::Calc qw(:all); print Day_of_Week($year,$month,$day);
Re: Get day of week from current date
by mtmcc (Hermit) on Jul 08, 2013 at 18:01 UTC
    If you need the text of the day to be in a specific format/case, you can still use localtime, and redefine the format using a hash:

    #!/usr/bin/perl use strict; my @date = split(" ", localtime(time)); my $day = $date[0]; my %days = (); %days = ("Mon", "Monday", "Tue", "Tuesday", "Wed", "Wednesday", "Thur" +, "Thursday", "Fri", "Friday", "Sat", "Saturday", "Sun", "Sunday"); print STDERR "Today is $days{$day}\n";

      If you want to do it that way, use localtime's list context. You can avoid the string parsing which could change with locale1:

      my @wday = qw/Monday Tuesday Wednesday Thursday Friday Saturday Sunday +/; printf "Today is %s.\n", $wday[ (localtime(time))[6] - 1 ];

      See localtime for details on the returned list.


      Update: A little lot more feedback.

      _______________
      1. Whoops. localtime: The format of this scalar value is not locale-dependent but built into Perl.

        Thanks rjt!

        Michael
      You Sir are a star and the good news is I even understand what you are doing :-) Thanks to everyone for the legup. Gavrilo
Re: Get day of week from current date
by Anonymous Monk on Jul 08, 2013 at 14:07 UTC
    Also search for "date" at http://search.cpan.org. One of the most thoroughly solved problems that there is .. even (I kid you not) for the Mayan calendar. If there is one single class of problem that you DON'T have to solve for yourself, date/time would be it.
Re: Get day of week from current date
by Cristoforo (Curate) on Jul 08, 2013 at 19:42 UTC
    A solution using Date::Simple.
    #!/usr/bin/perl use strict; use warnings; use 5.014; use Date::Simple qw/ date today /; my $dow = date('2002-02-21'); say $dow->day_of_week, ' ', $dow->strftime("%A"); $dow = today; say $dow->day_of_week, ' ', $dow->strftime("%A");
    Prints
    4 Thursday 1 Monday
Re: Get day of week from current date
by ambrus (Abbot) on Apr 18, 2014 at 11:42 UTC

    Let me show how you'd do this with the Date::Manip module

    use warnings; use strict; use Date::Manip::Date; my $today = Date::Manip::Date->new("today"); print $today->printf("Today is %A.\n"); my $day_of_week = $today->printf("%w"); # day of week as number, 7 mea +ns Sunday __END__