1

在过去的几个月里,我一直在使用以下代码,它从预定义的日期开始循环几个月,直到它到达今天的日期。

use Date::Pcalc qw(:all);

$startDay = 1;
$startMonth = '4';
$startYear = '2009';

$dateToday = `date +%Y-%m-%d`;
($yt,$mt,$dt) = split(/\-/,$dateToday);

while ($endMonth <= $mt || $startYear < $yt ) {

if ($startMonth eq '12') {
    $endMonth = 1;
    $endYear = $startYear + 1;
  } else {
    $endMonth = $startMonth + 1;
    $endYear = $startYear;
  }

  if ($startMonth eq '12') {
    $endYear = $startYear + 1;
  }

  ($meYear,$meMonth,$meDay) = Add_Delta_Days($endYear,$endMonth,$startDay,-1);
  $endOfMonth = "$meYear-$meMonth-$meDay";
  $monthText = Month_to_Text($startMonth);

  $startDate = "$startYear-$startMonth-1";
  $endDate = "$endYear-$endMonth-1";

print "$startDate - $endDate\n";

if ($startMonth eq '12') {
    $startMonth = 1;
    $startYear++;
  } else {
    $startMonth++
  }

}

在过去的几个月里,这一直很有效,但我意识到现在在 12 月,因为 $endmonth 永远不会大于 $mt (12),这会导致无限循环。

我一直无法找出任何替代方法来做到这一点。我觉得我应该能够相对容易地解决这个问题,但我似乎有严重的“开发者障碍”

提前感谢任何可以提供帮助的人。

4

2 回答 2

6
my $date = DateTime->new(
   time_zone => 'local',
   year      => $startYear,
   month     => $startMonth,
   day       => 1,
);

my $today = DateTime->today(time_zone => 'local');

while ($date <= $today) {
   say $date->ymd('-');
   $date->add( months => 1 );
}
于 2012-12-13T01:03:57.273 回答
2

我认为您的代码存在一些问题。但是让我们来解决第一个问题,即第 12 个月的结束日期,这会导致此语句中出现循环:

    while ($endMonth <= $mt || $startYear < $yt ) {

好的,一旦你有了当前日期、年份、月份和日期,你应该做的是这样的事情。你会注意到其他人提出了不同的方法来获取当前日期你应该接受这个建议。但是,一旦您有了日期,就应该采用以下代码:

    ($yt,$mt,$dt) = split(/\-/,$dateToday);
    # the line below will create a date like 201212 (yyyy mm) but if the month is a 1 digit month it will place a 0 in front of it to ensure your yymm variable always holds 6 characters in the format of yyyy mm - ok
    my $yymm = $yt . ${\(length($mt) == 1 ? '0' : '')} . $mt;
    # Now lets check the end date against the yymm
    # initialise end date as end_yymm - again it inserts a 0 for single digit month
    my $end_yymm = $startyear . ${\(length($startMonth) == 1 ? '0' : '')} . $startMonth;
    # the above should get the date as '200904' from your code provide
    # the while will check end_yymm like 200904 < 201212 - yes it is...
    ## the end_yymm will keep getting incremented each month and so will the year component at the end of each year until it reaches 201212
    ## then the question 201212 < 201212 will cause the while to end
    ## If you want it go into 201301 then say while ($end_yymm <= $yymm) {
    ## Hope you get the picture
    while ($end_yymm < $yymm) {

    if ($startMonth eq '12') {
        $endMonth = 1;
        $endYear = $startYear + 1;
    } else {
      $endMonth = $startMonth + 1;
      $endYear = $startYear;
    }

    ## Now this one seems to be repeating the endYear calculation as above - to me it seems redundant - maybe get rid of it
    if ($startMonth eq '12') {
       $endYear = $startYear + 1;
    }        

    ## Now that you have the end year and month incremented setup the end_yymm variable again to be picked up in the while statement:
    $end_yymm = $startyear . ${\(length($startMonth) == 1 ? '0' : '')} . $startMonth;

     # ...... carry on with the rest of your code

    } # end the while loop

那应该这样做。

祝一切顺利

于 2012-12-18T12:15:01.993 回答