因为一个朋友需要一段时间内工作日的具体日期,找了些资料,终于用一下代码完成了,特此记录
- #!/bin/env perl
- #FileName:get_workday.pl
- # scrit
- # 2011.12.07
- #取一段时间内的工作日
- use strict;
- use warnings;
- use Time::Local;
- sub get_date_range
- {
- my ($date_s, $date_e) = @_;
- my ($year_s, $mon_s, $day_s) = split("-", $date_s);
- my ($year_e, $mon_e, $day_e) = split("-", $date_e);
- my @date_range;
- my $seconds_s = timelocal(0, 0, 0, $day_s, $mon_s - 1, $year_s);
- my $seconds_e = timelocal(0, 0, 0, $day_e, $mon_e - 1, $year_e);
- my $seconds_in_one_day = 24 * 60 * 60;
- if ($seconds_s > $seconds_e)
- {
- ($seconds_s, $seconds_e) = ($seconds_e, $seconds_s);
- }
- while ($seconds_s <= $seconds_e)
- {
- my ($day, $mon, $year) = (localtime($seconds_s))[3, 4, 5];
- $mon += 1;
- $year += 1900;
- push(@date_range, sprintf("%04d-%02d-%02d", $year, $mon, $day));
- $seconds_s += $seconds_in_one_day;
- }
- return @date_range;
- }
- sub dealWithDate{
- my ($date)=@_;
- my ($year,$month,$day)=split('-',$date);
- my ($secs,$min,$hr,$dy,$mth,$yr,$wd,$yd,$ds);
- $secs=$min=0;
- $hr=12;
- $dy=1;
- $mth=$yr=$wd=$yd=$ds=0;
- $dy=$day;
- $mth=$month-1;
- $yr=$year-1900;
- my @timeArray=($secs,$min,$hr,$dy,$mth,$yr,$wd,$yd,$ds);
- my $secs1=timelocal(@timeArray);
- ($secs,$min,$hr,$dy,$mth,$yr,$wd,$yd,$ds)=localtime($secs1);
- if( $wd !=0 && $wd != 6)
- {
- return 1;
- }
- return 0;
- }
- if (2 != @ARGV)
- {
- #格式错误
- print "参数调用错误: \n";
- print "Usage: \n";
- print "perl get_workday.pl 2010-12-07 2011-12-07\n";
- }
- my $start_date = $ARGV[0];
- my $end_date = $ARGV[1];
- if( $start_date =~ /\d{4}-\d{2}-\d{2}/ && $end_date =~ /\d{4}-\d{2}-\d{2}/)
- {
- my @array = &get_date_range($start_date, $end_date);
- foreach(@array)
- {
- my $day = $_;
- my $tag = &dealWithDate($day);
- if($tag == 1)
- {
- print "$day\n";
- }
- }
- }
- else
- {
- #参数错误
- print "参数调用错误: \n";
- print "Usage: \n";
- print "perl get_workday.pl 2010-12-07 2011-12-07\n";
- }
阅读(3165) | 评论(1) | 转发(1) |