Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69546
  • 博文数量: 24
  • 博客积分: 81
  • 博客等级: 民兵
  • 技术积分: 136
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-08 00:56
文章分类
文章存档

2012年(9)

2011年(15)

我的朋友

分类:

2011-12-10 00:34:37

因为一个朋友需要一段时间内工作日的具体日期,找了些资料,终于用一下代码完成了,特此记录
 
 
 
  1. #!/bin/env perl
  2. #FileName:get_workday.pl
  3. # scrit
  4. # 2011.12.07
  5. #取一段时间内的工作日
  6. use strict;
  7. use warnings;
  8. use Time::Local;

  9. sub get_date_range
  10. {
  11.         my ($date_s, $date_e) = @_;
  12.         my ($year_s, $mon_s, $day_s) = split("-", $date_s);
  13.         my ($year_e, $mon_e, $day_e) = split("-", $date_e);
  14.         my @date_range;

  15.         my $seconds_s = timelocal(0, 0, 0, $day_s, $mon_s - 1, $year_s);
  16.         my $seconds_e = timelocal(0, 0, 0, $day_e, $mon_e - 1, $year_e);
  17.         my $seconds_in_one_day = 24 * 60 * 60;

  18.         if ($seconds_s > $seconds_e)
  19.         {
  20.                 ($seconds_s, $seconds_e) = ($seconds_e, $seconds_s);
  21.         }

  22.         while ($seconds_s <= $seconds_e)
  23.         {
  24.                 my ($day, $mon, $year) = (localtime($seconds_s))[3, 4, 5];
  25.                 $mon += 1;
  26.                 $year += 1900;
  27.                 push(@date_range, sprintf("%04d-%02d-%02d", $year, $mon, $day));
  28.                 $seconds_s += $seconds_in_one_day;
  29.         }

  30.         return @date_range;
  31. }



  32. sub dealWithDate{
  33. my ($date)=@_;
  34. my ($year,$month,$day)=split('-',$date);
  35. my ($secs,$min,$hr,$dy,$mth,$yr,$wd,$yd,$ds);
  36. $secs=$min=0;
  37. $hr=12;
  38. $dy=1;
  39. $mth=$yr=$wd=$yd=$ds=0;

  40. $dy=$day;
  41. $mth=$month-1;
  42. $yr=$year-1900;

  43. my @timeArray=($secs,$min,$hr,$dy,$mth,$yr,$wd,$yd,$ds);

  44. my $secs1=timelocal(@timeArray);
  45. ($secs,$min,$hr,$dy,$mth,$yr,$wd,$yd,$ds)=localtime($secs1);
  46. if( $wd !=0 && $wd != 6)
  47.  {
  48.   return 1;
  49. }
  50. return 0;
  51. }

  52. if (2 != @ARGV)
  53. {
  54.     #格式错误
  55.     print "参数调用错误: \n";
  56.     print "Usage: \n";
  57.     print "perl get_workday.pl 2010-12-07 2011-12-07\n";
  58. }
  59. my $start_date = $ARGV[0];
  60. my $end_date = $ARGV[1];
  61. if( $start_date =~ /\d{4}-\d{2}-\d{2}/ && $end_date =~ /\d{4}-\d{2}-\d{2}/)
  62. {
  63.      my @array = &get_date_range($start_date, $end_date);
  64.     foreach(@array)
  65.    {
  66.      my $day = $_;
  67.      my $tag = &dealWithDate($day);
  68.      if($tag == 1)
  69.      {
  70.        print "$day\n";
  71.      }
  72.    }
  73. }
  74. else
  75. {
  76.     #参数错误
  77.     print "参数调用错误: \n";
  78.     print "Usage: \n";
  79.     print "perl get_workday.pl 2010-12-07 2011-12-07\n";
  80. }
阅读(731) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~