How can I get this week's dates in Perl?

后端 未结 4 1781
感情败类
感情败类 2021-02-09 04:44

I have the following loop to calculate the dates of the current week and print them out. It works, but I am swimming in the amount of date/time possibilities in Perl and want t

4条回答
  •  醉梦人生
    2021-02-09 05:03

    Would this work:

    use strict;
    use warnings;
    use POSIX qw;
    my ( $day, $pmon, $pyear, $wday ) = ( localtime )[3..6];
    $day -= $wday - 1; # Get monday
    for my $d ( map { $day + $_ } 0..6 ) { 
        print strftime( '%A, %B %d, %Y', ( 0 ) x 3, $d, $pmon, $pyear ), "\n";
    }
    

    I'm printing them only as an illustration. You could store them as timestamps, like this:

    use POSIX qw;
    my @week = map { mktime(( 0 ) x 3, $day + $_, $pmon, $pyear ) } 0..6;
    

提交回复
热议问题