Chinaunix

标题: 如何用perl语句得到日期列表 [打印本页]

作者: xinganling    时间: 2009-04-13 11:45
标题: 如何用perl语句得到日期列表
需要两年的日期列表如下:
20080101
20080102
20080103
...
20080228
20080229
20080301
...

一条一条写太费时,而且太长,有没有办法用perl语句来实现,先谢了
作者: flw    时间: 2009-04-13 12:02
当然是循环了。一天是 24 小时。
你算出第一天的时间,然后循环,每次加 24 小时,然后再转换成你想要的格式就行了。
作者: hfahe    时间: 2009-04-13 13:13
恩  根据时间戳构造吧~~
作者: 枫影谁用了    时间: 2009-04-13 13:17
[root@localhost perl]# perl daylist.pl
2009-04-22
2009-04-21
2009-04-20
2009-04-19
2009-04-18
2009-04-17
2009-04-16
2009-04-15
2009-04-14
2009-04-13
[root@localhost perl]# cat daylist.pl
#!perl
use Date::Simple (':all');

my $DATE_START=today() -1;
my $MONTH;
my $DATE_END;

my $i_ = 10;
while ( $i_ >= 1 ){
my $date  = Date::Simple->new("$DATE_START");
$DATE_END=$date + $i_;
print "$DATE_END\n";
$i_--;
}
[root@localhost perl]#
作者: xinganling    时间: 2009-04-13 14:50
标题: 回复 #4 枫影谁用了 的帖子
C:\>./daylist.pl
Can't locate Date/Simple.pm in @INC (@INC contains: C:/Perl/site/lib C:/Perl/lib
.) at C:\daylist.pl line 2.
BEGIN failed--compilation aborted at C:\daylist.pl line 2.

C:\>
第二行是:
use Date::Simple (':all');
是Date::Simple不存在吗?  如何看Date::Simple是否存在?
作者: hfahe    时间: 2009-04-13 15:03
汗。。。装模块把。。
作者: shaneqi    时间: 2009-04-13 16:04
http://search.cpan.org/~izut/Date-Simple-3.03/lib/Date/Simple.pm
作者: MMMIX    时间: 2009-04-13 16:52
原帖由 xinganling 于 2009-4-13 14:50 发表
第二行是:
use Date::Simple (':all');
是Date::Simple不存在吗?

是的。
如何看Date::Simple是否存在?

直接用它,不存在的话 perl 会告诉你的。
作者: youzhengchuan    时间: 2009-04-13 17:21
随便写几个for不久实现了吗?
作者: youzhengchuan    时间: 2009-04-13 17:28
1 for($year = 2008;$year <= 2009 ;$year++){
      2         for($month = 1;$month <= 12;$month++){
      3                 for($day = 1;$day <= getMaxDay($year,$month);$day++){
      4                         print "$year-$month-$day\n";
      5                 }
      6         }
      7 }
      8
      9 sub getMaxDay() {
     10         return 30;#你自己写一个算法来算得当年当月有多少天即可
     11 }
作者: 小公猫    时间: 2009-04-13 18:30
这没必要用模块吧~
作者: hfahe    时间: 2009-04-14 09:32
用也可 不用也可 自己选择
作者: orangetouch    时间: 2009-04-14 11:18
perl -MDate::Calc -e 'print sprintf(("2009".sprintf("%02d",$_)."%02d\n") x Date::Calc::Days_in_Month(2009,$_),(1..Date::Calc::Days_in_Month(2009,$_)) )for ( 1.. 12 )'


[ 本帖最后由 orangetouch 于 2009-4-14 11:31 编辑 ]




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2