Perl:检查当天是否在两天之间

时间:2014-06-30 04:03:14

标签: perl

如何确定当天是否在其他两天之间。

如果其他两天像Tues-Thursday那样,下面的代码可以运行

但是,如果我想让我的用户给出像星期一那样的范围,那该怎么办呢。

    my $timestamp = time;
    my $dow       = ( localtime($timestamp) )[6];

    my $range = '5-1'; # 5 being friday and 1 being monday 
    $range =~ m/(\d+)-(\d+)/;
    my ( $start, $end ) = ( $1, $2 );

    if ( $start >= 0 && $end <= 6 && $dow >= $start && $dow <= $end ) {
        print "$dow falls between your range";
        }

1 个答案:

答案 0 :(得分:4)

my $in = ( $start <= $end
   ? $start <= $dow && $dow <= $end
   : !( $end < $dow && $dow < $start )
);

my $in = ( $start <= $end
   ? $start <= $dow && $dow <= $end
   : $dow <= $end || $start <= $dow
);