我如何在 Perl 中生成日期列表?

发布于 2024-11-08 12:55:55 字数 874 浏览 0 评论 0原文

我想编写一个脚本,用 perl 生成从现在到去年的日期。

例如,

                <option value="01/02/2010">Feb 07</option>
                <option value="01/03/2010">Mar 07</option>
                <option value="01/04/2010">Apr 07</option>
                <option value="01/05/2010">May 07</option>
                <option value="01/06/2010">Jun 07</option>
                <option value="01/07/2010">Jul 07</option>
                <option value="01/08/2010">Aug 07</option>
                <option value="01/09/2010">Sep 07</option>
                <option value="01/10/2010">Oct 07</option>
                <option value="01/11/2010">Nov 07</option>
                <option value="01/12/2010">Dec 07</option

我不知道该怎么做。我现在手动做

I want to write a script that generates dates between now and the last year in perl.

Ex

                <option value="01/02/2010">Feb 07</option>
                <option value="01/03/2010">Mar 07</option>
                <option value="01/04/2010">Apr 07</option>
                <option value="01/05/2010">May 07</option>
                <option value="01/06/2010">Jun 07</option>
                <option value="01/07/2010">Jul 07</option>
                <option value="01/08/2010">Aug 07</option>
                <option value="01/09/2010">Sep 07</option>
                <option value="01/10/2010">Oct 07</option>
                <option value="01/11/2010">Nov 07</option>
                <option value="01/12/2010">Dec 07</option

I have no idea how I can do it. I am doing it manually for now

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泛滥成性 2024-11-15 12:55:55

根据口味调整格式和减去的数量。

#!/usr/bin/perl

use strict;
use warnings;
use DateTime;

my $now = DateTime->now;
my $start_of_last_year = DateTime->new( year => $now->year - 1 );
while ( $now > $start_of_last_year ) {
        print $now->ymd('/'), "\n";
        $now->subtract( days => 1 );
}

Adjust formatting and amount subtracted for taste.

#!/usr/bin/perl

use strict;
use warnings;
use DateTime;

my $now = DateTime->now;
my $start_of_last_year = DateTime->new( year => $now->year - 1 );
while ( $now > $start_of_last_year ) {
        print $now->ymd('/'), "\n";
        $now->subtract( days => 1 );
}
千秋岁 2024-11-15 12:55:55

尝试:

#!/usr/bin/env perl

use strict;
use warnings;

use POSIX;

my $Now = time;
my @Now = localtime( $Now );

for my $count ( 1 .. 12 ){
  # month/day/year
  print strftime( "%m/%d/%Y\n", @Now );
  $Now[4] --;
  if( $Now[4] < 0 ){
    $Now[4] = 11;
    $Now[5] --;
  }
}

Try:

#!/usr/bin/env perl

use strict;
use warnings;

use POSIX;

my $Now = time;
my @Now = localtime( $Now );

for my $count ( 1 .. 12 ){
  # month/day/year
  print strftime( "%m/%d/%Y\n", @Now );
  $Now[4] --;
  if( $Now[4] < 0 ){
    $Now[4] = 11;
    $Now[5] --;
  }
}
避讳 2024-11-15 12:55:55
use 5.010;
use strict;
use warnings;

use POSIX qw<strftime>;

my @loc = localtime();

for ( 1..12 ) { 
    say strftime( qq{<option value="01/%m/%Y">%b 07</option>}, @loc );
    $loc[4]--;
}

输出:

<option value="01/05/2011">May 07</option>
<option value="01/04/2011">Apr 07</option>
<option value="01/03/2011">Mar 07</option>
<option value="01/02/2011">Feb 07</option>
<option value="01/01/2011">Jan 07</option>
<option value="01/12/2010">Dec 07</option>
<option value="01/11/2010">Nov 07</option>
<option value="01/10/2010">Oct 07</option>
<option value="01/09/2010">Sep 07</option>
<option value="01/08/2010">Aug 07</option>
<option value="01/07/2010">Jul 07</option>
<option value="01/06/2010">Jun 07</option>

只是为了笑,替代压缩版本:

use 5.010;
use strict;
use warnings;

use POSIX qw<strftime>;

say join( "\n"
        , map { 
             my $t = strftime( qq{<option value="01/%m/%Y">%b 07</option>}, @$_ ); 
             $_->[4]--; 
             $t; 
          }
          (( [ localtime ] ) x 12 )
       );
use 5.010;
use strict;
use warnings;

use POSIX qw<strftime>;

my @loc = localtime();

for ( 1..12 ) { 
    say strftime( qq{<option value="01/%m/%Y">%b 07</option>}, @loc );
    $loc[4]--;
}

Output:

<option value="01/05/2011">May 07</option>
<option value="01/04/2011">Apr 07</option>
<option value="01/03/2011">Mar 07</option>
<option value="01/02/2011">Feb 07</option>
<option value="01/01/2011">Jan 07</option>
<option value="01/12/2010">Dec 07</option>
<option value="01/11/2010">Nov 07</option>
<option value="01/10/2010">Oct 07</option>
<option value="01/09/2010">Sep 07</option>
<option value="01/08/2010">Aug 07</option>
<option value="01/07/2010">Jul 07</option>
<option value="01/06/2010">Jun 07</option>

Just for grins, alternative compressed version:

use 5.010;
use strict;
use warnings;

use POSIX qw<strftime>;

say join( "\n"
        , map { 
             my $t = strftime( qq{<option value="01/%m/%Y">%b 07</option>}, @$_ ); 
             $_->[4]--; 
             $t; 
          }
          (( [ localtime ] ) x 12 )
       );
若能看破又如何 2024-11-15 12:55:55

使用 Date::Simple

#!/usr/bin/perl
use strict;
use warnings;
use 5.012;
use Date::Simple qw/ today ymd /;

my $now = today;

for (my $date = ymd($now->year, 1, 1); $date <= $now; $date++) {
    say $date;  
}

如果您愿意去年(不是今年年初)您可以在循环中调用“ymd”,如下所示:

ymd($now->year()-1, 1, 1);

Using Date::Simple

#!/usr/bin/perl
use strict;
use warnings;
use 5.012;
use Date::Simple qw/ today ymd /;

my $now = today;

for (my $date = ymd($now->year, 1, 1); $date <= $now; $date++) {
    say $date;  
}

If you want last year (not beginning of this year) you could call 'ymd' in the loop as:

ymd($now->year()-1, 1, 1);
鸠书 2024-11-15 12:55:55

另一个值得研究的模块是 Date::Calc。我需要一个模块来替换我的一个脚本的一些尴尬的日期计算,它看起来可以做我需要它做的任何事情。 http://metacpan.org/pod/Date::Calc

Another module worth looking into is Date::Calc. I needed a module to replace some awkward date calculations for one of my scripts and it looks like it does anything I'd ever need it to do. http://metacpan.org/pod/Date::Calc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文