鉴于perl应该分成几个星期

时间:2014-10-07 22:44:13

标签: perl

我试图用perl.Say作为例如将一个月分成4周。我给出开始日期和结束日期的输入。 开课日期-09 / 01/2014 截止日期-09 / 30/2014

perl应该将日期范围分成4个不同的星期。你对我如何处理它有任何想法。

提前致谢..

1 个答案:

答案 0 :(得分:3)

4周持续28天。你已经用30天的时间来解决你的问题了。

假设您只想在星期日 - 星期六等待一周,那么使用Time::Piece的以下代码对您有所帮助:

use strict;
use warnings;

use Time::Piece;
use Time::Seconds;

my $start = '09/01/2014';
my $end   = '09/30/2014';
my $fmt   = '%m/%d/%Y';

# Normalized to Noon to avoid DST
my $week_start = Time::Piece->strptime( $start, $fmt ) + 12 * ONE_HOUR;
my $month_end  = Time::Piece->strptime( $end,   $fmt );

while (1) {
    print $week_start->strftime($fmt), ' - ';

    my $week_end = $week_start + ONE_DAY * ( 6 - $week_start->day_of_week );

    if ( $week_end > $month_end ) {
        print $end, "\n";
        last;
    }

    # Print End of Week and begin cycle for next week
    print $week_end->strftime($fmt), "\n";
    $week_start = $week_end + ONE_DAY;
}

输出:

09/01/2014 - 09/06/2014
09/07/2014 - 09/13/2014
09/14/2014 - 09/20/2014
09/21/2014 - 09/27/2014
09/28/2014 - 09/30/2014