以perl和ksh脚本显示最近3个月的星期日

以perl和ksh脚本显示最近3个月的星期日,perl,ksh,Perl,Ksh,我想用perl脚本显示最近3个月的星期天 例如,假设今天是2013-01-20星期日,从现在起的最后3个月是星期日 2013-01-20 . . 2013-01-06 . . 2012-12-30 2012-12-02 . . 2012-11-25 . . 2012-11-04 应根据当前日期和时间更改最近3个月的星期日 在linux的ksh脚本中需要同样的东西吗 提前谢谢 这是密码。它是上周日发出的。但我需要最后3个月的周日 #!/

我想用perl脚本显示最近3个月的星期天

例如,假设今天是2013-01-20星期日,从现在起的最后3个月是星期日

  2013-01-20
  .
  .
  2013-01-06
  .
  .
  2012-12-30

  2012-12-02
  .
  .
  2012-11-25
  .
  .
  2012-11-04
应根据当前日期和时间更改最近3个月的星期日

在linux的ksh脚本中需要同样的东西吗

提前谢谢


这是密码。它是上周日发出的。但我需要最后3个月的周日

#!/usr/bin/perl

$today = date(time);
$weekend = date2(time);

sub date {
     my($time) = @_;     

     @when = localtime($time);
     $dow=$when[6];
     $when[5]+=1900;
     $when[4]++;
     $date = $when[5] . "-" . $when[4] . "-" . $when[3];

     return $date;
}


sub date2 {
     my($time) = @_;     # incoming parameters

     $offset = 0;
     $offset = 60*60*24*$dow;
     @when = localtime($time - $offset);
     $when[5]+=1900;
     $when[4]++;
     $date = $when[5] . "-" . $when[4] . "-" . $when[3];

     return $date;
}


print "$weekend \n";
谢谢

在ksh中(使用pdksh和GNU coreutils日期进行测试):


使用Perl的DateTime模块的简单解决方案

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use DateTime;

# Get the current date and time
my $now = DateTime->now;

# Work out the previous Sunday
while ($now->day_of_week != 7) {
  $now->subtract(days => 1);
}

# Go back 13 weeks from the previous Sunday
my $then = $now->clone;
$then->subtract(weeks => 13);

# Decrement $now by a week at a time until
# you reach $then
while ($now >= $then) {
  say $now->ymd;
  $now->subtract(weeks => 1);
}

一旦你尽了最大努力,完全陷入困境,我们将很乐意帮助你完成编程,但Stack Overflow不是一个免费提供编程工作的网站。如果您展示您的代码并解释问题,那么我们将很乐意提供帮助。您似乎已经完成了困难的部分。只要从时间中减去7天,你就会得到你喜欢的前几个星期天。你所说的“过去3个月的星期天”是什么意思?你是指最近的十二个星期天,还是指前三个月同一天以来的所有星期天,还是指前三个月初以来的所有星期天,还是指九十天前以来的所有星期天,或者其他什么
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use DateTime;

# Get the current date and time
my $now = DateTime->now;

# Work out the previous Sunday
while ($now->day_of_week != 7) {
  $now->subtract(days => 1);
}

# Go back 13 weeks from the previous Sunday
my $then = $now->clone;
$then->subtract(weeks => 13);

# Decrement $now by a week at a time until
# you reach $then
while ($now >= $then) {
  say $now->ymd;
  $now->subtract(weeks => 1);
}