Perl:本月的第一个星期六和通话功能

时间:2013-07-30 15:40:32

标签: perl

友.. 在Perl中,我们如何检查它是否是该月的第一个星期六,如果它是真的,则调用另一个函数..

我在互联网上搜索,但我找到的所有选项都是用于ksh脚本..

--PERL
use warning;
use strict;

IF first_saturday = TRUE
  THEN
     msg "It's First Saturday"
     call delete()
  ELSE
     msg "Not First Saturday
END IF

感谢...

5 个答案:

答案 0 :(得分:5)

也许是这样的(使用@mom的localtime建议):

sub is_first_saturday {
    my @time_elements = localtime(time);
    my $day_of_week = $time_elements[6];
    my $day_of_month = $time_elements[3];
    return $day_of_week == 6 && $day_of_month < 8;
}

您可能希望将日期传递给函数,而不是今天假设。

答案 1 :(得分:0)

您可以尝试Date::Manip模块。

use strict;
use Date::Manip;

$main::TZ= 'GMT';
print UnixDate(ParseDate("first Saturday in July 2013"),"First Saturday of the month is %B %E, %Y.");

答案 2 :(得分:0)

我喜欢自从Perl 5.10作为标准模块包含的Time::Piece。不幸的是,它不包括一周的方法。但是,Date::Handler会这样做。

这是一个很好的干净界面,让你明白你想要的东西。太糟糕了,它不是标准的Perl模块,所以你必须从CPAN安装它。

use Date::Handler;

....

my $date = Date::Handler->new($time);  #Time is std Unix # of seconds since 01/01/1970

# Is it the sixth day of the week (Mon = 1) and the first week of the month?
if ( $date->WeekDay == 6 and $date->WeekOfMonth == 1 ) {
    print "It's the first Saturday of the month!\n";
}

... OR

my $date = Date::Handler->new($time);

# Is it the first Saturday of the month?
if ( $date->WeekDayName eq "Saturday" and $date->WeekOfMonth == 1 ) {
    print "It's the first Saturday of the month!\n";
}

您无法更轻松地查看代码正在执行的操作。

答案 3 :(得分:0)

use strict;
use DateTime;

# day: 1 = Monday, ..., 7 = Sunday
# nth: 1 = 1st time a day appears, ..., 5 = last time a day appears in a month
sub is_nth_day_of_month {
    my ( $nth, $day ) = @_;

    my $now = DateTime->now;

    return $now->day_of_week == $day && $now->weekday_of_month == $nth;
}

print is_nth_day_of_month(1, 6) ? "Yes\n" :  "No\n";

答案 4 :(得分:0)

#!usr/bin/perl -w
use strict;
my $day  = substr (`date`,0,3);
my $date = substr(`date`,8,2) ;
if($day =~/Wed/ and $date <= 7){
   print "Hey today is first Saturday\n";
}